webgpu_tsl_vfx_flames.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, time, 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. // main UV
  70. const mainUv = uv().toVar();
  71. mainUv.assign( spherizeUV( mainUv, 10 ).mul( 0.6 ).add( 0.2 ) ); // spherize
  72. mainUv.assign( mainUv.pow( vec2( 1, 2 ) ) ); // stretch
  73. mainUv.assign( mainUv.mul( 2, 1 ).sub( vec2( 0.5, 0 ) ) ); // scale
  74. // gradients
  75. const gradient1 = sin( time.mul( 10 ).sub( mainUv.y.mul( PI2 ).mul( 2 ) ) ).toVar();
  76. const gradient2 = mainUv.y.smoothstep( 0, 1 ).toVar();
  77. mainUv.x.addAssign( gradient1.mul( gradient2 ).mul( 0.2 ) );
  78. // cellular noise
  79. const cellularUv = mainUv.mul( 0.5 ).add( vec2( 0, time.negate().mul( 0.5 ) ) ).mod( 1 );
  80. const cellularNoise = texture( cellularTexture, cellularUv, 0 ).r.oneMinus().smoothstep( 0, 0.5 ).oneMinus();
  81. cellularNoise.mulAssign( gradient2 );
  82. // shape
  83. const shape = mainUv.sub( 0.5 ).mul( vec2( 3, 2 ) ).length().oneMinus().toVar();
  84. shape.assign( shape.sub( cellularNoise ) );
  85. // gradient color
  86. const gradientColor = texture( gradient.texture, vec2( shape.remap( 0, 1, 0, 1 ), 0 ) );
  87. // output
  88. const color = mix( gradientColor, vec3( 1 ), shape.step( 0.8 ).oneMinus() );
  89. const alpha = shape.smoothstep( 0, 0.3 );
  90. return vec4( color.rgb, alpha );
  91. } )();
  92. // flame 2 material
  93. const flame2Material = new THREE.SpriteNodeMaterial( { transparent: true, side: THREE.DoubleSide } );
  94. flame2Material.colorNode = Fn( () => {
  95. // main UV
  96. const mainUv = uv().toVar();
  97. mainUv.assign( spherizeUV( mainUv, 10 ).mul( 0.6 ).add( 0.2 ) ); // spherize
  98. mainUv.assign( mainUv.pow( vec2( 1, 3 ) ) ); // stretch
  99. mainUv.assign( mainUv.mul( 2, 1 ).sub( vec2( 0.5, 0 ) ) ); // scale
  100. // perlin noise
  101. const perlinUv = mainUv.add( vec2( 0, time.negate().mul( 1 ) ) ).mod( 1 );
  102. const perlinNoise = texture( perlinTexture, perlinUv, 0 ).sub( 0.5 ).mul( 1 );
  103. mainUv.x.addAssign( perlinNoise.x.mul( 0.5 ) );
  104. // gradients
  105. const gradient1 = sin( time.mul( 10 ).sub( mainUv.y.mul( PI2 ).mul( 2 ) ) );
  106. const gradient2 = mainUv.y.smoothstep( 0, 1 );
  107. const gradient3 = oneMinus( mainUv.y ).smoothstep( 0, 0.3 );
  108. mainUv.x.addAssign( gradient1.mul( gradient2 ).mul( 0.2 ) );
  109. // displaced perlin noise
  110. const displacementPerlinUv = mainUv.mul( 0.5 ).add( vec2( 0, time.negate().mul( 0.25 ) ) ).mod( 1 );
  111. const displacementPerlinNoise = texture( perlinTexture, displacementPerlinUv, 0 ).sub( 0.5 ).mul( 1 );
  112. const displacedPerlinUv = mainUv.add( vec2( 0, time.negate().mul( 0.5 ) ) ).add( displacementPerlinNoise ).mod( 1 );
  113. const displacedPerlinNoise = texture( perlinTexture, displacedPerlinUv, 0 ).sub( 0.5 ).mul( 1 );
  114. mainUv.x.addAssign( displacedPerlinNoise.mul( 0.5 ) );
  115. // cellular noise
  116. const cellularUv = mainUv.add( vec2( 0, time.negate().mul( 1.5 ) ) ).mod( 1 );
  117. const cellularNoise = texture( cellularTexture, cellularUv, 0 ).r.oneMinus().smoothstep( 0.25, 1 );
  118. // shape
  119. const shape = mainUv.sub( 0.5 ).mul( vec2( 6, 1 ) ).length().step( 0.5 );
  120. shape.assign( shape.mul( cellularNoise ) );
  121. shape.mulAssign( gradient3 );
  122. shape.assign( step( 0.01, shape ) );
  123. // output
  124. return vec4( vec3( 1 ), shape );
  125. } )();
  126. // billboarding - follow the camera rotation only horizontally
  127. flame1Material.vertexNode = billboarding();
  128. flame2Material.vertexNode = billboarding();
  129. // meshes
  130. const flame1 = new THREE.Sprite( flame1Material );
  131. flame1.center.set( 0.5, 0 );
  132. flame1.scale.x = 0.5; // optional
  133. flame1.position.x = - 0.5;
  134. scene.add( flame1 );
  135. const flame2 = new THREE.Sprite( flame2Material );
  136. flame2.center.set( 0.5, 0 );
  137. flame2.position.x = 0.5;
  138. scene.add( flame2 );
  139. // renderer
  140. renderer = new THREE.WebGPURenderer( { antialias: true } );
  141. renderer.setPixelRatio( window.devicePixelRatio );
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. renderer.setAnimationLoop( animate );
  144. document.body.appendChild( renderer.domElement );
  145. controls = new OrbitControls( camera, renderer.domElement );
  146. controls.enableDamping = true;
  147. controls.minDistance = 0.1;
  148. controls.maxDistance = 50;
  149. window.addEventListener( 'resize', onWindowResize );
  150. }
  151. function onWindowResize() {
  152. camera.aspect = window.innerWidth / window.innerHeight;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( window.innerWidth, window.innerHeight );
  155. }
  156. async function animate() {
  157. controls.update();
  158. renderer.render( scene, camera );
  159. }
  160. </script>
  161. </body>
  162. </html>
粤ICP备19079148号