Skip to content

Instantly share code, notes, and snippets.

@Trimad
Last active March 17, 2019 19:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trimad/d51e460931aa5088ddd62d2e46fe6f2c to your computer and use it in GitHub Desktop.
Save Trimad/d51e460931aa5088ddd62d2e46fe6f2c to your computer and use it in GitHub Desktop.
/* * * * * * *
Main File
Tristan Madden
1/3/2018
* * * * * * * */
//ffmpeg -i test.mp4 -i music.mp3 -codec copy -shortest output.mp4
//ffmpeg -r 60 -f image2 -s 3840x2160 -i frame-%04d.tif -c:v libx264 -preset slow -profile:v high -crf 9 -coder 1 -pix_fmt yuv420p -movflags +faststart -g 30 -bf 2 -c:a aac -b:a 384k -profile:a aac_low test4.mp4
OpenSimplexNoise noise;
int w = 140;
float magicAngle;
float maxD;
int howMany =128*64;
int orthoX;
int orthoY;
int orthoZ = w*w+4096;
PImage water;
PImage sand;
PImage grass;
PImage rock;
PImage dirt;
PImage snow;
boolean stopZoom = false;
void setup() {
  frameRate(60);
  water = loadImage("assets/water.png");
  sand = loadImage("assets/sand.jpg");
  grass = loadImage("assets/grass.jpg");
  rock = loadImage("assets/rock.jpg");
  dirt = loadImage("assets/dirt.jpg");
  snow = loadImage("assets/snow2.jpg");
  noise = new OpenSimplexNoise();
  fullScreen(P3D);
  //size(1080, 1080, P3D);
  magicAngle = atan(1 / sqrt(2));
  orthoX = width;
  orthoY = height;
}
void draw() {
  textureMode(IMAGE);
  textureWrap(REPEAT); 
  background(0);
  noStroke();
  translate(width/2, height*3, 0);
  ortho(-orthoX, orthoX, -orthoY, orthoY, orthoZ, -orthoZ); // Same as ortho()
  //directionalLight(255, 255, 255, -1, 0, 0);
  directionalLight(200, 255, 230, 0, -1, 0);
  directionalLight(255, 230, 255, 0, 0, -1);
  rotateX(magicAngle);
  rotateY(-QUARTER_PI);
  if (!stopZoom) {
    orthoX+=2;
    orthoY+=2;
  }
  if (orthoY >=height*3.5) {
    stopZoom = true;
  } 
  for (int z = 0; z < howMany; z += w) {
    for (int x = 0; x < howMany; x += w) {
      float m = map(getNoise(x, z), -1, 1, -500, 900);
      float mc = constrain(m, 0, 640);
      pushMatrix();
      //translate(x, -m+(m/2), z);
      translate(x, 0, z);
      //box(w, w+m, w);
      if (mc < 1) {      
        TexturedCube(water, mc);
      } else if (mc >= 1 && mc < 128) {
        TexturedCube(sand, mc);
      } else if (mc >= 128 && mc < 384) {
        TexturedCube(grass, m);
      } else if (mc >= 384 && mc < 512) {
        TexturedCube(dirt, m);
      } else if (mc >= 512 && mc < 640) {
        TexturedCube(rock, m*1.2);
      } else if (mc >= 640 ) {
        TexturedCube(snow, m);
      }
      popMatrix();
    }
  }
  saveFrame("frame-####.tif");
  if (frameCount >= 3721) { //1 minute at 60FPS
    exit();
  }
}
float getNoise(int x, int z) {
  int numFrames = 120;
  float t = (0.222*frameCount/numFrames)%1;
  float scale = 0.0008; //noise "zoom"
  float ns = (float)noise.eval(scale*x, scale*z, cos(TWO_PI*t), sin(TWO_PI*t));
  return ns;
}
void keyTyped() {
  int step = 256;
  if (int(key) == 113) {
    orthoX -= step;
    println(orthoX);
  }
  if (int(key) == 119) {
    orthoX += step;
    println(orthoX);
  }
  if (int(key) == 97) {
    orthoY -= step;
    println(orthoY);
  }
  if (int(key) == 115) {
    orthoY += step;
    println(orthoY);
  }
  if (int(key) == 122) {
    orthoZ -= step;
    println(orthoZ);
  }
  if (int(key) == 120) {
    orthoZ += step;
    println(orthoZ);
  }
}
void TexturedCube(PImage _tex, float m) {
  beginShape(QUADS);
  texture(_tex);
  /*
   // +X "right" face
   vertex( w/2, -w/2-m, w/2, 0, 0);
   vertex( w/2, -w/2-m, -w/2, _tex.width, 0);
   vertex( w/2, w/2, -w/2, _tex.width, w+m);
   vertex( w/2, w/2, w/2, 0, w+m);
   // +Y "bottom" face
   vertex(-w/2, w/2, w/2, 0, 0);
   vertex( w/2, w/2, w/2, w/2, 0);
   vertex( w/2, w/2, -w/2, w/2, w/2);
   vertex(-w/2, w/2, -w/2, 0, w/2);
   // +Z "front" face
   vertex(-w/2, -w/2-m, w/2, 0, 0);
   vertex( w/2, -w/2-m, w/2, _tex.width, 0);
   vertex( w/2, w/2, w/2, _tex.width, w+m);
   vertex(-w/2, w/2, w/2, 0, w+m);
   */
  // -X "left" face
  vertex(-w/2, -w/2-m, -w/2, 0, 0);
  vertex(-w/2, -w/2-m, w/2, _tex.width, 0);
  vertex(-w/2, w/2, w/2, _tex.width, w+m);
  vertex(-w/2, w/2, -w/2, 0, w+m);
  // -Y "top" face
  vertex(-w/2, -w/2-m, -w/2, 0, 0);
  vertex( w/2, -w/2-m, -w/2, _tex.width, 0);
  vertex( w/2, -w/2-m, w/2, _tex.width, _tex.width);
  vertex(-w/2, -w/2-m, w/2, 0, w);
  // -Z "back" face
  vertex( w/2, -w/2-m, -w/2, 0, 0);
  vertex(-w/2, -w/2-m, -w/2, _tex.width, 0);
  vertex(-w/2, w/2, -w/2, _tex.width, w+m);
  vertex( w/2, w/2, -w/2, 0, w+m);
  endShape();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment