Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

火星の地表

/**
* terrain of Mars 
*
* @author aa_debdeb
* @date 2016/09/21
*/

int cellNum = 500;
float cellSize = 1;
float nScale = 0.03;
float threshold = 0.015;
PVector position;

void setup(){
  size(500, 500);
  noStroke();
  position = new PVector(0, 0);
}

void draw(){

  float[][] altitudes = new float[cellNum + 1][cellNum + 1];
  for(int w = 0; w < cellNum + 1; w++){
    for(int h = 0; h < cellNum + 1; h++){
      altitudes[w][h] = noise(w * nScale + position.x, h * nScale + position.y);
    }
  }
  float[][] slopes = new float[cellNum][cellNum];
  for(int w = 1; w < cellNum + 1; w++){
    for(int h = 1; h < cellNum + 1; h++){
        float s = (altitudes[w - 1][h - 1] + altitudes[w][h - 1] + altitudes[w - 1][h]) / 3.0;
        slopes[w - 1][h - 1] = altitudes[w][h] - s;
    }
  }
  
  background(210, 105, 30);
  for(int w = 0; w < cellNum; w++){
    for(int h = 0; h < cellNum; h++){
      float r = map(noise(w * nScale, h * nScale, 0), 0, 1, 210, 160);
      float g = map(noise(w * nScale, h * nScale, 100), 0, 1, 105, 82);
      float b = map(noise(w * nScale, h * nScale, 200), 0, 1, 30, 45);
      fill(r, g, b);
      rect(w * cellSize, h * cellSize, cellSize, cellSize);
      fill(0, map(constrain(slopes[w][h], -threshold, threshold), -threshold, threshold, 0, 255));
      rect(w * cellSize, h * cellSize, cellSize, cellSize);
    }
  }
  
  if(mouseX < width / 2){
    position.x -= 0.02;
  } else {
    position.x += 0.02;
  }
  if(mouseY < height / 2){
    position.y -= 0.02;
  } else {
    position.y += 0.02;
  }
}
f:id:aa_debdeb:20160918173632j:plain