Advertisement
JontePonte

endlessterrain

Jan 12th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Profiling;
  4.  
  5. public class EndlessTerrain : MonoBehaviour
  6. {
  7.     public float maxViewDst;
  8.     public float colliderRange;
  9.  
  10.     public Transform viewer;
  11.     public static Vector2 viewerPosition;
  12.  
  13.     int chunksVisibleInViewDst;
  14.  
  15.     Dictionary<Vector2, TerrainChunk> chunkDictionary = new Dictionary<Vector2, TerrainChunk>();
  16.  
  17.     void Start()
  18.     {
  19.         chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / ChunkGenerator.ChunkDimensions.x);
  20.     }
  21.  
  22.     void Update()
  23.     {
  24.         viewerPosition = new Vector2(viewer.position.x, viewer.position.z);
  25.         UpdateVisibleChunks();
  26.     }
  27.  
  28.     void UpdateVisibleChunks()
  29.     {
  30.         int currentChunkCoordX = (int)Mathf.Round(viewerPosition.x / ChunkGenerator.ChunkDimensions.x);
  31.         int currentChunkCoordY = (int)Mathf.Round(viewerPosition.y / ChunkGenerator.ChunkDimensions.x);
  32.  
  33.         for (int yOffset = -chunksVisibleInViewDst; yOffset <= chunksVisibleInViewDst; yOffset++) //Loops through all the current chunks
  34.         {
  35.             for (int xOffset = -chunksVisibleInViewDst; xOffset <= chunksVisibleInViewDst; xOffset++)
  36.             {
  37.                 Vector2Int viewedChunkCoord = new Vector2Int(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
  38.  
  39.                 if (chunkDictionary.ContainsKey(viewedChunkCoord)) //Checks if a chunk with the correct cordinates has been loaded previously
  40.                 {
  41.                     TerrainChunk viewedChunk = chunkDictionary[viewedChunkCoord];
  42.                     viewedChunk.UpdateTerrainChunk();
  43.                 }
  44.                 else
  45.                 {
  46.                     chunkDictionary.Add(viewedChunkCoord, new TerrainChunk(viewedChunkCoord, transform, maxViewDst, colliderRange));
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     public class TerrainChunk
  53.     {
  54.         public GameObject chunkObject;
  55.         public bool visible;
  56.  
  57.         ChunkGenerator chunkGen;
  58.         MeshRenderer renderer;
  59.         MeshRenderer waterRenderer;
  60.  
  61.         Vector2Int position;
  62.         Bounds bounds;
  63.  
  64.         float maxViewDstSqr;
  65.         float colliderRangeSqr;
  66.  
  67.         bool hasCollider;
  68.  
  69.         public TerrainChunk(Vector2Int coord, Transform parent, float maxViewDst, float colliderRange)
  70.         {
  71.             Vector2Int chunkDimensions = ChunkGenerator.ChunkDimensions;
  72.  
  73.             position = coord * chunkDimensions.x;
  74.             bounds = new Bounds(new Vector2(position.x, position.y), Vector2.one * chunkDimensions.x);
  75.  
  76.             chunkGen = GameObject.Find("ChunkGenerator").GetComponent<ChunkGenerator>();
  77.  
  78.             chunkObject = chunkGen.CreateChunkObject(position);
  79.             chunkObject.transform.position = new Vector3(position.x - (chunkDimensions.x / 2), 0, position.y - (chunkDimensions.x / 2));
  80.             renderer = chunkObject.GetComponent<MeshRenderer>();
  81.             waterRenderer = chunkObject.transform.GetChild(0).GetComponent<MeshRenderer>();
  82.  
  83.             chunkObject.transform.parent = parent;
  84.             SetActive(false);
  85.  
  86.             maxViewDstSqr = maxViewDst * maxViewDst;
  87.             colliderRangeSqr = colliderRange * colliderRange;
  88.         }
  89.  
  90.         public void UpdateTerrainChunk()
  91.         {
  92.             float viewerDstFromNearestEdge = bounds.SqrDistance(viewerPosition);
  93.  
  94.             if (viewerDstFromNearestEdge < colliderRangeSqr && !hasCollider)
  95.             {
  96.                 chunkObject.AddComponent<MeshCollider>();
  97.                 hasCollider = true;
  98.             }
  99.  
  100.             bool visible = viewerDstFromNearestEdge <= maxViewDstSqr; //Checks if the chunk is too far away from the player, if so it deactivates it
  101.  
  102.             SetActive(visible);
  103.         }
  104.  
  105.         public void SetActive(bool visible)
  106.         {
  107.             renderer.enabled = visible;
  108.             waterRenderer.enabled = visible;
  109.             this.visible = visible;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement