funny colours (world)

This commit is contained in:
a dinosaur 2024-08-30 01:58:52 +10:00
parent c5dd2caf48
commit cc936248e5
5 changed files with 62 additions and 5 deletions

View File

@ -9,6 +9,7 @@ add_executable(Voxelotl MACOSX_BUNDLE
# Maths library
Math/FloatExtensions.swift
Math/IntegerExtensions.swift
Math/VectorExtensions.swift
Math/Matrix4x4.swift
Math/Point.swift

View File

@ -161,3 +161,37 @@ public extension SIMD4 {
self = other.values
}
}
public extension Color where T: FloatingPoint {
init(hue: T, saturation: T, value: T) {
if saturation == 0 {
self.init(r: value, g: value, b: value, a: 1)
} else {
let hue = hue.floorMod(360)
let rescale: T = 1 / 60
let interp = (hue - floor(hue * rescale) * 60) * rescale
let invInterp = (1 - interp)
let base = 1 - saturation
let dark = base * value
let rise = value * interp + dark * invInterp
let fall = dark * interp + value * invInterp
if hue < 60 {
self.init(r: value, g: rise, b: dark, a: 1)
} else if hue < 120 {
self.init(r: fall, g: value, b: dark, a: 1)
} else if hue < 180 {
self.init(r: dark, g: value, b: rise, a: 1)
} else if hue < 240 {
self.init(r: dark, g: fall, b: value, a: 1)
} else if hue < 300 {
self.init(r: rise, g: dark, b: value, a: 1)
} else {
self.init(r: value, g: dark, b: fall, a: 1)
}
}
}
}

View File

@ -10,4 +10,13 @@ public extension FloatingPoint {
@inline(__always) func smoothStep() -> Self { self * self * (3 - 2 * self) }
@inline(__always) func smootherStep() -> Self { self * self * self * (self * (self * 6 - 15) + 10) }
@inline(__always) func euclidianMod(_ divisor: Self) -> Self { self.floorMod(abs(divisor)) }
@inline(__always) func floorMod(_ divisor: Self) -> Self {
//fmod(fmod(self, divisor) + divisor, divisor)
(self.truncateMod(divisor) + divisor).truncateMod(divisor)
}
@inline(__always) func truncateMod(_ divisor: Self) -> Self {
self.truncatingRemainder(dividingBy: divisor)
}
}

View File

@ -0,0 +1,12 @@
public extension BinaryInteger {
@inline(__always) func euclidianMod(_ divisor: Self) -> Self {
self.floorMod(divisor < 0 ? divisor * -1 : divisor)
}
@inline(__always) func floorMod(_ divisor: Self) -> Self {
//(self % divisor + divisor) % divisor
(self.truncateMod(divisor) + divisor).truncateMod(divisor)
}
@inline(__always) func truncateMod(_ divisor: Self) -> Self {
self.quotientAndRemainder(dividingBy: divisor).remainder
}
}

View File

@ -19,6 +19,7 @@ public class World {
func generate(width: Int, height: Int, depth: Int, random: inout any RandomProvider) {
let noise = ImprovedPerlin<Float>(random: &random)
let noise2 = SimplexNoise<Float>(random: &random)
for x in 0..<width {
for y in 0..<height {
@ -30,12 +31,12 @@ public class World {
let fpos = SIMD3<Float>(position)
return if fpos.y / Float(Chunk.size)
+ noise.get(fpos * 0.05) * 1.1
+ noise.get(fpos * 0.1 + 500) * 0.5
+ noise.get(fpos * 0.3 + 100) * 0.23 < 0.6 {
+ noise.get(fpos * 0.10) * 0.5
+ noise.get(fpos * 0.30) * 0.23 < 0.6 {
.solid(.init(
r: Float16(noise.get(fpos * 0.1)),
g: Float16(noise.get(fpos * 0.1 + 10)),
b: Float16(noise.get(fpos * 0.1 + 100))).mix(.white, 0.6).linear)
hue: Float16(180 + noise2.get(fpos * 0.05) * 180),
saturation: Float16(0.5 + noise2.get(SIMD4(fpos * 0.05, 4)) * 0.5),
value: Float16(0.5 + noise2.get(SIMD4(fpos * 0.05, 9)) * 0.5).lerp(0.5, 1)).linear)
} else {
.air
}