コード例 #1
0
    /// <summary>
    /// Generate the mesh for a chunk
    /// </summary>
    /// <param name="chunk"></param>
    /// <param name="lodLevel"></param>
    /// <param name="forceReGenerate"></param>
    public void GenerateChunkMesh(float lodLevel = 10, bool forceReGenerate = false)
    {
        if (!forceReGenerate && MathUtility.CompareLodLevel(LODLevel, lodLevel))
        {
            return;
        }
        MeshData meshData = TerrainGeneration.AllMeshData[lodLevel];

        Vector3[] vertices = meshData.Vertices;
        Color[]   colors   = meshData.Colours;

        for (int x = 0; x < vertices.Length; x++)
        {
            float xCoord = (vertices[x].x + Position.x / Scale.x);
            float zCoord = (vertices[x].z + Position.z / Scale.z);
            vertices[x].y = TerrainGeneration.SampleHeightMapLocal(xCoord, zCoord);

            if (vertices[x].y < TerrainGeneration.instance.WaterLayer.position.y)
            {
                colors[x] = TerrainGeneration.instance.UnderWaterColour;
            }
            else
            {
                float colourHeight = Mathf.InverseLerp(_minColourHeight, _maxColourHeight, vertices[x].y);
                colors[x] = TerrainGeneration.instance.TerrainColours.Evaluate(colourHeight);
            }

            // Fix the seams between different size meshes
            if (TerrainGeneration.instance.IsFixingTerrainSeams && lodLevel < TerrainGeneration.instance.FarLodSize)
            {
                float widthSize = (TerrainGeneration.ChunkScale.x + lodLevel) / lodLevel;
                float previousHeight;
                float nextHeight;
                float result;

                if (ChunkNeighbours[3].LODLevel > LODLevel && Math.Abs(x % (widthSize * 2)) < _widthSizeTolerance && x != 0)
                {
                    previousHeight = vertices[x - (int)(widthSize * 2)].y;
                    nextHeight     = vertices[x].y;
                    result         = (previousHeight + nextHeight) / 2;

                    vertices[x - (int)widthSize].y = result;
                }
                if (ChunkNeighbours[1].LODLevel > LODLevel && x < widthSize && x % 2 == 0 && x != 0)
                {
                    previousHeight = vertices[x - 2].y;
                    nextHeight     = vertices[x].y;
                    result         = (previousHeight + nextHeight) / 2;

                    vertices[x - 1].y = result;
                }
                if (ChunkNeighbours[0].LODLevel > LODLevel && x > vertices.Length - widthSize && x % 2 == 0 && x != 0)
                {
                    previousHeight = vertices[x - 2].y;
                    nextHeight     = vertices[x].y;
                    result         = (previousHeight + nextHeight) / 2;

                    vertices[x - 1].y = result;
                }
                // x % (widthSize * 2) == (widthSize * 2) - 1
                if (ChunkNeighbours[2].LODLevel > LODLevel && Math.Abs(x % (widthSize * 2) - ((widthSize * 2) - 1)) < _widthSizeTolerance && x != 0)
                {
                    previousHeight = vertices[x - (int)(widthSize)].y;
                    nextHeight     = TerrainGeneration.SampleHeightMapLocal(vertices[x + (int)(widthSize)].x + Position.x / Scale.x, vertices[x + (int)(widthSize)].z + Position.z / Scale.z);
                    result         = (nextHeight + previousHeight) / 2;

                    vertices[x].y = result;
                }
            }
        }
        _vertices = vertices;
        _uvs      = meshData.Uvs;
        _tris     = meshData.Tris;
        _colors   = colors;
        //chunk.LODLevel = lodLevel;
    }