1
0

webgpu_tsl_vfx_flames.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - vfx flames</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">three.js webgpu</a> - vfx flames
  12. <br>
  13. Inspired by <a href="https://x.com/cmzw_/status/1799648702338158747" target="_blank" rel="noopener">@cmzw_</a>
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { PI2, oneMinus, spherizeUV, sin, step, texture, timerLocal, Fn, uv, vec2, vec3, vec4, mix, billboarding } from 'three/tsl';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, scene, renderer, controls;
  29. init();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  32. camera.position.set( 1, 1, 3 );
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x201919 );
  35. // textures
  36. const textureLoader = new THREE.TextureLoader();
  37. const cellularTexture = textureLoader.load( './textures/noises/voronoi/grayscale-256x256.png' );
  38. const perlinTexture = textureLoader.load( './textures/noises/perlin/rgb-256x256.png' );
  39. // gradient canvas
  40. const gradient = {};
  41. gradient.element = document.createElement( 'canvas' );
  42. gradient.element.width = 128;
  43. gradient.element.height = 1;
  44. gradient.context = gradient.element.getContext( '2d' );
  45. gradient.colors = [
  46. '#090033',
  47. '#5f1f93',
  48. '#e02e96',
  49. '#ffbd80',
  50. '#fff0db',
  51. ];
  52. gradient.texture = new THREE.CanvasTexture( gradient.element );
  53. gradient.texture.colorSpace = THREE.SRGBColorSpace;
  54. gradient.update = () => {
  55. const fillGradient = gradient.context.createLinearGradient( 0, 0, gradient.element.width, 0 );
  56. for ( let i = 0; i < gradient.colors.length; i ++ ) {
  57. const progress = i / ( gradient.colors.length - 1 );
  58. const color = gradient.colors[ i ];
  59. fillGradient.addColorStop( progress, color );
  60. }
  61. gradient.context.fillStyle = fillGradient;
  62. gradient.context.fillRect( 0, 0, gradient.element.width, gradient.element.height );
  63. gradient.texture.needsUpdate = true;
  64. };
  65. gradient.update();
  66. // flame 1 material
  67. const flame1Material = new THREE.SpriteNodeMaterial( { transparent: true, side: THREE.DoubleSide } );
  68. flame1Material.colorNode = Fn( () => {
  69. const time = timerLocal();
  70. // main UV
  71. const mainUv = uv().toVar();
  72. mainUv.assign( spherizeUV( mainUv, 10 ).mul( 0.6 ).add( 0.2 ) ); // spherize
  73. mainUv.assign( mainUv.pow( vec2( 1, 2 ) ) ); // stretch
  74. mainUv.assign( mainUv.mul( 2, 1 ).sub( vec2( 0.5, 0 ) ) ); // scale
  75. // gradients
  76. const gradient1 = sin( time.mul( 10 ).sub( mainUv.y.mul( PI2 ).mul( 2 ) ) ).toVar();
  77. const gradient2 = mainUv.y.smoothstep( 0, 1 ).toVar();
  78. mainUv.x.addAssign( gradient1.mul( gradient2 ).mul( 0.2 ) );
  79. // cellular noise
  80. const cellularUv = mainUv.mul( 0.5 ).add( vec2( 0, time.negate().mul( 0.5 ) ) ).mod( 1 );
  81. const cellularNoise = texture( cellularTexture, cellularUv, 0 ).r.oneMinus().smoothstep( 0, 0.5 ).oneMinus();
  82. cellularNoise.mulAssign( gradient2 );
  83. // shape
  84. const shape = mainUv.sub( 0.5 ).mul( vec2( 3, 2 ) ).length().oneMinus().toVar();
  85. shape.assign( shape.sub( cellularNoise ) );
  86. // gradient color
  87. const gradientColor = texture( gradient.texture, vec2( shape.remap( 0, 1, 0, 1 ), 0 ) );
  88. // output
  89. const color = mix( gradientColor, vec3( 1 ), shape.step( 0.8 ).oneMinus() );
  90. const alpha = shape.smoothstep( 0, 0.3 );
  91. return vec4( color.rgb, alpha );
  92. } )();
  93. // flame 2 material
  94. const flame2Material = new THREE.SpriteNodeMaterial( { transparent: true, side: THREE.DoubleSide } );
  95. flame2Material.colorNode = Fn( () => {
  96. const time = timerLocal();
  97. // main UV
  98. const mainUv = uv().toVar();
  99. mainUv.assign( spherizeUV( mainUv, 10 ).mul( 0.6 ).add( 0.2 ) ); // spherize
  100. mainUv.assign( mainUv.pow( vec2( 1, 3 ) ) ); // stretch
  101. mainUv.assign( mainUv.mul( 2, 1 ).sub( vec2( 0.5, 0 ) ) ); // scale
  102. // perlin noise
  103. const perlinUv = mainUv.add( vec2( 0, time.negate().mul( 1 ) ) ).mod( 1 );
  104. const perlinNoise = texture( perlinTexture, perlinUv, 0 ).sub( 0.5 ).mul( 1 );
  105. mainUv.x.addAssign( perlinNoise.x.mul( 0.5 ) );
  106. // gradients
  107. const gradient1 = sin( time.mul( 10 ).sub( mainUv.y.mul( PI2 ).mul( 2 ) ) );
  108. const gradient2 = mainUv.y.smoothstep( 0, 1 );
  109. const gradient3 = oneMinus( mainUv.y ).smoothstep( 0, 0.3 );
  110. mainUv.x.addAssign( gradient1.mul( gradient2 ).mul( 0.2 ) );
  111. // displaced perlin noise
  112. const displacementPerlinUv = mainUv.mul( 0.5 ).add( vec2( 0, time.negate().mul( 0.25 ) ) ).mod( 1 );
  113. const displacementPerlinNoise = texture( perlinTexture, displacementPerlinUv, 0 ).sub( 0.5 ).mul( 1 );
  114. const displacedPerlinUv = mainUv.add( vec2( 0, time.negate().mul( 0.5 ) ) ).add( displacementPerlinNoise ).mod( 1 );
  115. const displacedPerlinNoise = texture( perlinTexture, displacedPerlinUv, 0 ).sub( 0.5 ).mul( 1 );
  116. mainUv.x.addAssign( displacedPerlinNoise.mul( 0.5 ) );
  117. // cellular noise
  118. const cellularUv = mainUv.add( vec2( 0, time.negate().mul( 1.5 ) ) ).mod( 1 );
  119. const cellularNoise = texture( cellularTexture, cellularUv, 0 ).r.oneMinus().smoothstep( 0.25, 1 );
  120. // shape
  121. const shape = mainUv.sub( 0.5 ).mul( vec2( 6, 1 ) ).length().step( 0.5 );
  122. shape.assign( shape.mul( cellularNoise ) );
  123. shape.mulAssign( gradient3 );
  124. shape.assign( step( 0.01, shape ) );
  125. // output
  126. return vec4( vec3( 1 ), shape );
  127. } )();
  128. // billboarding - follow the camera rotation only horizontally
  129. flame1Material.vertexNode = billboarding();
  130. flame2Material.vertexNode = billboarding();
  131. // meshes
  132. const flame1 = new THREE.Sprite( flame1Material );
  133. flame1.center.set( 0.5, 0 );
  134. flame1.scale.x = 0.5; // optional
  135. flame1.position.x = - 0.5;
  136. scene.add( flame1 );
  137. const flame2 = new THREE.Sprite( flame2Material );
  138. flame2.center.set( 0.5, 0 );
  139. flame2.position.x = 0.5;
  140. scene.add( flame2 );
  141. // renderer
  142. renderer = new THREE.WebGPURenderer( { antialias: true } );
  143. renderer.setPixelRatio( window.devicePixelRatio );
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. renderer.setAnimationLoop( animate );
  146. document.body.appendChild( renderer.domElement );
  147. controls = new OrbitControls( camera, renderer.domElement );
  148. controls.enableDamping = true;
  149. controls.minDistance = 0.1;
  150. controls.maxDistance = 50;
  151. window.addEventListener( 'resize', onWindowResize );
  152. }
  153. function onWindowResize() {
  154. camera.aspect = window.innerWidth / window.innerHeight;
  155. camera.updateProjectionMatrix();
  156. renderer.setSize( window.innerWidth, window.innerHeight );
  157. }
  158. async function animate() {
  159. controls.update();
  160. renderer.render( scene, camera );
  161. }
  162. </script>
  163. </body>
  164. </html>