| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js WebGPU - SSGI</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</a> WebGPU - Post-Processing - SSGI<br />
- <a href="https://skfb.ly/YZoC" target="_blank" rel="noopener">Mirror's Edge Apartment</a> by
- <a href="https://sketchfab.com/aurelien_martel" target="_blank" rel="noopener">Aurélien Martel</a> is licensed under <a href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="noopener">Creative Commons Attribution-NonCommercial</a>.<br />
- </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, mrt, output, normalView, velocity, add, vec3, vec4 } from 'three/tsl';
- import { ssgi } from 'three/addons/tsl/display/SSGINode.js';
- import { traa } from 'three/addons/tsl/display/TRAANode.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import Stats from 'three/addons/libs/stats.module.js';
- let camera, scene, renderer, postProcessing, controls, stats;
- init();
- async function init() {
- camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 50 );
- camera.position.set( 2.8, 1.8, 5 );
- scene = new THREE.Scene();
- renderer = new THREE.WebGPURenderer();
- //renderer.setPixelRatio( window.devicePixelRatio ); // probably too costly for most hardware
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- renderer.toneMapping = THREE.NeutralToneMapping;
- renderer.toneMappingExposure = 1.5;
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- document.body.appendChild( stats.domElement );
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 1, 0 );
- controls.enablePan = true;
- controls.minDistance = 1;
- controls.maxDistance = 6;
- controls.update();
- //
- postProcessing = new THREE.PostProcessing( renderer );
- const scenePass = pass( scene, camera );
- scenePass.setMRT( mrt( {
- output: output,
- normal: normalView,
- velocity: velocity
- } ) );
- const scenePassColor = scenePass.getTextureNode( 'output' );
- const scenePassDepth = scenePass.getTextureNode( 'depth' );
- const scenePassNormal = scenePass.getTextureNode( 'normal' );
- const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
- // gi
- const giPass = ssgi( scenePassColor, scenePassDepth, scenePassNormal, camera );
- giPass.sliceCount.value = 2;
- giPass.stepCount.value = 8;
- // composite
- const gi = giPass.rgb;
- const ao = giPass.a;
-
- const compositePass = vec4( add( scenePassColor.rgb, gi ).mul( ao ), scenePassColor.a );
-
- // traa
- const traaPass = traa( compositePass, scenePassDepth, scenePassVelocity, camera );
- postProcessing.outputNode = traaPass;
- //
- const dracoLoader = new DRACOLoader();
- dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
- dracoLoader.setDecoderConfig( { type: 'js' } );
- const loader = new GLTFLoader();
- loader.setDRACOLoader( dracoLoader );
- loader.setPath( 'models/gltf/' );
- const gltf = await loader.loadAsync( 'mirrors_edge.glb' );
- const model = gltf.scene;
- scene.add( model );
- window.addEventListener( 'resize', onWindowResize );
- //
- const gui = new GUI();
- gui.title( 'SSGI settings' );
- gui.add( giPass.sliceCount, 'value', 1, 4 ).step( 1 ).name( 'slice count' );
- gui.add( giPass.stepCount, 'value', 1, 32 ).step( 1 ).name( 'step count' );
- gui.add( giPass.radius, 'value', 1, 25 ).name( 'radius' );
- gui.add( giPass.expFactor, 'value', 1, 3 ).name( 'exp factor' );
- gui.add( giPass.thickness, 'value', 0.01, 10 ).name( 'thickness' );
- gui.add( giPass.backfaceLighting, 'value', 0, 1 ).name( 'backface lighting' );
- gui.add( giPass.aoIntensity, 'value', 0, 4 ).name( 'AO intenstiy' );
- gui.add( giPass.giIntensity, 'value', 0, 100 ).name( 'GI intenstiy' );
- gui.add( giPass.useLinearThickness, 'value' ).name( 'use linear thickness' );
- gui.add( giPass.useScreenSpaceSampling, 'value' ).name( 'screen-space sampling' );
- gui.add( giPass, 'useTemporalFiltering' ).name( 'temporal filtering' );
- const params = {
- output: 0
- };
- const types = { Default: 0, AO: 1, GI: 2, Beauty: 3 };
- gui.add( params, 'output', types ).onChange( value => {
-
- if ( value === 1 ) {
- postProcessing.outputNode = vec4( vec3( ao ), 1 );
- } else if ( value === 2 ) {
- postProcessing.outputNode = vec4( gi, 1 );
- } else if ( value === 3 ) {
- postProcessing.outputNode = scenePassColor;
- } else {
- postProcessing.outputNode = traaPass;
- }
- postProcessing.needsUpdate = true;
- } );
- }
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- function animate() {
- controls.update();
- stats.update();
- postProcessing.render();
- }
- </script>
- </body>
- </html>
|