# [WGEN] Voronoi Noise

Discussion in 'Resources' started by mncat77, Jul 20, 2013.

Not open for further replies.
1. Offline

### Forge_User_61328877

Preamble
This thread is inspired by Hoolean 's Big World Generator Challenge and specifically to jtjj222 's post.
Also in order to understand this thread, you should know, how ChunkGenerators work and what noises are. If you don't know these things I suggest you to read through jtjj222 's always up-to-date definitive guide to terrain generation.

Voronoi Diagram
Now that you know what a noise is, how to use it and I feel like I have promoted enough threads, let's get to the voronoi noise.
Voronoi is used to divide an area into lots of polygons, based on points and their location in that area.
You can use Fortune's algorithm to create a Voronoi Diagram from a set of points in a plane.
Here is an example of how a Voronoi Diagram of a random set of points looks like: The Noise
So now that we know, how to create a Voronoi Diagram from a random set of points, how do we make a noise out of that?
We are going to use the distance to the reference point of the polygon we are currently in.
We can create lots of diffrent looking noises now using multiple octave or diffrences (horizontal in the image below) and diffrent distance methods (vertical in the image below):
View attachment 14361
Key:
First to Fourth = Distance to the nearest point to distance to the 4. nearest point (little correction here)
Difference21 = Second - Third
Difference32 = Third - Second
Crackle = Math.max(1.0, 10*Difference21);

Length = Math.sqrt(x*x+y*y+z*z) - Euclidean (shortest line). Square Root may be slow for some applications.
Length2 = x*x+y*y+z*z - Saves the slow Square Root for some calculations.
Manhatten = x+z
Chebychev = Math.max(x,z);
Minkowski4 = Math.pow(x*x*x*x+y*y*y*y+z*z*z*z,0.25);
Source: LumpyLumpy

Implementation in Java
And finally, here is an implemetation of the Voronoi noise in java, based on libnoiseforjava.
Code:java
`import java.util.Random; public class VoronoiGenerator { private static final double SQRT_2 = 1.4142135623730950488;private static final double SQRT_3 = 1.7320508075688772935; private long seed;private short distanceMethod;  public VoronoiGenerator(long seed, short distanceMethod /*TODO: int octaves*/) {this.seed = seed;this.distanceMethod = distanceMethod;} private double getDistance(double xDist, double zDist) {switch(distanceMethod) {case 0:return Math.sqrt(xDist * xDist + zDist * zDist) / SQRT_2;case 1:return xDist + zDist;case 2:return Math.pow(Math.E, Math.sqrt(xDist * xDist + zDist * zDist) / SQRT_2)/Math.E;default:return 1.0;}} private double getDistance(double xDist, double yDist, double zDist) {switch(distanceMethod) {case 0:return Math.sqrt(xDist * xDist + yDist * yDist + zDist * zDist) / SQRT_3;case 1:return xDist + yDist + zDist;default:return 1.0;}} public short getDistanceMethod() {return distanceMethod;} public long getSeed() {return seed;} public double noise(double x, double z, double frequency) {x *= frequency;z *= frequency; int xInt = (x > .0? (int)x: (int)x - 1);int zInt = (z > .0? (int)z: (int)z - 1); double minDist = 32000000.0; double xCandidate = 0;double zCandidate = 0; for(int zCur = zInt - 2; zCur <= zInt + 2; zCur++) {for(int xCur = xInt - 2; xCur <= xInt + 2; xCur++) { double xPos = xCur + valueNoise2D(xCur, zCur, seed);double zPos = zCur + valueNoise2D(xCur, zCur, new Random(seed).nextLong());double xDist = xPos - x;double zDist = zPos - z;double dist = xDist * xDist + zDist * zDist; if(dist < minDist) {minDist = dist;xCandidate = xPos;zCandidate = zPos;}}} double xDist = xCandidate - x;double zDist = zCandidate - z; return getDistance(xDist, zDist);} public double noise(double x, double y, double z, double frequency) {x *= frequency;y *= frequency;z *= frequency; int xInt = (x > .0? (int)x: (int)x - 1);int yInt = (y > .0? (int)y: (int)y - 1);int zInt = (z > .0? (int)z: (int)z - 1); double minDist = 32000000.0; double xCandidate = 0;double yCandidate = 0;double zCandidate = 0; Random rand = new Random(seed); for(int zCur = zInt - 2; zCur <= zInt + 2; zCur++) {for(int yCur = yInt - 2; yCur <= yInt + 2; yCur++) {for(int xCur = xInt - 2; xCur <= xInt + 2; xCur++) { double xPos = xCur + valueNoise3D (xCur, yCur, zCur, seed);double yPos = yCur + valueNoise3D (xCur, yCur, zCur, rand.nextLong());double zPos = zCur + valueNoise3D (xCur, yCur, zCur, rand.nextLong());double xDist = xPos - x;double yDist = yPos - y;double zDist = zPos - z;double dist = xDist * xDist + yDist * yDist + zDist * zDist; if(dist < minDist) {minDist = dist;xCandidate = xPos;yCandidate = yPos;zCandidate = zPos;}}}} double xDist = xCandidate - x;double yDist = yCandidate - y;double zDist = zCandidate - z; return getDistance(xDist, yDist, zDist);} public void setDistanceMethod(short distanceMethod) {this.distanceMethod = distanceMethod;} public void setSeed(long seed) {this.seed = seed;} public static double valueNoise2D (int x, int z, long seed) {long n = (1619 * x + 6971 * z + 1013 * seed) & 0x7fffffff;n = (n >> 13) ^ n;return 1.0 - ((double)((n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff) / 1073741824.0);} public static double valueNoise3D (int x, int y, int z, long seed) {long n = (1619 * x + 31337 * y + 6971 * z + 1013 * seed) & 0x7fffffff;n = (n >> 13) ^ n;return 1.0 - ((double)((n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff) / 1073741824.0);} }`

Images of this in action    Thanks again to jtjj222 for helping me a lot with this topic.

_DSH105_, Miro, Burnett1 and 4 others like this.
2. Offline

### jtjj222

For anyone who wants to learn more about how the noise is generated, take a look at http://aftbit.com/cell-noise-2/. Some practical examples of how to use this in your terrain generator will be coming up, and I plan to include this in the Always up-to-date definitive guide to terrain generation, it that's all right with mncat77

#2
mncat77 likes this.
3. Offline

### Miro

I'd really want a source to see this in action.. it confuses me.. How would i apply Difference21 to my chunkgenerator, from what i see, all i can change is the distance method. Anyways, nice job!

#3
4. Offline

### Forge_User_61328877

Thanks, some images of this in action can be found in the spoiler, also for Diffrence21 in action: this and the noise method to do that (the picture sets the block up or down with the condition that the Diffrence21 is <0.1 with 2 noises with diffrent frequencies) :
Code:java
`        public double noise(double x, double z, double frequency) {        x *= frequency;        z *= frequency;         int xInt = (x > .0? (int)x: (int)x - 1);        int zInt = (z > .0? (int)z: (int)z - 1);         double minDist = 32000000.0;         double xCandidate1 = 0;        double zCandidate1 = 0;        double xCandidate2 = 0;        double zCandidate2 = 0;         for(int zCur = zInt - 2; zCur <= zInt + 2; zCur++) {            for(int xCur = xInt - 2; xCur <= xInt + 2; xCur++) {                 double xPos = xCur + valueNoise2D(xCur, zCur, seed);                double zPos = zCur + valueNoise2D(xCur, zCur, new Random(seed).nextLong());                double xDist = xPos - x;                double zDist = zPos - z;                double dist = xDist*xDist+zDist*zDist;                 if(dist < minDist) {                    xCandidate2 = xCandidate1;                    zCandidate2 = zCandidate1;                    xCandidate1 = xPos;                    zCandidate1 = zPos;                    minDist = dist;                }            }        }         return getDistance(xCandidate2 - x, zCandidate2 - z) - getDistance(xCandidate1 - x, zCandidate1 - z);    }`

Miro likes this.
5. Offline

### Miro

Ah, Thanks!!
It'd be awesome if i could see the generateBlockSections aswell, since i can't seem to get that right

#5
6. Offline

### Forge_User_61328877

If you use the 2D version you make a for(int x=0; x<16; x++) {for(int z=0; z<16; z++) { /**/ }} loop and in that loop you multiply the value you get from the noisefunction with chunkX*16+x, chunkZ*16+z and use it as altitude, for 3d you add a y loop and use the return value of the noise method as density/material. If thats not enough read more up in the above mentioned always up-to-date definitive guide to terrain generation.

Miro likes this.
7. Offline

### Miro

I now get this: with this code:
Code:java
`         VoronoiGenerator gen1 = new VoronoiGenerator(world.getSeed(), (short) 0);          for (int x=0; x<16; x++) {            for (int z=0; z<16; z++) {                 int realX = x + ChunkX * 16; //used so that the noise function gives us                int realZ = z + ChunkZ * 16; //different values each chunk                double frequency = 0.1; // the reciprocal of the distance between points                int multitude = 10; //how much we multiply the value between -1 and 1. It will determine how "steep" the hills will be.                int sea_level = 64;                 double maxHeight = gen1.noise(realX, realZ, frequency) * multitude + sea_level;                for (int y=0;y<maxHeight;y++) {                    setBlock(x,y,z,chunk,Material.STONE); //set the current block to stone                }                   }        }`

but how would i re-create the "hills" and patterns on the screenshot? (Sorry, I'm probally being a noob, but I really appreciate that you are trying to help me)

#7
8. Offline

### Forge_User_61328877

The first image was made with a much smaller frequency with just 1 - the first (not Diffrence21 or anything) second one about with your frequency. The last image is actually a Voronoi noise combined with a simplex noise put on an exponential scale, so the voronoi creates hills that go really high up and almost cancels out the simplex there and the simplex creates a smooth ground almost cancelling out the Voronoi.
Edit:
For the last image:
Code:java
`  private SimplexOctaveGenerator simplex;private VoronoiGenerator voronoi; public double noise(double x, double z, double frequency, double amplitude, double frequency2) {    double v = voronoi.noise(x, z, frequency2);    double s = (simplex.noise(x, z, frequency, amplitude)+1)/2;    double p = Math.pow(v, Math.E);    return (p*v + (1-p) * s)/2;}`

Miro likes this.
9. Offline

### Miro

thanks for all your help! i ended up getting this: with this code:
Code:java
`         VoronoiGenerator gen1 = new VoronoiGenerator(world.getSeed(), (short) 0);         for (int x=0; x<16; x++) {            for (int z=0; z<16; z++) {                 int realX = x + ChunkX * 16; //used so that the noise function gives us                int realZ = z + ChunkZ * 16; //different values each chunk                double frequency = 0.02; // the reciprocal of the distance between points                int multitude = 8; //how much we multiply the value between -1 and 1. It will determine how "steep" the hills will be.                int sea_level = 38;                 double noise = gen1.noise(realX, realZ, frequency);                   double finalnoise = noise * multitude + sea_level;                 if(noise > 0.3){                for (int y=0;y<finalnoise;y++) {                    setBlock(x,y,z,chunk,Material.STONE); //set the current block to stone                }                }else{                    for (int y=0;y<sea_level;y++) {                        setBlock(x,y,z,chunk,Material.LAVA); //set the current block to stone                    }                }            }        }`

#9