mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 10:59:39 +00:00 
			
		
		
		
	autoupdate damaged chunks, removes instance drawing
This commit is contained in:
		@ -26,9 +26,6 @@ class Game: GameDelegate {
 | 
				
			|||||||
  var projection: matrix_float4x4 = .identity
 | 
					  var projection: matrix_float4x4 = .identity
 | 
				
			||||||
  var world = World()
 | 
					  var world = World()
 | 
				
			||||||
  var cubeMesh: RendererMesh?
 | 
					  var cubeMesh: RendererMesh?
 | 
				
			||||||
 | 
					 | 
				
			||||||
  var renderMode: Bool = false
 | 
					 | 
				
			||||||
  var damageChunks = [SIMD3<Int>: Mesh<VertexPositionNormalColorTexcoord, UInt16>]()
 | 
					 | 
				
			||||||
  var renderChunks = [SIMD3<Int>: RendererMesh]()
 | 
					  var renderChunks = [SIMD3<Int>: RendererMesh]()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func create(_ renderer: Renderer) {
 | 
					  func create(_ renderer: Renderer) {
 | 
				
			||||||
@ -54,15 +51,6 @@ class Game: GameDelegate {
 | 
				
			|||||||
#else
 | 
					#else
 | 
				
			||||||
    self.world.generate(width: 5, height: 3, depth: 5, seed: seed)
 | 
					    self.world.generate(width: 5, height: 3, depth: 5, seed: seed)
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Build chunk meshes
 | 
					 | 
				
			||||||
    self.rebuildChunkMeshes()
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  private func rebuildChunkMeshes() {
 | 
					 | 
				
			||||||
    self.world.forEachChunk { id, chunk in
 | 
					 | 
				
			||||||
      self.damageChunks[id] = ChunkMeshBuilder.build(world: self.world, chunkID: id)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func fixedUpdate(_ time: GameTime) {
 | 
					  func fixedUpdate(_ time: GameTime) {
 | 
				
			||||||
@ -76,30 +64,31 @@ class Game: GameDelegate {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    let deltaTime = min(Float(time.delta.asFloat), 1.0 / 15)
 | 
					    let deltaTime = min(Float(time.delta.asFloat), 1.0 / 15)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var reset = false, generate = false, toggleRenderMode = false
 | 
					    var reset = false, generate = false, regenChunk = false
 | 
				
			||||||
    if let pad = GameController.current?.state {
 | 
					    if let pad = GameController.current?.state {
 | 
				
			||||||
      if pad.pressed(.back) { reset = true }
 | 
					      if pad.pressed(.back) { reset = true }
 | 
				
			||||||
      if pad.pressed(.start) { generate = true }
 | 
					      if pad.pressed(.start) { generate = true }
 | 
				
			||||||
      if pad.pressed(.guide) { toggleRenderMode = true }
 | 
					      if pad.pressed(.guide) { regenChunk = true }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if Keyboard.pressed(.r) { reset = true }
 | 
					    if Keyboard.pressed(.r) { reset = true }
 | 
				
			||||||
    if Keyboard.pressed(.g) { generate = true }
 | 
					    if Keyboard.pressed(.g) { generate = true }
 | 
				
			||||||
    if Keyboard.pressed(.p, repeat: true) { toggleRenderMode = true }
 | 
					    if Keyboard.pressed(.p) { regenChunk = true }
 | 
				
			||||||
    if Keyboard.pressed(.leftBracket) { self.rebuildChunkMeshes() }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Player reset
 | 
					    // Player reset
 | 
				
			||||||
    if reset {
 | 
					    if reset {
 | 
				
			||||||
      self.resetPlayer()
 | 
					      self.resetPlayer()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // Regenerate
 | 
					    // Regenerate world
 | 
				
			||||||
    if generate {
 | 
					    if generate {
 | 
				
			||||||
      self.generateWorld()
 | 
					      self.generateWorld()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if toggleRenderMode {
 | 
					 | 
				
			||||||
      self.renderMode = !self.renderMode
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    self.player.update(deltaTime: deltaTime, world: world, camera: &camera)
 | 
					    self.player.update(deltaTime: deltaTime, world: world, camera: &camera)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Regenerate current chunk
 | 
				
			||||||
 | 
					    if regenChunk {
 | 
				
			||||||
 | 
					      self.world.generate(chunkID: World.makeID(position: self.player.position))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func draw(_ renderer: Renderer, _ time: GameTime) {
 | 
					  func draw(_ renderer: Renderer, _ time: GameTime) {
 | 
				
			||||||
@ -114,18 +103,15 @@ class Game: GameDelegate {
 | 
				
			|||||||
      specular: Color(rgba8888: 0x2F2F2F00).linear,
 | 
					      specular: Color(rgba8888: 0x2F2F2F00).linear,
 | 
				
			||||||
      gloss: 75)
 | 
					      gloss: 75)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if self.renderMode {
 | 
					 | 
				
			||||||
    // Update chunk meshes if needed
 | 
					    // Update chunk meshes if needed
 | 
				
			||||||
      if !self.damageChunks.isEmpty {
 | 
					    self.world.handleRenderDamagedChunks { id, chunk in
 | 
				
			||||||
        for i in self.damageChunks {
 | 
					      let mesh = ChunkMeshBuilder.build(world: self.world, chunkID: id)
 | 
				
			||||||
          if let new = renderer.createMesh(i.1) {
 | 
					      if let renderMesh = renderer.createMesh(mesh) {
 | 
				
			||||||
            self.renderChunks[i.0] = new
 | 
					        self.renderChunks[id] = renderMesh
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
            self.renderChunks.removeValue(forKey: i.0)
 | 
					        self.renderChunks.removeValue(forKey: id)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
        self.damageChunks = [:]
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (id, chunk) in self.renderChunks {
 | 
					    for (id, chunk) in self.renderChunks {
 | 
				
			||||||
      let drawPos = SIMD3<Float>(id &<< Chunk.shift)
 | 
					      let drawPos = SIMD3<Float>(id &<< Chunk.shift)
 | 
				
			||||||
@ -137,9 +123,8 @@ class Game: GameDelegate {
 | 
				
			|||||||
        environment: env,
 | 
					        environment: env,
 | 
				
			||||||
        camera: self.camera)
 | 
					        camera: self.camera)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var instances = self.renderMode ? [Instance]() : world.instances
 | 
					    var instances = [Instance]()
 | 
				
			||||||
    if let position = player.rayhitPos {
 | 
					    if let position = player.rayhitPos {
 | 
				
			||||||
      instances.append(
 | 
					      instances.append(
 | 
				
			||||||
        Instance(
 | 
					        Instance(
 | 
				
			||||||
 | 
				
			|||||||
@ -1,11 +1,19 @@
 | 
				
			|||||||
import Foundation
 | 
					import Foundation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class World {
 | 
					public class World {
 | 
				
			||||||
  private var _chunks: Dictionary<SIMD3<Int>, Chunk>
 | 
					  public typealias ChunkID = SIMD3<Int>
 | 
				
			||||||
 | 
					  @inline(__always) public static func makeID<F: BinaryFloatingPoint>(position: SIMD3<F>) -> ChunkID {
 | 
				
			||||||
 | 
					    makeID(position: SIMD3(Int(floor(position.x)), Int(floor(position.y)), Int(floor(position.z))))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  @inline(__always) public static func makeID(position: SIMD3<Int>) -> ChunkID { position &>> Chunk.shift }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private var _chunks: Dictionary<ChunkID, Chunk>
 | 
				
			||||||
 | 
					  private var _chunkDamage: Set<ChunkID>
 | 
				
			||||||
  private var _generator: WorldGenerator
 | 
					  private var _generator: WorldGenerator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public init() {
 | 
					  public init() {
 | 
				
			||||||
    self._chunks = [:]
 | 
					    self._chunks = [:]
 | 
				
			||||||
 | 
					    self._chunkDamage = []
 | 
				
			||||||
    self._generator = WorldGenerator()
 | 
					    self._generator = WorldGenerator()
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -16,10 +24,32 @@ public class World {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func setBlock(at position: SIMD3<Int>, type: BlockType) {
 | 
					  func setBlock(at position: SIMD3<Int>, type: BlockType) {
 | 
				
			||||||
    self._chunks[position &>> Chunk.shift]?.setBlock(at: position, type: type)
 | 
					    // Find the chunk containing the block position
 | 
				
			||||||
 | 
					    let chunkID = position &>> Chunk.shift
 | 
				
			||||||
 | 
					    if let idx = self._chunks.index(forKey: chunkID) {
 | 
				
			||||||
 | 
					      // Set the block and mark the containing chunk for render update
 | 
				
			||||||
 | 
					      self._chunks.values[idx].setBlock(at: position, type: type)
 | 
				
			||||||
 | 
					      self._chunkDamage.insert(chunkID)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // Mark adjacent chunks for render update when placing along the chunk border
 | 
				
			||||||
 | 
					      let internalPos = position &- chunkID &<< Chunk.shift
 | 
				
			||||||
 | 
					      for (i, ofs) in zip(internalPos.indices, [ SIMD3<Int>.X, .Y, .Z ]) {
 | 
				
			||||||
 | 
					        if internalPos[i] == 0 {
 | 
				
			||||||
 | 
					          let id = chunkID &- ofs
 | 
				
			||||||
 | 
					          if self._chunks.keys.contains(id) {
 | 
				
			||||||
 | 
					            self._chunkDamage.insert(id)
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        } else if internalPos[i] == Chunk.size - 1 {
 | 
				
			||||||
 | 
					          let id = chunkID &+ ofs
 | 
				
			||||||
 | 
					          if self._chunks.keys.contains(id) {
 | 
				
			||||||
 | 
					            self._chunkDamage.insert(id)
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func getChunk(id chunkID: SIMD3<Int>) -> Chunk? {
 | 
					  func getChunk(id chunkID: ChunkID) -> Chunk? {
 | 
				
			||||||
    self._chunks[chunkID]
 | 
					    self._chunks[chunkID]
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -37,25 +67,28 @@ public class World {
 | 
				
			|||||||
        for x in 0..<width {
 | 
					        for x in 0..<width {
 | 
				
			||||||
          let chunkID = SIMD3(x, y, z) &- orig
 | 
					          let chunkID = SIMD3(x, y, z) &- orig
 | 
				
			||||||
          self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
					          self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
				
			||||||
 | 
					          self._chunkDamage.insert(chunkID)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func generate(chunkID: SIMD3<Int>) {
 | 
					  func generate(chunkID: ChunkID) {
 | 
				
			||||||
    self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
					    self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
				
			||||||
 | 
					    self._chunkDamage.insert(chunkID)
 | 
				
			||||||
 | 
					    for i: ChunkID in [ .X, .Y, .Z ] {
 | 
				
			||||||
 | 
					      for otherID in [ chunkID &- i, chunkID &+ i ] {
 | 
				
			||||||
 | 
					        if self._chunks.keys.contains(otherID) {
 | 
				
			||||||
 | 
					          self._chunkDamage.insert(otherID)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  var instances: [Instance] {
 | 
					  func handleRenderDamagedChunks(_ body: (_ id: ChunkID, _ chunk: Chunk) -> Void) {
 | 
				
			||||||
    self._chunks.values.flatMap { chunk in
 | 
					    for id in self._chunkDamage {
 | 
				
			||||||
      chunk.compactMap { block, position in
 | 
					      body(id, self._chunks[id]!)
 | 
				
			||||||
        if case let .solid(color) = block.type {
 | 
					    }
 | 
				
			||||||
          Instance(
 | 
					    self._chunkDamage.removeAll(keepingCapacity: true)
 | 
				
			||||||
            position: SIMD3<Float>(position) + 0.5,
 | 
					 | 
				
			||||||
            scale:    .init(repeating: 0.5),
 | 
					 | 
				
			||||||
            color:    color)
 | 
					 | 
				
			||||||
        } else { nil }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user