EndlessTerrain.cs 7.6 KB

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