Пример #1
0
        private void CreateCustomMesh(ushort voxel, int x, int y, int z, Mesh mesh)
        {
            Voxel      voxelComponent = MasterEngine.getInstance().GetVoxelType(voxel);
            List <int> FacesList      = Faces[voxelComponent.VSubmeshIndex];

            // check if mesh exists
            if (mesh == null)
            {
                Debug.LogError("StarColony: The voxel id " + voxel + " uses a custom mesh, but no mesh has been assigned!");
                return;
            }


            // === mesh
            // check if we still have room for more vertices in the mesh
            if (Vertices.Count + mesh.vertices.Length > 65534)
            {
                CreateNewMeshObject();
            }

            // rotate vertices depending on the mesh rotation setting
            List <Vector3> rotatedVertices = new List <Vector3>();
            MeshRotation   rotation        = voxelComponent.VRotation;

            // 180 horizontal (reverse all x and z)
            if (rotation == MeshRotation.back)
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(new Vector3(-vertex.x, vertex.y, -vertex.z));
                }
            }

            // 90 right
            else if (rotation == MeshRotation.right)
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(new Vector3(vertex.z, vertex.y, -vertex.x));
                }
            }

            // 90 left
            else if (rotation == MeshRotation.left)
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(new Vector3(-vertex.z, vertex.y, vertex.x));
                }
            }

            // no rotation
            else
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(vertex);
                }
            }

            // vertices
            foreach (Vector3 vertex in rotatedVertices)
            {
                Vertices.Add(vertex + new Vector3(x, y, z));          // add all vertices from the mesh
            }

            // UVs
            foreach (Vector2 uv in mesh.uv)
            {
                UVs.Add(uv);
            }

            // faces
            foreach (int face in mesh.triangles)
            {
                FacesList.Add(FaceCount + face);
            }

            // Add to the face count
            FaceCount += mesh.vertexCount;


            // === collider
            if (MasterEngine.getInstance().GenerateColliders)
            {
                ColliderType colliderType = MasterEngine.getInstance().GetVoxelType(voxel).VColliderType;           // get collider type (solid/cube/none)

                // mesh collider
                if (colliderType == ColliderType.mesh)
                {
                    foreach (Vector3 vertex1 in rotatedVertices)
                    {
                        SolidColliderVertices.Add(vertex1 + new Vector3(x, y, z));                  // if mesh collider, just add the vertices & faces from this mesh to the solid collider mesh
                    }
                    foreach (int face1 in mesh.triangles)
                    {
                        SolidColliderFaces.Add(SolidFaceCount + face1);
                    }
                    SolidFaceCount += mesh.vertexCount;
                }

                // cube collider
                if (colliderType == ColliderType.cube)
                {
                    AddCubeMesh(x, y, z, true);            // if cube collider, add a cube to the solid mesh
                }
                // nocollide collider (for both ColliderType.mesh and ColliderType.none, but not for ColliderType.cube since it's redundant)
                else if (voxel != 0)             // only make a collider for non-empty voxels
                {
                    AddCubeMesh(x, y, z, false); // if no cube collider, add a cube to the nocollide mesh (we still need a collider on noCollide blocks for raycasts and such)
                }
            }
        }
Пример #2
0
        private void CreateCustomMesh(ushort voxel, int x, int y, int z, Mesh mesh)
        {
            Voxel      voxelComponent = Engine.GetVoxelType(voxel);
            List <int> FacesList      = Faces[voxelComponent.VSubmeshIndex];

            // 检查mesh是否存在
            if (mesh == null)
            {
                Debug.LogError("CubeMaster: voxel: " + voxel + " 使用了自定义的mesh,没有分配mesh!");
                return;
            }


            // === mesh

            // 检测是否还有足够的空间来存储更多的顶点
            if (Vertices.Count + mesh.vertices.Length > 65534)
            {
                CreateNewMeshObject();
            }

            //旋转顶点
            List <Vector3> rotatedVertices = new List <Vector3>();
            MeshRotation   rotation        = voxelComponent.VRotation;

            // x-->z 水平180
            if (rotation == MeshRotation.back)
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(new Vector3(-vertex.x, vertex.y, -vertex.z));
                }
            }

            // 右90
            else if (rotation == MeshRotation.right)
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(new Vector3(vertex.z, vertex.y, -vertex.x));
                }
            }

            // 左90
            else if (rotation == MeshRotation.left)
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(new Vector3(-vertex.z, vertex.y, vertex.x));
                }
            }

            // 无
            else
            {
                foreach (Vector3 vertex in mesh.vertices)
                {
                    rotatedVertices.Add(vertex);
                }
            }

            // 顶点
            foreach (Vector3 vertex in rotatedVertices)
            {
                Vertices.Add(vertex + new Vector3(x, y, z));                 // add all vertices from the mesh
            }

            // UV
            foreach (Vector2 uv in mesh.uv)
            {
                UVs.Add(uv);
            }

            // face
            foreach (int face in mesh.triangles)
            {
                FacesList.Add(FaceCount + face);
            }

            FaceCount += mesh.vertexCount;


            // === 碰撞器
            if (Engine.GenerateColliders)
            {
                ColliderType colliderType = Engine.GetVoxelType(voxel).VColliderType;

                // 网格碰撞器
                if (colliderType == ColliderType.mesh)
                {
                    foreach (Vector3 vertex1 in rotatedVertices)
                    {
                        SolidColliderVertices.Add(vertex1 + new Vector3(x, y, z));                         // 如果是meshcollider,就从这个mesh取点面添加到solidcollider
                    }
                    foreach (int face1 in mesh.triangles)
                    {
                        SolidColliderFaces.Add(SolidFaceCount + face1);
                    }
                    SolidFaceCount += mesh.vertexCount;
                }

                // cube collider
                if (colliderType == ColliderType.cube)
                {
                    AddCubeMesh(x, y, z, true);
                }
                else if (voxel != 0)
                {
                    AddCubeMesh(x, y, z, false);
                }
            }
        }