/// <summary> /// Generate a terrain for preview the NoiseManager values. /// </summary> public void GenerateTerrain() { if (chunkDict.Count != 0) { foreach (Chunk chunk in chunkDict.Values) { Destroy(chunk.gameObject); } chunkDict.Clear(); } int halfSize = Mathf.FloorToInt(testSize / 2); for (int z = -halfSize; z < halfSize + 1; z++) { for (int x = -halfSize; x < halfSize + 1; x++) { Vector2Int key = new Vector2Int(x, z); GameObject chunkObj = new GameObject("Chunk_" + key.x + "|" + key.y, typeof(MeshFilter), typeof(MeshRenderer)); chunkObj.transform.parent = transform; chunkObj.transform.position = new Vector3(key.x * Constants.CHUNK_SIDE, 0, key.y * Constants.CHUNK_SIDE); Vector2Int offsetKey = new Vector2Int(x + chunkOffset.x, z + chunkOffset.y); chunkDict.Add(key, chunkObj.AddComponent <Chunk>().ChunkInit(noiseManager.GenerateChunkData(offsetKey), key.x, key.y, fakeRegion, false)); } } }
/// <summary> /// Load one chunk per frame from the chunkLoadList /// </summary> void LoadChunkFromList() { if (chunkLoadList.Count == 0) { return; } Vector2Int key = chunkLoadList[0]; Vector2Int regionPos = new Vector2Int(Mathf.FloorToInt(((float)key.x) / Constants.REGION_SIZE), Mathf.FloorToInt(((float)key.y) / Constants.REGION_SIZE)); if (!regionDict.ContainsKey(regionPos))//In case that the chunk isn't in the loaded regions we remove it, tp or too fast movement. { chunkLoadList.RemoveAt(0); return; } GameObject chunkObj = new GameObject("Chunk_" + key.x + "|" + key.y, typeof(MeshFilter), typeof(MeshRenderer)); chunkObj.transform.parent = transform; chunkObj.layer = chunkLayer; chunkObj.tag = "Terrain"; chunkObj.transform.position = new Vector3(key.x * Constants.CHUNK_SIDE, 0, key.y * Constants.CHUNK_SIDE); //Debug.Log("Try load: "+x+"|"+z +" in "+regionPos); Vector2Int keyInsideChunk = new Vector2Int(key.x - regionPos.x * Constants.REGION_SIZE, key.y - regionPos.y * Constants.REGION_SIZE); //We get X and Y in the world position, we need calculate the x and y in the region. int chunkIndexInRegion = regionDict[regionPos].GetChunkIndex(keyInsideChunk.x, keyInsideChunk.y); if (chunkIndexInRegion != 0)//Load chunk from a region data { chunkDict.Add(key, chunkObj.AddComponent <Chunk>().ChunkInit(regionDict[regionPos].GetChunkData(chunkIndexInRegion), keyInsideChunk.x, keyInsideChunk.y, regionDict[regionPos], false)); } else //Generate chunk with the noise generator { chunkDict.Add(key, chunkObj.AddComponent <Chunk>().ChunkInit(noiseManager.GenerateChunkData(key), keyInsideChunk.x, keyInsideChunk.y, regionDict[regionPos], Constants.SAVE_GENERATED_CHUNKS)); } chunkLoadList.RemoveAt(0); }