# https://github.com/rogeriobego/terrain\_3d\_perlinNoise

**URL:** https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141
**Category:** Gallery
**Created:** [February 28, 2021, 4:31pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141 "2021-02-28T16:31:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 28, 2021, 4:31pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141/1 "2021-02-28T16:31:47Z")

</div>

```auto
import peasy.*;
PeasyCam cam; 
int cols, rows, s;
int w,h;
float t=0, vt=0.01; // t and speed of t
float xoffInit=0, vXoffInit=0.01; // xoffInit and speed of xoffInit
float inc=0.025; // increment of noise()
boolean showGrid=false;
boolean showMessage=false; // show message depending on position 
float xMessage, yMessage, zMessage;
color greenLight=color(50,150,50);
color greenDark=color(10,50,10);
color white=color(255);
color cyan=color(0,90,90);
color blue=color(0,0,70);

void setup(){
   size(600,600,P3D);
   s=5;
   w=800;
   h=800;
   cols=w/s;
   rows=h/s;
   cam=new PeasyCam(this,400,400,200,700);
}

void draw(){
  lights();
  background(blue);
  
  pushMatrix();
  translate(0,height*0.3,-100);
  rotateX(PI/3);

  if (showGrid){
    stroke(100); noFill();
  } else {
    noStroke();
  }
  
  xoffInit+=vXoffInit;
  float yoff=t;
  
  //show a message at location t=0 and xOffInit=0
  if (t>=-rows*inc && t<=0 && xoffInit>=-cols*inc && xoffInit<=0){
    //rows*inc and cols*inc is 4 now
    showMessage=true;
  } else {
    showMessage=false;
  }
  
  for (int y=0;y<rows;y++){
     beginShape(TRIANGLE_STRIP);
     float xoff=xoffInit;
     
     for (int x=0;x<cols;x++){
       
       // computes the height (z and z2)
       float z=map(noise(xoff,yoff),0,1,-100,100);
       yoff+=inc;
       float z2=map(noise(xoff,yoff),0,1,-100,100);
       yoff-=inc;
       
       // define fill color based on z value
       if (showGrid==false){
          // change Hue value of greenDark and greenLight (0 until same color) 
          // based on noise() using xoff and yoff
          colorMode(HSB);
          float n=noise(xoff+1,yoff+1);
          float hueGD=map(n,0,1,0,hue(greenDark)*2);
          float hueGL=map(n,0,1,0,hue(greenLight)*2);
          color gd=color(constrain(hueGD,0,hue(greenDark)),saturation(greenDark),brightness(greenDark));
          color gl=color(constrain(hueGL,0,hue(greenDark)),saturation(greenLight),brightness(greenLight));
          colorMode(RGB);
   
          if (z>25){ // snow (color from white to greenLight)
            fill(lerpColor(gl,white,map(z,25,40,0,1)));
          } else if (z>-25){ // forest (greenLight to greenDark)
            fill(lerpColor(gd,gl,map(z,-25,25,0,1)));
          } else if (z>-40){ // forest (green dark)
            fill(gd);
          }else { // lake (cyan) flat (force z=-40)
            z=-40;
            z2=-40;
            fill(cyan);
          }
        }

        vertex(x*s,y*s,z);
        vertex(x*s,(y+1)*s,z2);
        xoff+=inc;
        
        if (showMessage){
           if (int(xoff*100.0)==0 && int(yoff*100.0)==0){
             xMessage=x*s;
             yMessage=y*s;
             zMessage=100;
           }
        }
     }
     yoff+=inc;
     endShape();
  }
  t-=vt;

  // show Message
  if (showMessage){
     fill(255,0,0);
     pushMatrix();
     translate(xMessage,yMessage,zMessage);
     stroke(255,0,0);
     line(0,0,0,0,0,-200);
     rotateX(-PI/4);
     textSize(20);
     text("rogerio.bego@hotmail.com",0,0);
     popMatrix();
  }

  popMatrix();
  
  // information
  fill(255,0,0);
  textSize(30);
  text("t="+nf(t,1,2),10,30);
  text("xoffInit="+nf(xoffInit,1,2),10,60);
  text("press arrow keys or space",width*0.6,30);
  text("or pgUp / pgDown",width*0.6,60);
  text("or drag mouse buttons",width*0.6,90);
  
}

void keyPressed(){
  if (key==CODED){
    if (keyCode==UP){ 
       vt+=0.01; 
    } else if (keyCode==DOWN){ 
       vt-=0.01;
    } else if (keyCode==LEFT){
       vXoffInit-=0.01; 
    } else if (keyCode==RIGHT){
       vXoffInit+=0.01;
    }
  } else if (keyCode==16){ // pgup  
       inc+=0.001; 
  } else if (keyCode==11){ // pgdown   
       inc-=0.001;
  } else if (key==' '){ // space bar showGrid ON/OFF
     showGrid=!showGrid; 
  }
}

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 28, 2021, 4:32pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141/2 "2021-02-28T16:32:30Z")

</div>

![Untitled 44](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ad35d92bc9ee7d2a8d6c28ac33d00419d2645aa4.jpeg)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 28, 2021, 4:33pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141/3 "2021-02-28T16:33:11Z")

</div>

![Untitled 45](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5dc719dc4f98a992c3259551474aedb63f665312.jpeg)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 28, 2021, 4:36pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141/4 "2021-02-28T16:36:05Z")

</div>

[![](https://img.youtube.com/vi/IKB1hWWedMk/maxresdefault.jpg "Coding Challenge 11: 3D Terrain Generation with Perlin Noise in Processing") ](https://www.youtube.com/watch?v=IKB1hWWedMk)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 28, 2021, 4:42pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141/5 "2021-02-28T16:42:43Z")

</div>

[![](https://img.youtube.com/vi/mpnxzte8RT0/hqdefault.jpg "terrain 3d perlinNoise processing") ](https://www.youtube.com/watch?v=mpnxzte8RT0)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 28, 2021, 4:51pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141/6 "2021-02-28T16:51:40Z")

</div>

[![](https://img.youtube.com/vi/gNFpAW4kc5o/hqdefault.jpg "terrain 3d perlinNoise seasons processing") ](https://www.youtube.com/watch?v=gNFpAW4kc5o)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [February 28, 2021, 4:55pm UTC](https://discourse.processing.org/t/https-github-com-rogeriobego-terrain-3d-perlinnoise/28141/7 "2021-02-28T16:55:04Z")

</div>

How interpolate colors using learpColor

 ![terrain_3D_learpColor](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e399548ca16bd046a8cc491d06759ca0bc28283f.png)
