using System.Collections; using System.Collections.Generic; using UnityEngine; public class Chunk : MonoBehaviour { private Mesh mesh; private const int MESH_RESOLUTION_X = 64; private const int MESH_RESOLUTION_Y = 64; public const float CHUNK_SIZE = 64.0f; private const float MESH_TILE_RES = CHUNK_SIZE / MESH_RESOLUTION_X; private List localPooledPrefabs = new List(); protected void Awake() { mesh = new Mesh(); Vector3[] vertices = new Vector3[MESH_RESOLUTION_X * MESH_RESOLUTION_Y * 4]; int[] triangles = new int[MESH_RESOLUTION_X*MESH_RESOLUTION_Y*3*2]; int vertIndex = 0; int triangleIndex = 0; for(int x=0; x().mesh = mesh; MeshCollider meshCollider = GetComponent(); meshCollider.sharedMesh = mesh; } public void UpdateMesh() { Vector3[] vertices = mesh.vertices; Vector3 vertex; Vector3 worldSpaceVertexPosition; for(int n=0;n points = PoissonDiscSampling.GeneratePoints(7.0f, new Vector2(CHUNK_SIZE, CHUNK_SIZE)); Vector2 globalPos = new Vector2(this.transform.position.x, this.transform.position.z); foreach(Vector2 point in points) { Vector2 globalPoint = point + globalPos; float noise_scale = 0.1f; float noise = Mathf.PerlinNoise(1000.0f + globalPoint.x * noise_scale, 1000.0f + globalPoint.y * noise_scale) * 2.0f - 1.0f; float distanceFromPath = TerrainGenerator.Instance.GetDistanceFromPath(globalPoint.x, globalPoint.y); float height = TerrainGenerator.Instance.GetHeight(globalPoint.x, globalPoint.y); if(height < 16.0f && noise > 0.01f && Mathf.Abs(distanceFromPath) > 1.0f) { GameObject g = PoolManager.Instance.GetInstance(TerrainGenerator.Instance.TreePrefab); if(g != null) { g.transform.position = new Vector3(globalPoint.x, height, globalPoint.y); localPooledPrefabs.Add(g); } } } if(this.transform.position.x == 0 && this.transform.position.z > 0) { int MAX_TREES_PATH = 8; int additionalTrees = (int)Random.Range(0, MAX_TREES_PATH); for(int n=0;n this.transform.position.z || Random.Range(0.0f, 1.0f) < 0.25f ? MAX_PICKUPITEMS_PATH : 0; for(int n=0;n