technicolour icecream hellscape terrain

This commit is contained in:
2024-09-05 22:32:30 +10:00
parent 301aa28c4d
commit fed6a26882
6 changed files with 97 additions and 42 deletions

View File

@ -58,3 +58,51 @@ public extension LayeredNoise where Generator: CoherentNoise4D {
$0 + $1 } * self._amplitudeAdjusted
}
}
//MARK: - Experimental
public struct LayeredNoiseAlt<Generator: CoherentNoise> {
public typealias Scalar = Generator.Scalar
public let octaves: Int
public let frequency: Scalar
public let amplitude: Scalar
private let _generator: Generator
private let _amplitudeAdjusted: Scalar
}
public extension LayeredNoiseAlt where Generator: CoherentNoiseRandomInit {
init<Random: RandomProvider>(random: inout Random, octaves: Int, frequency: Scalar, amplitude: Scalar = 1) {
self.octaves = octaves
self.frequency = frequency
self.amplitude = amplitude
self._generator = Generator(random: &random)
self._amplitudeAdjusted = amplitude / 2
}
}
public extension LayeredNoiseAlt where Generator: CoherentNoise3D & CoherentNoise2D {
func get(_ point: SIMD2<Scalar>) -> Scalar {
let layerOffset: Int = 1
return (1..<self.octaves).map { term in
let mul = Scalar(1 + term)
let point3D = SIMD3(point * frequency * mul, Scalar(term * layerOffset))
return self._generator.get(point3D) / mul
}.reduce(self._generator.get(point * frequency)) {
$0 + $1 } * self._amplitudeAdjusted
}
}
public extension LayeredNoiseAlt where Generator: CoherentNoise4D & CoherentNoise3D {
func get(_ point: SIMD3<Scalar>) -> Scalar {
let layerOffset: Int = 1
return (1..<self.octaves).map { term in
let mul = Scalar(1 + term)
let point4D = SIMD4(point * frequency * mul, Scalar(term * layerOffset))
return self._generator.get(point4D) / mul
}.reduce(self._generator.get(point * frequency)) {
$0 + $1 } * self._amplitudeAdjusted
}
}

View File

@ -3,22 +3,6 @@ import Foundation
public struct SimplexNoise<Scalar: BinaryFloatingPoint & SIMDScalar>: CoherentNoise2D, CoherentNoise3D, CoherentNoise4D, CoherentNoiseRandomInit, CoherentNoiseTableInit {
private let p: [Int16], pMod12: [Int16]
private let grad3: [SIMD3<Scalar>] = [
.init(1, 1, 0), .init(-1, 1, 0), .init(1, -1, 0), .init(-1, -1, 0),
.init(1, 0, 1), .init(-1, 0, 1), .init(1, 0, -1), .init(-1, 0, -1),
.init(0, 1, 1), .init( 0, -1, 1), .init(0, 1, -1), .init( 0, -1, -1)
]
private let grad4: [SIMD4<Scalar>] = [
.init( 0, 1, 1, 1), .init( 0, 1, 1, -1), .init( 0, 1, -1, 1), .init( 0, 1, -1, -1),
.init( 0, -1, 1, 1), .init( 0, -1, 1, -1), .init( 0, -1, -1, 1), .init( 0, -1, -1, -1),
.init( 1, 0, 1, 1), .init( 1, 0, 1, -1), .init( 1, 0, -1, 1), .init( 1, 0, -1, -1),
.init(-1, 0, 1, 1), .init(-1, 0, 1, -1), .init(-1, 0, -1, 1), .init(-1, 0, -1, -1),
.init( 1, 1, 0, 1), .init( 1, 1, 0, -1), .init( 1, -1, 0, 1), .init( 1, -1, 0, -1),
.init(-1, 1, 0, 1), .init(-1, 1, 0, -1), .init(-1, -1, 0, 1), .init(-1, -1, 0, -1),
.init( 1, 1, 1, 0), .init( 1, 1, -1, 0), .init( 1, -1, 1, 0), .init( 1, -1, -1, 0),
.init(-1, 1, 1, 0), .init(-1, 1, -1, 0), .init(-1, -1, 1, 0), .init(-1, -1, -1, 0)
]
public init(permutation: [Int16]) {
assert(permutation.count == 0x100)
self.p = permutation
@ -58,7 +42,7 @@ public struct SimplexNoise<Scalar: BinaryFloatingPoint & SIMDScalar>: CoherentNo
return 0
} else {
t *= t
return t * t * self.grad3[gradID].xy.dot(corner)
return t * t * SIMD2<Scalar>(grad3[gradID].xy).dot(corner)
}
}
let noise0 = cornerContribution(corner0, gradIndex0)
@ -125,7 +109,7 @@ public struct SimplexNoise<Scalar: BinaryFloatingPoint & SIMDScalar>: CoherentNo
return 0
} else {
t *= t
return t * t * self.grad3[gradID].dot(corner)
return t * t * SIMD3<Scalar>(grad3[gradID]).dot(corner)
}
}
let noise0 = cornerContribution(corner0, gradCorner0)
@ -197,7 +181,7 @@ public struct SimplexNoise<Scalar: BinaryFloatingPoint & SIMDScalar>: CoherentNo
return 0
} else {
t *= t
return t * t * self.grad4[gradID].dot(corner)
return t * t * SIMD4<Scalar>(grad4[gradID]).dot(corner)
}
}
let noise0 = cornerContribution(corner0, gradIndex0)
@ -212,3 +196,20 @@ public struct SimplexNoise<Scalar: BinaryFloatingPoint & SIMDScalar>: CoherentNo
@inline(__always) fileprivate func perm(_ idx: Int) -> Int { Int(self.p[idx & 0xFF]) }
@inline(__always) fileprivate func permMod12(_ idx: Int) -> Int { Int(self.pMod12[idx & 0xFF]) }
}
fileprivate let grad3: [SIMD3<Int16>] = [
.init(1, 1, 0), .init(-1, 1, 0), .init(1, -1, 0), .init(-1, -1, 0),
.init(1, 0, 1), .init(-1, 0, 1), .init(1, 0, -1), .init(-1, 0, -1),
.init(0, 1, 1), .init( 0, -1, 1), .init(0, 1, -1), .init( 0, -1, -1)
]
fileprivate let grad4: [SIMD4<Int16>] = [
.init( 0, 1, 1, 1), .init( 0, 1, 1, -1), .init( 0, 1, -1, 1), .init( 0, 1, -1, -1),
.init( 0, -1, 1, 1), .init( 0, -1, 1, -1), .init( 0, -1, -1, 1), .init( 0, -1, -1, -1),
.init( 1, 0, 1, 1), .init( 1, 0, 1, -1), .init( 1, 0, -1, 1), .init( 1, 0, -1, -1),
.init(-1, 0, 1, 1), .init(-1, 0, 1, -1), .init(-1, 0, -1, 1), .init(-1, 0, -1, -1),
.init( 1, 1, 0, 1), .init( 1, 1, 0, -1), .init( 1, -1, 0, 1), .init( 1, -1, 0, -1),
.init(-1, 1, 0, 1), .init(-1, 1, 0, -1), .init(-1, -1, 0, 1), .init(-1, -1, 0, -1),
.init( 1, 1, 1, 0), .init( 1, 1, -1, 0), .init( 1, -1, 1, 0), .init( 1, -1, -1, 0),
.init(-1, 1, 1, 0), .init(-1, 1, -1, 0), .init(-1, -1, 1, 0), .init(-1, -1, -1, 0)
]