Fire.js 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /**
  2. * @author Mike Piecuch / https://github.com/mikepiecuch
  3. *
  4. * Based on research paper "Real-Time Fluid Dynamics for Games" by Jos Stam
  5. * http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/GDC03.pdf
  6. *
  7. */
  8. THREE.Fire = function ( geometry, options ) {
  9. THREE.Mesh.call( this, geometry );
  10. this.type = 'Fire';
  11. this.clock = new THREE.Clock();
  12. options = options || {};
  13. var textureWidth = options.textureWidth || 512;
  14. var textureHeight = options.textureHeight || 512;
  15. var oneOverWidth = 1.0 / textureWidth;
  16. var oneOverHeight = 1.0 / textureHeight;
  17. var debug = ( options.debug === undefined ) ? false : options.debug;
  18. this.color1 = options.color1 || new THREE.Color( 0xffffff );
  19. this.color2 = options.color2 || new THREE.Color( 0xffa000 );
  20. this.color3 = options.color3 || new THREE.Color( 0x000000 );
  21. this.colorBias = ( options.colorBias === undefined ) ? 0.8 : options.colorBias;
  22. this.diffuse = ( options.diffuse === undefined ) ? 1.33 : options.diffuse;
  23. this.viscosity = ( options.viscosity === undefined ) ? 0.25 : options.viscosity;
  24. this.expansion = ( options.expansion === undefined ) ? - 0.25 : options.expansion;
  25. this.swirl = ( options.swirl === undefined ) ? 50.0 : options.swirl;
  26. this.burnRate = ( options.burnRate === undefined ) ? 0.3 : options.burnRate;
  27. this.drag = ( options.drag === undefined ) ? 0.35 : options.drag;
  28. this.airSpeed = ( options.airSpeed === undefined ) ? 6.0 : options.airSpeed;
  29. this.windVector = options.windVector || new THREE.Vector2( 0.0, 0.75 );
  30. this.speed = ( options.speed === undefined ) ? 500.0 : options.speed;
  31. this.massConservation = ( options.massConservation === undefined ) ? false : options.massConservation;
  32. var size = textureWidth * textureHeight;
  33. this.sourceData = new Uint8Array( 4 * size );
  34. this.clearSources = function () {
  35. for ( var y = 0; y < textureHeight; y ++ ) {
  36. for ( var x = 0; x < textureWidth; x ++ ) {
  37. var i = y * textureWidth + x;
  38. var stride = i * 4;
  39. this.sourceData[ stride ] = 0;
  40. this.sourceData[ stride + 1 ] = 0;
  41. this.sourceData[ stride + 2 ] = 0;
  42. this.sourceData[ stride + 3 ] = 0;
  43. }
  44. }
  45. this.sourceMaterial.uniforms.sourceMap.value = this.internalSource;
  46. this.sourceMaterial.needsUpdate = true;
  47. return this.sourceData;
  48. };
  49. this.addSource = function ( u, v, radius, density = null, windX = null, windY = null ) {
  50. var startX = Math.max( Math.floor( ( u - radius ) * textureWidth ), 0 );
  51. var startY = Math.max( Math.floor( ( v - radius ) * textureHeight ), 0 );
  52. var endX = Math.min( Math.floor( ( u + radius ) * textureWidth ), textureWidth );
  53. var endY = Math.min( Math.floor( ( v + radius ) * textureHeight ), textureHeight );
  54. for ( var y = startY; y < endY; y ++ ) {
  55. for ( var x = startX; x < endX; x ++ ) {
  56. var diffX = x * oneOverWidth - u;
  57. var diffY = y * oneOverHeight - v;
  58. if ( diffX * diffX + diffY * diffY < radius * radius ) {
  59. var i = y * textureWidth + x;
  60. var stride = i * 4;
  61. if ( density != null ) {
  62. this.sourceData[ stride ] = Math.min( Math.max( density, 0.0 ), 1.0 ) * 255;
  63. }
  64. if ( windX != null ) {
  65. var wind = Math.min( Math.max( windX, - 1.0 ), 1.0 );
  66. wind = ( wind < 0.0 ) ? Math.floor( wind * 127 ) + 255 : Math.floor( wind * 127 );
  67. this.sourceData[ stride + 1 ] = wind;
  68. }
  69. if ( windY != null ) {
  70. var wind = Math.min( Math.max( windY, - 1.0 ), 1.0 );
  71. wind = ( wind < 0.0 ) ? Math.floor( wind * 127 ) + 255 : Math.floor( wind * 127 );
  72. this.sourceData[ stride + 2 ] = wind;
  73. }
  74. }
  75. }
  76. }
  77. this.internalSource.needsUpdate = true;
  78. return this.sourceData;
  79. };
  80. // When setting source map, red channel is density. Green and blue channels
  81. // encode x and y velocity respectively as signed chars:
  82. // (0 -> 127 = 0.0 -> 1.0, 128 -> 255 = -1.0 -> 0.0 )
  83. this.setSourceMap = function ( texture ) {
  84. this.sourceMaterial.uniforms.sourceMap.value = texture;
  85. };
  86. var parameters = {
  87. minFilter: THREE.NearestFilter,
  88. magFilter: THREE.NearestFilter,
  89. depthBuffer: false,
  90. stencilBuffer: false
  91. };
  92. this.field0 = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  93. this.field0.background = new THREE.Color( 0x000000 );
  94. this.field1 = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  95. this.field0.background = new THREE.Color( 0x000000 );
  96. this.fieldProj = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  97. this.field0.background = new THREE.Color( 0x000000 );
  98. if ( ! THREE.Math.isPowerOfTwo( textureWidth ) ||
  99. ! THREE.Math.isPowerOfTwo( textureHeight ) ) {
  100. this.field0.texture.generateMipmaps = false;
  101. this.field1.texture.generateMipmaps = false;
  102. this.fieldProj.texture.generateMipmaps = false;
  103. }
  104. this.fieldScene = new THREE.Scene();
  105. this.fieldScene.background = new THREE.Color( 0x000000 );
  106. this.orthoCamera = new THREE.OrthographicCamera( textureWidth / - 2, textureWidth / 2, textureHeight / 2, textureHeight / - 2, 1, 2 );
  107. this.orthoCamera.position.z = 1;
  108. this.fieldGeometry = new THREE.PlaneBufferGeometry( textureWidth, textureHeight );
  109. this.internalSource = new THREE.DataTexture( this.sourceData, textureWidth, textureHeight, THREE.RGBAFormat );
  110. // Source Shader
  111. var shader = THREE.Fire.SourceShader;
  112. this.sourceMaterial = new THREE.ShaderMaterial( {
  113. uniforms: shader.uniforms,
  114. vertexShader: shader.vertexShader,
  115. fragmentShader: shader.fragmentShader,
  116. transparent: false
  117. } );
  118. this.clearSources();
  119. this.sourceMesh = new THREE.Mesh( this.fieldGeometry, this.sourceMaterial );
  120. this.fieldScene.add( this.sourceMesh );
  121. // Diffuse Shader
  122. var shader = THREE.Fire.DiffuseShader;
  123. this.diffuseMaterial = new THREE.ShaderMaterial( {
  124. uniforms: shader.uniforms,
  125. vertexShader: shader.vertexShader,
  126. fragmentShader: shader.fragmentShader,
  127. transparent: false
  128. } );
  129. this.diffuseMaterial.uniforms.oneOverWidth.value = oneOverWidth;
  130. this.diffuseMaterial.uniforms.oneOverHeight.value = oneOverHeight;
  131. this.diffuseMesh = new THREE.Mesh( this.fieldGeometry, this.diffuseMaterial );
  132. this.fieldScene.add( this.diffuseMesh );
  133. // Drift Shader
  134. shader = THREE.Fire.DriftShader;
  135. this.driftMaterial = new THREE.ShaderMaterial( {
  136. uniforms: shader.uniforms,
  137. vertexShader: shader.vertexShader,
  138. fragmentShader: shader.fragmentShader,
  139. transparent: false
  140. } );
  141. this.driftMaterial.uniforms.oneOverWidth.value = oneOverWidth;
  142. this.driftMaterial.uniforms.oneOverHeight.value = oneOverHeight;
  143. this.driftMesh = new THREE.Mesh( this.fieldGeometry, this.driftMaterial );
  144. this.fieldScene.add( this.driftMesh );
  145. // Projection Shader 1
  146. shader = THREE.Fire.ProjectionShader1;
  147. this.projMaterial1 = new THREE.ShaderMaterial( {
  148. uniforms: shader.uniforms,
  149. vertexShader: shader.vertexShader,
  150. fragmentShader: shader.fragmentShader,
  151. transparent: false
  152. } );
  153. this.projMaterial1.uniforms.oneOverWidth.value = oneOverWidth;
  154. this.projMaterial1.uniforms.oneOverHeight.value = oneOverHeight;
  155. this.projMesh1 = new THREE.Mesh( this.fieldGeometry, this.projMaterial1 );
  156. this.fieldScene.add( this.projMesh1 );
  157. // Projection Shader 2
  158. shader = THREE.Fire.ProjectionShader2;
  159. this.projMaterial2 = new THREE.ShaderMaterial( {
  160. uniforms: shader.uniforms,
  161. vertexShader: shader.vertexShader,
  162. fragmentShader: shader.fragmentShader,
  163. transparent: false
  164. } );
  165. this.projMaterial2.uniforms.oneOverWidth.value = oneOverWidth;
  166. this.projMaterial2.uniforms.oneOverHeight.value = oneOverHeight;
  167. this.projMesh2 = new THREE.Mesh( this.fieldGeometry, this.projMaterial2 );
  168. this.fieldScene.add( this.projMesh2 );
  169. // Projection Shader 3
  170. shader = THREE.Fire.ProjectionShader3;
  171. this.projMaterial3 = new THREE.ShaderMaterial( {
  172. uniforms: shader.uniforms,
  173. vertexShader: shader.vertexShader,
  174. fragmentShader: shader.fragmentShader,
  175. transparent: false
  176. } );
  177. this.projMaterial3.uniforms.oneOverWidth.value = oneOverWidth;
  178. this.projMaterial3.uniforms.oneOverHeight.value = oneOverHeight;
  179. this.projMesh3 = new THREE.Mesh( this.fieldGeometry, this.projMaterial3 );
  180. this.fieldScene.add( this.projMesh3 );
  181. // Color Shader
  182. if ( debug ) {
  183. shader = THREE.Fire.DebugShader;
  184. } else {
  185. shader = THREE.Fire.ColorShader;
  186. }
  187. this.material = new THREE.ShaderMaterial( {
  188. uniforms: shader.uniforms,
  189. vertexShader: shader.vertexShader,
  190. fragmentShader: shader.fragmentShader,
  191. transparent: true
  192. } );
  193. this.material.uniforms.densityMap.value = this.field1.texture;
  194. this.configShaders = function ( dt ) {
  195. this.diffuseMaterial.uniforms.diffuse.value = dt * 0.05 * this.diffuse;
  196. this.diffuseMaterial.uniforms.viscosity.value = dt * 0.05 * this.viscosity;
  197. this.diffuseMaterial.uniforms.expansion.value = Math.exp( this.expansion * - 1.0 );
  198. this.diffuseMaterial.uniforms.swirl.value = Math.exp( this.swirl * - 0.1 );
  199. this.diffuseMaterial.uniforms.drag.value = Math.exp( this.drag * - 0.1 );
  200. this.diffuseMaterial.uniforms.burnRate.value = this.burnRate * dt * 0.01;
  201. this.driftMaterial.uniforms.windVector.value = this.windVector;
  202. this.driftMaterial.uniforms.airSpeed.value = dt * this.airSpeed * 0.001 * textureHeight;
  203. this.material.uniforms.color1.value = this.color1;
  204. this.material.uniforms.color2.value = this.color2;
  205. this.material.uniforms.color3.value = this.color3;
  206. this.material.uniforms.colorBias.value = this.colorBias;
  207. };
  208. this.clearDiffuse = function () {
  209. this.diffuseMaterial.uniforms.expansion.value = 1.0;
  210. this.diffuseMaterial.uniforms.swirl.value = 1.0;
  211. this.diffuseMaterial.uniforms.drag.value = 1.0;
  212. this.diffuseMaterial.uniforms.burnRate.value = 0.0;
  213. };
  214. this.swapTextures = function () {
  215. var swap = this.field0;
  216. this.field0 = this.field1;
  217. this.field1 = swap;
  218. };
  219. this.saveRenderState = function ( renderer ) {
  220. this.savedRenderTarget = renderer.getRenderTarget();
  221. this.savedVrEnabled = renderer.vr.enabled;
  222. this.savedShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  223. this.savedAntialias = renderer.antialias;
  224. this.savedToneMapping = renderer.toneMapping;
  225. };
  226. this.restoreRenderState = function ( renderer ) {
  227. renderer.vr.enabled = this.savedVrEnabled;
  228. renderer.shadowMap.autoUpdate = this.savedShadowAutoUpdate;
  229. renderer.setRenderTarget( this.savedRenderTarget );
  230. renderer.antialias = this.savedAntialias;
  231. renderer.toneMapping = this.savedToneMapping;
  232. };
  233. this.renderSource = function ( renderer ) {
  234. this.sourceMesh.visible = true;
  235. this.sourceMaterial.uniforms.densityMap.value = this.field0.texture;
  236. renderer.render( this.fieldScene, this.orthoCamera, this.field1 );
  237. this.sourceMesh.visible = false;
  238. this.swapTextures();
  239. };
  240. this.renderDiffuse = function ( renderer ) {
  241. this.diffuseMesh.visible = true;
  242. this.diffuseMaterial.uniforms.densityMap.value = this.field0.texture;
  243. renderer.render( this.fieldScene, this.orthoCamera, this.field1 );
  244. this.diffuseMesh.visible = false;
  245. this.swapTextures();
  246. };
  247. this.renderDrift = function ( renderer ) {
  248. this.driftMesh.visible = true;
  249. this.driftMaterial.uniforms.densityMap.value = this.field0.texture;
  250. renderer.render( this.fieldScene, this.orthoCamera, this.field1 );
  251. this.driftMesh.visible = false;
  252. this.swapTextures();
  253. };
  254. this.renderProject = function ( renderer ) {
  255. // Projection pass 1
  256. this.projMesh1.visible = true;
  257. this.projMaterial1.uniforms.densityMap.value = this.field0.texture;
  258. renderer.render( this.fieldScene, this.orthoCamera, this.fieldProj );
  259. this.projMesh1.visible = false;
  260. this.projMaterial2.uniforms.densityMap.value = this.fieldProj.texture;
  261. // Projection pass 2
  262. this.projMesh2.visible = true;
  263. for ( var i = 0; i < 20; i ++ ) {
  264. renderer.render( this.fieldScene, this.orthoCamera, this.field1 );
  265. var temp = this.field1;
  266. this.field1 = this.fieldProj;
  267. this.fieldProj = temp;
  268. this.projMaterial2.uniforms.densityMap.value = this.fieldProj.texture;
  269. }
  270. this.projMesh2.visible = false;
  271. this.projMaterial3.uniforms.densityMap.value = this.field0.texture;
  272. this.projMaterial3.uniforms.projMap.value = this.fieldProj.texture;
  273. // Projection pass 3
  274. this.projMesh3.visible = true;
  275. renderer.render( this.fieldScene, this.orthoCamera, this.field1 );
  276. this.projMesh3.visible = false;
  277. this.swapTextures();
  278. };
  279. this.onBeforeRender = function ( renderer, scene, camera ) {
  280. var delta = this.clock.getDelta();
  281. if ( delta > 0.1 ) {
  282. delta = 0.1;
  283. }
  284. var dt = delta * ( this.speed * 0.1 );
  285. this.configShaders( dt );
  286. this.saveRenderState( renderer );
  287. renderer.vr.enabled = false; // Avoid camera modification and recursion
  288. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  289. renderer.antialias = false;
  290. renderer.toneMapping = THREE.NoToneMapping;
  291. this.sourceMesh.visible = false;
  292. this.diffuseMesh.visible = false;
  293. this.driftMesh.visible = false;
  294. this.projMesh1.visible = false;
  295. this.projMesh2.visible = false;
  296. this.projMesh3.visible = false;
  297. this.renderSource( renderer );
  298. this.clearDiffuse();
  299. for ( var i = 0; i < 21; i ++ ) {
  300. this.renderDiffuse( renderer );
  301. }
  302. this.configShaders( dt );
  303. this.renderDiffuse( renderer );
  304. this.renderDrift( renderer );
  305. if ( this.massConservation ) {
  306. this.renderProject( renderer );
  307. this.renderProject( renderer );
  308. }
  309. // Final result out for coloring
  310. this.material.map = this.field1.texture;
  311. this.material.transparent = true;
  312. this.material.minFilter = THREE.LinearFilter,
  313. this.material.magFilter = THREE.LinearFilter,
  314. this.restoreRenderState( renderer );
  315. };
  316. };
  317. THREE.Fire.prototype = Object.create( THREE.Mesh.prototype );
  318. THREE.Fire.prototype.constructor = THREE.Fire;
  319. THREE.Fire.SourceShader = {
  320. uniforms: {
  321. 'sourceMap': {
  322. type: 't',
  323. value: null
  324. },
  325. 'densityMap': {
  326. type: 't',
  327. value: null
  328. }
  329. },
  330. vertexShader: [
  331. 'varying vec2 vUv;',
  332. 'void main() {',
  333. ' vUv = uv;',
  334. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  335. ' gl_Position = projectionMatrix * mvPosition;',
  336. '}'
  337. ].join( "\n" ),
  338. fragmentShader: [
  339. 'uniform sampler2D sourceMap;',
  340. 'uniform sampler2D densityMap;',
  341. 'varying vec2 vUv;',
  342. 'void main() {',
  343. ' vec4 source = texture2D( sourceMap, vUv );',
  344. ' vec4 current = texture2D( densityMap, vUv );',
  345. ' vec2 v0 = (current.gb - step(0.5, current.gb)) * 2.0;',
  346. ' vec2 v1 = (source.gb - step(0.5, source.gb)) * 2.0;',
  347. ' vec2 newVel = v0 + v1;',
  348. ' newVel = clamp(newVel, -0.99, 0.99);',
  349. ' newVel = newVel * 0.5 + step(0.0, -newVel);',
  350. ' float newDensity = source.r + current.a;',
  351. ' float newTemp = source.r + current.r;',
  352. ' newDensity = clamp(newDensity, 0.0, 1.0);',
  353. ' newTemp = clamp(newTemp, 0.0, 1.0);',
  354. ' gl_FragColor = vec4(newTemp, newVel.xy, newDensity);',
  355. '}'
  356. ].join( "\n" )
  357. };
  358. THREE.Fire.DiffuseShader = {
  359. uniforms: {
  360. 'oneOverWidth': {
  361. type: 'f',
  362. value: null
  363. },
  364. 'oneOverHeight': {
  365. type: 'f',
  366. value: null
  367. },
  368. 'diffuse': {
  369. type: 'f',
  370. value: null
  371. },
  372. 'viscosity': {
  373. type: 'f',
  374. value: null
  375. },
  376. 'expansion': {
  377. type: 'f',
  378. value: null
  379. },
  380. 'swirl': {
  381. type: 'f',
  382. value: null
  383. },
  384. 'drag': {
  385. type: 'f',
  386. value: null
  387. },
  388. 'burnRate': {
  389. type: 'f',
  390. value: null
  391. },
  392. 'densityMap': {
  393. type: 't',
  394. value: null
  395. }
  396. },
  397. vertexShader: [
  398. 'varying vec2 vUv;',
  399. 'void main() {',
  400. ' vUv = uv;',
  401. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  402. ' gl_Position = projectionMatrix * mvPosition;',
  403. '}'
  404. ].join( "\n" ),
  405. fragmentShader: [
  406. 'uniform float oneOverWidth;',
  407. 'uniform float oneOverHeight;',
  408. 'uniform float diffuse;',
  409. 'uniform float viscosity;',
  410. 'uniform float expansion;',
  411. 'uniform float swirl;',
  412. 'uniform float burnRate;',
  413. 'uniform float drag;',
  414. 'uniform sampler2D densityMap;',
  415. 'varying vec2 vUv;',
  416. 'void main() {',
  417. ' vec4 dC = texture2D( densityMap, vUv );',
  418. ' vec4 dL = texture2D( densityMap, vec2(vUv.x - oneOverWidth, vUv.y) );',
  419. ' vec4 dR = texture2D( densityMap, vec2(vUv.x + oneOverWidth, vUv.y) );',
  420. ' vec4 dU = texture2D( densityMap, vec2(vUv.x, vUv.y - oneOverHeight) );',
  421. ' vec4 dD = texture2D( densityMap, vec2(vUv.x, vUv.y + oneOverHeight) );',
  422. ' vec4 dUL = texture2D( densityMap, vec2(vUv.x - oneOverWidth, vUv.y - oneOverHeight) );',
  423. ' vec4 dUR = texture2D( densityMap, vec2(vUv.x + oneOverWidth, vUv.y - oneOverHeight) );',
  424. ' vec4 dDL = texture2D( densityMap, vec2(vUv.x - oneOverWidth, vUv.y + oneOverHeight) );',
  425. ' vec4 dDR = texture2D( densityMap, vec2(vUv.x + oneOverWidth, vUv.y + oneOverHeight) );',
  426. ' dC.yz = (dC.yz - step(0.5, dC.yz)) * 2.0;',
  427. ' dL.yz = (dL.yz - step(0.5, dL.yz)) * 2.0;',
  428. ' dR.yz = (dR.yz - step(0.5, dR.yz)) * 2.0;',
  429. ' dU.yz = (dU.yz - step(0.5, dU.yz)) * 2.0;',
  430. ' dD.yz = (dD.yz - step(0.5, dD.yz)) * 2.0;',
  431. ' dUL.yz = (dUL.yz - step(0.5, dUL.yz)) * 2.0;',
  432. ' dUR.yz = (dUR.yz - step(0.5, dUR.yz)) * 2.0;',
  433. ' dDL.yz = (dDL.yz - step(0.5, dDL.yz)) * 2.0;',
  434. ' dDR.yz = (dDR.yz - step(0.5, dDR.yz)) * 2.0;',
  435. ' vec4 result = (dC + vec4(diffuse, viscosity, viscosity, diffuse) * ( dL + dR + dU + dD + dUL + dUR + dDL + dDR )) / (1.0 + 8.0 * vec4(diffuse, viscosity, viscosity, diffuse)) - vec4(0.0, 0.0, 0.0, 0.001);',
  436. ' float temperature = result.r;',
  437. ' temperature = clamp(temperature - burnRate, 0.0, 1.0);',
  438. ' vec2 velocity = result.yz;',
  439. ' vec2 expansionVec = vec2(dL.w - dR.w, dU.w - dD.w);',
  440. ' vec2 swirlVec = vec2((dL.z - dR.z) * 0.5, (dU.y - dD.y) * 0.5);',
  441. ' velocity = velocity + (1.0 - expansion) * expansionVec + (1.0 - swirl) * swirlVec;',
  442. ' velocity = velocity - (1.0 - drag) * velocity;',
  443. ' gl_FragColor = vec4(temperature, velocity * 0.5 + step(0.0, -velocity), result.w);',
  444. ' gl_FragColor = gl_FragColor * step(oneOverWidth, vUv.x);',
  445. ' gl_FragColor = gl_FragColor * step(oneOverHeight, vUv.y);',
  446. ' gl_FragColor = gl_FragColor * step(vUv.x, 1.0 - oneOverWidth);',
  447. ' gl_FragColor = gl_FragColor * step(vUv.y, 1.0 - oneOverHeight);',
  448. '}'
  449. ].join( "\n" )
  450. };
  451. THREE.Fire.DriftShader = {
  452. uniforms: {
  453. 'oneOverWidth': {
  454. type: 'f',
  455. value: null
  456. },
  457. 'oneOverHeight': {
  458. type: 'f',
  459. value: null
  460. },
  461. 'windVector': {
  462. type: 'v2',
  463. value: new THREE.Vector2( 0.0, 0.0 )
  464. },
  465. 'airSpeed': {
  466. type: 'f',
  467. value: null
  468. },
  469. 'densityMap': {
  470. type: 't',
  471. value: null
  472. }
  473. },
  474. vertexShader: [
  475. 'varying vec2 vUv;',
  476. 'void main() {',
  477. ' vUv = uv;',
  478. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  479. ' gl_Position = projectionMatrix * mvPosition;',
  480. '}'
  481. ].join( "\n" ),
  482. fragmentShader: [
  483. 'uniform float oneOverWidth;',
  484. 'uniform float oneOverHeight;',
  485. 'uniform vec2 windVector;',
  486. 'uniform float airSpeed;',
  487. 'uniform sampler2D densityMap;',
  488. 'varying vec2 vUv;',
  489. 'void main() {',
  490. ' vec2 velocity = texture2D( densityMap, vUv ).gb;',
  491. ' velocity = (velocity - step(0.5, velocity)) * 2.0;',
  492. ' velocity = velocity + windVector;',
  493. ' vec2 sourcePos = vUv - airSpeed * vec2(oneOverWidth, oneOverHeight) * velocity;',
  494. ' vec2 units = sourcePos / vec2(oneOverWidth, oneOverHeight);',
  495. ' vec2 intPos = floor(units);',
  496. ' vec2 frac = units - intPos;',
  497. ' intPos = intPos * vec2(oneOverWidth, oneOverHeight);',
  498. ' vec4 dX0Y0 = texture2D( densityMap, intPos + vec2(0.0, -oneOverHeight) );',
  499. ' vec4 dX1Y0 = texture2D( densityMap, intPos + vec2(oneOverWidth, 0.0) );',
  500. ' vec4 dX0Y1 = texture2D( densityMap, intPos + vec2(0.0, oneOverHeight) );',
  501. ' vec4 dX1Y1 = texture2D( densityMap, intPos + vec2(oneOverWidth, oneOverHeight) );',
  502. ' dX0Y0.gb = (dX0Y0.gb - step(0.5, dX0Y0.gb)) * 2.0;',
  503. ' dX1Y0.gb = (dX1Y0.gb - step(0.5, dX1Y0.gb)) * 2.0;',
  504. ' dX0Y1.gb = (dX0Y1.gb - step(0.5, dX0Y1.gb)) * 2.0;',
  505. ' dX1Y1.gb = (dX1Y1.gb - step(0.5, dX1Y1.gb)) * 2.0;',
  506. ' vec4 source = mix(mix(dX0Y0, dX1Y0, frac.x), mix(dX0Y1, dX1Y1, frac.x), frac.y);',
  507. ' source.gb = source.gb * 0.5 + step(0.0, -source.gb);',
  508. ' gl_FragColor = source;',
  509. '}'
  510. ].join( "\n" )
  511. };
  512. THREE.Fire.ProjectionShader1 = {
  513. uniforms: {
  514. 'oneOverWidth': {
  515. type: 'f',
  516. value: null
  517. },
  518. 'oneOverHeight': {
  519. type: 'f',
  520. value: null
  521. },
  522. 'densityMap': {
  523. type: 't',
  524. value: null
  525. }
  526. },
  527. vertexShader: [
  528. 'varying vec2 vUv;',
  529. 'void main() {',
  530. ' vUv = uv;',
  531. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  532. ' gl_Position = projectionMatrix * mvPosition;',
  533. '}'
  534. ].join( "\n" ),
  535. fragmentShader: [
  536. 'uniform float oneOverWidth;',
  537. 'uniform float oneOverHeight;',
  538. 'uniform sampler2D densityMap;',
  539. 'varying vec2 vUv;',
  540. 'void main() {',
  541. ' float dL = texture2D( densityMap, vec2(vUv.x - oneOverWidth, vUv.y) ).g;',
  542. ' float dR = texture2D( densityMap, vec2(vUv.x + oneOverWidth, vUv.y) ).g;',
  543. ' float dU = texture2D( densityMap, vec2(vUv.x, vUv.y - oneOverHeight) ).b;',
  544. ' float dD = texture2D( densityMap, vec2(vUv.x, vUv.y + oneOverHeight) ).b;',
  545. ' dL = (dL - step(0.5, dL)) * 2.0;',
  546. ' dR = (dR - step(0.5, dR)) * 2.0;',
  547. ' dU = (dU - step(0.5, dU)) * 2.0;',
  548. ' dD = (dD - step(0.5, dD)) * 2.0;',
  549. ' float h = (oneOverWidth + oneOverHeight) * 0.5;',
  550. ' float div = -0.5 * h * (dR - dL + dD - dU);',
  551. ' gl_FragColor = vec4( 0.0, 0.0, div * 0.5 + step(0.0, -div), 0.0);',
  552. '}'
  553. ].join( "\n" )
  554. };
  555. THREE.Fire.ProjectionShader2 = {
  556. uniforms: {
  557. 'oneOverWidth': {
  558. type: 'f',
  559. value: null
  560. },
  561. 'oneOverHeight': {
  562. type: 'f',
  563. value: null
  564. },
  565. 'densityMap': {
  566. type: 't',
  567. value: null
  568. }
  569. },
  570. vertexShader: [
  571. 'varying vec2 vUv;',
  572. 'void main() {',
  573. ' vUv = uv;',
  574. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  575. ' gl_Position = projectionMatrix * mvPosition;',
  576. '}'
  577. ].join( "\n" ),
  578. fragmentShader: [
  579. 'uniform float oneOverWidth;',
  580. 'uniform float oneOverHeight;',
  581. 'uniform sampler2D densityMap;',
  582. 'varying vec2 vUv;',
  583. 'void main() {',
  584. ' float div = texture2D( densityMap, vUv ).b;',
  585. ' float pL = texture2D( densityMap, vec2(vUv.x - oneOverWidth, vUv.y) ).g;',
  586. ' float pR = texture2D( densityMap, vec2(vUv.x + oneOverWidth, vUv.y) ).g;',
  587. ' float pU = texture2D( densityMap, vec2(vUv.x, vUv.y - oneOverHeight) ).g;',
  588. ' float pD = texture2D( densityMap, vec2(vUv.x, vUv.y + oneOverHeight) ).g;',
  589. ' float divNorm = (div - step(0.5, div)) * 2.0;',
  590. ' pL = (pL - step(0.5, pL)) * 2.0;',
  591. ' pR = (pR - step(0.5, pR)) * 2.0;',
  592. ' pU = (pU - step(0.5, pU)) * 2.0;',
  593. ' pD = (pD - step(0.5, pD)) * 2.0;',
  594. ' float p = (divNorm + pR + pL + pD + pU) * 0.25;',
  595. ' gl_FragColor = vec4( 0.0, p * 0.5 + step(0.0, -p), div, 0.0);',
  596. '}'
  597. ].join( "\n" )
  598. };
  599. THREE.Fire.ProjectionShader3 = {
  600. uniforms: {
  601. 'oneOverWidth': {
  602. type: 'f',
  603. value: null
  604. },
  605. 'oneOverHeight': {
  606. type: 'f',
  607. value: null
  608. },
  609. 'densityMap': {
  610. type: 't',
  611. value: null
  612. },
  613. 'projMap': {
  614. type: 't',
  615. value: null
  616. }
  617. },
  618. vertexShader: [
  619. 'varying vec2 vUv;',
  620. 'void main() {',
  621. ' vUv = uv;',
  622. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  623. ' gl_Position = projectionMatrix * mvPosition;',
  624. '}'
  625. ].join( "\n" ),
  626. fragmentShader: [
  627. 'uniform float oneOverWidth;',
  628. 'uniform float oneOverHeight;',
  629. 'uniform sampler2D densityMap;',
  630. 'uniform sampler2D projMap;',
  631. 'varying vec2 vUv;',
  632. 'void main() {',
  633. ' vec4 orig = texture2D(densityMap, vUv);',
  634. ' float pL = texture2D( projMap, vec2(vUv.x - oneOverWidth, vUv.y) ).g;',
  635. ' float pR = texture2D( projMap, vec2(vUv.x + oneOverWidth, vUv.y) ).g;',
  636. ' float pU = texture2D( projMap, vec2(vUv.x, vUv.y - oneOverHeight) ).g;',
  637. ' float pD = texture2D( projMap, vec2(vUv.x, vUv.y + oneOverHeight) ).g;',
  638. ' float uNorm = (orig.g - step(0.5, orig.g)) * 2.0;',
  639. ' float vNorm = (orig.b - step(0.5, orig.b)) * 2.0;',
  640. ' pL = (pL - step(0.5, pL)) * 2.0;',
  641. ' pR = (pR - step(0.5, pR)) * 2.0;',
  642. ' pU = (pU - step(0.5, pU)) * 2.0;',
  643. ' pD = (pD - step(0.5, pD)) * 2.0;',
  644. ' float h = (oneOverWidth + oneOverHeight) * 0.5;',
  645. ' float u = uNorm - (0.5 * (pR - pL) / h);',
  646. ' float v = vNorm - (0.5 * (pD - pU) / h);',
  647. ' gl_FragColor = vec4( orig.r, u * 0.5 + step(0.0, -u), v * 0.5 + step(0.0, -v), orig.a);',
  648. '}'
  649. ].join( "\n" )
  650. };
  651. THREE.Fire.ColorShader = {
  652. uniforms: {
  653. 'color1': {
  654. type: 'c',
  655. value: null
  656. },
  657. 'color2': {
  658. type: 'c',
  659. value: null
  660. },
  661. 'color3': {
  662. type: 'c',
  663. value: null
  664. },
  665. 'colorBias': {
  666. type: 'f',
  667. value: null
  668. },
  669. 'densityMap': {
  670. type: 't',
  671. value: null
  672. }
  673. },
  674. vertexShader: [
  675. 'varying vec2 vUv;',
  676. 'void main() {',
  677. ' vUv = uv;',
  678. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  679. ' gl_Position = projectionMatrix * mvPosition;',
  680. '}'
  681. ].join( "\n" ),
  682. fragmentShader: [
  683. 'uniform vec3 color1;',
  684. 'uniform vec3 color2;',
  685. 'uniform vec3 color3;',
  686. 'uniform float colorBias;',
  687. 'uniform sampler2D densityMap;',
  688. 'varying vec2 vUv;',
  689. 'void main() {',
  690. ' float density = texture2D( densityMap, vUv ).a;',
  691. ' float temperature = texture2D( densityMap, vUv ).r;',
  692. ' float bias = clamp(colorBias, 0.0001, 0.9999);',
  693. ' vec3 blend1 = mix(color3, color2, temperature / bias) * (1.0 - step(bias, temperature));',
  694. ' vec3 blend2 = mix(color2, color1, (temperature - bias) / (1.0 - bias) ) * step(bias, temperature);',
  695. ' gl_FragColor = vec4(blend1 + blend2, density);',
  696. '}'
  697. ].join( "\n" )
  698. };
  699. THREE.Fire.DebugShader = {
  700. uniforms: {
  701. 'color1': {
  702. type: 'c',
  703. value: null
  704. },
  705. 'color2': {
  706. type: 'c',
  707. value: null
  708. },
  709. 'color3': {
  710. type: 'c',
  711. value: null
  712. },
  713. 'colorBias': {
  714. type: 'f',
  715. value: null
  716. },
  717. 'densityMap': {
  718. type: 't',
  719. value: null
  720. }
  721. },
  722. vertexShader: [
  723. 'varying vec2 vUv;',
  724. 'void main() {',
  725. ' vUv = uv;',
  726. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  727. ' gl_Position = projectionMatrix * mvPosition;',
  728. '}'
  729. ].join( "\n" ),
  730. fragmentShader: [
  731. 'uniform sampler2D densityMap;',
  732. 'varying vec2 vUv;',
  733. 'void main() {',
  734. ' float density;',
  735. ' density = texture2D( densityMap, vUv ).a;',
  736. ' vec2 vel = texture2D( densityMap, vUv ).gb;',
  737. ' vel = (vel - step(0.5, vel)) * 2.0;',
  738. ' float r = density;',
  739. ' float g = max(abs(vel.x), density * 0.5);',
  740. ' float b = max(abs(vel.y), density * 0.5);',
  741. ' float a = max(density * 0.5, max(abs(vel.x), abs(vel.y)));',
  742. ' gl_FragColor = vec4(r, g, b, a);',
  743. '}'
  744. ].join( "\n" )
  745. };