Archived
Private
Public Access
1
0
This repository has been archived on 2026-02-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ProjectBackup/Unity/Landmass Generation/Assets/Scripts/Utils/EndlessTerrain.cs
2022-11-12 13:10:03 +01:00

71 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Utils {
public class EndlessTerrain : MonoBehaviour {
public const float ViewDistance = 300;
public Transform viewer;
public static Vector2 ViewerPosition;
private int _chunkSize;
private int _chunksVisibleInViewDistance;
private Dictionary<Vector2, TerrainChunk> _terrainChunks = new();
private void Start() {
_chunkSize = MapGenerator.ChunkSize - 1;
_chunksVisibleInViewDistance = Mathf.RoundToInt(ViewDistance / _chunkSize);
}
private void Update() {
ViewerPosition = new Vector2(viewer.position.x, viewer.position.z);
UpdateVisibleChunks();
}
private void UpdateVisibleChunks() {
int currentChunkCoordX = Mathf.RoundToInt(ViewerPosition.x / _chunkSize);
int currentChunkCoordY = Mathf.RoundToInt(ViewerPosition.y / _chunkSize);
for (int yOffset = -_chunksVisibleInViewDistance; yOffset <= _chunksVisibleInViewDistance; yOffset++)
for (int xOffset = -_chunksVisibleInViewDistance; xOffset <= _chunksVisibleInViewDistance;) {
Vector2 viewedChunkCoord = new Vector2(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
if (_terrainChunks.ContainsKey(viewedChunkCoord))
_terrainChunks[viewedChunkCoord].UpdateTerrainChunk();
else
_terrainChunks.Add(viewedChunkCoord, new TerrainChunk(viewedChunkCoord, _chunkSize));
}
}
private class TerrainChunk {
public Vector2 Position;
public GameObject MeshObject;
public Bounds Bounds;
public TerrainChunk(Vector2 coord, int size) {
Position = coord * size;
Bounds = new Bounds(Position, Vector3.one * size);
Vector3 position = new Vector3(Position.x, 0, Position.y);
MeshObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
MeshObject.transform.position = position;
MeshObject.transform.localScale = Vector3.one * size / 10f;
SetVisible(false);
}
public void UpdateTerrainChunk() {
float viewerDistance = Mathf.Sqrt(Bounds.SqrDistance(ViewerPosition));
bool visible = viewerDistance <= ViewDistance;
SetVisible(visible);
}
public void SetVisible(bool visible) {
MeshObject.SetActive(visible);
}
}
}
}