Fire.js 26 KB

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