webgpu_tsl_vfx_flames.html 7.2 KB

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