Advertisement
Guest User

RandomTerrain

a guest
May 20th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. int cols;
  2. int rows;
  3. int scale;
  4. int h = 3000;
  5. int w = 2000;
  6. float[][] terrain;
  7.  
  8. float flying= -7; //Determines move speed (use positive values to move backwards)
  9. float roughness = 150; //Determines gradient of randomness
  10. float peak = -1; //Determines height of terrain peaks (use values 0<peak<1 to lower)
  11.  
  12.  
  13. void setup(){
  14. size(1000, 1000, P3D);
  15. translate(width/2, height/2);
  16.  
  17. scale = 20;
  18. cols = w/scale;
  19. rows = h/scale;
  20.  
  21.  
  22.  
  23. }
  24.  
  25.  
  26. void draw(){
  27. if (flying > 0){
  28. flying += .05;
  29. }
  30. else if (flying < 0){
  31. flying -= .05;
  32. }
  33. lights();
  34.  
  35.  
  36. terrain = new float [cols][rows];
  37. float yOff = flying;
  38. for(int y=0; y<rows; y++){
  39. float xOff = 0;
  40. for(int x=0; x<cols; x++){
  41. terrain[x][y] = map(noise(xOff, yOff), 0, 1, -roughness, roughness)*peak;
  42. xOff += .1;
  43. }
  44. yOff += .1;
  45. }
  46.  
  47.  
  48. background(0);
  49.  
  50. translate(width/2, height/2);
  51. rotateX(PI/3);
  52. translate(-width/2 - 300, -height/2);
  53.  
  54. for(int y=0; y<rows - 1; y++){
  55. beginShape(TRIANGLE_STRIP);
  56. for(int x=0; x<cols; x++){
  57. //noFill();
  58. //stroke(255);
  59.  
  60. fill(55);
  61. noStroke();
  62. vertex(x*scale, y*scale, terrain[x][y]);
  63. vertex(x*scale, (y+1)*scale, terrain[x][y+1]);
  64.  
  65.  
  66. }
  67. endShape();
  68. }
  69.  
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement