Fire.js 26 KB

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