Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SamyBencherif/af642cf04c359fb5c5adf875b2253cde to your computer and use it in GitHub Desktop.
Save SamyBencherif/af642cf04c359fb5c5adf875b2253cde to your computer and use it in GitHub Desktop.
int w, h;
/*
This program is based off of Daniel Shiffman's Perlin
Noise Terrain Generation Demo, found here:
https://www.youtube.com/watch?v=IKB1hWWedMk
The goal of this program is to demonstrate that
the issue where the terrain appears to be undulating
rather than the camera flying forward can also be
resolved at low speeds and low vertex counts.
This was made quickly so please excuse the hardcoded metrics.
*/
void setup()
{
w = h = 500;
size(500,500,P3D);
}
void draw()
{
background(0);
stroke(0,0,0,60);
strokeWeight(1);
fill(200);
lights();
translate(w/2, h/2);
rotateX(PI/2.5);
scale(3);
float t = millis()/400.;
for (int y=-65; y<100; y+=10)
{
beginShape(TRIANGLE_STRIP);
for (int x=-152; x<136; x+=10)
{
//There are two important things working together here. The vertex offset and the noise offset.
//This is most similar to the way motion was being created in the video.
//An issue with this is that our movements in noise space weren't
//a multiple of the grid size, therefore the geometry will change slightly
//as new inbetween values are introduced.
//noise offset
//vertex(x,y,map(noise(x/30.,(y-t)/30.),0,1,-20,20));
//vertex(x,y+10,map(noise(x/30.,(y+10-t)/30.),0,1,-20,20));
//This is a corrected version where each step in noise space is exactly
//one grid unit. Another problem is that this motion will look jerky and
//unnatural, especially at low speeds, similar to how motion looks under
//very low fps.
//noise offset constrained
//vertex(x,y,map(noise(x/30.,(y-10*floor(t/10))/30.),0,1,-20,20));
//vertex(x,y+10,map(noise(x/30.,(y+10-10*floor(t/10))/30.),0,1,-20,20));
//Since the previous method has noticable gaps before an appropriately sized
//noise space step can be made, we should offset the vertexes in 3d space a
//little bit so the vertexes hit all the in-between points they're supposed to
//without revealing unwanted extra detail from the perlin noise. This is what the
//vertex offset formula looks like. Notice how it resets after each unit due to
//the mod function
//vertex offset
//vertex(x,y+t%10,map(noise(x/30.,y/30.),0,1,-20,20));
//vertex(x,y+10+t%10,map(noise(x/30.,(y+10)/30.),0,1,-20,20));
//Finally we combine the contrained noise offset and the vertex offset for a
//completely corrected result. The vertexes will move while we're waiting for
//the next oppurtunity to make a step in noise space that won't change the
//geometry.
//result
vertex(x,y+t%10,map(noise(x/30.,(y-10*floor(t/10))/30.),0,1,-20,20));
vertex(x,y+10+t%10,map(noise(x/30.,(y+10-10*floor(t/10))/30.),0,1,-20,20));
}
endShape();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment