メニュー 閉じる

106. ProcessingでNyARToolkitを使ってなんちゃってAR06

マーカーから花火を打ち上げてみました。

http://www.velvet-number.com/p5_test/test074/20190306_mov01.mov
http://www.velvet-number.com/p5_test/test074/20190306_mov02.mov

import processing.video.*;
import jp.nyatla.nyar4psg.*;

Capture cam;

MultiMarker mm;

ArrayList<Particles> particles = new ArrayList<Particles> ();

boolean mm_exist = false;

void setup () {

	size (480, 270, P3D);
	colorMode (HSB, 100);

	String[] cameras = Capture.list ();

	printArray (cameras);

	cam = new Capture (this, cameras[6]);
	cam.start ();

	mm = new MultiMarker (this, width, height, "camera_para.dat", NyAR4PsgConfig.CONFIG_PSG);
	mm.addNyIdMarker (1, 80);

}

void draw () {

	if (cam.available () != true) {
		return;
	}

	cam.read ();

	mm.detect (cam);
	mm.drawBackground (cam);

	if (!mm.isExist (0)) {
		mm_exist = false;
	} else {
		mm_exist = true;
	}

	particles.add (new Particles ());

	if (mm_exist == true) {
		for (int i = 0; i < particles.size (); i++) {
			mm.beginTransform (0);
			particles.get (i).update ();
			particles.get (i).render ();
			mm.endTransform ();
		}
	}

	if (particles.size () > 100) {
		//particles.remove (0);
	}

	//saveFrame ();

}

class Particles {

	ArrayList<Fireworks> fireworks = new ArrayList<Fireworks> ();

	float posx;
	float posy;
	float posz;
	float vz;
	float bs;

	float h_value;

	boolean fire_flag;

	Particles () {

		posx = random (-40.0, 40.0);
		posy = random (-40.0, 40.0);
		posz = 0.0;

		vz = random (2.0, 10.0);

		bs = random (2.0, 8.0);

		h_value = random (100.0);

		fire_flag = true;

	}

	void update () {

		posz += vz;
		vz *= 0.98;

		if (vz < 0.1) {
			if (fire_flag == true) {
				fire_flag = false;
				for (int i = 0; i < 1000; i++) {
					fireworks.add (new Fireworks (posx, posy, posz, h_value));
				}
			}
			for (int i = 0; i < 100; i++) {
				fireworks.get (i).update ();
				fireworks.get (i).render ();
				if (fireworks.get (i).alpha < 0.1) {
					particles.remove (this);
				}
			}
		}

	}

	void render () {
	
		stroke (0);
		strokeWeight (1);
		fill (h_value, 100, 100, 50);
		translate (posx, posy, posz);
		box (bs);

	}

}

class Fireworks {

	float alpha;

	PVector location;
	PVector velocity;
	PVector acceleration;

	float direction01;
	float direction02;
	float tmp_x;
	float tmp_y;
	float tmp_z;

	float col;
		
	PVector init_force;

	Fireworks (float dx, float dy, float dz, float c) {

		alpha = 100.0;

		location = new PVector (dx, dy, dz);
		velocity = new PVector (0.0, 0.0, 0.0);
		acceleration = new PVector (0.0, 0.0, 0.0);

		direction01 = random (-PI, PI);
		direction02 = random (-PI, PI);
		tmp_x = cos (direction01) * sin (direction02);
		tmp_y = sin (direction01);
		tmp_z = cos (direction01) * cos (direction02);

		init_force = new PVector (tmp_x, tmp_y, tmp_z);

		col = c;

		init_force.mult (random (20.0, 80.0));

	}

	void update () {

		alpha -= 2.0;

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

	}

	void render () {

		push ();
		translate (location.x, location.y, location.z);
		stroke (0, alpha);
		strokeWeight (1);
		fill (col, 100, 100, alpha);
		box (2);
		pop ();

	}

}

 

Posted in processing