Files
voxelotl-engine/Sources/Voxelotl/Generator/StandardWorldGenerator.swift

39 lines
1.5 KiB
Swift
Raw Normal View History

struct StandardWorldGenerator: WorldGenerator {
private var heightNoise: LayeredNoiseAlt<SimplexNoise<Float>>!
2024-09-06 01:38:57 +10:00
private var terrainNoise: LayeredNoise<ImprovedPerlin<Float>>!
private var colorNoise: LayeredNoiseAlt<SimplexNoise<Float>>!
2024-08-30 21:56:39 +10:00
public mutating func reset(seed: UInt64) {
2024-09-06 01:38:57 +10:00
var random = PCG32Random(seed: SplitMix64.createState(seed: seed))
2024-08-30 21:56:39 +10:00
2024-09-06 01:38:57 +10:00
self.heightNoise = .init(random: &random, octaves: 200, frequency: 0.0002, amplitude: 2.000)
self.terrainNoise = .init(random: &random, octaves: 10, frequency: 0.01, amplitude: 0.437)
self.colorNoise = .init(random: &random, octaves: 150, frequency: 0.0006667, amplitude: 17.000)
2024-08-30 21:56:39 +10:00
}
public func makeChunk(id chunkID: SIMD3<Int>) -> Chunk {
let chunkOrigin = chunkID &<< Chunk.shift
var chunk = Chunk(position: chunkOrigin)
for z in 0..<Chunk.size {
for x in 0..<Chunk.size {
let height = self.heightNoise.get(SIMD2<Float>(chunkOrigin.xz &+ SIMD2<Int>(x, z)))
for y in 0..<Chunk.size {
let ipos = SIMD3(x, y, z)
let fpos = SIMD3<Float>(chunkOrigin &+ ipos)
let height = fpos.y / 64.0 + height
2024-09-06 01:38:57 +10:00
let value = height + self.terrainNoise.get(fpos * SIMD3(1, 2, 1))
let block: BlockType = if value < 0 {
.solid(.init(
hue: Float(180 + self.colorNoise.get(fpos) * 180),
2024-09-06 01:38:57 +10:00
saturation: 0.47, value: 0.9).linear)
} else {
.air
}
chunk.setBlock(internal: ipos, type: block)
}
2024-08-30 21:56:39 +10:00
}
}
2024-08-30 21:56:39 +10:00
return chunk
}
}