123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - VFX Tornado</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js webgpu</a> - VFX Tornado
- <br>
- Based on <a href="https://threejs-journey.com/lessons/galaxy-generator" target="_blank" rel="noopener">Three.js Journey</a> lesson
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/tsl": "../build/three.webgpu.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- 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';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- let camera, scene, renderer, postProcessing, controls;
- init();
- function init() {
- camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 50 );
- camera.position.set( 1, 1, 3 );
- scene = new THREE.Scene();
- // textures
- const textureLoader = new THREE.TextureLoader();
- const perlinTexture = textureLoader.load( './textures/noises/perlin/rgb-256x256.png' );
- perlinTexture.wrapS = THREE.RepeatWrapping;
- perlinTexture.wrapT = THREE.RepeatWrapping;
- // TSL functions
- const toRadialUv = Fn( ( [ uv, multiplier, rotation, offset ] ) => {
- const centeredUv = uv.sub( 0.5 ).toVar();
- const distanceToCenter = centeredUv.length();
- const angle = atan2( centeredUv.y, centeredUv.x );
- const radialUv = vec2( angle.add( PI ).div( PI2 ), distanceToCenter ).toVar();
- radialUv.mulAssign( multiplier );
- radialUv.x.addAssign( rotation );
- radialUv.y.addAssign( offset );
- return radialUv;
- } );
- const toSkewedUv = Fn( ( [ uv, skew ] ) => {
- return vec2(
- uv.x.add( uv.y.mul( skew.x ) ),
- uv.y.add( uv.x.mul( skew.y ) )
- );
- } );
- const twistedCylinder = Fn( ( [ position, parabolStrength, parabolOffset, parabolAmplitude, time ] ) => {
- const angle = atan2( position.z, position.x ).toVar();
- const elevation = position.y;
- // parabol
- const radius = parabolStrength.mul( position.y.sub( parabolOffset ) ).pow( 2 ).add( parabolAmplitude ).toVar();
- // turbulences
- radius.addAssign( sin( elevation.sub( time ).mul( 20 ).add( angle.mul( 2 ) ) ).mul( 0.05 ) );
- const twistedPosition = vec3(
- cos( angle ).mul( radius ),
- elevation,
- sin( angle ).mul( radius )
- );
- return twistedPosition;
- } );
- // uniforms
- const emissiveColor = uniform( color( '#ff8b4d' ) );
- const timeScale = uniform( 0.2 );
- const parabolStrength = uniform( 1 );
- const parabolOffset = uniform( 0.3 );
- const parabolAmplitude = uniform( 0.2 );
- // tornado floor
- const floorMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, wireframe: false } );
- floorMaterial.outputNode = Fn( () => {
- const time = timerLocal().mul( timeScale );
- // noise 1
- const noise1Uv = toRadialUv(
- uv(),
- vec2( 0.5, 0.5 ),
- time,
- time
- );
- noise1Uv.assign( toSkewedUv(
- noise1Uv,
- vec2( - 1, 0 )
- ) );
- noise1Uv.mulAssign( vec2( 4, 1 ) );
- const noise1 = texture( perlinTexture, noise1Uv, 1 ).r.remap( 0.45, 0.7 );
- // noise 2
- const noise2Uv = toRadialUv(
- uv(),
- vec2( 2, 8 ),
- time.mul( 2 ),
- time.mul( 8 )
- );
- noise2Uv.assign( toSkewedUv(
- noise2Uv,
- vec2( - 0.25, 0 )
- ) );
- noise2Uv.mulAssign( vec2( 2, 0.25 ) );
- const noise2 = texture( perlinTexture, noise2Uv, 1 ).b.remap( 0.45, 0.7 );
- // outer fade
- const distanceToCenter = uv().sub( 0.5 ).toVar();
- const outerFade = min(
- oneMinus( distanceToCenter.length() ).smoothstep( 0.5, 0.9 ),
- distanceToCenter.length().smoothstep( 0, 0.2 )
- );
- // effect
- const effect = noise1.mul( noise2 ).mul( outerFade ).toVar();
- // output
- return vec4(
- emissiveColor.mul( float( 0.2 ).step( effect ) ).mul( 3 ), // Emissive
- effect.smoothstep( 0, 0.01 ) // Alpha
- );
- } )();
- const floor = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), floorMaterial );
- floor.rotation.x = - Math.PI * 0.5;
- scene.add( floor );
- // tornado cylinder geometry
- const cylinderGeometry = new THREE.CylinderGeometry( 1, 1, 1, 20, 20, true );
- cylinderGeometry.translate( 0, 0.5, 0 );
- // tornado emissive cylinder
- const emissiveMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide, wireframe: false } );
- emissiveMaterial.positionNode = twistedCylinder( positionLocal, parabolStrength, parabolOffset, parabolAmplitude.sub( 0.05 ), timerLocal().mul( timeScale ) );
- emissiveMaterial.outputNode = Fn( () => {
- const time = timerLocal().mul( timeScale );
- // noise 1
- const noise1Uv = uv().add( vec2( time, time.negate() ) ).toVar();
- noise1Uv.assign( toSkewedUv(
- noise1Uv,
- vec2( - 1, 0 )
- ) );
- noise1Uv.mulAssign( vec2( 2, 0.25 ) );
- const noise1 = texture( perlinTexture, noise1Uv, 1 ).r.remap( 0.45, 0.7 );
- // noise 2
- const noise2Uv = uv().add( vec2( time.mul( 0.5 ), time.negate() ) ).toVar();
- noise2Uv.assign( toSkewedUv(
- noise2Uv,
- vec2( - 1, 0 )
- ) );
- noise2Uv.mulAssign( vec2( 5, 1 ) );
- const noise2 = texture( perlinTexture, noise2Uv, 1 ).g.remap( 0.45, 0.7 );
- // outer fade
- const outerFade = min(
- uv().y.smoothstep( 0, 0.1 ),
- oneMinus( uv().y ).smoothstep( 0, 0.4 )
- );
- // effect
- const effect = noise1.mul( noise2 ).mul( outerFade );
- const emissiveColorLuminance = luminance( emissiveColor );
- // output
- return vec4(
- emissiveColor.mul( 1.2 ).div( emissiveColorLuminance ), // emissive
- effect.smoothstep( 0, 0.1 ) // alpha
- );
- } )();
- const emissive = new THREE.Mesh( cylinderGeometry, emissiveMaterial );
- emissive.scale.set( 1, 1, 1 );
- scene.add( emissive );
- // tornado dark cylinder
- const darkMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide, wireframe: false } );
- darkMaterial.positionNode = twistedCylinder( positionLocal, parabolStrength, parabolOffset, parabolAmplitude, timerLocal().mul( timeScale ) );
- darkMaterial.outputNode = Fn( () => {
- const time = timerLocal().mul( timeScale ).add( 123.4 );
- // noise 1
- const noise1Uv = uv().add( vec2( time, time.negate() ) ).toVar();
- noise1Uv.assign( toSkewedUv(
- noise1Uv,
- vec2( - 1, 0 )
- ) );
- noise1Uv.mulAssign( vec2( 2, 0.25 ) );
- const noise1 = texture( perlinTexture, noise1Uv, 1 ).g.remap( 0.45, 0.7 );
- // noise 2
- const noise2Uv = uv().add( vec2( time.mul( 0.5 ), time.negate() ) ).toVar();
- noise2Uv.assign( toSkewedUv(
- noise2Uv,
- vec2( - 1, 0 )
- ) );
- noise2Uv.mulAssign( vec2( 5, 1 ) );
- const noise2 = texture( perlinTexture, noise2Uv, 1 ).b.remap( 0.45, 0.7 );
- // outer fade
- const outerFade = min(
- uv().y.smoothstep( 0, 0.2 ),
- oneMinus( uv().y ).smoothstep( 0, 0.4 )
- );
- // effect
- const effect = noise1.mul( noise2 ).mul( outerFade );
- return vec4(
- vec3( 0 ),
- effect.smoothstep( 0, 0.01 )
- );
- } )();
- const dark = new THREE.Mesh( cylinderGeometry, darkMaterial );
- dark.scale.set( 1, 1, 1 );
- scene.add( dark );
- // renderer
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setClearColor( 0x201919 );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- // post processing
- postProcessing = new THREE.PostProcessing( renderer );
- const scenePass = pass( scene, camera );
- const scenePassColor = scenePass.getTextureNode( 'output' );
- const bloomPass = bloom( scenePassColor, 1, 0.1, 1 );
- postProcessing.outputNode = scenePassColor.add( bloomPass );
- // controls
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.y = 0.4;
- controls.enableDamping = true;
- controls.minDistance = 0.1;
- controls.maxDistance = 50;
- window.addEventListener( 'resize', onWindowResize );
- // debug
- const gui = new GUI();
- gui.addColor( { color: emissiveColor.value.getHexString( THREE.SRGBColorSpace ) }, 'color' ).onChange( value => emissiveColor.value.set( value ) ).name( 'emissiveColor' );
- gui.add( timeScale, 'value', - 1, 1, 0.01 ).name( 'timeScale' );
- gui.add( parabolStrength, 'value', 0, 2, 0.01 ).name( 'parabolStrength' );
- gui.add( parabolOffset, 'value', 0, 1, 0.01 ).name( 'parabolOffset' );
- gui.add( parabolAmplitude, 'value', 0, 2, 0.01 ).name( 'parabolAmplitude' );
- const bloomGui = gui.addFolder( 'bloom' );
- bloomGui.add( bloomPass.strength, 'value', 0, 10, 0.01 ).name( 'strength' );
- bloomGui.add( bloomPass.radius, 'value', 0, 1, 0.01 ).name( 'radius' );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- async function animate() {
- controls.update();
- postProcessing.render();
- }
- </script>
- </body>
- </html>
|