コード例 #1
0
    private void UpdateMeshData()
    {
        var vertices  = new List <Vector3>();
        var triangles = new List <int>();
        var uv        = new List <Vector2>();

        for (var x = 0; x < _size; x++)
        {
            for (var y = 0; y < _size; y++)
            {
                for (var z = 0; z < _height; z++)
                {
                    var voxelData = _voxels[x, y, z];

                    if (voxelData != null)
                    {
                        if (voxelData.Visibility == null)
                        {
                            voxelData.Visibility = GetVisibilityData(x, y, z);
                        }

                        if (x == 0 && NeighbourChunks[0] != null)
                        {
                            voxelData.Visibility.Left = NeighbourChunks[0]._voxels[_size - 1, y, z] == null;
                        }

                        if (x == _size - 1 && NeighbourChunks[1] != null)
                        {
                            voxelData.Visibility.Right = NeighbourChunks[1]._voxels[0, y, z] == null;
                        }

                        if (y == 0 && NeighbourChunks[2] != null)
                        {
                            voxelData.Visibility.Back = NeighbourChunks[2]._voxels[x, _size - 1, z] == null;
                        }

                        if (y == _size - 1 && NeighbourChunks[3] != null)
                        {
                            voxelData.Visibility.Front = NeighbourChunks[3]._voxels[x, 0, z] == null;
                        }

                        _voxelBuilder.Position    = new Vector3(x, z, y);
                        _voxelBuilder.TopFace     = voxelData.Visibility.Top;
                        _voxelBuilder.BottomFace  = voxelData.Visibility.Bottom;
                        _voxelBuilder.FrontFace   = voxelData.Visibility.Front;
                        _voxelBuilder.BackFace    = voxelData.Visibility.Back;
                        _voxelBuilder.RightFace   = voxelData.Visibility.Right;
                        _voxelBuilder.LeftFace    = voxelData.Visibility.Left;
                        _voxelBuilder.TextureType = voxelData.Type;

                        _voxelBuilder.GenerateAndAddToLists(vertices, triangles, uv);
                    }
                }
            }
        }

        var meshFilter   = GetComponent <MeshFilter>();
        var meshCollider = GetComponent <MeshCollider>();

        meshFilter.mesh.Clear();
        meshFilter.mesh.vertices  = vertices.ToArray();
        meshFilter.mesh.triangles = triangles.ToArray();
        meshFilter.mesh.uv        = uv.ToArray();
        meshFilter.mesh.RecalculateNormals();

        meshCollider.sharedMesh = meshFilter.mesh;
    }