Add support for derivatives in FBM

This commit is contained in:
Astrash 2024-10-09 20:39:45 +11:00
parent 158ffba2a5
commit 9f425c6159

View File

@ -7,6 +7,7 @@
package com.dfsek.terra.addons.noise.samplers.noise.fractal;
import com.dfsek.terra.api.noise.DerivativeNoiseSampler;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.util.MathUtil;
@ -52,4 +53,59 @@ public class BrownianMotionSampler extends FractalNoiseFunction {
return sum;
}
@Override
public boolean isDifferentiable() {
return input instanceof DerivativeNoiseSampler dSampler && dSampler.isDifferentiable();
}
@Override
public double[] getNoiseDerivativeRaw(long seed, double x, double y) {
double[] sum = {0, 0, 0};
double amp = fractalBounding;
for(int i = 0; i < octaves; i++) {
// This should only be called after `input` is verified as a `DerivativeNoiseSampler`
// so this should be a safe cast
double[] noise = ((DerivativeNoiseSampler) input).noised(seed++, x, y);
sum[0] += noise[0] * amp;
// Directional derivative of each octave can be subject to the same addition and product
// as per derivative sum and product rules in order to produce the correct final derivative
sum[1] += noise[1] * amp;
sum[2] += noise[2] * amp;
amp *= MathUtil.lerp(weightedStrength, 1.0, Math.min(noise[0] + 1, 2) * 0.5);
x *= lacunarity;
y *= lacunarity;
amp *= gain;
}
return sum;
}
@Override
public double[] getNoiseDerivativeRaw(long seed, double x, double y, double z) {
double[] sum = {0, 0, 0};
double amp = fractalBounding;
for(int i = 0; i < octaves; i++) {
double[] noise = ((DerivativeNoiseSampler) input).noised(seed++, x, y, z);
sum[0] += noise[0] * amp;
// See comment in 2D version
sum[1] += noise[1] * amp;
sum[2] += noise[2] * amp;
amp *= MathUtil.lerp(weightedStrength, 1.0, (noise[0] + 1) * 0.5);
x *= lacunarity;
y *= lacunarity;
z *= lacunarity;
amp *= gain;
}
return sum;
}
}