| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - postprocessing anamorphic</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <meta property="og:title" content="three.js webgpu - postprocessing anamorphic">
- <meta property="og:type" content="website">
- <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_anamorphic.html">
- <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_anamorphic.jpg">
- <link type="text/css" rel="stylesheet" href="example.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
- <div class="title-wrapper">
- <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Anamorphic Lensflares</span>
- </div>
- <small>
- Anamorphic bloom filter using custom high pass filter.
- </small>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/webgpu": "../build/three.webgpu.js",
- "three/tsl": "../build/three.tsl.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three/webgpu';
- import { pass, screenUV, uniform, uv, vec2, vec3, color, Fn, vec4, Loop, float, mix, luminance, smoothstep, rtt, time, viewportSize, instanceIndex, positionLocal } from 'three/tsl';
- import { bloom } from 'three/addons/tsl/display/BloomNode.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- let camera, scene, renderer, instancedMesh, controls;
- let renderPipeline;
-
- const _dummy = new THREE.Object3D();
- init();
- async function init() {
- const container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 250 );
- camera.position.set( 0, 0, 20 );
- scene = new THREE.Scene();
- scene.backgroundNode = Fn( () => {
- const dist = screenUV.distance( 0.5 ).mul( 2.0 );
- return mix( color( 0x111111 ), color( 0x000000 ), dist );
- } )();
- const timeScale = uniform( 0.5 );
- const material = new THREE.MeshBasicNodeMaterial( {
- color: 0xffffff,
- // Animate the spheres up and down continuously using TSL and their instanceIndex as a phase offset
- positionNode: positionLocal.add( vec3( 0, time.add( float( instanceIndex ).mul( 0.5 ) ).mul( timeScale ).sin().mul( 5.0 ), 0 ) )
- } );
- const maxCount = 200;
- instancedMesh = new THREE.InstancedMesh( new THREE.SphereGeometry( 0.1, 32, 32 ), material, maxCount );
- const _color = new THREE.Color();
- for ( let i = 0; i < maxCount; i ++ ) {
- _dummy.position.x = ( Math.random() - 0.5 ) * 20;
- _dummy.position.y = ( Math.random() - 0.5 ) * 20;
- _dummy.position.z = ( Math.random() - 0.5 ) * 20;
- _dummy.updateMatrix();
- instancedMesh.setMatrixAt( i, _dummy.matrix );
- _color.setHex( Math.random() * 0xffffff );
- instancedMesh.setColorAt( i, _color );
- }
- scene.add( instancedMesh );
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.toneMapping = THREE.NeutralToneMapping;
- renderer.toneMappingExposure = 1;
- renderer.inspector = new Inspector();
- renderer.setAnimationLoop( render );
- container.appendChild( renderer.domElement );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 2;
- controls.maxDistance = 25;
- // post-processing
- const scenePass = pass( scene, camera );
- const intensity = uniform( 5.0 );
- const tintColor = uniform( new THREE.Color( 0x7a8aff ) );
- const threshold = uniform( 0.3 );
- const radius = uniform( 0.0 );
- const samples = uniform( 80 );
- const bloomPass = bloom( scenePass.getTextureNode().toInspector( 'Color' ), intensity, radius, threshold );
- bloomPass.setResolutionScale( 0.25 );
- // Custom high pass for anamorphic bloom
- bloomPass.highPassFn = Fn( ( { input, threshold, smoothWidth } ) => {
- // Extract bright areas to a texture (computed once, sampled many times)
- const v = luminance( input.rgb );
- const alpha = smoothstep( threshold, threshold.add( smoothWidth ), v );
- const brightPass = rtt( mix( vec4( 0 ), input, alpha ), null, null, {
- wrapS: THREE.MirroredRepeatWrapping,
- wrapT: THREE.MirroredRepeatWrapping
- } );
- // Horizontal blur
- const total = vec4( 0 );
- const halfSamples = samples.div( 2 );
- const invSize = vec2( 1.0 ).div( viewportSize );
- Loop( { start: halfSamples.negate(), end: halfSamples }, ( { i } ) => {
- let softness = float( i ).abs().div( halfSamples ).oneMinus();
- softness = softness.pow( 2.0 );
- const shiftedUV = vec2( uv().x.add( invSize.x.mul( i ).mul( 4.0 ) ), uv().y );
- total.addAssign( brightPass.sample( shiftedUV ).mul( softness ) );
- } );
- return total.div( samples.div( 3.0 ) );
- } );
- const anamorphicPass = bloomPass.mul( tintColor ).toInspector( 'Anamorphic' );
- renderPipeline = new THREE.RenderPipeline( renderer );
- renderPipeline.outputNode = scenePass.add( anamorphicPass );
- // gui
- const gui = renderer.inspector.createParameters( 'Settings' );
- gui.add( intensity, 'value', 0, 10 ).name( 'intensity' );
- gui.add( threshold, 'value', 0, .9 ).name( 'threshold' );
- gui.add( samples, 'value', 2, 128, 1 ).name( 'samples' );
- gui.addColor( tintColor, 'value' ).name( 'tint color' );
- gui.add( radius, 'value', 0, 1, 0.01 ).name( 'bloom radius' );
- gui.add( timeScale, 'value', 0, 1 ).name( 'time scale' );
- //
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function render() {
- controls.update();
- renderPipeline.render();
- }
- </script>
- </body>
- </html>
|