webgpu_tsl_vfx_tornado.html 9.8 KB

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