Skip to content

Commit

Permalink
Merge branch 'dev/fractal-gavoro-pseudoerosion' into ver/6.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
duplexsystem committed Sep 21, 2024
2 parents 06457bf + 823afd5 commit b14b355
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.inject.annotations.Inject;
import com.dfsek.terra.api.noise.DerivativeNoiseSampler;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.util.reflection.TypeKey;
Expand Down Expand Up @@ -71,7 +72,8 @@ public void initialize() {
(type, o, loader, depthTracker) -> DistanceSampler.DistanceFunction.valueOf((String) o))
.applyLoader(DimensionApplicableNoiseSampler.class, DimensionApplicableNoiseSampler::new)
.applyLoader(FunctionTemplate.class, FunctionTemplate::new)
.applyLoader(CubicSpline.Point.class, CubicSplinePointTemplate::new);
.applyLoader(CubicSpline.Point.class, CubicSplinePointTemplate::new)
.applyLoader(DerivativeNoiseSampler.class, DerivativeNoiseSamplerTemplate::new);

noiseRegistry.register(addon.key("LINEAR"), LinearNormalizerTemplate::new);
noiseRegistry.register(addon.key("NORMAL"), NormalNormalizerTemplate::new);
Expand All @@ -94,7 +96,8 @@ public void initialize() {
noiseRegistry.register(addon.key("PERLIN"), () -> new SimpleNoiseTemplate(PerlinSampler::new));
noiseRegistry.register(addon.key("SIMPLEX"), () -> new SimpleNoiseTemplate(SimplexSampler::new));
noiseRegistry.register(addon.key("GABOR"), GaborNoiseTemplate::new);

noiseRegistry.register(addon.key("PSEUDOEROSION"), PseudoErosionTemplate::new);
noiseRegistry.register(addon.key("DERIVATIVE"), DerivativeFractalTemplate::new);

noiseRegistry.register(addon.key("VALUE"), () -> new SimpleNoiseTemplate(ValueSampler::new));
noiseRegistry.register(addon.key("VALUE_CUBIC"), () -> new SimpleNoiseTemplate(ValueCubicSampler::new));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dfsek.terra.addons.noise.config.templates;

import com.dfsek.tectonic.api.config.template.annotations.Value;
import com.dfsek.tectonic.api.exception.ValidationException;

import com.dfsek.terra.api.noise.DerivativeNoiseSampler;
import com.dfsek.terra.api.noise.NoiseSampler;


public class DerivativeNoiseSamplerTemplate extends SamplerTemplate<DerivativeNoiseSampler> {

@Value(".")
private NoiseSampler sampler;

@Override
public boolean validate() throws ValidationException {
if (!DerivativeNoiseSampler.isDifferentiable(sampler)) throw new ValidationException("Provided sampler does not support calculating a derivative");
return super.validate();
}

@Override
public DerivativeNoiseSampler get() {
return (DerivativeNoiseSampler) sampler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.dfsek.terra.addons.noise.config.templates.noise;

import com.dfsek.tectonic.api.config.template.annotations.Default;
import com.dfsek.tectonic.api.config.template.annotations.Value;

import com.dfsek.terra.addons.noise.config.templates.SamplerTemplate;
import com.dfsek.terra.addons.noise.samplers.noise.simplex.DerivativeFractal;


public class DerivativeFractalTemplate extends SamplerTemplate<DerivativeFractal> {

@Value("octaves")
@Default
private int octaves = 3;

@Value("gain")
@Default
private double gain = 0.5;

@Value("lacunarity")
@Default
private double lacunarity = 2.0;

@Value("frequency")
@Default
private double frequency = 0.02;

@Override
public DerivativeFractal get() {
return new DerivativeFractal(octaves, gain, lacunarity, frequency);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.dfsek.terra.addons.noise.config.templates.noise;

import com.dfsek.tectonic.api.config.template.annotations.Default;
import com.dfsek.tectonic.api.config.template.annotations.Value;

import com.dfsek.terra.addons.noise.config.templates.SamplerTemplate;
import com.dfsek.terra.addons.noise.samplers.noise.simplex.PseudoErosion;
import com.dfsek.terra.api.noise.DerivativeNoiseSampler;


public class PseudoErosionTemplate extends SamplerTemplate<PseudoErosion> {

@Value("octaves")
@Default
private int octaves = 4;

@Value("lacunarity")
@Default
private double lacunarity = 2.0;

@Value("gain")
@Default
private double gain = 0.5;

@Value("slope-strength")
@Default
private double slopeStrength = 1.0;

@Value("branch-strength")
@Default
private double branchStrength = 1.0;

@Value("strength")
@Default
private double strength = 0.04;

@Value("erosion-frequency")
@Default
private double erosionFrequency = 0.02;

@Value("sampler")
private DerivativeNoiseSampler heightSampler;

@Value("slope-mask.enable")
@Default
private boolean slopeMask = true;

@Value("slope-mask.none")
@Default
private double slopeMaskNone = -0.5;

@Value("slope-mask.full")
@Default
private double slopeMaskFull = 1;

@Value("jitter")
@Default
private double jitterModifier = 1;

@Value("average-impulses")
@Default
private boolean averageErosionImpulses = true;

@Override
public PseudoErosion get() {
return new PseudoErosion(octaves, gain, lacunarity,
slopeStrength, branchStrength, strength,
erosionFrequency, heightSampler, slopeMask, slopeMaskFull, slopeMaskNone, jitterModifier, averageErosionImpulses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.dfsek.terra.addons.noise.samplers.noise.simplex;

import com.dfsek.terra.api.noise.DerivativeNoiseSampler;

import static com.dfsek.terra.addons.noise.samplers.noise.simplex.PseudoErosion.dot;
import static com.dfsek.terra.addons.noise.samplers.noise.simplex.PseudoErosion.hash;
import static com.dfsek.terra.addons.noise.samplers.noise.simplex.PseudoErosion.hashX;
import static com.dfsek.terra.addons.noise.samplers.noise.simplex.PseudoErosion.hashY;


/**
* Temporary sampler that provides derivatives to test pseudoerosion, should be replaced with
* derivative versions of existing samplers
*/
public class DerivativeFractal implements DerivativeNoiseSampler {

private final int heightOctaves;
private final double heightGain;
private final double heightLacunarity;
private final double frequency;

public DerivativeFractal(int octaves, double gain, double lacunarity, double frequency) {
this.heightOctaves = octaves;
this.heightGain = gain;
this.heightLacunarity = lacunarity;
this.frequency = frequency;
}

private static float[] baseNoise(float px, float py) {
float ix = (float)Math.floor(px);
float iy = (float)Math.floor(py);
float fx = px - ix;
float fy = py - iy;

float ux = fx * fx * fx * (fx * (fx * 6.0f - 15.0f) + 10.0f);
float uy = fy * fy * fy * (fy * (fy * 6.0f - 15.0f) + 10.0f);
float dux = fx * fx * 30.0f * (fx * (fx - 2.0f) + 1.0f);
float duy = fy * fy * 30.0f * (fy * (fy - 2.0f) + 1.0f);

float gan = hash(ix, iy);
float gax = hashX(gan);
float gay = hashY(gan);

float gbn = hash(ix + 1, iy);
float gbx = hashX(gbn);
float gby = hashY(gbn);

float gcn = hash(ix, iy + 1);
float gcx = hashX(gcn);
float gcy = hashY(gcn);

float gdn = hash(ix + 1, iy + 1);
float gdx = hashX(gdn);
float gdy = hashY(gdn);

float va = dot(gax, gay, fx, fy);
float vb = dot(gbx, gby, fx - 1, fy);
float vc = dot(gcx, gcy, fx, fy - 1);
float vd = dot(gdx, gdy, fx - 1, fy - 1);

float u2x = gax + (gbx - gax) * ux + (gcx - gax) * uy + (gax - gbx - gcx + gdx) * ux * uy + dux * (uy * (va - vb - vc + vd) + vb - va);
float u2y = gay + (gby - gay) * ux + (gcy - gay) * uy + (gay - gby - gcy + gdy) * ux * uy + duy * (ux * (va - vb - vc + vd) + vc - va);

return new float[] { va + ux * (vb - va) + uy * (vc - va) + ux * uy * (va - vb - vc + vd), u2x, u2y };
}

@Override
public boolean isDifferentiable() {
return true;
}

@Override
public double[] noised(long seed, double x, double y) {
x *= frequency;
y *= frequency;
double[] out = { 0.0f, 0.0f, 0.0f };
float heightFreq = 1.0f;
float heightAmp = 1f;
float cumAmp = 0.0f;
for (int i = 0; i < heightOctaves; i++) {
float[] noise = baseNoise((float) (x * heightFreq), (float) (y * heightFreq));
out[0] += noise[0] * heightAmp;
out[1] += noise[1] * heightAmp * heightFreq;
out[2] += noise[2] * heightAmp * heightFreq;
cumAmp += heightAmp;
heightAmp *= heightGain;
heightFreq *= heightLacunarity;
}
out[0] /= cumAmp;
out[1] /= cumAmp;
out[2] /= cumAmp;
return out;
}

@Override
public double[] noised(long seed, double x, double y, double z) {
return noised(seed, x, z);
}

@Override
public double noise(long seed, double x, double y) {
return noised(seed, x, y)[0];
}

@Override
public double noise(long seed, double x, double y, double z) {
return noised(seed, x, y, z)[0];
}
}
Loading

0 comments on commit b14b355

Please sign in to comment.