webgl_fire.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - fire</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> fire and smoke demo
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import Stats from './jsm/libs/stats.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { Fire } from './jsm/objects/Fire.js';
  18. var camera, scene, renderer, stats;
  19. var fire;
  20. var params = {
  21. color1: '#ffffff',
  22. color2: '#ffa000',
  23. color3: '#000000',
  24. colorBias: 0.8,
  25. burnRate: 0.35,
  26. diffuse: 1.33,
  27. viscosity: 0.25,
  28. expansion: - 0.25,
  29. swirl: 50.0,
  30. drag: 0.35,
  31. airSpeed: 12.0,
  32. windX: 0.0,
  33. windY: 0.75,
  34. speed: 500.0,
  35. massConservation: false
  36. };
  37. init();
  38. animate();
  39. function init() {
  40. var width = window.innerWidth;
  41. var height = window.innerHeight;
  42. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
  43. camera.position.z = 25;
  44. scene = new THREE.Scene();
  45. scene.background = new THREE.Color( 0x000000 );
  46. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  47. scene.add( ambientLight );
  48. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  49. camera.add( pointLight );
  50. scene.add( camera );
  51. var plane = new THREE.PlaneBufferGeometry( 20, 20 );
  52. fire = new Fire( plane, {
  53. textureWidth: 512,
  54. textureHeight: 512,
  55. debug: false
  56. } );
  57. fire.position.z = - 2;
  58. scene.add( fire );
  59. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. renderer.autoClear = false;
  63. document.body.appendChild( renderer.domElement );
  64. function updateColor1( value ) {
  65. fire.color1.set( value );
  66. }
  67. function updateColor2( value ) {
  68. fire.color2.set( value );
  69. }
  70. function updateColor3( value ) {
  71. fire.color3.set( value );
  72. }
  73. function updateColorBias( value ) {
  74. fire.colorBias = value;
  75. }
  76. function updateBurnRate( value ) {
  77. fire.burnRate = value;
  78. }
  79. function updateDiffuse( value ) {
  80. fire.diffuse = value;
  81. }
  82. function updateViscosity( value ) {
  83. fire.viscosity = value;
  84. }
  85. function updateExpansion( value ) {
  86. fire.expansion = value;
  87. }
  88. function updateSwirl( value ) {
  89. fire.swirl = value;
  90. }
  91. function updateDrag( value ) {
  92. fire.drag = value;
  93. }
  94. function updateAirSpeed( value ) {
  95. fire.airSpeed = value;
  96. }
  97. function updateWindX( value ) {
  98. fire.windVector.x = value;
  99. }
  100. function updateWindY( value ) {
  101. fire.windVector.y = value;
  102. }
  103. function updateSpeed( value ) {
  104. fire.speed = value;
  105. }
  106. function updateMassConservation( value ) {
  107. fire.massConservation = value;
  108. }
  109. params.Single = function () {
  110. fire.clearSources();
  111. fire.addSource( 0.5, 0.1, 0.1, 1.0, 0.0, 1.0 );
  112. };
  113. params.Multiple = function () {
  114. fire.clearSources();
  115. fire.addSource( 0.45, 0.1, 0.1, 0.5, 0.0, 1.0 );
  116. fire.addSource( 0.55, 0.1, 0.1, 0.5, 0.0, 1.0 );
  117. };
  118. params.Text = function () {
  119. var text = "Three JS";
  120. var size = 180;
  121. var color = "#FF0040";
  122. var canvas = document.createElement( "canvas" );
  123. canvas.width = 1024;
  124. canvas.height = 1024;
  125. var context = canvas.getContext( "2d" );
  126. context.font = size + "pt Arial";
  127. context.strokeStyle = "black";
  128. context.strokeRect( 0, 0, canvas.width, canvas.height );
  129. context.textAlign = "center";
  130. context.textBaseline = "middle";
  131. context.lineWidth = 5;
  132. context.strokeStyle = color;
  133. context.fillStyle = "black";
  134. context.strokeText( text, canvas.width / 2, canvas.height * 0.75 );
  135. var texture = new THREE.Texture( canvas );
  136. texture.needsUpdate = true;
  137. fire.setSourceMap( texture );
  138. };
  139. // dat.gui
  140. var gui = new GUI();
  141. gui.add( params, "Single" );
  142. gui.add( params, "Multiple" );
  143. gui.add( params, "Text" );
  144. gui.addColor( params, 'color1' ).onChange( updateColor1 );
  145. gui.addColor( params, 'color2' ).onChange( updateColor2 );
  146. gui.addColor( params, 'color3' ).onChange( updateColor3 );
  147. gui.add( params, 'colorBias', 0.0, 1.0 ).onChange( updateColorBias );
  148. gui.add( params, 'burnRate', 0.0, 10.0 ).onChange( updateBurnRate );
  149. gui.add( params, 'diffuse', 0.0, 5.0 ).step( 0.01 ).onChange( updateDiffuse );
  150. gui.add( params, 'viscosity', 0.0, 5.0 ).step( 0.01 ).onChange( updateViscosity );
  151. gui.add( params, 'expansion', - 1.0, 1.0 ).step( 0.01 ).onChange( updateExpansion );
  152. gui.add( params, 'swirl', 0.0, 50.0 ).step( 0.01 ).onChange( updateSwirl );
  153. gui.add( params, 'drag', 0.0, 1.0 ).onChange( updateDrag );
  154. gui.add( params, 'airSpeed', 0.0, 50.0 ).onChange( updateAirSpeed );
  155. gui.add( params, 'windX', - 5, 5 ).step( 0.01 ).onChange( updateWindX );
  156. gui.add( params, 'windY', - 5, 5 ).step( 0.01 ).onChange( updateWindY );
  157. gui.add( params, 'speed', 0, 1000 ).onChange( updateSpeed );
  158. gui.add( params, 'massConservation' ).onChange( updateMassConservation );
  159. function updateAll() {
  160. updateColor1( params.color1 );
  161. updateColor2( params.color2 );
  162. updateColor3( params.color3 );
  163. updateColorBias( params.colorBias );
  164. updateBurnRate( params.burnRate );
  165. updateDiffuse( params.diffuse );
  166. updateViscosity( params.viscosity );
  167. updateExpansion( params.expansion );
  168. updateSwirl( params.swirl );
  169. updateDrag( params.drag );
  170. updateAirSpeed( params.airSpeed );
  171. updateWindX( params.windX );
  172. updateWindY( params.windY );
  173. updateSpeed( params.speed );
  174. updateMassConservation( params.massConservation );
  175. for ( var i in gui.__controllers ) {
  176. gui.__controllers[ i ].updateDisplay();
  177. }
  178. }
  179. params.Candle = function () {
  180. params.color1 = 0xffffff;
  181. params.color2 = 0xffa000;
  182. params.color3 = 0x000000;
  183. params.windX = 0.0;
  184. params.windY = 0.5;
  185. params.colorBias = 0.3;
  186. params.burnRate = 1.6;
  187. params.diffuse = 1.33;
  188. params.viscosity = 1.33;
  189. params.expansion = 0.0;
  190. params.swirl = 0.0;
  191. params.drag = 0.0;
  192. params.airSpeed = 8.0;
  193. params.speed = 500.0;
  194. params.massConservation = false;
  195. updateAll();
  196. };
  197. gui.add( params, 'Candle' );
  198. params.Torch = function () {
  199. params.color1 = 0xffdcaa;
  200. params.color2 = 0xffa000;
  201. params.color3 = 0x000000;
  202. params.windX = 0.0;
  203. params.windY = 0.75;
  204. params.colorBias = 0.9;
  205. params.burnRate = 1.0;
  206. params.diffuse = 1.33;
  207. params.viscosity = 0.25;
  208. params.expansion = 0.0;
  209. params.swirl = 50.0;
  210. params.drag = 0.35;
  211. params.airSpeed = 10.0;
  212. params.speed = 500.0;
  213. params.massConservation = false;
  214. updateAll();
  215. };
  216. gui.add( params, 'Torch' );
  217. params.Campfire = function () {
  218. params.color1 = 0xffffff;
  219. params.color2 = 0xffa000;
  220. params.color3 = 0x000000;
  221. params.windX = 0.0;
  222. params.windY = 0.75;
  223. params.colorBias = 0.8;
  224. params.burnRate = 0.3;
  225. params.diffuse = 1.33;
  226. params.viscosity = 0.25;
  227. params.expansion = - 0.25;
  228. params.swirl = 50.0;
  229. params.drag = 0.35;
  230. params.airSpeed = 12.0;
  231. params.speed = 500.0;
  232. params.massConservation = false;
  233. updateAll();
  234. };
  235. gui.add( params, 'Campfire' );
  236. params.Fireball = function () {
  237. params.color1 = 0xffffff;
  238. params.color2 = 0xffa000;
  239. params.color3 = 0x000000;
  240. params.windX = 0.0;
  241. params.windY = 0.75;
  242. params.colorBias = 0.8;
  243. params.burnRate = 1.2;
  244. params.diffuse = 3.0;
  245. params.viscosity = 0.0;
  246. params.expansion = 0.0;
  247. params.swirl = 6.0;
  248. params.drag = 0.0;
  249. params.airSpeed = 20.0;
  250. params.speed = 500.0;
  251. params.massConservation = false;
  252. updateAll();
  253. };
  254. gui.add( params, 'Fireball' );
  255. params.Iceball = function () {
  256. params.color1 = 0x00bdf7;
  257. params.color2 = 0x1b3fb6;
  258. params.color3 = 0x001869;
  259. params.windX = 0.0;
  260. params.windY = - 0.25;
  261. params.colorBias = 0.25;
  262. params.burnRate = 2.6;
  263. params.diffuse = 5.0;
  264. params.viscosity = 0.5;
  265. params.expansion = 0.75;
  266. params.swirl = 30.0;
  267. params.drag = 0.0;
  268. params.airSpeed = 40.0;
  269. params.speed = 500.0;
  270. params.massConservation = false;
  271. updateAll();
  272. };
  273. gui.add( params, 'Iceball' );
  274. params.Smoke = function () {
  275. params.color1 = 0xd2d2d2;
  276. params.color2 = 0xd7d7d7;
  277. params.color3 = 0x000000;
  278. params.windX = - 0.05;
  279. params.windY = 0.15;
  280. params.colorBias = 0.95;
  281. params.burnRate = 0.0;
  282. params.diffuse = 1.5;
  283. params.viscosity = 0.25;
  284. params.expansion = 0.2;
  285. params.swirl = 3.75;
  286. params.drag = 0.4;
  287. params.airSpeed = 18.0;
  288. params.speed = 500.0;
  289. params.massConservation = false;
  290. updateAll();
  291. };
  292. gui.add( params, 'Smoke' );
  293. params.Cigar = function () {
  294. params.color1 = 0xc5c5c5;
  295. params.color2 = 0x787878;
  296. params.color3 = 0x000000;
  297. params.windX = 0.0;
  298. params.windY = 0.3;
  299. params.colorBias = 0.55;
  300. params.burnRate = 0.0;
  301. params.diffuse = 1.3;
  302. params.viscosity = 0.05;
  303. params.expansion = - 0.05;
  304. params.swirl = 3.7;
  305. params.drag = 0.6;
  306. params.airSpeed = 6.0;
  307. params.speed = 500.0;
  308. params.massConservation = false;
  309. updateAll();
  310. };
  311. gui.add( params, 'Cigar' );
  312. gui.open();
  313. params.Campfire();
  314. params.Single();
  315. stats = new Stats();
  316. document.body.appendChild( stats.dom );
  317. window.addEventListener( 'resize', onWindowResize, false );
  318. }
  319. function onWindowResize() {
  320. var width = window.innerWidth;
  321. var height = window.innerHeight;
  322. camera.aspect = width / height;
  323. camera.updateProjectionMatrix();
  324. renderer.setSize( window.innerWidth, window.innerHeight );
  325. }
  326. function animate() {
  327. requestAnimationFrame( animate );
  328. renderer.clear();
  329. renderer.render( scene, camera );
  330. stats.update();
  331. }
  332. </script>
  333. </body>
  334. </html>