Example #1
0
        public RenderTriangle[] ToRenderTriangles(BlockData.BlockRotation rotation, int state, int connectionFlags = 0)
        {
            if (variants == null)
            {
                int rotationI = ((int)rotation) / 90;
                if (cachedStateAlternatives[rotationI].ContainsKey(state))
                {
                    return(cachedStateAlternatives[rotationI][state]);
                }
                else
                {
                    List <RenderTriangle> results = new List <RenderTriangle>();
                    if (parent != "")
                    {
                        if (cachedParent == null)
                        {
                            cachedParent = FromJSONFilePath(parent);
                        }
                        results.AddRange(cachedParent.ToRenderTriangles(rotation, state));
                    }
                    foreach (BlockModelElement element in elements)
                    {
                        element.rootModel = this;
                        results.AddRange(element.ToRenderTriangles(rotation, state));
                    }

                    RenderTriangle[] actualResults = results.ToArray();
                    cachedStateAlternatives[rotationI][state] = actualResults;
                    return(actualResults);
                }
            }
            else
            {
                if (parent != "" && cachedParent == null)
                {
                    cachedParent = FromJSONFilePath(parent);
                }
                if (collection == null)
                {
                    collection = new RotationUtils.RotationVariantCollection(variants, this);
                }
                RotationUtils.RotationVariant renderThing = collection.GetRotationVariant(connectionFlags);
                if (renderThing != null)
                {
                    return(renderThing.ToRenderTriangles(rotation, state, cachedParent));
                }
                else
                {
                    return(collection.GetRotationVariant(0).ToRenderTriangles(rotation, state, cachedParent));
                }
            }
        }
Example #2
0
        public RenderTriangle[] ToRenderTriangles(out bool dependsOnState, BlockData.BlockRotation rotation)
        {
            List <RenderTriangle> results = new List <RenderTriangle>();

            dependsOnState = false;
            if (parent != "")
            {
                if (cachedParent != null)
                {
                    cachedParent = FromJSONFilePath(parent);
                }
                RenderTriangle[] parentRes = cachedParent.ToRenderTriangles(out dependsOnState, rotation);
                if (parentRes != null)
                {
                    results.AddRange(parentRes);
                }
            }
            if (!dependsOnState)
            {
                foreach (BlockModelElement element in elements)
                {
                    if (element.DependsOnState())
                    {
                        dependsOnState = true;
                    }
                }

                if (variants != null)
                {
                    dependsOnState = true;
                }
            }
            if (dependsOnState)
            {
                return(null);
            }
            else
            {
                foreach (BlockModelElement element in elements)
                {
                    element.rootModel = this;
                    results.AddRange(element.ToRenderTriangles(rotation));
                }
                return(results.ToArray());
            }
        }
Example #3
0
        public Blocks.RenderTriangle[] ToRenderTriangles(BlockData.BlockRotation blockRotation = BlockData.BlockRotation.Degrees0, int state = 0)
        {
            CubeMesh res = new CubeMesh();

            // -0.5 to 0.5 -> 0 to 1
            //res = res + new Vector3(0.5f, 0.5f, 0.5f);


            Vector3 fromPos;

            if (fromVars != null && fromVars.Length == 3)
            {
                float fromValX = ParseString(fromVars[0]).GetValue(state);
                float fromValY = ParseString(fromVars[1]).GetValue(state);
                float fromValZ = ParseString(fromVars[2]).GetValue(state);
                fromPos = new Vector3(fromValX, fromValY, fromValZ);
            }
            else
            {
                fromPos = new Vector3((float)from[0], (float)from[1], (float)from[2]);
            }


            Vector3 toPos;

            if (toVars != null && toVars.Length == 3)
            {
                float toValX = ParseString(toVars[0]).GetValue(state);
                float toValY = ParseString(toVars[1]).GetValue(state);
                float toValZ = ParseString(toVars[2]).GetValue(state);
                toPos = new Vector3(toValX, toValY, toValZ);
            }
            else
            {
                toPos = new Vector3((float)to[0], (float)to[1], (float)to[2]);
            }

            // -16 to 32 -> -1 to 2
            //fromPos = fromPos + new Vector3(16, 16, 16);
            fromPos /= 16;
            //toPos = toPos + new Vector3(16, 16, 16);
            toPos /= 16;
            // (0,0,0) to (16,16,16) is a regular cube (0 to 1, 0 to 1, 0 to 1) in minecraft


            ////// (0,0,0) to (1,1,1) -> fromPos to toPos /////
            Vector3 diff = toPos - fromPos;

            res = res * diff + fromPos;

            if (rotation != null)
            {
                res = res.ApplyingTransformationToEachVertex(x => { return(rotation.ApplyToPoint(x)); });
            }
            int rotationDegrees = 0;

            if (blockRotation == BlockData.BlockRotation.Degrees0)
            {
            }
            else if (blockRotation == BlockData.BlockRotation.Degrees180)
            {
                rotationDegrees = 180;
            }
            else if (blockRotation == BlockData.BlockRotation.Degrees270)
            {
                rotationDegrees = 270;
            }
            else if (blockRotation == BlockData.BlockRotation.Degrees90)
            {
                rotationDegrees = 90;
            }

            if (rotationDegrees != 0)
            {
                BlockModelRotation customRotation = new BlockModelRotation
                {
                    axis   = "y",
                    angle  = rotationDegrees,
                    origin = new double[] { 8, 0, 8 }
                };
                res = res.ApplyingTransformationToEachVertex(x => { return(customRotation.ApplyToPoint(x)); });

                //Debug.Log("got " + customRotation.ApplyToPoint(new Vector3(0, 0, 1)) + " is the res");
            }

            //Debug.Log("my texture value is " + texture);

            //Debug.Log("my to value is " + toPos[0] + " " + toPos[1] + " " + toPos[2]);
            //Debug.Log("my from value is " + fromPos[0] + " " + fromPos[1] + " " + fromPos[2]);
            // offset uvs x value to match correct texture
            if (texture != null)
            {
                int texIndex = rootModel.TexToIndex(texture);
                //Debug.Log("tex of " + texture + " maps to index " + texIndex);
                res += new Vector2(texIndex / 64.0f, 0.0f);
            }

            //res = res * new Vector2(1/64.0f, 1.0f/256.0f);

            //res = res * new Vector2(1.0f, 1.0f);



            // 0 to 1 -> -0.5 to 0.5
            //res = res - new Vector3(0.5f, 0.5f, 0.5f);

            return(res.ToRenderTriangles());
        }