Exemple #1
0
        // Used by slabs and stairs.
        /// <summary>
        /// Attempts to create the uvs based on the CubeComponent's faces.
        /// </summary>
        public UvPlane(TexturePos pos, CubeComponent cubeComponent, Direction faceDirection)
        {
            this.texturePos = pos;

            Vector2 v0;
            Vector2 v1;

            if (faceDirection == Direction.NORTH)
            {
                v0 = new Vector2(32 - cubeComponent.pos.x, cubeComponent.neg.y);
                v1 = new Vector2(32 - cubeComponent.neg.x, cubeComponent.pos.y);
            }
            else if (faceDirection == Direction.EAST)
            {
                v0 = new Vector2(cubeComponent.neg.z, cubeComponent.neg.y);
                v1 = new Vector2(cubeComponent.pos.z, cubeComponent.pos.y);
            }
            else if (faceDirection == Direction.SOUTH)
            {
                v0 = new Vector2(cubeComponent.neg.x, cubeComponent.neg.y);
                v1 = new Vector2(cubeComponent.pos.x, cubeComponent.pos.y);
            }
            else if (faceDirection == Direction.WEST)
            {
                v0 = new Vector2(32 - cubeComponent.pos.z, cubeComponent.neg.y);
                v1 = new Vector2(32 - cubeComponent.neg.z, cubeComponent.pos.y);
            }
            else if (faceDirection == Direction.UP)
            {
                v0 = new Vector2(cubeComponent.neg.x, cubeComponent.neg.z);
                v1 = new Vector2(cubeComponent.pos.x, cubeComponent.pos.z);
            }
            else     // d == Direction.DOWN
            {
                v0 = new Vector2(cubeComponent.neg.x, cubeComponent.neg.z);
                v1 = new Vector2(cubeComponent.pos.x, cubeComponent.pos.z);
            }

            v1.x -= 1;
            v1.y -= 1;

            this.uv0 = v0;
            this.uv1 = new Vector2(v0.x, v1.y);
            this.uv2 = v1;
            this.uv3 = new Vector2(v1.x, v0.y);
        }
        public void addCube(BlockRendererPrimitive renderer, Block block, int meta, CubeComponent cube, int renderFace, int worldX, int worldY, int worldZ)
        {
            // Define the points.
            Vector3 ppp = cube.pos;
            Vector3 ppn = new Vector3(cube.pos.x, cube.pos.y, cube.neg.z);
            Vector3 npn = new Vector3(cube.neg.x, cube.pos.y, cube.neg.z);
            Vector3 npp = new Vector3(cube.neg.x, cube.pos.y, cube.pos.z);
            Vector3 pnp = new Vector3(cube.pos.x, cube.neg.y, cube.pos.z);
            Vector3 pnn = new Vector3(cube.pos.x, cube.neg.y, cube.neg.z);
            Vector3 nnn = cube.neg;
            Vector3 nnp = new Vector3(cube.neg.x, cube.neg.y, cube.pos.z);

            // Rotate the cube if needed.
            if (!cube.rotation.isZero())
            {
                Vector3    pivot = new Vector3(16, 16, 16);
                Quaternion angle = cube.rotation.getAngle();
                ppp = MathHelper.rotateVecAround(ppp, pivot, angle);
                ppn = MathHelper.rotateVecAround(ppn, pivot, angle);
                npn = MathHelper.rotateVecAround(npn, pivot, angle);
                npp = MathHelper.rotateVecAround(npp, pivot, angle);
                pnp = MathHelper.rotateVecAround(pnp, pivot, angle);
                pnn = MathHelper.rotateVecAround(pnn, pivot, angle);
                nnn = MathHelper.rotateVecAround(nnn, pivot, angle);
                nnp = MathHelper.rotateVecAround(nnp, pivot, angle);
            }

            // Offset the cube if needed.
            if (cube.offset != Vector3.zero)
            {
                ppp += cube.offset;
                ppn += cube.offset;
                npn += cube.offset;
                npp += cube.offset;
                pnp += cube.offset;
                pnn += cube.offset;
                nnn += cube.offset;
                nnp += cube.offset;
            }

            // Convert the pixel units to world units before rendering.
            ppp = this.pixelToWorld(ppp);
            ppn = this.pixelToWorld(ppn);
            npn = this.pixelToWorld(npn);
            npp = this.pixelToWorld(npp);
            pnp = this.pixelToWorld(pnp);
            pnn = this.pixelToWorld(pnn);
            nnn = this.pixelToWorld(nnn);
            nnp = this.pixelToWorld(nnp);

            Vector3 worldPos = new Vector3(worldX, worldY, worldZ);

            // Note: The vertices that check if the face is out of the cell, meaning it need adjacent light, is arbitrary,
            // the same effect should be given requardless of the vertex.

            // North +Z
            if ((renderFace & 1) == 1)
            {
                this.func01(
                    renderer, worldPos,
                    pnp,
                    ppp,
                    npp,
                    nnp,
                    renderer.getUvPlane(block, meta, Direction.NORTH, cube).getMeshUvs(this.allocatedUvArray),
                    pnp.z >= 0.5f ? BlockPos.north : BlockPos.zero);
            }
            // East +X
            if (((renderFace >> 1) & 1) == 1)
            {
                this.func01(
                    renderer, worldPos,
                    pnn,
                    ppn,
                    ppp,
                    pnp,
                    renderer.getUvPlane(block, meta, Direction.EAST, cube).getMeshUvs(this.allocatedUvArray),
                    pnn.x >= 0.5f ? BlockPos.east : BlockPos.zero);
            }
            // South -Z
            if (((renderFace >> 2) & 1) == 1)
            {
                this.func01(
                    renderer, worldPos,
                    nnn,
                    npn,
                    ppn,
                    pnn,
                    renderer.getUvPlane(block, meta, Direction.SOUTH, cube).getMeshUvs(this.allocatedUvArray),
                    nnn.z <= -0.5f ? BlockPos.south : BlockPos.zero);
            }
            // West -X
            if (((renderFace >> 3) & 1) == 1)
            {
                this.func01(
                    renderer, worldPos,
                    nnp,
                    npp,
                    npn,
                    nnn,
                    renderer.getUvPlane(block, meta, Direction.WEST, cube).getMeshUvs(this.allocatedUvArray),
                    nnp.x <= -0.5f ? BlockPos.west : BlockPos.zero);
            }
            // Up +Y
            if (((renderFace >> 4) & 1) == 1)
            {
                this.func01(
                    renderer, worldPos,
                    npn,
                    npp,
                    ppp,
                    ppn,
                    renderer.getUvPlane(block, meta, Direction.UP, cube).getMeshUvs(this.allocatedUvArray),
                    npn.y >= 0.5f ? BlockPos.up : BlockPos.zero);
            }
            // Down -Y
            if (((renderFace >> 5) & 1) == 1)
            {
                this.func01(
                    renderer, worldPos,
                    nnp,
                    nnn,
                    pnn,
                    pnp,
                    renderer.getUvPlane(block, meta, Direction.DOWN, cube).getMeshUvs(this.allocatedUvArray),
                    nnn.y <= -0.5f ? BlockPos.down : BlockPos.zero);
            }
        }