LoginSignup
7
8

More than 5 years have passed since last update.

クリスマスなのでprocessingで夜の森を書く

Last updated at Posted at 2015-12-24

メリークルシミマス

元ネタ

参考にさせていただきました!

processingで木を描く

ウックリスマス...

0058.png

int step = 12;
int count = 0;
int lightNum = 200;

float tr_scale;
float tr_angle;
float tr_length;
float tr_startd;
float tr_startx;
float tr_starty;
float offset = -90;
float triangle = 30;


Light[] lights = {};

PGraphics backPg;
PGraphics frontPg;


void setup(){
  size (1366, 768);
  colorMode(RGB, 256);
  background(255);

  frontPg = createGraphics(width, height);
  backPg  = createGraphics(width, height);

  createLayer(backPg, 40, false);
  createLayer(frontPg, 20, true);

  for (int i = 0; i < lightNum; i++) {
    lights = (Light[])append(lights, new Light());
  }
}

void draw() {
  image(backPg, 0, 0);

  for (int i = 0; i < lightNum; i++) {
    lights[i].draw();
  }

  image(frontPg, 0, 0);
}


void createLayer(PGraphics context, int num, boolean hasBg) {
  tr_scale  = 0.99;
  tr_angle  = 24.0;
  tr_length = 180.0;
  tr_startd = 0.0;
  tr_startx = width / 2;
  tr_starty = height;

  context.beginDraw();

  if (hasBg == true) {
    backDraw(context);
  }


  createTree (context, tr_startx, tr_starty, tr_length, tr_startd, step);

  for (int i = 0; i < num; i++) {
    if (hasBg == true) {
      backDraw(context);
    }

    if (count == step) {
      count = 0;
      createTree(context, random (0, width), tr_starty, random(tr_length), tr_startd, step);
    }
  }

  context.endDraw();
}

void createTree (PGraphics context, float x01, float y01, float len, float deg, int n){
  context.stroke(color(#00695C, float(5 / (n + 1))));

  context.strokeWeight(int ((n + 1) / 4));

  float x02 = x01 + len * cos(radians(deg + offset));
  float y02 = y01 + len * sin(radians(deg + offset));

  context.line(x01, y01, x02, y02);

  if (n > 0) {
    float deg01 = random(-tr_angle, triangle);
    float scl01 = random(random(10, 20), len * tr_scale);
    createTree(context, x02, y02, scl01, deg + deg01, n - 1);

    float deg02 = random(-tr_angle, triangle);
    float scl02 = random(random(10, 20), len * tr_scale);
    createTree(context, x02, y02, scl02, deg + deg02, n - 1);
  }

  count = n;
}

void backDraw(PGraphics context) {
  context.noStroke();
  context.fill(#2a2f47, 9.5);

  context.rectMode(CORNER);

  context.rect(0, 0, width, height);
}

class Light {
  float x, y, brightness;
  float cnt;
  float v;

  Light() {
    x = random(width);
    y = random(height);
    cnt = 0;
    v = 0.6;
  }

  void draw() {
    noStroke();

    fill(color(#FFFF00, round(cnt -0.5)));
    ellipse(x + random(-1, 1), y + random(-1, 1), 8 + round(cnt / 10), 8 + round(cnt / 10));

    fill(color(#FFFF00, round(cnt)));
    ellipse(x, y, 5, 5);

    cnt += (v + random(-0.1, 0.5));

    if (cnt > 20.0) {
      v = -0.4;
    }

    if (cnt < 1.0) {
      v = 0.4;
    }
  }
}

ディスプレイによって出る色が違うのでアレで悩みます...
JavaScript版のprocessingに移植すると崩れる...

動かすと蛍みたいに光が揺れてちょっと綺麗
クリスマスそっちのけで乱数と向き合う方が楽し(ry

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8