Fire.js 26 KB

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