3
\$\begingroup\$

I'm currently working on a procedural generated terrain.

I've written the Chunk script, and it works great. When I generate a few chunks in Unity, it runs alright, but it could definitely run better!!! Currently I am not instantiating new chunks at runtime, but when I do, it starts lagging.

I've been looking at multithreading as a solution, but I think it's early in the process to begin win that.

Any suggestions??? Anything can help.

Thanks in advance.

namespace NTerrain
{
    public class Chunk
    {
        public float _chunkTileSize;
        public Vector3[] _chunkVertices;
        public Vector3 _chunkPosition { get; }
        public int[] _chunkTriangles;
        public Mesh _chunkMesh;
        public MeshFilter _chunkMeshFilter;
        public MeshRenderer _chunkMeshRenderer;
        public GameObject _chunkObject;
        public LOD _chunkLOD;
        public ChunkType _chunkType;

        public Chunk(LOD chunkLOD, ChunkType chunkType, Vector3 chunkPosition) {
            _chunkLOD = chunkLOD;
            _chunkType = chunkType;
            _chunkPosition = chunkPosition;

            _chunkObject = new GameObject();
            _chunkObject.transform.position = _chunkPosition;
            _chunkMeshFilter = _chunkObject.AddComponent<MeshFilter>();
            _chunkMeshRenderer = _chunkObject.AddComponent<MeshRenderer>();

            UpdateChunk(LOD.High);
        }

        public void UpdateChunk(LOD levelOfDetail)
        {
            _chunkLOD = levelOfDetail;

            int squareCount = new int();

            switch (_chunkLOD)
            {
                case LOD.Low:
                    squareCount = 4;
                    break;
                case LOD.Normal:
                    squareCount = 8;
                    break;
                case LOD.High:
                    squareCount = 16;
                    break;
            }

            UpdateMesh(squareCount);
        }

        void UpdateMesh(int levelOfDetail)
        {
            if (_chunkMesh != null)
            {
                _chunkMesh.Clear();
                _chunkVertices = null;
                _chunkTriangles = null;
            }
            else {
                _chunkMesh = new Mesh();
            }

            _chunkTileSize = 16 / levelOfDetail;

            _chunkVertices = new Vector3[levelOfDetail * levelOfDetail * 6];

            int vertexCount = 0;
            float fixedPos = (-1) * (_chunkTileSize * levelOfDetail) / 2;

            for (int y = 0; y < levelOfDetail; y++){
                for (int x = 0; x < levelOfDetail; x++){

                    float xChunkTileSize = x * _chunkTileSize;
                    float yChunkTileSize = y * _chunkTileSize;

                    _chunkVertices[vertexCount] = new Vector3(fixedPos + xChunkTileSize, 0, fixedPos + yChunkTileSize);
                    _chunkVertices[vertexCount + 1] = new Vector3(fixedPos + xChunkTileSize, 0, fixedPos + yChunkTileSize + _chunkTileSize);
                    _chunkVertices[vertexCount + 2] = new Vector3(fixedPos + xChunkTileSize + _chunkTileSize, 0, fixedPos + yChunkTileSize + _chunkTileSize);

                    _chunkVertices[vertexCount + 3] = new Vector3(fixedPos + xChunkTileSize, 0, fixedPos + yChunkTileSize);
                    _chunkVertices[vertexCount + 4] = new Vector3(fixedPos + xChunkTileSize + _chunkTileSize, 0, fixedPos + yChunkTileSize + _chunkTileSize);
                    _chunkVertices[vertexCount + 5] = new Vector3(fixedPos + xChunkTileSize + _chunkTileSize, 0, fixedPos + yChunkTileSize);

                    vertexCount += 6;
                }
            }

            _chunkTriangles = new int[levelOfDetail * levelOfDetail * 6];

            for (int i = 0; i < _chunkTriangles.Length; i++) {
                _chunkTriangles[i] = i;
            }

            _chunkMesh.vertices = _chunkVertices;
            _chunkMesh.triangles = _chunkTriangles;
            _chunkMesh.RecalculateNormals();

            _chunkObject.GetComponent<MeshFilter>().mesh = _chunkMesh;

        }
    }

    public enum LOD
    {
        Low,
        Normal,
        High
    }

    public enum ChunkType
    {
        Forest,
        Desert,
        Arctic
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.