Bring back lazingly initialized perlin noise

This commit is contained in:
DBotThePony 2024-04-10 10:07:44 +07:00
parent 318b689d2d
commit b3b51aefa5
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 40 additions and 10 deletions

View File

@ -36,16 +36,16 @@ abstract class AbstractPerlinNoise(val parameters: PerlinNoiseParameters) {
protected data class Setup(val b0: Int, val b1: Int, val r0: Double, val r1: Double)
@JvmField protected val p = IntArray(parameters.scale * 2 + 2)
@JvmField protected val g1 = DoubleArray(parameters.scale * 2 + 2)
protected val p by lazy(LazyThreadSafetyMode.NONE) { IntArray(parameters.scale * 2 + 2) }
protected val g1 by lazy(LazyThreadSafetyMode.NONE) { DoubleArray(parameters.scale * 2 + 2) }
// flat arrays for performance
@JvmField protected val g2_0 = DoubleArray(parameters.scale * 2 + 2)
@JvmField protected val g2_1 = DoubleArray(parameters.scale * 2 + 2)
protected val g2_0 by lazy(LazyThreadSafetyMode.NONE) { DoubleArray(parameters.scale * 2 + 2) }
protected val g2_1 by lazy(LazyThreadSafetyMode.NONE) { DoubleArray(parameters.scale * 2 + 2) }
@JvmField protected val g3_0 = DoubleArray(parameters.scale * 2 + 2)
@JvmField protected val g3_1 = DoubleArray(parameters.scale * 2 + 2)
@JvmField protected val g3_2 = DoubleArray(parameters.scale * 2 + 2)
protected val g3_0 by lazy(LazyThreadSafetyMode.NONE) { DoubleArray(parameters.scale * 2 + 2) }
protected val g3_1 by lazy(LazyThreadSafetyMode.NONE) { DoubleArray(parameters.scale * 2 + 2) }
protected val g3_2 by lazy(LazyThreadSafetyMode.NONE) { DoubleArray(parameters.scale * 2 + 2) }
init {
if (parameters.seed != null && parameters.type != PerlinNoiseParameters.Type.UNITIALIZED) {
@ -53,13 +53,34 @@ abstract class AbstractPerlinNoise(val parameters: PerlinNoiseParameters) {
}
}
fun init(seed: Long, now: Boolean = false) {
private var init = false
private val initLock = Any()
protected fun checkInit() {
check(hasSeedSpecified) { "No noise seed specified" }
if (!init) {
synchronized(initLock) {
if (!init) {
init0(seed)
init = true
}
}
}
}
fun init(seed: Long) {
if (parameters.type == PerlinNoiseParameters.Type.UNITIALIZED)
return
this.hasSeedSpecified = true
this.seed = seed
synchronized(initLock) {
this.init = false
this.hasSeedSpecified = true
this.seed = seed
}
}
private fun init0(seed: Long) {
val p = p
val g1 = g1
val g2_0 = g2_0

View File

@ -9,6 +9,7 @@ class BillowNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double): Double {
checkInit()
var sum = 0.0
var p = x * parameters.frequency
var scale = 1.0
@ -23,6 +24,7 @@ class BillowNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double, y: Double): Double {
checkInit()
var sum = 0.0
var px = x * parameters.frequency
var py = y * parameters.frequency
@ -39,6 +41,7 @@ class BillowNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double, y: Double, z: Double): Double {
checkInit()
var sum = 0.0
var px = x * parameters.frequency
var py = y * parameters.frequency

View File

@ -8,6 +8,7 @@ class PerlinNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double): Double {
checkInit()
var sum = 0.0
var p = x * parameters.frequency
var scale = 1.0
@ -22,6 +23,7 @@ class PerlinNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double, y: Double): Double {
checkInit()
var sum = 0.0
var px = x * parameters.frequency
var py = y * parameters.frequency
@ -38,6 +40,7 @@ class PerlinNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double, y: Double, z: Double): Double {
checkInit()
var sum = 0.0
var px = x * parameters.frequency
var py = y * parameters.frequency

View File

@ -9,6 +9,7 @@ class RidgedNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double): Double {
checkInit()
var sum = 0.0
var p = x * parameters.frequency
var scale = 1.0
@ -31,6 +32,7 @@ class RidgedNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double, y: Double): Double {
checkInit()
var sum = 0.0
var px = x * parameters.frequency
var py = y * parameters.frequency
@ -55,6 +57,7 @@ class RidgedNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(param
}
override fun get(x: Double, y: Double, z: Double): Double {
checkInit()
var sum = 0.0
var px = x * parameters.frequency
var py = y * parameters.frequency