split worldgen

This commit is contained in:
2024-08-30 21:56:39 +10:00
parent 9951d56a12
commit c99155fb47
4 changed files with 55 additions and 41 deletions

View File

@ -2,11 +2,11 @@ import Foundation
public class World {
private var _chunks: Dictionary<SIMD3<Int>, Chunk>
private var noise: ImprovedPerlin<Float>!
private var noise2: SimplexNoise<Float>!
private var _generator: WorldGenerator
public init() {
self._chunks = [:]
self._generator = WorldGenerator()
}
func getBlock(at position: SIMD3<Int>) -> Block {
@ -19,40 +19,21 @@ public class World {
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<Float>(random: &random)
self.noise2 = SimplexNoise<Float>(random: &random)
for x in 0..<width {
func generate(width: Int, height: Int, depth: Int, seed: UInt64) {
self._generator.reset(seed: seed)
let orig = SIMD3(width, height, depth) / 2
for z in 0..<depth {
for y in 0..<height {
for z in 0..<depth {
let chunkID = SIMD3(x, y, z) &- SIMD3(width, height, depth) / 2
self.generate(chunkID: chunkID)
for x in 0..<width {
let chunkID = SIMD3(x, y, z) &- orig
self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
}
}
}
}
func generate(chunkID: SIMD3<Int>) {
let chunkOrigin = chunkID &<< Chunk.shift
var chunk = Chunk(position: chunkOrigin)
chunk.fill(allBy: { position in
let fpos = SIMD3<Float>(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
self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
}
var instances: [Instance] {