Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Profiling;
- public class EndlessTerrain : MonoBehaviour
- {
- public float maxViewDst;
- public float colliderRange;
- public Transform viewer;
- public static Vector2 viewerPosition;
- int chunksVisibleInViewDst;
- Dictionary<Vector2, TerrainChunk> chunkDictionary = new Dictionary<Vector2, TerrainChunk>();
- void Start()
- {
- chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / ChunkGenerator.ChunkDimensions.x);
- }
- void Update()
- {
- viewerPosition = new Vector2(viewer.position.x, viewer.position.z);
- UpdateVisibleChunks();
- }
- void UpdateVisibleChunks()
- {
- int currentChunkCoordX = (int)Mathf.Round(viewerPosition.x / ChunkGenerator.ChunkDimensions.x);
- int currentChunkCoordY = (int)Mathf.Round(viewerPosition.y / ChunkGenerator.ChunkDimensions.x);
- for (int yOffset = -chunksVisibleInViewDst; yOffset <= chunksVisibleInViewDst; yOffset++) //Loops through all the current chunks
- {
- for (int xOffset = -chunksVisibleInViewDst; xOffset <= chunksVisibleInViewDst; xOffset++)
- {
- Vector2Int viewedChunkCoord = new Vector2Int(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
- if (chunkDictionary.ContainsKey(viewedChunkCoord)) //Checks if a chunk with the correct cordinates has been loaded previously
- {
- TerrainChunk viewedChunk = chunkDictionary[viewedChunkCoord];
- viewedChunk.UpdateTerrainChunk();
- }
- else
- {
- chunkDictionary.Add(viewedChunkCoord, new TerrainChunk(viewedChunkCoord, transform, maxViewDst, colliderRange));
- }
- }
- }
- }
- public class TerrainChunk
- {
- public GameObject chunkObject;
- public bool visible;
- ChunkGenerator chunkGen;
- MeshRenderer renderer;
- MeshRenderer waterRenderer;
- Vector2Int position;
- Bounds bounds;
- float maxViewDstSqr;
- float colliderRangeSqr;
- bool hasCollider;
- public TerrainChunk(Vector2Int coord, Transform parent, float maxViewDst, float colliderRange)
- {
- Vector2Int chunkDimensions = ChunkGenerator.ChunkDimensions;
- position = coord * chunkDimensions.x;
- bounds = new Bounds(new Vector2(position.x, position.y), Vector2.one * chunkDimensions.x);
- chunkGen = GameObject.Find("ChunkGenerator").GetComponent<ChunkGenerator>();
- chunkObject = chunkGen.CreateChunkObject(position);
- chunkObject.transform.position = new Vector3(position.x - (chunkDimensions.x / 2), 0, position.y - (chunkDimensions.x / 2));
- renderer = chunkObject.GetComponent<MeshRenderer>();
- waterRenderer = chunkObject.transform.GetChild(0).GetComponent<MeshRenderer>();
- chunkObject.transform.parent = parent;
- SetActive(false);
- maxViewDstSqr = maxViewDst * maxViewDst;
- colliderRangeSqr = colliderRange * colliderRange;
- }
- public void UpdateTerrainChunk()
- {
- float viewerDstFromNearestEdge = bounds.SqrDistance(viewerPosition);
- if (viewerDstFromNearestEdge < colliderRangeSqr && !hasCollider)
- {
- chunkObject.AddComponent<MeshCollider>();
- hasCollider = true;
- }
- bool visible = viewerDstFromNearestEdge <= maxViewDstSqr; //Checks if the chunk is too far away from the player, if so it deactivates it
- SetActive(visible);
- }
- public void SetActive(bool visible)
- {
- renderer.enabled = visible;
- waterRenderer.enabled = visible;
- this.visible = visible;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement