Path: blob/main/projects/HexGL/bkcore.coffee/threejs/Particles.js
4627 views
// Generated by CoffeeScript 1.4.012/*3Particle system wrapper/helper45@class bkcore.threejs.Particles6@author Thibaut 'BKcore' Despoulain <http://bkcore.com>7*/8910(function() {11var Particle, Particles, exports, _base;1213Particles = (function() {14/*15Creates a new particle system using given parameters1617@param {Object{max, spawnRate, spawn, velocity, randomness,18force, spawnRadius, life, friction, color, color2, tint,19texture, size, blending, depthTest, transparent, opacity}} opts20*/2122function Particles(opts) {23var _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref14, _ref15, _ref16, _ref17, _ref18, _ref19, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;24this.black = new THREE.Color(0x000000);25this.white = new THREE.Color(0xffffff);26this.material = new THREE.ParticleBasicMaterial({27color: (_ref = opts.tint) != null ? _ref : 0xffffff,28map: (_ref1 = opts.texture) != null ? _ref1 : null,29size: (_ref2 = opts.size) != null ? _ref2 : 4,30blending: (_ref3 = opts.blending) != null ? _ref3 : THREE.AdditiveBlending,31depthTest: (_ref4 = opts.depthTest) != null ? _ref4 : false,32transparent: (_ref5 = opts.transparent) != null ? _ref5 : true,33vertexColors: true,34opacity: (_ref6 = opts.opacity) != null ? _ref6 : 1.0,35sizeAttenuation: true36});37this.max = (_ref7 = opts.max) != null ? _ref7 : 1000;38this.spawnRate = (_ref8 = opts.spawnRate) != null ? _ref8 : 0;39this.spawn = (_ref9 = opts.spawn) != null ? _ref9 : new THREE.Vector3();40this.velocity = (_ref10 = opts.velocity) != null ? _ref10 : new THREE.Vector3();41this.randomness = (_ref11 = opts.randomness) != null ? _ref11 : new THREE.Vector3();42this.force = (_ref12 = opts.force) != null ? _ref12 : new THREE.Vector3();43this.spawnRadius = (_ref13 = opts.spawnRadius) != null ? _ref13 : new THREE.Vector3();44this.life = (_ref14 = opts.life) != null ? _ref14 : 60;45this.ageing = 1 / this.life;46this.friction = (_ref15 = opts.friction) != null ? _ref15 : 1.0;47this.color = new THREE.Color((_ref16 = opts.color) != null ? _ref16 : 0xffffff);48this.color2 = opts.color2 != null ? new THREE.Color(opts.color2) : null;49this.position = (_ref17 = opts.position) != null ? _ref17 : new THREE.Vector3();50this.rotation = (_ref18 = opts.rotation) != null ? _ref18 : new THREE.Vector3();51this.sort = (_ref19 = opts.sort) != null ? _ref19 : false;52this.pool = [];53this.buffer = [];54this.geometry = null;55this.system = null;56this.build();57}5859/*60Emits given number of particles61@param int count62*/636465Particles.prototype.emit = function(count) {66var emitable, i, p, _i, _results;67emitable = Math.min(count, this.pool.length);68_results = [];69for (i = _i = 0; 0 <= emitable ? _i <= emitable : _i >= emitable; i = 0 <= emitable ? ++_i : --_i) {70p = this.pool.pop();71p.available = false;72p.position.copy(this.spawn).addSelf(this.randomVector().multiplySelf(this.spawnRadius));73p.velocity.copy(this.velocity).addSelf(this.randomVector().multiplySelf(this.randomness));74p.force.copy(this.force);75p.basecolor.copy(this.color);76if (this.color2 != null) {77p.basecolor.lerpSelf(this.color2, Math.random());78}79_results.push(p.life = 1.0);80}81return _results;82};8384/*85@private86*/878889Particles.prototype.build = function() {90var i, p, _i, _ref;91this.geometry = new THREE.Geometry();92this.geometry.dynamic = true;93this.pool = [];94this.buffer = [];95for (i = _i = 0, _ref = this.max; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {96p = new bkcore.threejs.Particle();97this.pool.push(p);98this.buffer.push(p);99this.geometry.vertices.push(p.position);100this.geometry.colors.push(p.color);101}102this.system = new THREE.ParticleSystem(this.geometry, this.material);103this.system.position = this.position;104this.system.rotation = this.rotation;105return this.system.sort = this.sort;106};107108/*109@private110*/111112113Particles.prototype.randomVector = function() {114return new THREE.Vector3(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1);115};116117/*118Updates particles (should be call in a RAF loop)119@param float dt time delta ~1.0120*/121122123Particles.prototype.update = function(dt) {124var df, dv, i, l, p, _i, _ref;125df = new THREE.Vector3();126dv = new THREE.Vector3();127for (i = _i = 0, _ref = this.buffer.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {128p = this.buffer[i];129if (p.available) {130continue;131}132p.life -= this.ageing;133if (p.life <= 0) {134p.reset();135this.pool.push(p);136continue;137}138l = p.life > 0.5 ? 1.0 : p.life + 0.5;139p.color.setRGB(l * p.basecolor.r, l * p.basecolor.g, l * p.basecolor.b);140if (this.friction !== 1.0) {141p.velocity.multiplyScalar(this.friction);142}143df.copy(p.force).multiplyScalar(dt);144p.velocity.addSelf(df);145dv.copy(p.velocity).multiplyScalar(dt);146p.position.addSelf(dv);147}148if (this.spawnRate > 0) {149this.emit(this.spawnRate);150}151this.geometry.verticesNeedUpdate = true;152return this.geometry.colorsNeedUpdate = true;153};154155return Particles;156157})();158159/*160Particle sub class161162@class bkcore.threejs.Particle163@author Thibaut 'BKcore' Despoulain <http://bkcore.com>164*/165166167Particle = (function() {168169function Particle() {170this.position = new THREE.Vector3(-10000, -10000, -10000);171this.velocity = new THREE.Vector3();172this.force = new THREE.Vector3();173this.color = new THREE.Color(0x000000);174this.basecolor = new THREE.Color(0x000000);175this.life = 0.0;176this.available = true;177}178179Particle.prototype.reset = function() {180this.position.set(0, -100000, 0);181this.velocity.set(0, 0, 0);182this.force.set(0, 0, 0);183this.color.setRGB(0, 0, 0);184this.basecolor.setRGB(0, 0, 0);185this.life = 0.0;186return this.available = true;187};188189return Particle;190191})();192193/*194Exports195@package bkcore.threejs196*/197198199exports = exports != null ? exports : this;200201exports.bkcore || (exports.bkcore = {});202203(_base = exports.bkcore).threejs || (_base.threejs = {});204205exports.bkcore.threejs.Particle = Particle;206207exports.bkcore.threejs.Particles = Particles;208209}).call(this);210211212