Fire.js 26 KB

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