webgpu_tsl_vfx_flames.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, dot, sin, step, texture, timerLocal, tslFn, uv, vec2, vec3, vec4, mix } 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. // TSL functions
  40. const spherizeUv = tslFn( ( [ input, center, strength, offset ] ) => {
  41. const delta = input.sub( center );
  42. const delta2 = dot( delta, delta );
  43. const delta4 = delta2.mul( delta2 );
  44. const deltaOffset = delta4.mul( strength );
  45. return input.add( delta.mul( deltaOffset ) ).add( offset );
  46. } );
  47. // gradient canvas
  48. const gradient = {};
  49. gradient.element = document.createElement( 'canvas' );
  50. gradient.element.width = 128;
  51. gradient.element.height = 1;
  52. gradient.context = gradient.element.getContext( '2d' );
  53. gradient.colors = [
  54. '#090033',
  55. '#5f1f93',
  56. '#e02e96',
  57. '#ffbd80',
  58. '#fff0db',
  59. ];
  60. gradient.texture = new THREE.CanvasTexture( gradient.element );
  61. gradient.texture.colorSpace = THREE.SRGBColorSpace;
  62. gradient.update = () => {
  63. const fillGradient = gradient.context.createLinearGradient( 0, 0, gradient.element.width, 0 );
  64. for ( let i = 0; i < gradient.colors.length; i ++ ) {
  65. const progress = i / ( gradient.colors.length - 1 );
  66. const color = gradient.colors[ i ];
  67. fillGradient.addColorStop( progress, color );
  68. }
  69. gradient.context.fillStyle = fillGradient;
  70. gradient.context.fillRect( 0, 0, gradient.element.width, gradient.element.height );
  71. gradient.texture.needsUpdate = true;
  72. };
  73. gradient.update();
  74. // flame 1 material
  75. const flame1Material = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide } );
  76. flame1Material.colorNode = tslFn( () => {
  77. const time = timerLocal();
  78. // main UV
  79. const mainUv = uv().toVar();
  80. mainUv.assign( spherizeUv( mainUv, vec2( 0.5 ), 10, vec2( 0 ) ).mul( 0.6 ).add( 0.2 ) ); // spherize
  81. mainUv.assign( mainUv.pow( vec2( 1, 2 ) ) ); // stretch
  82. mainUv.assign( mainUv.mul( 2, 1 ).sub( vec2( 0.5, 0 ) ) ); // scale
  83. // gradients
  84. const gradient1 = sin( time.mul( 10 ).sub( mainUv.y.mul( PI2 ).mul( 2 ) ) ).toVar();
  85. const gradient2 = mainUv.y.smoothstep( 0, 1 ).toVar();
  86. mainUv.x.addAssign( gradient1.mul( gradient2 ).mul( 0.2 ) );
  87. // cellular noise
  88. const cellularUv = mainUv.mul( 0.5 ).add( vec2( 0, time.negate().mul( 0.5 ) ) ).mod( 1 );
  89. const cellularNoise = texture( cellularTexture, cellularUv, 0 ).r.oneMinus().smoothstep( 0, 0.5 ).oneMinus();
  90. cellularNoise.mulAssign( gradient2 );
  91. // shape
  92. const shape = mainUv.sub( 0.5 ).mul( vec2( 3, 2 ) ).length().oneMinus().toVar();
  93. shape.assign( shape.sub( cellularNoise ) );
  94. // gradient color
  95. const gradientColor = texture( gradient.texture, vec2( shape.remap( 0, 1, 0, 1 ), 0 ) );
  96. // output
  97. const color = mix( gradientColor, vec3( 1 ), shape.step( 0.8 ).oneMinus() );
  98. const alpha = shape.smoothstep( 0, 0.3 );
  99. return vec4( color.rgb, alpha );
  100. } )();
  101. // flame 2 material
  102. const flame2Material = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide } );
  103. flame2Material.colorNode = tslFn( () => {
  104. const time = timerLocal();
  105. // main UV
  106. const mainUv = uv().toVar();
  107. mainUv.assign( spherizeUv( mainUv, vec2( 0.5 ), 10, vec2( 0 ) ).mul( 0.6 ).add( 0.2 ) ); // spherize
  108. mainUv.assign( mainUv.pow( vec2( 1, 3 ) ) ); // stretch
  109. mainUv.assign( mainUv.mul( 2, 1 ).sub( vec2( 0.5, 0 ) ) ); // scale
  110. // perlin noise
  111. const perlinUv = mainUv.add( vec2( 0, time.negate().mul( 1 ) ) ).mod( 1 );
  112. const perlinNoise = texture( perlinTexture, perlinUv, 0 ).sub( 0.5 ).mul( 1 );
  113. mainUv.x.addAssign( perlinNoise.x.mul( 0.5 ) );
  114. // gradients
  115. const gradient1 = sin( time.mul( 10 ).sub( mainUv.y.mul( PI2 ).mul( 2 ) ) );
  116. const gradient2 = mainUv.y.smoothstep( 0, 1 );
  117. const gradient3 = mainUv.y.smoothstep( 1, 0.7 );
  118. mainUv.x.addAssign( gradient1.mul( gradient2 ).mul( 0.2 ) );
  119. // displaced perlin noise
  120. const displacementPerlinUv = mainUv.mul( 0.5 ).add( vec2( 0, time.negate().mul( 0.25 ) ) ).mod( 1 );
  121. const displacementPerlinNoise = texture( perlinTexture, displacementPerlinUv, 0 ).sub( 0.5 ).mul( 1 );
  122. const displacedPerlinUv = mainUv.add( vec2( 0, time.negate().mul( 0.5 ) ) ).add( displacementPerlinNoise ).mod( 1 );
  123. const displacedPerlinNoise = texture( perlinTexture, displacedPerlinUv, 0 ).sub( 0.5 ).mul( 1 );
  124. mainUv.x.addAssign( displacedPerlinNoise.mul( 0.5 ) );
  125. // cellular noise
  126. const cellularUv = mainUv.add( vec2( 0, time.negate().mul( 1.5 ) ) ).mod( 1 );
  127. const cellularNoise = texture( cellularTexture, cellularUv, 0 ).r.oneMinus().smoothstep( 0.25, 1 );
  128. // shape
  129. const shape = mainUv.sub( 0.5 ).mul( vec2( 6, 1 ) ).length().step( 0.5 );
  130. shape.assign( shape.mul( cellularNoise ) );
  131. shape.mulAssign( gradient3 );
  132. shape.assign( step( 0.01, shape ) );
  133. // output
  134. return vec4( vec3( 1 ), shape );
  135. } )();
  136. // geometry
  137. const geometry = new THREE.PlaneGeometry( 1, 1, 64, 64 );
  138. // meshes
  139. const flame1 = new THREE.Mesh( geometry, flame1Material );
  140. flame1.position.x = - 0.5;
  141. scene.add( flame1 );
  142. const flame2 = new THREE.Mesh( geometry, flame2Material );
  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. document.body.appendChild( renderer.domElement );
  151. controls = new OrbitControls( camera, renderer.domElement );
  152. controls.enableDamping = true;
  153. controls.minDistance = 0.1;
  154. controls.maxDistance = 50;
  155. window.addEventListener( 'resize', onWindowResize );
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. camera.updateProjectionMatrix();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. }
  162. async function animate() {
  163. controls.update();
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>
粤ICP备19079148号