メニュー 閉じる

073. p5.jsでキラキラの泡がどんどん出てくるアニメーション

066と068を組み合わせて、specularMaterialを加えてみました。光沢のある泡になりました。

http://www.velvet-number.com/p5_test/test051/

var star_system = [];

var fire_num = 200.0;

var targetx;
var targety;

var rotx = 0.0;
var roty = 0.0;

function setup () {

	pixelDensity (displayDensity ());
	createCanvas (windowWidth, windowHeight, WEBGL);
	colorMode (RGB, 256);
	background (0);

}

function draw () {

	background (0, 0, 0);

	ambientLight (80, 100, 20);
  	pointLight (255, 0, 0, 2000.0, 0.0, 2000.0);
  	pointLight (255, 0, 0, -2000.0, 0.0, -2000.0);
  	pointLight (0, 255, 0, 0.0, 2000.0, 2000.0);
  	pointLight (0, 255, 0, 0.0, -2000.0, -2000.0);
  	pointLight (255, 0, 0, 2000.0, 0.0, 2000.0);
  	pointLight (255, 0, 0, -2000.0, 0.0, -2000.0);
  	pointLight (0, 255, 0, 0.0, 2000.0, 2000.0);
  	pointLight (0, 255, 0, 0.0, -2000.0, -2000.0);

  	camera (0.0, 0.0, -1000.0,
			0.0, 0.0, 0.0,
			0.0, 1.0, 0.0);

  	targetx = mouseX;
  	targety = mouseY;

  	rotx += (targetx - rotx) * 0.1;
  	roty += (targety - roty) * 0.1;

  	rotateY (rotx * 0.01);
  	rotateX (roty * 0.01);

	if (frameCount % 20 == 0) {
		star_system.push (new StarSystem (random (-width / 2.0, width / 2.0), random (-200.0, 200.0), random (-200.0, 200.0)));
	}

	for (var i = 0; i < star_system.length; i++) {
		star_system[i].update ();
		if (star_system[i].life_time > 10.0) {
			star_system.splice (i, 1);
		}
	}

}

function StarSystem (sx, sy, sz) {

	var stars = [];

	this.life_time = 0.0;

	for (var i = 0; i < fire_num; i++) {
		stars.push (new StarCreate (sx, sy, sz));
	}

	this.update = function () {

		for (var i = 0; i < fire_num; i++) {
			stars[i].update ();
			stars[i].render ();
		}

		this.life_time += 0.1;

	}

}

function StarCreate (dx, dy, dz) {

	var s_size = random (10.0, 80.0);

	this.location = new p5.Vector (dx, dy, dz);
	this.velocity = new p5.Vector (0.0, 0.0, 0.0);
	this.acceleration = new p5.Vector (0.0, 0.0, 0.0);
	this.direction01 = random (-PI, PI);
	this.direction02 = random (-PI, PI);
	this.tmp_x = cos (this.direction01) * sin (this.direction02);
	this.tmp_y = sin (this.direction01);
	this.tmp_z = cos (this.direction01) * cos (this.direction02);
	this.init_force = new p5.Vector (this.tmp_x, this.tmp_y, this.tmp_z);
	this.init_force.mult (random (80.0, 120.0));

	this.update = function () {

		this.init_force.mult (0.2);
		this.acceleration.add (this.init_force);
		this.velocity.add (this.acceleration);
		this.velocity.mult (0.98);
		this.location.add (this.velocity);
		this.acceleration.mult (0.0);

	}

	this.render = function () {

		push ();
		translate (this.location.x, this.location.y, this.location.z);
		noStroke ();
		specularMaterial (255);
		sphere (s_size);
		pop ();

	}

}

function mousePressed () {

	star_system = [];

}

 

Posted in p5.js , processing