From 54ed81b15d60c389569c5235059ec21c6ac6051b Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Thu, 5 Sep 2024 21:16:29 -0400 Subject: [PATCH] fix ghost chunks and wait for generation on startup --- Sources/Voxelotl/ChunkGeneration.swift | 4 ++++ Sources/Voxelotl/ChunkMeshGeneration.swift | 2 +- Sources/Voxelotl/Game.swift | 8 ++++++-- Sources/Voxelotl/World.swift | 4 ++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Sources/Voxelotl/ChunkGeneration.swift b/Sources/Voxelotl/ChunkGeneration.swift index 9f67b61..c3467cc 100644 --- a/Sources/Voxelotl/ChunkGeneration.swift +++ b/Sources/Voxelotl/ChunkGeneration.swift @@ -50,6 +50,10 @@ public struct ChunkGeneration { } } + public func waitForActiveOperations() { + self.queue.waitUntilAllOperationsAreFinished() + } + public mutating func acceptReadyChunks() { guard let world = self.world else { return diff --git a/Sources/Voxelotl/ChunkMeshGeneration.swift b/Sources/Voxelotl/ChunkMeshGeneration.swift index 3e1256f..a58ccfa 100644 --- a/Sources/Voxelotl/ChunkMeshGeneration.swift +++ b/Sources/Voxelotl/ChunkMeshGeneration.swift @@ -2,7 +2,7 @@ import Foundation public struct ChunkMeshGeneration { private let queue: OperationQueue - private let localReadyMeshes = ConcurrentDictionary, RendererMesh>() + private let localReadyMeshes = ConcurrentDictionary, RendererMesh?>() weak var game: Game? weak var renderer: Renderer? diff --git a/Sources/Voxelotl/Game.swift b/Sources/Voxelotl/Game.swift index 7c599d6..6871058 100644 --- a/Sources/Voxelotl/Game.swift +++ b/Sources/Voxelotl/Game.swift @@ -7,13 +7,14 @@ class Game: GameDelegate { var projection: matrix_float4x4 = .identity var world = World(generator: StandardWorldGenerator()) var cubeMesh: RendererMesh? - var renderChunks = [SIMD3: RendererMesh]() + var renderChunks = [SIMD3: RendererMesh?]() var chunkMeshGeneration: ChunkMeshGeneration! var modelBatch: ModelBatch! func create(_ renderer: Renderer) { self.resetPlayer() self.generateWorld() + self.world.waitForActiveOperations() self.cubeMesh = renderer.createMesh(CubeMeshBuilder.build(bound: .fromUnitCube(position: .zero, scale: .one))) @@ -104,8 +105,11 @@ class Game: GameDelegate { self.modelBatch.begin(camera: camera, environment: env) for (id, chunk) in self.renderChunks { + if chunk == nil { + continue + } let drawPos = SIMD3(id &<< Chunk.shift) - self.modelBatch.draw(.init(mesh: chunk, material: material), position: drawPos) + self.modelBatch.draw(.init(mesh: chunk!, material: material), position: drawPos) } if let position = player.rayhitPos { diff --git a/Sources/Voxelotl/World.swift b/Sources/Voxelotl/World.swift index 40687a5..7828e78 100644 --- a/Sources/Voxelotl/World.swift +++ b/Sources/Voxelotl/World.swift @@ -106,6 +106,10 @@ public class World { self._chunkGeneration.acceptReadyChunks() } + public func waitForActiveOperations() { + self._chunkGeneration.waitForActiveOperations() + } + func handleRenderDamagedChunks(_ body: (_ id: ChunkID, _ chunk: Chunk) -> Void) { for id in self._chunkDamage { body(id, self._chunks[id]!)