GPUParticleSystem.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * GPU Particle System
  3. * @author flimshaw - Charlie Hoey - http://charliehoey.com
  4. *
  5. * A simple to use, general purpose GPU system. Particles are spawn-and-forget with
  6. * several options available, and do not require monitoring or cleanup after spawning.
  7. * Because the paths of all particles are completely deterministic once spawned, the scale
  8. * and direction of time is also variable.
  9. *
  10. * Currently uses a static wrapping perlin noise texture for turbulence, and a small png texture for
  11. * particles, but adding support for a particle texture atlas or changing to a different type of turbulence
  12. * would be a fairly light day's work.
  13. *
  14. * Shader and javascript packing code derrived from several Stack Overflow examples.
  15. *
  16. */
  17. THREE.GPUParticleSystem = function ( options ) {
  18. THREE.Object3D.apply( this, arguments );
  19. options = options || {};
  20. // parse options and use defaults
  21. this.PARTICLE_COUNT = options.maxParticles || 1000000;
  22. this.PARTICLE_CONTAINERS = options.containerCount || 1;
  23. this.PARTICLE_NOISE_TEXTURE = options.particleNoiseTex || null;
  24. this.PARTICLE_SPRITE_TEXTURE = options.particleSpriteTex || null;
  25. this.PARTICLES_PER_CONTAINER = Math.ceil( this.PARTICLE_COUNT / this.PARTICLE_CONTAINERS );
  26. this.PARTICLE_CURSOR = 0;
  27. this.time = 0;
  28. this.particleContainers = [];
  29. this.rand = [];
  30. // custom vertex and fragement shader
  31. var GPUParticleShader = {
  32. vertexShader: [
  33. 'uniform float uTime;',
  34. 'uniform float uScale;',
  35. 'uniform sampler2D tNoise;',
  36. 'attribute vec3 positionStart;',
  37. 'attribute float startTime;',
  38. 'attribute vec3 velocity;',
  39. 'attribute float turbulence;',
  40. 'attribute vec3 color;',
  41. 'attribute float size;',
  42. 'attribute float lifeTime;',
  43. 'varying vec4 vColor;',
  44. 'varying float lifeLeft;',
  45. 'void main() {',
  46. // unpack things from our attributes'
  47. ' vColor = vec4( color, 1.0 );',
  48. // convert our velocity back into a value we can use'
  49. ' vec3 newPosition;',
  50. ' vec3 v;',
  51. ' float timeElapsed = uTime - startTime;',
  52. ' lifeLeft = 1.0 - ( timeElapsed / lifeTime );',
  53. ' gl_PointSize = ( uScale * size ) * lifeLeft;',
  54. ' v.x = ( velocity.x - 0.5 ) * 3.0;',
  55. ' v.y = ( velocity.y - 0.5 ) * 3.0;',
  56. ' v.z = ( velocity.z - 0.5 ) * 3.0;',
  57. ' newPosition = positionStart + ( v * 10.0 ) * timeElapsed;',
  58. ' vec3 noise = texture2D( tNoise, vec2( newPosition.x * 0.015 + ( uTime * 0.05 ), newPosition.y * 0.02 + ( uTime * 0.015 ) ) ).rgb;',
  59. ' vec3 noiseVel = ( noise.rgb - 0.5 ) * 30.0;',
  60. ' newPosition = mix( newPosition, newPosition + vec3( noiseVel * ( turbulence * 5.0 ) ), ( timeElapsed / lifeTime ) );',
  61. ' if( v.y > 0. && v.y < .05 ) {',
  62. ' lifeLeft = 0.0;',
  63. ' }',
  64. ' if( v.x < - 1.45 ) {',
  65. ' lifeLeft = 0.0;',
  66. ' }',
  67. ' if( timeElapsed > 0.0 ) {',
  68. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );',
  69. ' } else {',
  70. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  71. ' lifeLeft = 0.0;',
  72. ' gl_PointSize = 0.;',
  73. ' }',
  74. '}'
  75. ].join( '\n' ),
  76. fragmentShader: [
  77. 'float scaleLinear( float value, vec2 valueDomain ) {',
  78. ' return ( value - valueDomain.x ) / ( valueDomain.y - valueDomain.x );',
  79. '}',
  80. 'float scaleLinear( float value, vec2 valueDomain, vec2 valueRange ) {',
  81. ' return mix( valueRange.x, valueRange.y, scaleLinear( value, valueDomain ) );',
  82. '}',
  83. 'varying vec4 vColor;',
  84. 'varying float lifeLeft;',
  85. 'uniform sampler2D tSprite;',
  86. 'void main() {',
  87. ' float alpha = 0.;',
  88. ' if( lifeLeft > 0.995 ) {',
  89. ' alpha = scaleLinear( lifeLeft, vec2( 1.0, 0.995 ), vec2( 0.0, 1.0 ) );',
  90. ' } else {',
  91. ' alpha = lifeLeft * 0.75;',
  92. ' }',
  93. ' vec4 tex = texture2D( tSprite, gl_PointCoord );',
  94. ' gl_FragColor = vec4( vColor.rgb * tex.a, alpha * tex.a );',
  95. '}'
  96. ].join( '\n' )
  97. };
  98. // preload a million random numbers
  99. var i;
  100. for ( i = 1e5; i > 0; i -- ) {
  101. this.rand.push( Math.random() - 0.5 );
  102. }
  103. this.random = function () {
  104. return ++ i >= this.rand.length ? this.rand[ i = 1 ] : this.rand[ i ];
  105. };
  106. var textureLoader = new THREE.TextureLoader();
  107. this.particleNoiseTex = this.PARTICLE_NOISE_TEXTURE || textureLoader.load( 'textures/perlin-512.png' );
  108. this.particleNoiseTex.wrapS = this.particleNoiseTex.wrapT = THREE.RepeatWrapping;
  109. this.particleSpriteTex = this.PARTICLE_SPRITE_TEXTURE || textureLoader.load( 'textures/particle2.png' );
  110. this.particleSpriteTex.wrapS = this.particleSpriteTex.wrapT = THREE.RepeatWrapping;
  111. this.particleShaderMat = new THREE.ShaderMaterial( {
  112. transparent: true,
  113. depthWrite: false,
  114. uniforms: {
  115. 'uTime': {
  116. value: 0.0
  117. },
  118. 'uScale': {
  119. value: 1.0
  120. },
  121. 'tNoise': {
  122. value: this.particleNoiseTex
  123. },
  124. 'tSprite': {
  125. value: this.particleSpriteTex
  126. }
  127. },
  128. blending: THREE.AdditiveBlending,
  129. vertexShader: GPUParticleShader.vertexShader,
  130. fragmentShader: GPUParticleShader.fragmentShader
  131. } );
  132. // define defaults for all values
  133. this.particleShaderMat.defaultAttributeValues.particlePositionsStartTime = [ 0, 0, 0, 0 ];
  134. this.particleShaderMat.defaultAttributeValues.particleVelColSizeLife = [ 0, 0, 0, 0 ];
  135. this.init = function () {
  136. for ( var i = 0; i < this.PARTICLE_CONTAINERS; i ++ ) {
  137. var c = new THREE.GPUParticleContainer( this.PARTICLES_PER_CONTAINER, this );
  138. this.particleContainers.push( c );
  139. this.add( c );
  140. }
  141. };
  142. this.spawnParticle = function ( options ) {
  143. this.PARTICLE_CURSOR ++;
  144. if ( this.PARTICLE_CURSOR >= this.PARTICLE_COUNT ) {
  145. this.PARTICLE_CURSOR = 1;
  146. }
  147. var currentContainer = this.particleContainers[ Math.floor( this.PARTICLE_CURSOR / this.PARTICLES_PER_CONTAINER ) ];
  148. currentContainer.spawnParticle( options );
  149. };
  150. this.update = function ( time ) {
  151. for ( var i = 0; i < this.PARTICLE_CONTAINERS; i ++ ) {
  152. this.particleContainers[ i ].update( time );
  153. }
  154. };
  155. this.dispose = function () {
  156. this.particleShaderMat.dispose();
  157. this.particleNoiseTex.dispose();
  158. this.particleSpriteTex.dispose();
  159. for ( var i = 0; i < this.PARTICLE_CONTAINERS; i ++ ) {
  160. this.particleContainers[ i ].dispose();
  161. }
  162. };
  163. this.init();
  164. };
  165. THREE.GPUParticleSystem.prototype = Object.create( THREE.Object3D.prototype );
  166. THREE.GPUParticleSystem.prototype.constructor = THREE.GPUParticleSystem;
  167. // Subclass for particle containers, allows for very large arrays to be spread out
  168. THREE.GPUParticleContainer = function ( maxParticles, particleSystem ) {
  169. THREE.Object3D.apply( this, arguments );
  170. this.PARTICLE_COUNT = maxParticles || 100000;
  171. this.PARTICLE_CURSOR = 0;
  172. this.time = 0;
  173. this.offset = 0;
  174. this.count = 0;
  175. this.DPR = window.devicePixelRatio;
  176. this.GPUParticleSystem = particleSystem;
  177. this.particleUpdate = false;
  178. // geometry
  179. this.particleShaderGeo = new THREE.BufferGeometry();
  180. this.particleShaderGeo.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT * 3 ), 3 ).setDynamic( true ) );
  181. this.particleShaderGeo.addAttribute( 'positionStart', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT * 3 ), 3 ).setDynamic( true ) );
  182. this.particleShaderGeo.addAttribute( 'startTime', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT ), 1 ).setDynamic( true ) );
  183. this.particleShaderGeo.addAttribute( 'velocity', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT * 3 ), 3 ).setDynamic( true ) );
  184. this.particleShaderGeo.addAttribute( 'turbulence', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT ), 1 ).setDynamic( true ) );
  185. this.particleShaderGeo.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT * 3 ), 3 ).setDynamic( true ) );
  186. this.particleShaderGeo.addAttribute( 'size', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT ), 1 ).setDynamic( true ) );
  187. this.particleShaderGeo.addAttribute( 'lifeTime', new THREE.BufferAttribute( new Float32Array( this.PARTICLE_COUNT ), 1 ).setDynamic( true ) );
  188. // material
  189. this.particleShaderMat = this.GPUParticleSystem.particleShaderMat;
  190. var position = new THREE.Vector3();
  191. var velocity = new THREE.Vector3();
  192. var color = new THREE.Color();
  193. this.spawnParticle = function ( options ) {
  194. var positionStartAttribute = this.particleShaderGeo.getAttribute( 'positionStart' );
  195. var startTimeAttribute = this.particleShaderGeo.getAttribute( 'startTime' );
  196. var velocityAttribute = this.particleShaderGeo.getAttribute( 'velocity' );
  197. var turbulenceAttribute = this.particleShaderGeo.getAttribute( 'turbulence' );
  198. var colorAttribute = this.particleShaderGeo.getAttribute( 'color' );
  199. var sizeAttribute = this.particleShaderGeo.getAttribute( 'size' );
  200. var lifeTimeAttribute = this.particleShaderGeo.getAttribute( 'lifeTime' );
  201. options = options || {};
  202. // setup reasonable default values for all arguments
  203. position = options.position !== undefined ? position.copy( options.position ) : position.set( 0, 0, 0 );
  204. velocity = options.velocity !== undefined ? velocity.copy( options.velocity ) : velocity.set( 0, 0, 0 );
  205. color = options.color !== undefined ? color.set( options.color ) : color.set( 0xffffff );
  206. var positionRandomness = options.positionRandomness !== undefined ? options.positionRandomness : 0;
  207. var velocityRandomness = options.velocityRandomness !== undefined ? options.velocityRandomness : 0;
  208. var colorRandomness = options.colorRandomness !== undefined ? options.colorRandomness : 1;
  209. var turbulence = options.turbulence !== undefined ? options.turbulence : 1;
  210. var lifetime = options.lifetime !== undefined ? options.lifetime : 5;
  211. var size = options.size !== undefined ? options.size : 10;
  212. var sizeRandomness = options.sizeRandomness !== undefined ? options.sizeRandomness : 0;
  213. var smoothPosition = options.smoothPosition !== undefined ? options.smoothPosition : false;
  214. if ( this.DPR !== undefined ) size *= this.DPR;
  215. var i = this.PARTICLE_CURSOR;
  216. // position
  217. positionStartAttribute.array[ i * 3 + 0 ] = position.x + ( particleSystem.random() * positionRandomness );
  218. positionStartAttribute.array[ i * 3 + 1 ] = position.y + ( particleSystem.random() * positionRandomness );
  219. positionStartAttribute.array[ i * 3 + 2 ] = position.z + ( particleSystem.random() * positionRandomness );
  220. if ( smoothPosition === true ) {
  221. positionStartAttribute.array[ i * 3 + 0 ] += - ( velocity.x * particleSystem.random() );
  222. positionStartAttribute.array[ i * 3 + 1 ] += - ( velocity.y * particleSystem.random() );
  223. positionStartAttribute.array[ i * 3 + 2 ] += - ( velocity.z * particleSystem.random() );
  224. }
  225. // velocity
  226. var maxVel = 2;
  227. var velX = velocity.x + particleSystem.random() * velocityRandomness;
  228. var velY = velocity.y + particleSystem.random() * velocityRandomness;
  229. var velZ = velocity.z + particleSystem.random() * velocityRandomness;
  230. velX = THREE.Math.clamp( ( velX - ( - maxVel ) ) / ( maxVel - ( - maxVel ) ), 0, 1 );
  231. velY = THREE.Math.clamp( ( velY - ( - maxVel ) ) / ( maxVel - ( - maxVel ) ), 0, 1 );
  232. velZ = THREE.Math.clamp( ( velZ - ( - maxVel ) ) / ( maxVel - ( - maxVel ) ), 0, 1 );
  233. velocityAttribute.array[ i * 3 + 0 ] = velX;
  234. velocityAttribute.array[ i * 3 + 1 ] = velY;
  235. velocityAttribute.array[ i * 3 + 2 ] = velZ;
  236. // color
  237. color.r = THREE.Math.clamp( color.r + particleSystem.random() * colorRandomness, 0, 1 );
  238. color.g = THREE.Math.clamp( color.g + particleSystem.random() * colorRandomness, 0, 1 );
  239. color.b = THREE.Math.clamp( color.b + particleSystem.random() * colorRandomness, 0, 1 );
  240. colorAttribute.array[ i * 3 + 0 ] = color.r;
  241. colorAttribute.array[ i * 3 + 1 ] = color.g;
  242. colorAttribute.array[ i * 3 + 2 ] = color.b;
  243. // turbulence, size, lifetime and starttime
  244. turbulenceAttribute.array[ i ] = turbulence;
  245. sizeAttribute.array[ i ] = size + particleSystem.random() * sizeRandomness;
  246. lifeTimeAttribute.array[ i ] = lifetime;
  247. startTimeAttribute.array[ i ] = this.time + particleSystem.random() * 2e-2;
  248. // offset
  249. if ( this.offset === 0 ) {
  250. this.offset = this.PARTICLE_CURSOR;
  251. }
  252. // counter and cursor
  253. this.count ++;
  254. this.PARTICLE_CURSOR ++;
  255. if ( this.PARTICLE_CURSOR >= this.PARTICLE_COUNT ) {
  256. this.PARTICLE_CURSOR = 0;
  257. }
  258. this.particleUpdate = true;
  259. };
  260. this.init = function () {
  261. this.particleSystem = new THREE.Points( this.particleShaderGeo, this.particleShaderMat );
  262. this.particleSystem.frustumCulled = false;
  263. this.add( this.particleSystem );
  264. };
  265. this.update = function ( time ) {
  266. this.time = time;
  267. this.particleShaderMat.uniforms.uTime.value = time;
  268. this.geometryUpdate();
  269. };
  270. this.geometryUpdate = function () {
  271. if ( this.particleUpdate === true ) {
  272. this.particleUpdate = false;
  273. var positionStartAttribute = this.particleShaderGeo.getAttribute( 'positionStart' );
  274. var startTimeAttribute = this.particleShaderGeo.getAttribute( 'startTime' );
  275. var velocityAttribute = this.particleShaderGeo.getAttribute( 'velocity' );
  276. var turbulenceAttribute = this.particleShaderGeo.getAttribute( 'turbulence' );
  277. var colorAttribute = this.particleShaderGeo.getAttribute( 'color' );
  278. var sizeAttribute = this.particleShaderGeo.getAttribute( 'size' );
  279. var lifeTimeAttribute = this.particleShaderGeo.getAttribute( 'lifeTime' );
  280. if ( this.offset + this.count < this.PARTICLE_COUNT ) {
  281. positionStartAttribute.updateRange.offset = this.offset * positionStartAttribute.itemSize;
  282. startTimeAttribute.updateRange.offset = this.offset * startTimeAttribute.itemSize;
  283. velocityAttribute.updateRange.offset = this.offset * velocityAttribute.itemSize;
  284. turbulenceAttribute.updateRange.offset = this.offset * turbulenceAttribute.itemSize;
  285. colorAttribute.updateRange.offset = this.offset * colorAttribute.itemSize;
  286. sizeAttribute.updateRange.offset = this.offset * sizeAttribute.itemSize;
  287. lifeTimeAttribute.updateRange.offset = this.offset * lifeTimeAttribute.itemSize;
  288. positionStartAttribute.updateRange.count = this.count * positionStartAttribute.itemSize;
  289. startTimeAttribute.updateRange.count = this.count * startTimeAttribute.itemSize;
  290. velocityAttribute.updateRange.count = this.count * velocityAttribute.itemSize;
  291. turbulenceAttribute.updateRange.count = this.count * turbulenceAttribute.itemSize;
  292. colorAttribute.updateRange.count = this.count * colorAttribute.itemSize;
  293. sizeAttribute.updateRange.count = this.count * sizeAttribute.itemSize;
  294. lifeTimeAttribute.updateRange.count = this.count * lifeTimeAttribute.itemSize;
  295. } else {
  296. positionStartAttribute.updateRange.offset = 0;
  297. startTimeAttribute.updateRange.offset = 0;
  298. velocityAttribute.updateRange.offset = 0;
  299. turbulenceAttribute.updateRange.offset = 0;
  300. colorAttribute.updateRange.offset = 0;
  301. sizeAttribute.updateRange.offset = 0;
  302. lifeTimeAttribute.updateRange.offset = 0;
  303. // Use -1 to update the entire buffer, see #11476
  304. positionStartAttribute.updateRange.count = - 1;
  305. startTimeAttribute.updateRange.count = - 1;
  306. velocityAttribute.updateRange.count = - 1;
  307. turbulenceAttribute.updateRange.count = - 1;
  308. colorAttribute.updateRange.count = - 1;
  309. sizeAttribute.updateRange.count = - 1;
  310. lifeTimeAttribute.updateRange.count = - 1;
  311. }
  312. positionStartAttribute.needsUpdate = true;
  313. startTimeAttribute.needsUpdate = true;
  314. velocityAttribute.needsUpdate = true;
  315. turbulenceAttribute.needsUpdate = true;
  316. colorAttribute.needsUpdate = true;
  317. sizeAttribute.needsUpdate = true;
  318. lifeTimeAttribute.needsUpdate = true;
  319. this.offset = 0;
  320. this.count = 0;
  321. }
  322. };
  323. this.dispose = function () {
  324. this.particleShaderGeo.dispose();
  325. };
  326. this.init();
  327. };
  328. THREE.GPUParticleContainer.prototype = Object.create( THREE.Object3D.prototype );
  329. THREE.GPUParticleContainer.prototype.constructor = THREE.GPUParticleContainer;