| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - ambient occlusion</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 - postprocessing - ambient occlusion<br />
- <a href="https://skfb.ly/oCnNx" target="_blank" rel="noopener">Minimalistic Modern Bedroom</a> by
- <a href="https://sketchfab.com/dylanheyes" target="_blank" rel="noopener">dylanheyes</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</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, metalness, normalView, roughness, velocity, blendColor, sample, vec2 } from 'three/tsl';
- import { ao } from 'three/addons/tsl/display/GTAONode.js';
- import { ssr } from 'three/addons/tsl/display/SSRNode.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 { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.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;
- let aoPass, ssrPass;
- const params = {
- distanceExponent: 1,
- distanceFallOff: 1,
- radius: 0.25,
- scale: 1,
- thickness: 1,
- denoiseRadius: 5,
- lumaPhi: 5,
- depthPhi: 5,
- normalPhi: 5
- };
- init();
- async function init() {
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 50 );
- camera.position.set( 1, 1.3, 5 );
- scene = new THREE.Scene();
- renderer = new THREE.WebGPURenderer();
- // renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- renderer.toneMapping = THREE.NeutralToneMapping;
- renderer.toneMappingExposure = 3.0;
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- document.body.appendChild( stats.domElement );
- await renderer.init();
- const environment = new RoomEnvironment();
- const pmremGenerator = new THREE.PMREMGenerator( renderer );
- scene.background = new THREE.Color( 0x666666 );
- scene.environment = pmremGenerator.fromScene( environment ).texture;
- environment.dispose();
- pmremGenerator.dispose();
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 0.5, - 1 );
- controls.update();
- controls.enableDamping = true;
- //
- postProcessing = new THREE.PostProcessing( renderer );
- const scenePass = pass( scene, camera );
- scenePass.setMRT( mrt( {
- output: output,
- normal: normalView,
- metalrough: vec2( metalness, roughness ),
- velocity: velocity
- } ) );
- const scenePassColor = scenePass.getTextureNode( 'output' );
- const scenePassDepth = scenePass.getTextureNode( 'depth' );
- const scenePassNormal = scenePass.getTextureNode( 'normal' );
- const scenePassMetalRough = scenePass.getTextureNode( 'metalrough' );
- const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
- const metalRoughTexture = scenePass.getTexture( 'metalrough' );
- metalRoughTexture.type = THREE.UnsignedByteType;
- // ao
- aoPass = ao( scenePassDepth, scenePassNormal, camera );
- aoPass.resolutionScale = 0.5;
-
- // ssr
- const customMetalness = sample( ( uv ) => {
- const metalRough = scenePassMetalRough.sample( uv );
- return metalRough.r.mul( metalRough.g.add( 0.25 ) );
- } );
- ssrPass = ssr( scenePassColor, scenePassDepth, scenePassNormal, customMetalness, camera );
- ssrPass.resolutionScale = 1.0;
- postProcessing.outputNode = traa(
- blendColor( scenePassColor.mul( aoPass ), ssrPass ),
- scenePassDepth, scenePassVelocity, camera
- );
- //
- 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( 'minimalistic_modern_bedroom.glb' );
- const model = gltf.scene;
- model.position.set( 0, 1, 0 );
- scene.add( model );
- // Fix scene
- const duvet = model.getObjectByName( 'Bedroom_Duvet_0' );
- duvet.material.metalness = 0;
- const carpet = model.getObjectByName( 'Bedroom_Carpet_0' );
- carpet.material.metalness = 0;
- const windows = model.getObjectByName( 'Bedroom_Windows_0' );
- windows.material.emissive = new THREE.Color( 0xffffff );
- windows.material.emissiveIntensity = 1.0;
- model.traverse( ( o ) => {
- if ( o.material ) {
- if ( o.material.map ) o.material.map.anisotropy = 8;
- if ( o.material.roughnessMap ) o.material.roughnessMap.anisotropy = 8;
- // Remove emissive from all materials
- o.material.emissive = new THREE.Color( 0x000000 );
- o.material.emissiveMap = null;
- // Transparent objects (e.g. loaded via GLTFLoader) might have "depthWrite" set to "false".
- // This is wanted when rendering the beauty pass however it produces wrong results when computing
- // AO since depth and normal data are out of sync. Computing normals from depth by not using MRT
- // can mitigate the issue although the depth information (and thus the normals) are not correct in
- // first place. Besides, normal estimation is computationally more expensive than just sampling a
- // normal texture. So depending on your scene, consider to enable "depthWrite" for all transparent objects.
- o.material.depthWrite = true;
- }
- } );
- //
- window.addEventListener( 'resize', onWindowResize );
- //
- const gui = new GUI();
- gui.title( 'AO settings' );
- gui.add( params, 'distanceExponent' ).min( 1 ).max( 4 ).onChange( updateParameters );
- gui.add( params, 'distanceFallOff' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
- gui.add( params, 'radius' ).min( 0.1 ).max( 1 ).onChange( updateParameters );
- gui.add( params, 'scale' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
- gui.add( params, 'thickness' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
- }
- function updateParameters() {
- aoPass.distanceExponent.value = params.distanceExponent;
- aoPass.distanceFallOff.value = params.distanceFallOff;
- aoPass.radius.value = params.radius;
- aoPass.scale.value = params.scale;
- aoPass.thickness.value = params.thickness;
- }
- 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>
|