Path: blob/main/projects/HexGL/bkcore/threejs/Particles.js
4627 views
/*!1* @class bkcore.threejs.Particles2*3* Particle system wrapper/helper4*5* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>6*/78/*!9* @package bkcore.threejs10*/11var bkcore = bkcore || {};12bkcore.threejs = bkcore.threejs || {};1314/**15* Creates a new particle system using given parameters16* @param {Object{max, spawnRate, spawn, velocity, randomness, force, spawnRadius, life, friction, color, color2, tint, texture, size, blending, depthTest, transparent, opacity}} opts17*/18bkcore.threejs.Particles = function(opts)19{20this.black = new THREE.Color(0x000000);21this.white = new THREE.Color(0xffffff);2223this.material = new THREE.ParticleBasicMaterial({24color: opts.tint == undefined ? 0xffffff : opts.tint,25map: opts.texture == undefined ? null : opts.texture,26size: opts.size == undefined ? 4 : opts.size,27blending: opts.blending == undefined ? THREE.AdditiveBlending : opts.blending,28depthTest: opts.depthTest == undefined ? false : opts.depthTest,29transparent: opts.transparent == undefined ? true : opts.transparent,30vertexColors: true,31opacity: opts.opacity == undefined ? 1.0 : opts.opacity,32sizeAttenuation: true33});3435this.max = opts.max == undefined ? 1000 : opts.max;36this.spawnRate = opts.spawnRate == undefined ? 0 : opts.spawnRate;3738this.spawn = opts.spawn == undefined ? new THREE.Vector3() : opts.spawn;39this.velocity = opts.velocity == undefined ? new THREE.Vector3() : opts.velocity;40this.randomness = opts.randomness == undefined ? new THREE.Vector3() : opts.randomness;41this.force = opts.force == undefined ? new THREE.Vector3() : opts.force;42this.spawnRadius = opts.spawnRadius == undefined ? new THREE.Vector3() : opts.spawnRadius;43this.life = opts.life == undefined ? 60 : opts.life;44this.ageing = 1 / this.life;45this.friction = opts.friction == undefined ? 1.0 : opts.friction;46this.color = new THREE.Color(opts.color == undefined ? 0xffffff : opts.color);47this.color2 = opts.color2 == undefined ? null : new THREE.Color(opts.color2);4849this.position = opts.position == undefined ? new THREE.Vector3() : opts.position;50this.rotation = opts.rotation == undefined ? new THREE.Vector3() : opts.rotation;51this.sort = opts.sort == undefined ? false : opts.sort;5253this.pool = [];54this.buffer = [];55this.geometry = null;56this.system = null;5758this.build();59}6061bkcore.threejs.Particles.prototype.build = function()62{63this.geometry = new THREE.Geometry();64this.geometry.dynamic = true;6566this.pool = [];67this.buffer = [];6869for(var i = 0; i < this.max; ++i)70{71var p = new bkcore.threejs.Particle();72this.pool.push(p);73this.buffer.push(p);74this.geometry.vertices.push(p.position);75this.geometry.colors.push(p.color);76}7778this.system = new THREE.ParticleSystem(this.geometry, this.material);79this.system.position = this.position;80this.system.rotation = this.rotation;81this.system.sort = this.sort;82}8384/**85* Emits given number of particles86* @param int count87*/88bkcore.threejs.Particles.prototype.emit = function(count)89{90var emitable = Math.min(count, this.pool.length);91for(var i = 0; i < emitable; ++i)92{93var p = this.pool.pop();94p.available = false;95p.position.copy(this.spawn)96.addSelf(97this.randomVector()98.multiplySelf(this.spawnRadius)99);100p.velocity.copy(this.velocity)101.addSelf(102this.randomVector()103.multiplySelf(this.randomness)104);105p.force.copy(this.force);106p.basecolor.copy(this.color);107if(this.color2 != undefined) p.basecolor.lerpSelf(this.color2, Math.random());108p.life = 1.0;109}110}111112bkcore.threejs.Particles.prototype.randomVector = function()113{114return new THREE.Vector3(115Math.random()*2-1,116Math.random()*2-1,117Math.random()*2-1118);119}120121/**122* Updates particles (should be call in a RAF loop)123* @param float dt time delta ~1.0124*/125bkcore.threejs.Particles.prototype.update = function(dt)126{127var p, l;128var df = new THREE.Vector3();129var dv = new THREE.Vector3();130for(var i = 0; i < this.buffer.length; ++i)131{132133p = this.buffer[i];134135if(p.available) continue;136137p.life -= this.ageing;138139if(p.life <= 0 && !p.available)140{141p.reset();142this.pool.push(p);143continue;144}145146l = p.life > 0.5 ? 1.0 : p.life + 0.5;147p.color.setRGB(148l * p.basecolor.r,149l * p.basecolor.g,150l * p.basecolor.b151);152153if(this.friction != 1.0)154p.velocity.multiplyScalar(this.friction);155156df.copy(p.force).multiplyScalar(dt);157p.velocity.addSelf(df);158159dv.copy(p.velocity).multiplyScalar(dt);160p.position.addSelf(dv);161}162163if(this.spawnRate > 0)164this.emit(this.spawnRate);165166this.geometry.verticesNeedUpdate = true;167this.geometry.colorsNeedUpdate = true;168}169170bkcore.threejs.Particle = function()171{172this.position = new THREE.Vector3(-10000,-10000,-10000);173this.velocity = new THREE.Vector3();174this.force = new THREE.Vector3();175this.color = new THREE.Color(0x000000);176this.basecolor = new THREE.Color(0x000000);177this.life = 0.0;178this.available = true;179}180181bkcore.threejs.Particle.prototype.reset = function()182{183this.position.set(0,-100000,0);184this.velocity.set(0,0,0);185this.force.set(0,0,0);186this.color.setRGB(0,0,0);187this.basecolor.setRGB(0,0,0);188this.life = 0.0;189this.available = true;190}191192