Example #1
0
        public static void AppendCubeModel(JsonCube cube, Dictionary <string, string> modelTextureVariables, Dictionary <string, TextureMapElement> textureMap, ref List <float> vertexes, ref List <float> normals, ref List <float> uvs)
        {
            foreach (var pair in cube.faces.OrderBy(p => (int)p.Key))
            {
                Facing         side        = pair.Key;
                JsonCubeFaceUv textureNode = pair.Value;

                float minU = 0;
                float minV = 0;
                float maxU = 1;
                float maxV = 1;

                if (modelTextureVariables.TryGetValue(textureNode.texture, out var textureNameForFace) && textureMap.TryGetValue(textureNameForFace, out var tme))
                {
                    var percentageU1 = MathHelper.Clamp(textureNode.uv[0] / 16f, 0, 1);
                    var percentageV1 = MathHelper.Clamp(textureNode.uv[1] / 16f, 0, 1);
                    var percentageU2 = MathHelper.Clamp(textureNode.uv[2] / 16f, 0, 1);
                    var percentageV2 = MathHelper.Clamp(textureNode.uv[3] / 16f, 0, 1);

                    Vector2 size = tme.UVMax - tme.UVMin;

                    minU = tme.UVMin.X + size.X * percentageU1;
                    minV = tme.UVMin.Y + size.Y * percentageV1;
                    maxU = tme.UVMin.X + size.X * percentageU2;
                    maxV = tme.UVMin.Y + size.Y * percentageV2;
                }

                uvs.Add(minU);
                uvs.Add(minV);

                uvs.Add(minU);
                uvs.Add(maxV);

                uvs.Add(maxU);
                uvs.Add(minV);

                uvs.Add(maxU);
                uvs.Add(minV);

                uvs.Add(minU);
                uvs.Add(maxV);

                uvs.Add(maxU);
                uvs.Add(maxV);

                Vector3 rot = Vector3.Zero;
                Vector3 ori = Vector3.Zero;

                if (cube.rotation != null)
                {
                    ori = new Vector3(cube.rotation.origin[0], cube.rotation.origin[1], cube.rotation.origin[2]) / 16f;

                    rot[(int)cube.rotation.axis] = MathHelper.DegreesToRadians(cube.rotation.angle);
                }

                AppendFace(side, cube.from, cube.to, rot, ori, ref vertexes, ref normals);
            }
        }
Example #2
0
        public static void AppendCubeModel(JsonCube cube, Dictionary <string, string> modelTextureVariables, Dictionary <string, TextureMapElement> textureMap, ref float[] vertexes, ref float[] normals, ref float[] uvs, int n)
        {
            int startIndex2 = n * 72;
            int startIndex3 = n * 108;

            int faceIndex = 0;

            foreach (var pair in cube.faces.OrderBy(p => (int)p.Key))
            {
                int uvIndex = 12 * faceIndex;

                Facing         side        = pair.Key;
                JsonCubeFaceUv textureNode = pair.Value;

                //edit: textureNode.Texture can be anything. it is a variable defined by the modeller
                //textureNode.Texture isn't the name of the texture file! it is '#side', '#block', '#bottom', ... TODO - if '#' is not present, use the texture from the texture map

                if (modelTextureVariables.TryGetValue(textureNode.texture, out var textureNameForFace) && textureMap.TryGetValue(textureNameForFace, out var tme))
                {
                    var percentageU1 = MathHelper.Clamp(textureNode.uv[0] / 16f, 0, 1);
                    var percentageV1 = MathHelper.Clamp(textureNode.uv[1] / 16f, 0, 1);
                    var percentageU2 = MathHelper.Clamp(textureNode.uv[2] / 16f, 0, 1);
                    var percentageV2 = MathHelper.Clamp(textureNode.uv[3] / 16f, 0, 1);

                    Vector2 size = tme.UVMax - tme.UVMin;

                    var minU = tme.UVMin.X + size.X * percentageU1;
                    var minV = tme.UVMin.Y + size.Y * percentageV1;
                    var maxU = tme.UVMin.X + size.X * percentageU2;
                    var maxV = tme.UVMin.Y + size.Y * percentageV2;

                    uvs[startIndex2 + uvIndex]     = minU;
                    uvs[startIndex2 + uvIndex + 1] = minV;

                    uvs[startIndex2 + uvIndex + 2] = minU;
                    uvs[startIndex2 + uvIndex + 3] = maxV;

                    uvs[startIndex2 + uvIndex + 4] = maxU;
                    uvs[startIndex2 + uvIndex + 5] = minV;

                    uvs[startIndex2 + uvIndex + 6] = maxU;
                    uvs[startIndex2 + uvIndex + 7] = minV;

                    uvs[startIndex2 + uvIndex + 8] = minU;
                    uvs[startIndex2 + uvIndex + 9] = maxV;

                    uvs[startIndex2 + uvIndex + 10] = maxU;
                    uvs[startIndex2 + uvIndex + 11] = maxV;
                }

                Vector3 rot = Vector3.Zero;
                Vector3 ori = Vector3.Zero;

                if (cube.rotation != null)
                {
                    ori = new Vector3(cube.rotation.origin[0], cube.rotation.origin[1], cube.rotation.origin[2]) / 16f;

                    rot[(int)cube.rotation.axis] = MathHelper.DegreesToRadians(cube.rotation.angle);
                }

                AppendFace(side, cube.from, cube.to, rot, ori, ref vertexes, ref normals, startIndex3 + 18 * faceIndex);

                faceIndex++;
            }
        }