webgpu_tsl_vfx_tornado.html 9.7 KB

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