import Foundation public class World { private var _chunks: Dictionary, Chunk> private var noise: ImprovedPerlin! private var noise2: SimplexNoise! public init() { self._chunks = [:] } func getBlock(at position: SIMD3) -> Block { return if let chunk = self._chunks[position &>> Chunk.shift] { chunk.getBlock(at: position) } else { Block(.air) } } func setBlock(at position: SIMD3, type: BlockType) { self._chunks[position &>> Chunk.shift]?.setBlock(at: position, type: type) } func generate(width: Int, height: Int, depth: Int, random: inout any RandomProvider) { self.noise = ImprovedPerlin(random: &random) self.noise2 = SimplexNoise(random: &random) for x in 0..) { let chunkOrigin = chunkID &<< Chunk.shift var chunk = Chunk(position: chunkOrigin) chunk.fill(allBy: { position in let fpos = SIMD3(position) let threshold: Float = 0.6 let value = fpos.y / Float(Chunk.size) + self.noise.get(fpos * 0.05) * 1.1 + self.noise.get(fpos * 0.10) * 0.5 + self.noise.get(fpos * 0.30) * 0.23 return if value < threshold { .solid(.init( hue: Float16(180 + self.noise2.get(fpos * 0.05) * 180), saturation: Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 4)) * 0.5), value: Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 9)) * 0.5).lerp(0.5, 1)).linear) } else { .air } }) self._chunks[chunkID] = chunk } var instances: [Instance] { self._chunks.values.flatMap { chunk in chunk.compactMap { block, position in if case let .solid(color) = block.type { Instance( position: SIMD3(position) + 0.5, scale: .init(repeating: 0.5), color: color) } else { nil } } } } }