webgpu_tsl_vfx_flames.html 7.5 KB

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