メニュー 閉じる

129. p5.jsでドラッグした場所からキューブをわちゃわちゃ改訂版

089のわちゃわちゃキューブの出方とライトを変更。

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

var star_system = [];

var fire_num = 1.0;

var targetx;
var targety;

var rotx = 0.0;
var roty = 0.0;

var r_val = 0;
var g_val = 0;
var b_val = 0;

function setup () {

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

}

function draw () {

	background (0, 0, 0);

	ambientLight (10, 10, 120);
  	pointLight (r_val, g_val, b_val, 2000.0, 0.0, 2000.0);
  	pointLight (r_val, g_val, b_val, -2000.0, 0.0, -2000.0);
  	pointLight (r_val, g_val, b_val, 0.0, 2000.0, 2000.0);
  	pointLight (r_val, g_val, b_val, 0.0, -2000.0, -2000.0);
  	pointLight (r_val, g_val, b_val, 2000.0, 0.0, 2000.0);
  	pointLight (r_val, g_val, b_val, -2000.0, 0.0, -2000.0);
  	pointLight (r_val, g_val, b_val, 0.0, 2000.0, 2000.0);
  	pointLight (r_val, g_val, b_val, 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);

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

	r_val = map (targetx, 0.0, width, 0, 255);
	g_val = map (targety, 0.0, height, 0, 255);
	b_val += 1;

	if (b_val > 255) {
		b_val = 0;
	}

}

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 = 0.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);

		s_size += (this.velocity.x + this.velocity.y) / 2.0;

	}

	this.render = function () {

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

	}

}

function mousePressed () {

	star_system.push (new StarSystem (mouseX - width / 2.0, mouseY - height / 2.0, random (-200.0, 200.0)));

}

function mouseDragged () {

	star_system.push (new StarSystem (mouseX - width / 2.0, mouseY - height / 2.0, random (-200.0, 200.0)));

}

 

Posted in p5.js