Path: blob/main/projects/HexGL/bkcore.coffee/threejs/Particles.coffee
4627 views
###1Particle system wrapper/helper23@class bkcore.threejs.Particles4@author Thibaut 'BKcore' Despoulain <http://bkcore.com>5###6class Particles78###9Creates a new particle system using given parameters1011@param {Object{max, spawnRate, spawn, velocity, randomness,12force, spawnRadius, life, friction, color, color2, tint,13texture, size, blending, depthTest, transparent, opacity}} opts14###15constructor: (opts)->1617@black = new THREE.Color(0x000000)18@white = new THREE.Color(0xffffff)1920@material = new THREE.ParticleBasicMaterial(21color: opts.tint ? 0xffffff22map: opts.texture ? null23size: opts.size ? 424blending: opts.blending ? THREE.AdditiveBlending25depthTest: opts.depthTest ? false26transparent: opts.transparent ? true27vertexColors: true28opacity: opts.opacity ? 1.029sizeAttenuation: true30)3132@max = opts.max ? 100033@spawnRate = opts.spawnRate ? 03435@spawn = opts.spawn ? new THREE.Vector3()36@velocity = opts.velocity ? new THREE.Vector3()37@randomness = opts.randomness ? new THREE.Vector3()38@force = opts.force ? new THREE.Vector3()39@spawnRadius = opts.spawnRadius ? new THREE.Vector3()40@life = opts.life ? 6041@ageing = 1 / @life42@friction = opts.friction ? 1.043@color = new THREE.Color(opts.color ? 0xffffff)44@color2 = if opts.color2? then new THREE.Color(opts.color2) else null4546@position = opts.position ? new THREE.Vector3()47@rotation = opts.rotation ? new THREE.Vector3()48@sort = opts.sort ? false4950@pool = []51@buffer = []52@geometry = null53@system = null5455@build()5657###58Emits given number of particles59@param int count60###61emit: (count)->6263emitable = Math.min(count, @pool.length)6465for i in [0..emitable]6667p = @pool.pop()68p.available = false6970p.position.copy(@spawn).addSelf(71@randomVector().multiplySelf(@spawnRadius)72)73p.velocity.copy(@velocity).addSelf(74@randomVector().multiplySelf(@randomness)75)76p.force.copy(@force)77p.basecolor.copy(@color)7879if @color2?80p.basecolor.lerpSelf(@color2, Math.random())8182p.life = 1.08384###85@private86###87build: ()->8889@geometry = new THREE.Geometry()90@geometry.dynamic = true9192@pool = []93@buffer = []9495for i in [0..@max]9697p = new bkcore.threejs.Particle()98@pool.push(p)99@buffer.push(p)100@geometry.vertices.push(p.position)101@geometry.colors.push(p.color)102103@system = new THREE.ParticleSystem(@geometry, @material)104@system.position = @position105@system.rotation = @rotation106@system.sort = @sort107108###109@private110###111randomVector: ()->112113return new THREE.Vector3(114Math.random()*2-1,115Math.random()*2-1,116Math.random()*2-1117)118119###120Updates particles (should be call in a RAF loop)121@param float dt time delta ~1.0122###123update: (dt)->124125df = new THREE.Vector3()126dv = new THREE.Vector3()127128for i in [0..@buffer.length]129130p = @buffer[i]131continue if p.available132133p.life -= @ageing134135if p.life <= 0136p.reset()137@pool.push(p)138continue139140141l = if p.life > 0.5 then 1.0 else p.life + 0.5142p.color.setRGB(143l * p.basecolor.r,144l * p.basecolor.g,145l * p.basecolor.b146)147148if @friction != 1.0149p.velocity.multiplyScalar(@friction)150151df.copy(p.force).multiplyScalar(dt)152p.velocity.addSelf(df)153154dv.copy(p.velocity).multiplyScalar(dt)155p.position.addSelf(dv)156157if @spawnRate > 0158@emit(@spawnRate)159160@geometry.verticesNeedUpdate = true161@geometry.colorsNeedUpdate = true162163###164Particle sub class165166@class bkcore.threejs.Particle167@author Thibaut 'BKcore' Despoulain <http://bkcore.com>168###169class Particle170171constructor: ()->172173@position = new THREE.Vector3(-10000,-10000,-10000)174@velocity = new THREE.Vector3()175@force = new THREE.Vector3()176@color = new THREE.Color(0x000000)177@basecolor = new THREE.Color(0x000000)178@life = 0.0179@available = true180181reset: ()->182@position.set(0,-100000,0)183@velocity.set(0,0,0)184@force.set(0,0,0)185@color.setRGB(0,0,0)186@basecolor.setRGB(0,0,0)187@life = 0.0188@available = true189190###191Exports192@package bkcore.threejs193###194exports = exports ? @195exports.bkcore ||= {}196exports.bkcore.threejs ||= {}197exports.bkcore.threejs.Particle = Particle198exports.bkcore.threejs.Particles = Particles199200201