-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
TSL: Pre-pass using global context #32276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+117
−46
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f7708b6
add global context
sunag a26d64f
prepass - wip
sunag 7aa3eb5
fix resize
sunag 2ac5f74
update example
sunag 4d23ba5
Merge branch 'dev' into dev-prepass
sunag bc62baa
remove GlobalContextNode
sunag df0299f
Update Three.TSL.js
sunag bffbda9
cleanup
sunag 7fb0b4e
fix example
sunag 93eda91
fix recursion
sunag ce07579
cleanup
sunag 8ff31dd
cleanup
sunag 1b2e1f3
cleanup
sunag e897785
Revert "cleanup"
sunag 83f9434
cleanup
sunag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| <!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="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>AO</span> | ||
| </div> | ||
|
|
||
| <small> | ||
| Ambient Occlusion based on GTAO.<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>. | ||
| </small> | ||
| </div> | ||
|
|
||
| <script type="importmap"> | ||
| { | ||
| "imports": { | ||
| "three": "../src/three.webgpu.js", | ||
| "three/webgpu": "../src/three.webgpu.js", | ||
| "three/tsl": "../src/three.tsl.js", | ||
| "three/addons/": "./jsm/" | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <script type="module"> | ||
|
|
||
| import * as THREE from 'three/webgpu'; | ||
| import { sample, pass, mrt, globalContext, screenUV, normalView, velocity, vec3, vec4, directionToColor, colorToDirection, colorSpaceToWorking } from 'three/tsl'; | ||
| import { ao } from 'three/addons/tsl/display/GTAONode.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 { Inspector } from 'three/addons/inspector/Inspector.js'; | ||
|
|
||
| let camera, scene, renderer, postProcessing, controls; | ||
|
|
||
| let aoPass, traaPass; | ||
|
|
||
| const params = { | ||
| samples: 16, | ||
| distanceExponent: 1, | ||
| distanceFallOff: 1, | ||
| radius: 0.25, | ||
| scale: 1, | ||
| thickness: 1, | ||
| aoOnly: false | ||
| }; | ||
|
|
||
| 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.inspector = new Inspector(); | ||
| document.body.appendChild( renderer.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.enablePan = false; | ||
| controls.enableDamping = true; | ||
| controls.minDistance = 2; | ||
| controls.maxDistance = 8; | ||
|
|
||
| // | ||
|
|
||
| postProcessing = new THREE.PostProcessing( renderer ); | ||
|
|
||
| // | ||
|
|
||
| const prePass = pass( scene, camera ).toInspector( 'Normal', ( inspectNode ) => colorSpaceToWorking( inspectNode, THREE.SRGBColorSpace ) ); | ||
| prePass.name = 'Pre-Pass'; | ||
| prePass.setMRT( mrt( { | ||
| output: directionToColor( normalView ), | ||
| velocity: velocity | ||
| } ) ); | ||
| prePass.transparent = false; | ||
|
|
||
| const prePassNormal = sample( ( uv ) => { | ||
|
|
||
| return colorToDirection( prePass.getTextureNode().sample( uv ) ); | ||
|
|
||
| } ); | ||
|
|
||
| // bandwidth optimization | ||
|
|
||
| const normalTexture = prePass.getTexture( 'output' ); | ||
| normalTexture.type = THREE.UnsignedByteType; | ||
|
|
||
| // | ||
|
|
||
| const scenePass = pass( scene, camera ).toInspector( 'Color' ); | ||
| scenePass.name = 'Scene Pass'; | ||
|
|
||
| const prePassDepth = prePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => { | ||
sunag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return prePass.getLinearDepthNode(); | ||
|
|
||
| } ); | ||
| const scenePassVelocity = prePass.getTextureNode( 'velocity' ).toInspector( 'Velocity' ); | ||
sunag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // ao | ||
|
|
||
| aoPass = ao( prePassDepth, prePassNormal, camera ).toInspector( 'GTAO', ( inspectNode ) => inspectNode.r ); | ||
| aoPass.name = 'GTAO Pass'; | ||
| aoPass.resolutionScale = .5; // running AO in half resolution is often sufficient | ||
| aoPass.useTemporalFiltering = true; | ||
|
|
||
| // global context | ||
|
|
||
| scenePass.globalContext = globalContext( { | ||
| ao: aoPass.getTextureNode().sample( screenUV ).r | ||
| } ); | ||
|
|
||
| // traa | ||
|
|
||
| traaPass = traa( scenePass, prePassDepth, 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( 'minimalistic_modern_bedroom.glb' ); | ||
|
|
||
| const model = gltf.scene; | ||
| model.position.set( 0, 1, 0 ); | ||
| scene.add( model ); | ||
|
|
||
| // | ||
|
|
||
| const planeRefractor = new THREE.Mesh( new THREE.PlaneGeometry( 1.8, 2 ), new THREE.MeshStandardNodeMaterial( { transparent: true, opacity: .1 } ) ); | ||
| planeRefractor.material.transparent = true; | ||
| planeRefractor.position.z = 0; | ||
| planeRefractor.position.y = 0.5; | ||
| scene.add( planeRefractor ); | ||
sunag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // | ||
|
|
||
| window.addEventListener( 'resize', onWindowResize ); | ||
|
|
||
| // | ||
|
|
||
| const gui = renderer.inspector.createParameters( 'Settings' ); | ||
| gui.add( params, 'samples', 4, 32, 1 ).onChange( updateParameters ); | ||
| gui.add( params, 'distanceExponent', 1, 2 ).onChange( updateParameters ); | ||
| gui.add( params, 'distanceFallOff', 0.01, 1 ).onChange( updateParameters ); | ||
| gui.add( params, 'radius', 0.1, 1 ).onChange( updateParameters ); | ||
| gui.add( params, 'scale', 0.01, 2 ).onChange( updateParameters ); | ||
| gui.add( params, 'thickness', 0.01, 2 ).onChange( updateParameters ); | ||
| gui.add( aoPass, 'useTemporalFiltering' ).name( 'temporal filtering' ); | ||
| gui.add( params, 'aoOnly' ).onChange( ( value ) => { | ||
|
|
||
| if ( value === true ) { | ||
|
|
||
| postProcessing.outputNode = vec4( vec3( aoPass.r ), 1 ); | ||
|
|
||
| } else { | ||
|
|
||
|
|
||
| postProcessing.outputNode = traaPass; | ||
|
|
||
| } | ||
|
|
||
| postProcessing.needsUpdate = true; | ||
|
|
||
| } ); | ||
|
|
||
| } | ||
|
|
||
| function updateParameters() { | ||
|
|
||
| aoPass.samples.value = params.samples; | ||
| 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(); | ||
|
|
||
| postProcessing.render(); | ||
|
|
||
| } | ||
|
|
||
| </script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.