EndlessTerrain.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. 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 Vector2(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
  51. if (terrainChunkDictionary.ContainsKey(viewedChunkCoord))
  52. {
  53. terrainChunkDictionary[viewedChunkCoord].UpdateTerrainChunk();
  54. }
  55. else
  56. {
  57. terrainChunkDictionary.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. MeshCollider meshCollider;
  70. LODInfo[] detailLevels;
  71. LODMesh[] lodMeshes;
  72. LODMesh collisionLODMesh;
  73. MapData mapData;
  74. bool mapDataReceived;
  75. int previousLODIndex = -1;
  76. public TerrainChunk(Vector2 coord, int size, LODInfo[] detailLevels, Transform parent, Material material)
  77. {
  78. this.detailLevels = detailLevels;
  79. position = coord * size;
  80. bounds = new Bounds(position, Vector2.one * size);
  81. Vector3 positionV3 = new Vector3(position.x, 0, position.y);
  82. meshObject = new GameObject("Terrain Chunk");
  83. meshObject.tag = "Floor";
  84. meshRenderer = meshObject.AddComponent<MeshRenderer>();
  85. meshFilter = meshObject.AddComponent<MeshFilter>();
  86. meshRenderer.material = material;
  87. meshObject.transform.position = positionV3 * mapGenerator.terrainData.uniformScale;
  88. meshObject.transform.parent = parent;
  89. meshObject.transform.localScale = Vector3.one * mapGenerator.terrainData.uniformScale;
  90. meshCollider = meshObject.AddComponent<MeshCollider>();
  91. SetVisible(false);
  92. lodMeshes = new LODMesh[detailLevels.Length];
  93. for (int i = 0; i < detailLevels.Length; i++)
  94. {
  95. lodMeshes[i] = new LODMesh(detailLevels[i].lod, UpdateTerrainChunk);
  96. if (detailLevels[i].useForCollider)
  97. {
  98. collisionLODMesh = lodMeshes[i];
  99. }
  100. }
  101. mapGenerator.RequestMapData(position,OnMapDataReceived);
  102. }
  103. void OnMapDataReceived(MapData mapData)
  104. {
  105. this.mapData = mapData;
  106. mapDataReceived = true;
  107. UpdateTerrainChunk();
  108. }
  109. public void UpdateTerrainChunk()
  110. {
  111. if (mapDataReceived)
  112. {
  113. float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
  114. bool visible = viewerDstFromNearestEdge <= maxViewDist;
  115. if (visible)
  116. {
  117. int lodIndex = 0;
  118. for (int i = 0; i < detailLevels.Length - 1; i++)
  119. {
  120. if (viewerDstFromNearestEdge > detailLevels[i].visibleDstThreshold)
  121. {
  122. lodIndex = i + 1;
  123. }
  124. else
  125. {
  126. break;
  127. }
  128. }
  129. if (lodIndex != previousLODIndex)
  130. {
  131. LODMesh lodMesh = lodMeshes[lodIndex];
  132. if (lodMesh.hasMesh)
  133. {
  134. previousLODIndex = lodIndex;
  135. meshFilter.mesh = lodMesh.mesh;
  136. }
  137. else if (!lodMesh.hasRequestedMesh)
  138. {
  139. lodMesh.RequestMesh(mapData);
  140. }
  141. }
  142. if(lodIndex == 0)
  143. {
  144. if (collisionLODMesh.hasMesh)
  145. {
  146. meshCollider.sharedMesh = collisionLODMesh.mesh;
  147. }
  148. else if (!collisionLODMesh.hasRequestedMesh)
  149. {
  150. collisionLODMesh.RequestMesh(mapData);
  151. }
  152. }
  153. terrainChunksVisibleLastUpdate.Add(this);
  154. }
  155. SetVisible(visible);
  156. }
  157. }
  158. public void SetVisible(bool visible)
  159. {
  160. meshObject.SetActive(visible);
  161. }
  162. public bool IsVisible()
  163. {
  164. return meshObject.activeSelf;
  165. }
  166. }
  167. class LODMesh
  168. {
  169. public Mesh mesh;
  170. public bool hasRequestedMesh;
  171. public bool hasMesh;
  172. int lod;
  173. System.Action updateCallback;
  174. public LODMesh(int lod, System.Action updateCallback)
  175. {
  176. this.lod = lod;
  177. this.updateCallback = updateCallback;
  178. }
  179. void OnMeshDataReceived(MeshData meshData)
  180. {
  181. mesh = meshData.CreateMesh();
  182. hasMesh = true;
  183. updateCallback();
  184. }
  185. public void RequestMesh(MapData mapData)
  186. {
  187. hasRequestedMesh = true;
  188. mapGenerator.RequestMeshData(mapData, lod, OnMeshDataReceived);
  189. }
  190. }
  191. [System.Serializable]
  192. public struct LODInfo
  193. {
  194. public int lod;
  195. public float visibleDstThreshold;
  196. public bool useForCollider;
  197. }
  198. }