Ejemplo n.º 1
0
        // ==== mesh generation =======================================================================================

        private void CreateFace(ushort voxel, Facing facing, ColliderType colliderType, int x, int y, int z)
        {
            Voxel      voxelComponent = Engine.GetVoxelType(voxel);
            List <int> FacesList      = Faces[voxelComponent.VSubmeshIndex];

            // ==== Vertices ====

            // add the positions of the vertices depending on the facing of the face
            if (facing == Facing.forward)
            {
                Vertices.Add(new Vector3(x + 0.5001f, y + 0.5001f, z + 0.5f));
                Vertices.Add(new Vector3(x - 0.5001f, y + 0.5001f, z + 0.5f));
                Vertices.Add(new Vector3(x - 0.5001f, y - 0.5001f, z + 0.5f));
                Vertices.Add(new Vector3(x + 0.5001f, y - 0.5001f, z + 0.5f));
                if (colliderType == ColliderType.cube && Engine.GenerateColliders)
                {
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));
                }
            }
            else if (facing == Facing.up)
            {
                Vertices.Add(new Vector3(x - 0.5001f, y + 0.5f, z + 0.5001f));
                Vertices.Add(new Vector3(x + 0.5001f, y + 0.5f, z + 0.5001f));
                Vertices.Add(new Vector3(x + 0.5001f, y + 0.5f, z - 0.5001f));
                Vertices.Add(new Vector3(x - 0.5001f, y + 0.5f, z - 0.5001f));
                if (colliderType == ColliderType.cube && Engine.GenerateColliders)
                {
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
                }
            }
            else if (facing == Facing.right)
            {
                Vertices.Add(new Vector3(x + 0.5f, y + 0.5001f, z - 0.5001f));
                Vertices.Add(new Vector3(x + 0.5f, y + 0.5001f, z + 0.5001f));
                Vertices.Add(new Vector3(x + 0.5f, y - 0.5001f, z + 0.5001f));
                Vertices.Add(new Vector3(x + 0.5f, y - 0.5001f, z - 0.5001f));
                if (colliderType == ColliderType.cube && Engine.GenerateColliders)
                {
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));
                }
            }
            else if (facing == Facing.back)
            {
                Vertices.Add(new Vector3(x - 0.5001f, y + 0.5001f, z - 0.5f));
                Vertices.Add(new Vector3(x + 0.5001f, y + 0.5001f, z - 0.5f));
                Vertices.Add(new Vector3(x + 0.5001f, y - 0.5001f, z - 0.5f));
                Vertices.Add(new Vector3(x - 0.5001f, y - 0.5001f, z - 0.5f));
                if (colliderType == ColliderType.cube && Engine.GenerateColliders)
                {
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));
                }
            }
            else if (facing == Facing.down)
            {
                Vertices.Add(new Vector3(x - 0.5001f, y - 0.5f, z - 0.5001f));
                Vertices.Add(new Vector3(x + 0.5001f, y - 0.5f, z - 0.5001f));
                Vertices.Add(new Vector3(x + 0.5001f, y - 0.5f, z + 0.5001f));
                Vertices.Add(new Vector3(x - 0.5001f, y - 0.5f, z + 0.5001f));
                if (colliderType == ColliderType.cube && Engine.GenerateColliders)
                {
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));
                }
            }
            else if (facing == Facing.left)
            {
                Vertices.Add(new Vector3(x - 0.5f, y + 0.5001f, z + 0.5001f));
                Vertices.Add(new Vector3(x - 0.5f, y + 0.5001f, z - 0.5001f));
                Vertices.Add(new Vector3(x - 0.5f, y - 0.5001f, z - 0.5001f));
                Vertices.Add(new Vector3(x - 0.5f, y - 0.5001f, z + 0.5001f));
                if (colliderType == ColliderType.cube && Engine.GenerateColliders)
                {
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));
                    SolidColliderVertices.Add(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));
                }
            }

            // ==== UVs =====
            float   tUnit   = Engine.GetTextureUnit(voxel);
            Vector2 tOffset = Engine.GetTextureOffset(voxel, facing); // get texture offset for this voxel type

            float pad = tUnit * Engine.TexturePadding;

            UVs.Add(new Vector2(tUnit * tOffset.x + pad, tUnit * tOffset.y + tUnit - pad));         // top left
            UVs.Add(new Vector2(tUnit * tOffset.x + tUnit - pad, tUnit * tOffset.y + tUnit - pad)); // top right
            UVs.Add(new Vector2(tUnit * tOffset.x + tUnit - pad, tUnit * tOffset.y + pad));         // bottom right
            UVs.Add(new Vector2(tUnit * tOffset.x + pad, tUnit * tOffset.y + pad));                 // bottom left

            // ==== Faces ====

            // add the faces
            FacesList.Add(FaceCount + 0);
            FacesList.Add(FaceCount + 1);
            FacesList.Add(FaceCount + 3);
            FacesList.Add(FaceCount + 1);
            FacesList.Add(FaceCount + 2);
            FacesList.Add(FaceCount + 3);
            if (colliderType == ColliderType.cube && Engine.GenerateColliders)
            {
                SolidColliderFaces.Add(SolidFaceCount + 0);
                SolidColliderFaces.Add(SolidFaceCount + 1);
                SolidColliderFaces.Add(SolidFaceCount + 3);
                SolidColliderFaces.Add(SolidFaceCount + 1);
                SolidColliderFaces.Add(SolidFaceCount + 2);
                SolidColliderFaces.Add(SolidFaceCount + 3);
            }

            // Add to the face count
            FaceCount += 4; // we're adding 4 because there are 4 vertices in each face.
            if (colliderType == ColliderType.cube && Engine.GenerateColliders)
            {
                SolidFaceCount += 4;
            }

            // Check the amount of vertices so far and create a new mesh if necessary
            if (Vertices.Count > 65530)
            {
                CreateNewMeshObject();
            }
        }