1
0

webgpu_tsl_vfx_tornado.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - VFX Tornado</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 Tornado
  12. <br>
  13. Based on <a href="https://threejs-journey.com/lessons/galaxy-generator" target="_blank" rel="noopener">Three.js Journey</a> lesson
  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 { luminance, cos, float, min, timerLocal, atan2, uniform, pass, bloom, PI, PI2, color, positionLocal, oneMinus, sin, texture, Fn, uv, vec2, vec3, vec4 } from 'three/tsl';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. let camera, scene, renderer, postProcessing, controls;
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 50 );
  33. camera.position.set( 1, 1, 3 );
  34. scene = new THREE.Scene();
  35. // textures
  36. const textureLoader = new THREE.TextureLoader();
  37. const perlinTexture = textureLoader.load( './textures/noises/perlin/rgb-256x256.png' );
  38. perlinTexture.wrapS = THREE.RepeatWrapping;
  39. perlinTexture.wrapT = THREE.RepeatWrapping;
  40. // TSL functions
  41. const toRadialUv = Fn( ( [ uv, multiplier, rotation, offset ] ) => {
  42. const centeredUv = uv.sub( 0.5 ).toVar();
  43. const distanceToCenter = centeredUv.length();
  44. const angle = atan2( centeredUv.y, centeredUv.x );
  45. const radialUv = vec2( angle.add( PI ).div( PI2 ), distanceToCenter ).toVar();
  46. radialUv.mulAssign( multiplier );
  47. radialUv.x.addAssign( rotation );
  48. radialUv.y.addAssign( offset );
  49. return radialUv;
  50. } );
  51. const toSkewedUv = Fn( ( [ uv, skew ] ) => {
  52. return vec2(
  53. uv.x.add( uv.y.mul( skew.x ) ),
  54. uv.y.add( uv.x.mul( skew.y ) )
  55. );
  56. } );
  57. const twistedCylinder = Fn( ( [ position, parabolStrength, parabolOffset, parabolAmplitude, time ] ) => {
  58. const angle = atan2( position.z, position.x ).toVar();
  59. const elevation = position.y;
  60. // parabol
  61. const radius = parabolStrength.mul( position.y.sub( parabolOffset ) ).pow( 2 ).add( parabolAmplitude ).toVar();
  62. // turbulences
  63. radius.addAssign( sin( elevation.sub( time ).mul( 20 ).add( angle.mul( 2 ) ) ).mul( 0.05 ) );
  64. const twistedPosition = vec3(
  65. cos( angle ).mul( radius ),
  66. elevation,
  67. sin( angle ).mul( radius )
  68. );
  69. return twistedPosition;
  70. } );
  71. // uniforms
  72. const emissiveColor = uniform( color( '#ff8b4d' ) );
  73. const timeScale = uniform( 0.2 );
  74. const parabolStrength = uniform( 1 );
  75. const parabolOffset = uniform( 0.3 );
  76. const parabolAmplitude = uniform( 0.2 );
  77. // tornado floor
  78. const floorMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, wireframe: false } );
  79. floorMaterial.outputNode = Fn( () => {
  80. const time = timerLocal().mul( timeScale );
  81. // noise 1
  82. const noise1Uv = toRadialUv(
  83. uv(),
  84. vec2( 0.5, 0.5 ),
  85. time,
  86. time
  87. );
  88. noise1Uv.assign( toSkewedUv(
  89. noise1Uv,
  90. vec2( - 1, 0 )
  91. ) );
  92. noise1Uv.mulAssign( vec2( 4, 1 ) );
  93. const noise1 = texture( perlinTexture, noise1Uv, 1 ).r.remap( 0.45, 0.7 );
  94. // noise 2
  95. const noise2Uv = toRadialUv(
  96. uv(),
  97. vec2( 2, 8 ),
  98. time.mul( 2 ),
  99. time.mul( 8 )
  100. );
  101. noise2Uv.assign( toSkewedUv(
  102. noise2Uv,
  103. vec2( - 0.25, 0 )
  104. ) );
  105. noise2Uv.mulAssign( vec2( 2, 0.25 ) );
  106. const noise2 = texture( perlinTexture, noise2Uv, 1 ).b.remap( 0.45, 0.7 );
  107. // outer fade
  108. const distanceToCenter = uv().sub( 0.5 ).toVar();
  109. const outerFade = min(
  110. oneMinus( distanceToCenter.length() ).smoothstep( 0.5, 0.9 ),
  111. distanceToCenter.length().smoothstep( 0, 0.2 )
  112. );
  113. // effect
  114. const effect = noise1.mul( noise2 ).mul( outerFade ).toVar();
  115. // output
  116. return vec4(
  117. emissiveColor.mul( float( 0.2 ).step( effect ) ).mul( 3 ), // Emissive
  118. effect.smoothstep( 0, 0.01 ) // Alpha
  119. );
  120. } )();
  121. const floor = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), floorMaterial );
  122. floor.rotation.x = - Math.PI * 0.5;
  123. scene.add( floor );
  124. // tornado cylinder geometry
  125. const cylinderGeometry = new THREE.CylinderGeometry( 1, 1, 1, 20, 20, true );
  126. cylinderGeometry.translate( 0, 0.5, 0 );
  127. // tornado emissive cylinder
  128. const emissiveMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide, wireframe: false } );
  129. emissiveMaterial.positionNode = twistedCylinder( positionLocal, parabolStrength, parabolOffset, parabolAmplitude.sub( 0.05 ), timerLocal().mul( timeScale ) );
  130. emissiveMaterial.outputNode = Fn( () => {
  131. const time = timerLocal().mul( timeScale );
  132. // noise 1
  133. const noise1Uv = uv().add( vec2( time, time.negate() ) ).toVar();
  134. noise1Uv.assign( toSkewedUv(
  135. noise1Uv,
  136. vec2( - 1, 0 )
  137. ) );
  138. noise1Uv.mulAssign( vec2( 2, 0.25 ) );
  139. const noise1 = texture( perlinTexture, noise1Uv, 1 ).r.remap( 0.45, 0.7 );
  140. // noise 2
  141. const noise2Uv = uv().add( vec2( time.mul( 0.5 ), time.negate() ) ).toVar();
  142. noise2Uv.assign( toSkewedUv(
  143. noise2Uv,
  144. vec2( - 1, 0 )
  145. ) );
  146. noise2Uv.mulAssign( vec2( 5, 1 ) );
  147. const noise2 = texture( perlinTexture, noise2Uv, 1 ).g.remap( 0.45, 0.7 );
  148. // outer fade
  149. const outerFade = min(
  150. uv().y.smoothstep( 0, 0.1 ),
  151. oneMinus( uv().y ).smoothstep( 0, 0.4 )
  152. );
  153. // effect
  154. const effect = noise1.mul( noise2 ).mul( outerFade );
  155. const emissiveColorLuminance = luminance( emissiveColor );
  156. // output
  157. return vec4(
  158. emissiveColor.mul( 1.2 ).div( emissiveColorLuminance ), // emissive
  159. effect.smoothstep( 0, 0.1 ) // alpha
  160. );
  161. } )();
  162. const emissive = new THREE.Mesh( cylinderGeometry, emissiveMaterial );
  163. emissive.scale.set( 1, 1, 1 );
  164. scene.add( emissive );
  165. // tornado dark cylinder
  166. const darkMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide, wireframe: false } );
  167. darkMaterial.positionNode = twistedCylinder( positionLocal, parabolStrength, parabolOffset, parabolAmplitude, timerLocal().mul( timeScale ) );
  168. darkMaterial.outputNode = Fn( () => {
  169. const time = timerLocal().mul( timeScale ).add( 123.4 );
  170. // noise 1
  171. const noise1Uv = uv().add( vec2( time, time.negate() ) ).toVar();
  172. noise1Uv.assign( toSkewedUv(
  173. noise1Uv,
  174. vec2( - 1, 0 )
  175. ) );
  176. noise1Uv.mulAssign( vec2( 2, 0.25 ) );
  177. const noise1 = texture( perlinTexture, noise1Uv, 1 ).g.remap( 0.45, 0.7 );
  178. // noise 2
  179. const noise2Uv = uv().add( vec2( time.mul( 0.5 ), time.negate() ) ).toVar();
  180. noise2Uv.assign( toSkewedUv(
  181. noise2Uv,
  182. vec2( - 1, 0 )
  183. ) );
  184. noise2Uv.mulAssign( vec2( 5, 1 ) );
  185. const noise2 = texture( perlinTexture, noise2Uv, 1 ).b.remap( 0.45, 0.7 );
  186. // outer fade
  187. const outerFade = min(
  188. uv().y.smoothstep( 0, 0.2 ),
  189. oneMinus( uv().y ).smoothstep( 0, 0.4 )
  190. );
  191. // effect
  192. const effect = noise1.mul( noise2 ).mul( outerFade );
  193. return vec4(
  194. vec3( 0 ),
  195. effect.smoothstep( 0, 0.01 )
  196. );
  197. } )();
  198. const dark = new THREE.Mesh( cylinderGeometry, darkMaterial );
  199. dark.scale.set( 1, 1, 1 );
  200. scene.add( dark );
  201. // renderer
  202. renderer = new THREE.WebGPURenderer( { antialias: true } );
  203. renderer.setClearColor( 0x201919 );
  204. renderer.setPixelRatio( window.devicePixelRatio );
  205. renderer.setSize( window.innerWidth, window.innerHeight );
  206. renderer.setAnimationLoop( animate );
  207. document.body.appendChild( renderer.domElement );
  208. // post processing
  209. postProcessing = new THREE.PostProcessing( renderer );
  210. const scenePass = pass( scene, camera );
  211. const scenePassColor = scenePass.getTextureNode( 'output' );
  212. const bloomPass = bloom( scenePassColor, 1, 0.1, 1 );
  213. postProcessing.outputNode = scenePassColor.add( bloomPass );
  214. // controls
  215. controls = new OrbitControls( camera, renderer.domElement );
  216. controls.target.y = 0.4;
  217. controls.enableDamping = true;
  218. controls.minDistance = 0.1;
  219. controls.maxDistance = 50;
  220. window.addEventListener( 'resize', onWindowResize );
  221. // debug
  222. const gui = new GUI();
  223. gui.addColor( { color: emissiveColor.value.getHexString( THREE.SRGBColorSpace ) }, 'color' ).onChange( value => emissiveColor.value.set( value ) ).name( 'emissiveColor' );
  224. gui.add( timeScale, 'value', - 1, 1, 0.01 ).name( 'timeScale' );
  225. gui.add( parabolStrength, 'value', 0, 2, 0.01 ).name( 'parabolStrength' );
  226. gui.add( parabolOffset, 'value', 0, 1, 0.01 ).name( 'parabolOffset' );
  227. gui.add( parabolAmplitude, 'value', 0, 2, 0.01 ).name( 'parabolAmplitude' );
  228. const bloomGui = gui.addFolder( 'bloom' );
  229. bloomGui.add( bloomPass.strength, 'value', 0, 10, 0.01 ).name( 'strength' );
  230. bloomGui.add( bloomPass.radius, 'value', 0, 1, 0.01 ).name( 'radius' );
  231. }
  232. function onWindowResize() {
  233. camera.aspect = window.innerWidth / window.innerHeight;
  234. camera.updateProjectionMatrix();
  235. renderer.setSize( window.innerWidth, window.innerHeight );
  236. }
  237. async function animate() {
  238. controls.update();
  239. postProcessing.render();
  240. }
  241. </script>
  242. </body>
  243. </html>