コード例 #1
0
        // ==== 更新voxel

        public void RebuildMesh()
        {
            if (!initialized)
            {
                Initialize();
            }

            //销毁额外的mesh容器
            foreach (Transform child in transform)
            {
                Destroy(child.gameObject);
            }

            int x = 0, y = 0, z = 0;

            // 刷新一下
            chunk.GetNeighbors();

            //对于每一个voxel,检查表面是否暴露,若是,把面加入到数组中
            while (x < SideLength)
            {
                while (y < SideLength)
                {
                    while (z < SideLength)
                    {
                        ushort voxel = chunk.GetVoxel(x, y, z); // current voxel data
                        if (voxel != 0)
                        {                                       // 不管空的
                            Voxel voxelType = Engine.GetVoxelType(voxel);
                            if (voxelType.VCustomMesh == false)
                            {                            //是个块
                                Transparency transparency = voxelType.VTransparency;
                                ColliderType colliderType = voxelType.VColliderType;

                                if (CheckAdjacent(x, y, z, Direction.forward, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.forward, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.back, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.back, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.up, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.up, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.down, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.down, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.right, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.right, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.left, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.left, colliderType, x, y, z);
                                }

                                if (colliderType == ColliderType.none && Engine.GenerateColliders)
                                {
                                    AddCubeMesh(x, y, z, false);
                                }
                            }
                            else
                            {                             // 不是个块
                                if (CheckAllAdjacent(x, y, z) == false)
                                {                         //如果相邻voxel都是不透明的
                                    CreateCustomMesh(voxel, x, y, z, voxelType.VMesh);
                                }
                            }
                        }
                        z += 1;
                    }
                    z  = 0;
                    y += 1;
                }
                y  = 0;
                x += 1;
            }

            // 更新mesh
            UpdateMesh(GetComponent <MeshFilter>().mesh);
        }
コード例 #2
0
 public ushort GetVoxel()
 {
     return(chunk.GetVoxel(index));
 }