Skip to content

Commit

Permalink
Add FadeFunctions to Perlin and Value Noise.
Browse files Browse the repository at this point in the history
  • Loading branch information
Articdive committed May 2, 2021
1 parent 8925a4f commit 2b36195
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 152 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "de.articdive"
version = "2.0.1"
version = "2.1.0-SNAPSHOT"

plugins {
java
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/de/articdive/jnoise/fade_functions/FadeFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* JNoise
* Copyright (C) 2021 Articdive (Lukas Mansour)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.articdive.jnoise.fade_functions;

/**
* @author Lukas Mansour
*/
public interface FadeFunction {
/**
* Specifies a value to fade, this is used to remove interpolation artefacts.
*
* @param t value (position) in the unit cube to fade.
*/
double fade(double t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JNoise
* Copyright (C) 2021 Articdive (Lukas Mansour)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.articdive.jnoise.fade_functions;

/**
* @author Lukas Mansour
*/
public enum FadeFunctionType implements FadeFunction {
NONE {
@Override
public double fade(double t) {
return t;
}
},
SMOOTHSTEP {
@Override
public double fade(double t) {
return t * t * (3 - 2 * t); // -(2t^3) + 3t^2
}
},
IMPROVED_PERLIN_NOISE {
@Override
public double fade(double t) {
return t * t * t * (t * (t * 6 - 15) + 10); // 6t^5 - (15t^4) + 10t^3
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ public double lerp(double x, double a, double b) {
return a + (b - a) * x * x * x * x;
}
},
QUINTIC {
@Override
public double lerp(double x, double a, double b) {
return a + (b - a) * x * x * x * x * x;
}
},
COSINE {
@Override
public double lerp(double x, double a, double b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import de.articdive.jnoise.JNoise;
import de.articdive.jnoise.api.NoiseBuilder;
import de.articdive.jnoise.fade_functions.FadeFunction;
import de.articdive.jnoise.fade_functions.FadeFunctionType;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.interpolation.InterpolationType;
import org.jetbrains.annotations.NotNull;
Expand All @@ -31,6 +33,7 @@ public final class PerlinNoiseBuilder extends NoiseBuilder {
private long seed = 1729;
private double frequency = 1.00;
private Interpolation interpolation = InterpolationType.LINEAR;
private FadeFunction fadeFunction = FadeFunctionType.IMPROVED_PERLIN_NOISE;

/**
* Sets the seed for the {@link PerlinNoiseGenerator}.
Expand Down Expand Up @@ -71,9 +74,21 @@ public PerlinNoiseBuilder setInterpolation(@NotNull Interpolation interpolation)
return this;
}

/**
* Sets the FadeFunction for the {@link PerlinNoiseGenerator}.
*
* @param fadeFunction the new {@link FadeFunction} for the {@link PerlinNoiseGenerator}.
* @return {@link PerlinNoiseBuilder} this
*/
@NotNull
public PerlinNoiseBuilder setFadeFunction(@NotNull FadeFunction fadeFunction) {
this.fadeFunction = fadeFunction;
return this;
}

@Override
@NotNull
public JNoise build() {
return JNoise.build(new PerlinNoiseGenerator(seed, interpolation, frequency));
return JNoise.build(new PerlinNoiseGenerator(seed, interpolation, fadeFunction, frequency));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package de.articdive.jnoise.noise.perlin;

import de.articdive.jnoise.api.NoiseGenerator;
import de.articdive.jnoise.fade_functions.FadeFunction;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.util.HashUtil;
import de.articdive.jnoise.util.vectors.Vector2D;
Expand Down Expand Up @@ -78,11 +79,13 @@ public final class PerlinNoiseGenerator extends NoiseGenerator<PerlinNoiseResult

private final long seed;
private final Interpolation interpolation;
private final FadeFunction fadeFunction;
private final double frequency;

PerlinNoiseGenerator(long seed, @NotNull Interpolation interpolation, double frequency) {
PerlinNoiseGenerator(long seed, @NotNull Interpolation interpolation, @NotNull FadeFunction fadeFunction, double frequency) {
this.seed = seed;
this.interpolation = interpolation;
this.fadeFunction = fadeFunction;
this.frequency = frequency;
}

Expand All @@ -100,8 +103,8 @@ public PerlinNoiseResult evaluateNoise(double x, double y) {
y -= Math.floor(y);
// Compute fade values (fractal values for interpolation to remove artifacts)
double[] fractals = new double[]{
fade(x),
fade(y)
fadeFunction.fade(x),
fadeFunction.fade(y)
};
double[] dots = new double[]{
// (VECTOR_2D.length - 1) = 7.
Expand Down Expand Up @@ -130,9 +133,9 @@ public PerlinNoiseResult evaluateNoise(double x, double y, double z) {
z -= iZ;
// Compute fade values (fractal values for interpolation to remove artifacts)
double[] fractals = new double[]{
fade(x),
fade(y),
fade(z)
fadeFunction.fade(x),
fadeFunction.fade(y),
fadeFunction.fade(z)
};
double[] dots = new double[]{
// (VECTOR_3D.length - 1) = 19.
Expand Down Expand Up @@ -175,10 +178,10 @@ public PerlinNoiseResult evaluateNoise(double x, double y, double z, double w) {
w -= iW;
// Compute fade values (fractal values for interpolation to remove artifacts)
double[] fractals = new double[]{
fade(x),
fade(y),
fade(z),
fade(w)
fadeFunction.fade(x),
fadeFunction.fade(y),
fadeFunction.fade(z),
fadeFunction.fade(w)
};
double[] dots = new double[]{
// (VECTOR_4D.length - 1) = 19.
Expand Down Expand Up @@ -217,14 +220,4 @@ public PerlinNoiseResult evaluateNoise(double x, double y, double z, double w) {
};
return new PerlinNoiseResult(interpolation.lerp(fractals, dots));
}

/**
* Fade method used to remove interpolation artifacts.
*
* @param t value to fade.
* @return faded t value.
*/
private static double fade(double t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import de.articdive.jnoise.JNoise;
import de.articdive.jnoise.api.NoiseBuilder;
import de.articdive.jnoise.fade_functions.FadeFunction;
import de.articdive.jnoise.fade_functions.FadeFunctionType;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.interpolation.InterpolationType;
import org.jetbrains.annotations.NotNull;
Expand All @@ -31,6 +33,7 @@ public final class ValueNoiseBuilder extends NoiseBuilder {
private long seed = 1729;
private double frequency = 1.00;
private Interpolation interpolation = InterpolationType.LINEAR;
private FadeFunction fadeFunction = FadeFunctionType.NONE;

/**
* Sets the seed for the {@link ValueNoiseGenerator}.
Expand Down Expand Up @@ -71,9 +74,21 @@ public ValueNoiseBuilder setInterpolation(@NotNull Interpolation interpolation)
return this;
}

/**
* Sets the FadeFunction for the {@link ValueNoiseGenerator}.
*
* @param fadeFunction the new {@link FadeFunction} for the {@link ValueNoiseGenerator}.
* @return {@link ValueNoiseBuilder} this
*/
@NotNull
public ValueNoiseBuilder setFadeFunction(@NotNull FadeFunction fadeFunction) {
this.fadeFunction = fadeFunction;
return this;
}

@Override
@NotNull
public JNoise build() {
return JNoise.build(new ValueNoiseGenerator(seed, interpolation, frequency));
return JNoise.build(new ValueNoiseGenerator(seed, interpolation, fadeFunction, frequency));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package de.articdive.jnoise.noise.value;

import de.articdive.jnoise.api.NoiseGenerator;
import de.articdive.jnoise.fade_functions.FadeFunction;
import de.articdive.jnoise.interpolation.Interpolation;
import org.jetbrains.annotations.NotNull;

Expand All @@ -36,11 +37,13 @@
public final class ValueNoiseGenerator extends NoiseGenerator<ValueNoiseResult> {
private final long seed;
private final Interpolation interpolation;
private final FadeFunction fadeFunction;
private final double frequency;

ValueNoiseGenerator(long seed, @NotNull Interpolation interpolation, double frequency) {
ValueNoiseGenerator(long seed, @NotNull Interpolation interpolation, @NotNull FadeFunction fadeFunction, double frequency) {
this.seed = seed;
this.interpolation = interpolation;
this.fadeFunction = fadeFunction;
this.frequency = frequency;
}

Expand All @@ -52,8 +55,8 @@ public ValueNoiseResult evaluateNoise(double x, double y) {
long iX = (long) Math.floor(x);
long iY = (long) Math.floor(y);
double[] fractals = new double[]{
x - iX,
y - iY
fadeFunction.fade(x - iX),
fadeFunction.fade(y - iY)
};
double[] vals = new double[]{
evaluateCoord2D((int) iX, (int) iY),
Expand All @@ -74,9 +77,9 @@ public ValueNoiseResult evaluateNoise(double x, double y, double z) {
long iY = (long) Math.floor(y);
long iZ = (long) Math.floor(z);
double[] fractals = new double[]{
x - iX,
y - iY,
z - iZ
fadeFunction.fade(x - iX),
fadeFunction.fade(y - iY),
fadeFunction.fade(z - iZ)
};
double[] vals = new double[]{
evaluateCoord3D(iX, iY, iZ),
Expand All @@ -103,10 +106,10 @@ public ValueNoiseResult evaluateNoise(double x, double y, double z, double w) {
long iZ = (long) Math.floor(z);
long iW = (long) Math.floor(w);
double[] fractals = new double[]{
x - iX,
y - iY,
z - iZ,
w - iW
fadeFunction.fade(x - iX),
fadeFunction.fade(y - iY),
fadeFunction.fade(z - iZ),
fadeFunction.fade(w - iW)
};
double[] vals = new double[]{
evaluateCoord4D(iX, iY, iZ, iW),
Expand Down
Loading

0 comments on commit 2b36195

Please sign in to comment.