public void GenerateChunk(Vector2i _generateKey) { //Debug.Log("" + " Start in Thread " + _generateKey); if (GeneratedChunksMap.Count > 0 && !HasNighbour(_generateKey)) { //Debug.Log("Chunk isnĀ“t at startpoint and has no neighbours" + _generateKey); // Que this thread again Thread t = new Thread(() => GenerateChunk(_generateKey)); t.IsBackground = true; threads.Add(t); t.Name = _generateKey + "Thread"; isThreadActive = false; return; } BezierChunk existTest; GeneratedChunksMap.TryGetValue(_generateKey, out existTest); if (existTest != null) { existTest.RebuildChunk(); } else { // create new empty chunk BezierChunk toGenerate = new BezierChunk(); toGenerate.Resolution = Resolution; toGenerate.Seed = StartSeed + _generateKey.GetHashCode(); //Debug.Log(toGenerate.Seed); toGenerate.Steepness = Steepness; toGenerate.MaxOverhang = MaxOverhang; toGenerate.OverhangRatio = OverhangRatio; toGenerate.PatchAmount = Patchamount; toGenerate.Positionkey = _generateKey; toGenerate.GenRef = GenRef; // link neighbours BezierChunk tempLeft; BezierChunk tempRight; BezierChunk tempTop; BezierChunk tempBot; GeneratedChunksMap.TryGetValue(GetNeighbourKeyLeft(_generateKey), out tempLeft); GeneratedChunksMap.TryGetValue(GetNeighbourKeyRight(_generateKey), out tempRight); GeneratedChunksMap.TryGetValue(GetNeighbourKeyTop(_generateKey), out tempTop); GeneratedChunksMap.TryGetValue(GetNeighbourKeyBot(_generateKey), out tempBot); toGenerate.Left = tempLeft; toGenerate.Right = tempRight; toGenerate.Up = tempTop; toGenerate.Down = tempBot; // Assign the noise and generates it toGenerate.AssignNoise(); toGenerate.AverageMidPoint = toGenerate.ChunkNoise.AverageMidPoint; // let the chunk actually calculate toGenerate.AssignPatches(); // give this chunk free to make the mesh from the noise //Thread.Sleep(5000); ChunkMeshToGeneration.Add(toGenerate); //Debug.Log("Thread finished with generation of " + _generateKey); } }
public void GenerateMeshs(int ChunkGenerationDistance) { for (int i = ChunkMeshToGeneration.Count - 1; i >= 0; i--) { BezierChunk toGenerate = ChunkMeshToGeneration[i]; Vector2i _genKey = toGenerate.Positionkey; toGenerate.CalculateMetaMesh(); // Instantiate the Chunk in Unity toGenerate.InstantiateThisChunk(); // add the chunk to the map GeneratedChunksMap.Add(_genKey, toGenerate); // update the noise of the chunkneighbours UpdateNeighbours(_genKey); // delete the created chunk out of the list of chunks which are in creation ChunkMeshToGeneration.RemoveAt(i); ChunksInGeneration.Remove(_genKey); //Debug.Log(_genKey + " generated"); isThreadActive = false; } if (ChunkMeshToGeneration.Count == 0) { UpdateChunksToGenerate(ChunkGenerationDistance); } }