Created
January 2, 2019 03:32
-
-
Save NeusFear/8cc653369a372047a099d130fe8ed454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package neusfear.theforgecore.modules.islands.helpers; | |
import java.util.Random; | |
public class Perlin { | |
private long seed; | |
private Random rand; | |
private int octave; | |
public Perlin(long seed, int octave) { | |
this.seed = seed; | |
this.octave = octave; | |
rand = new Random(); | |
} | |
public double getNoiseLevelAtPosition(int x, int z) { | |
x = Math.abs(x); | |
z = Math.abs(z); | |
int xmin = (int) (double) x / octave; | |
int xmax = xmin + 1; | |
int zmin = (int) (double) z / octave; | |
int zmax = zmin + 1; | |
Coordinate a = new Coordinate(xmin, zmin); | |
Coordinate b = new Coordinate(xmax, zmin); | |
Coordinate c = new Coordinate(xmax, zmax); | |
Coordinate d = new Coordinate(xmin, zmax); | |
double ra = getRandomAtPosition(a); | |
double rb = getRandomAtPosition(b); | |
double rc = getRandomAtPosition(c); | |
double rd = getRandomAtPosition(d); | |
return (double) cosineInterpolate( //Interpolate Z direction | |
cosineInterpolate((float) ra, (float) rb, (float) (x - xmin * octave) / octave), //Interpolate X1 | |
cosineInterpolate((float) rd, (float) rc, (float) (x - xmin * octave) / octave), //Interpolate X2 | |
((float)z - (float)zmin * (float)octave) / (float)octave); | |
} | |
private float cosineInterpolate(float a, float b, float x) { | |
float ft = (float) (x * Math.PI); | |
float f = (float) ((1f - Math.cos(ft)) * .5f); | |
return a * (1f - f) + b * f; | |
} | |
private double getRandomAtPosition(Coordinate coord) { | |
double var = 10000 * (Math.sin(coord.getX()) + Math.cos(coord.getZ()) + Math.tan(seed)); | |
rand.setSeed((long) var); | |
return rand.nextDouble(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment