EndlessTerrain.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. public class EndlessTerrain : MonoBehaviour
  5. {
  6. const float scale = 5f;
  7. const float viewerMoveThreshHoldForChunkUpdate = 25f;
  8. const float sqrViewerMoveThreshHoldForChunkUpdate = viewerMoveThreshHoldForChunkUpdate * viewerMoveThreshHoldForChunkUpdate;
  9. public LODInfo[] detailLevels;
  10. public static float maxViewDst;
  11. public Transform viewer;
  12. public Material mapMaterial;
  13. public static Vector2 viewerPosition;
  14. Vector2 viewerPositionOld;
  15. static MapGenerator mapGenerator;
  16. int chunkSize;
  17. int chunksVisibleInViewDst;
  18. Dictionary<Vector2, TerrainChunk> terrainChunkDictionnary = new Dictionary<Vector2, TerrainChunk>();
  19. static List<TerrainChunk> terrainChunksVisibleLastUpdate = new List<TerrainChunk>();
  20. private void Start()
  21. {
  22. mapGenerator = FindFirstObjectByType<MapGenerator>();
  23. maxViewDst = detailLevels[detailLevels.Length-1].visibleDstThreshold;
  24. chunkSize = MapGenerator.mapChunkSize - 1;
  25. chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / chunkSize);
  26. UpdateVisibleChunks();
  27. }
  28. private void Update()
  29. {
  30. viewerPosition = new Vector2(viewer.position.x, viewer.position.z) / scale;
  31. if ((viewerPositionOld - viewerPosition).sqrMagnitude > sqrViewerMoveThreshHoldForChunkUpdate)
  32. {
  33. viewerPositionOld = viewerPosition;
  34. UpdateVisibleChunks();
  35. }
  36. }
  37. void UpdateVisibleChunks()
  38. {
  39. for (int i = 0; i < terrainChunksVisibleLastUpdate.Count; i++)
  40. {
  41. terrainChunksVisibleLastUpdate[i].SetVisible(false);
  42. }
  43. terrainChunksVisibleLastUpdate.Clear();
  44. int currentChunkCoordX = Mathf.RoundToInt(viewerPosition.x / chunkSize);
  45. int currentChunkCoordY = Mathf.RoundToInt(viewerPosition.y / chunkSize);
  46. for (int yOffset = -chunksVisibleInViewDst; yOffset <=chunksVisibleInViewDst; yOffset++)
  47. {
  48. for (int xOffset = -chunksVisibleInViewDst; xOffset <= chunksVisibleInViewDst; xOffset++)
  49. {
  50. Vector2 viewedChunkCoord = new(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
  51. if (terrainChunkDictionnary.ContainsKey(viewedChunkCoord))
  52. {
  53. terrainChunkDictionnary[viewedChunkCoord].UpdateTerrainChunk();
  54. }
  55. else
  56. {
  57. terrainChunkDictionnary.Add(viewedChunkCoord, new TerrainChunk(viewedChunkCoord, chunkSize, detailLevels, transform, mapMaterial));
  58. }
  59. }
  60. }
  61. }
  62. public class TerrainChunk
  63. {
  64. GameObject meshObject;
  65. Vector2 position;
  66. Bounds bounds;
  67. MeshRenderer meshRenderer;
  68. MeshFilter meshFilter;
  69. LODInfo[] detailLevels;
  70. LODMesh[] lodMeshes;
  71. MapData mapData;
  72. bool mapDataReceived;
  73. int previousLODIndex = -1;
  74. public TerrainChunk(Vector2 coord, int size, LODInfo[] detailLevels, Transform parent, Material material)
  75. {
  76. this.detailLevels = detailLevels;
  77. position = coord * size;
  78. bounds = new Bounds(position, Vector2.one * size);
  79. Vector3 positionV3 = new(position.x, 0, position.y);
  80. meshObject = new GameObject("TerrainChunk");
  81. meshRenderer = meshObject.AddComponent<MeshRenderer>();
  82. meshFilter = meshObject.AddComponent<MeshFilter>();
  83. meshRenderer.material = material;
  84. meshObject.transform.position = positionV3 * scale;
  85. meshObject.transform.parent = parent;
  86. meshObject.transform.localScale = Vector3.one * scale;
  87. SetVisible(false);
  88. lodMeshes = new LODMesh[detailLevels.Length];
  89. for (int i = 0; i < detailLevels.Length; i++)
  90. {
  91. lodMeshes[i] = new LODMesh(detailLevels[i].lod, UpdateTerrainChunk);
  92. }
  93. mapGenerator.RequestMapData(position, OnMapDataReceived);
  94. }
  95. void OnMapDataReceived(MapData mapData)
  96. {
  97. this.mapData = mapData;
  98. mapDataReceived = true;
  99. Texture2D texture = TextureGenerator.TextureFromColourMap(mapData.colourMap, MapGenerator.mapChunkSize, MapGenerator.mapChunkSize);
  100. meshRenderer.material.mainTexture = texture;
  101. UpdateTerrainChunk();
  102. }
  103. public void UpdateTerrainChunk()
  104. {
  105. if (mapDataReceived)
  106. {
  107. float viewDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
  108. bool visible = viewDstFromNearestEdge <= maxViewDst;
  109. if (visible)
  110. {
  111. int lodIndex = 0;
  112. for (int i = 0; i < detailLevels.Length - 1; i++)
  113. {
  114. if (viewDstFromNearestEdge > detailLevels[i].visibleDstThreshold)
  115. {
  116. lodIndex = i + 1;
  117. }
  118. else
  119. {
  120. break;
  121. }
  122. }
  123. if (lodIndex != previousLODIndex)
  124. {
  125. LODMesh lodMesh = lodMeshes[lodIndex];
  126. if (lodMesh.hasMesh)
  127. {
  128. previousLODIndex = lodIndex;
  129. meshFilter.mesh = lodMesh.mesh;
  130. }
  131. else if (!lodMesh.hasRequestedMesh)
  132. {
  133. lodMesh.RequestMesh(mapData);
  134. }
  135. }
  136. terrainChunksVisibleLastUpdate.Add(this);
  137. }
  138. SetVisible(visible);
  139. }
  140. }
  141. public void SetVisible(bool visible)
  142. {
  143. meshObject.SetActive(visible);
  144. }
  145. public bool IsVisible()
  146. {
  147. return meshObject.activeSelf;
  148. }
  149. }
  150. class LODMesh
  151. {
  152. public Mesh mesh;
  153. public bool hasRequestedMesh;
  154. public bool hasMesh;
  155. int lod;
  156. System.Action updateCallback;
  157. public LODMesh(int lod, System.Action updateCallback)
  158. {
  159. this.lod = lod;
  160. this.updateCallback = updateCallback;
  161. }
  162. void OnMeshDataReceived(MeshData meshData)
  163. {
  164. mesh = meshData.CreateMesh();
  165. hasMesh = true;
  166. updateCallback();
  167. }
  168. public void RequestMesh(MapData mapData)
  169. {
  170. hasRequestedMesh = true;
  171. mapGenerator.RequestMeshData(mapData, lod, OnMeshDataReceived);
  172. }
  173. }
  174. [System.Serializable]
  175. public struct LODInfo
  176. {
  177. public int lod;
  178. public float visibleDstThreshold;
  179. }
  180. }