Fire.js 26 KB

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