-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
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
Examples: Depth peeling example #24227
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e81d6ae
depthpeeling example
ingun37 177016f
ok I just realized a problem it gets darker as depth grows
ingun37 8052bf3
use over composite
ingun37 5267480
pixel ratio
ingun37 95d6fe3
depth peeling make screenshiot
ingun37 8375e21
window resizing
ingun37 3b175df
don't use DepthPeeling class
ingun37 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,272 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>three.js webgl - geometry - cube</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> | ||
|
||
<!-- Import maps polyfill --> | ||
<!-- Remove this when import maps will be widely supported --> | ||
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script> | ||
|
||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"three": "../build/three.module.js" | ||
} | ||
} | ||
</script> | ||
|
||
<script type="module"> | ||
|
||
import * as THREE from 'three'; | ||
import { OrbitControls } from './jsm/controls/OrbitControls.js'; | ||
import { FullScreenQuad } from './jsm/postprocessing/Pass.js'; | ||
import { CopyShader } from './jsm/shaders/CopyShader.js'; | ||
import { GUI } from './jsm/libs/lil-gui.module.min.js'; | ||
|
||
const renderer = new THREE.WebGLRenderer(); | ||
const scene = new THREE.Scene(); | ||
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); | ||
|
||
const globalUniforms = { | ||
uPrevDepthTexture: { value: null }, | ||
uReciprocalScreenSize: { value: new THREE.Vector2( 1, 1 ) }, | ||
}; | ||
const layers = []; | ||
const one = new THREE.DataTexture( new Uint8Array( [ 1, 1, 1, 1 ] ), 1, 1 ); | ||
const quad = new FullScreenQuad( | ||
new THREE.ShaderMaterial( { | ||
...CopyShader, | ||
transparent: true, | ||
depthTest: false, | ||
depthWrite: false, | ||
} ) | ||
); | ||
let depthPeelingScene = new THREE.Scene(); | ||
|
||
const AppState = { | ||
Enabled: true, | ||
Depth: 3, | ||
}; | ||
|
||
function resizeLayers( width, height, pixelRatio ) { | ||
|
||
const w = width * pixelRatio; | ||
const h = height * pixelRatio; | ||
globalUniforms.uReciprocalScreenSize.value.set( | ||
1 / w, | ||
1 / h | ||
); | ||
|
||
layers.forEach( ( rt ) => { | ||
|
||
rt.setSize( w, h ); | ||
rt.depthTexture.dispose(); | ||
rt.depthTexture = new THREE.DepthTexture( w, h ); | ||
|
||
} ); | ||
|
||
} | ||
|
||
function resizeDepth( width, height, pixelRatio, depth ) { | ||
|
||
while ( depth < layers.length ) layers.pop()?.dispose(); | ||
|
||
const w = width * pixelRatio; | ||
const h = height * pixelRatio; | ||
while ( layers.length < depth ) | ||
layers.push( | ||
new THREE.WebGLRenderTarget( w, h, { | ||
depthTexture: new THREE.DepthTexture( w, h ), | ||
} ) | ||
); | ||
|
||
} | ||
|
||
function peelDepth( renderer, camera ) { | ||
|
||
const originalRenderTarget = renderer.getRenderTarget(); | ||
const originalAutoClear = renderer.autoClear; | ||
renderer.autoClear = false; | ||
|
||
layers.reduceRight( ( prevDepth, layer ) => { | ||
|
||
globalUniforms.uPrevDepthTexture.value = prevDepth; | ||
renderer.setRenderTarget( layer ); | ||
renderer.clear(); | ||
renderer.render( depthPeelingScene, camera ); | ||
return layer.depthTexture; | ||
|
||
}, one ); | ||
|
||
renderer.setRenderTarget( originalRenderTarget ); | ||
renderer.clear(); | ||
|
||
for ( const layer of layers ) { | ||
|
||
quad.material.uniforms.tDiffuse.value = | ||
layer.texture; | ||
quad.material.needsUpdate = true; | ||
quad.render( renderer ); | ||
|
||
} | ||
|
||
renderer.autoClear = originalAutoClear; | ||
|
||
} | ||
|
||
init().then( animate ); | ||
|
||
async function init() { | ||
|
||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
document.body.appendChild( renderer.domElement ); | ||
|
||
camera.position.z = 5; | ||
|
||
renderer.shadowMap.enabled = true; | ||
|
||
const dirLight = new THREE.DirectionalLight(); | ||
dirLight.position.set( 0, 3, 0 ); | ||
dirLight.castShadow = true; | ||
scene.add( dirLight ); | ||
scene.add( new THREE.AmbientLight( undefined, 0.5 ) ); | ||
|
||
const sphere = new THREE.Mesh( | ||
new THREE.SphereBufferGeometry(), | ||
new THREE.MeshStandardMaterial() | ||
); | ||
sphere.translateY( 3 ).translateX( 1.5 ); | ||
sphere.castShadow = true; | ||
scene.add( sphere ); | ||
|
||
const knot = new THREE.Mesh( | ||
new THREE.TorusKnotBufferGeometry( undefined, undefined, 128, 32 ), | ||
new THREE.MeshStandardMaterial( { | ||
transparent: true, | ||
opacity: 0.7, | ||
side: THREE.DoubleSide, | ||
} ) | ||
); | ||
knot.receiveShadow = true; | ||
scene.add( knot ); | ||
|
||
scene.add( | ||
new THREE.Mesh( | ||
new THREE.BoxBufferGeometry(), | ||
new THREE.MeshBasicMaterial( { color: 0xf0a000 } ) | ||
) | ||
); | ||
|
||
const texture = await new THREE.TextureLoader().loadAsync( | ||
'textures/sprite0.png' | ||
); | ||
const plane = new THREE.Mesh( | ||
new THREE.PlaneBufferGeometry( 3, 3 ), | ||
new THREE.MeshStandardMaterial( { map: texture, side: THREE.DoubleSide } ) | ||
); | ||
plane.translateX( - 1.6 ); | ||
plane.translateY( 1.5 ); | ||
scene.add( plane ); | ||
|
||
const plane2 = new THREE.Mesh( | ||
new THREE.PlaneBufferGeometry( 3, 3 ), | ||
new THREE.MeshStandardMaterial( { | ||
map: texture, | ||
transparent: true, | ||
side: THREE.DoubleSide, | ||
} ) | ||
); | ||
plane2 | ||
.translateX( - 1.2 ) | ||
.translateY( - 1.5 ) | ||
.translateZ( 0 ) | ||
.rotateY( - 2 * Math.PI * ( 1 / 10 ) ); | ||
scene.add( plane2 ); | ||
|
||
depthPeelingScene = scene.clone( true ); | ||
|
||
resizeLayers( window.innerWidth, window.innerHeight, renderer.getPixelRatio() ); | ||
resizeDepth( window.innerWidth, window.innerHeight, renderer.getPixelRatio(), AppState.Depth ); | ||
|
||
depthPeelingScene.traverse( ( obj ) => { | ||
|
||
if ( obj instanceof THREE.Mesh && obj.material instanceof THREE.Material ) { | ||
|
||
const clonedMaterial = obj.material.clone(); | ||
clonedMaterial.blending = THREE.NoBlending; | ||
clonedMaterial.onBeforeCompile = ( shader ) => { | ||
|
||
shader.uniforms.uReciprocalScreenSize = globalUniforms.uReciprocalScreenSize; | ||
shader.uniforms.uPrevDepthTexture = globalUniforms.uPrevDepthTexture; | ||
shader.fragmentShader = ` | ||
// --- DEPTH PEELING SHADER CHUNK (START) (uniform definition) | ||
uniform vec2 uReciprocalScreenSize; | ||
uniform sampler2D uPrevDepthTexture; | ||
// --- DEPTH PEELING SHADER CHUNK (END) | ||
${shader.fragmentShader} | ||
`; | ||
//peel depth | ||
shader.fragmentShader = shader.fragmentShader.replace( | ||
/}$/gm, | ||
` | ||
// --- DEPTH PEELING SHADER CHUNK (START) (peeling) | ||
vec2 screenPos = gl_FragCoord.xy * uReciprocalScreenSize; | ||
float prevDepth = texture2D(uPrevDepthTexture,screenPos).x; | ||
if( prevDepth >= gl_FragCoord.z ) | ||
discard; | ||
// --- DEPTH PEELING SHADER CHUNK (END) | ||
} | ||
` | ||
); | ||
|
||
}; | ||
|
||
obj.material = clonedMaterial; | ||
obj.material.needsUpdate = true; | ||
|
||
} | ||
|
||
} ); | ||
|
||
const orbit = new OrbitControls( camera, renderer.domElement ); | ||
orbit.update(); | ||
orbit.addEventListener( 'change', animate ); | ||
|
||
const gui = new GUI(); | ||
gui.add( AppState, 'Enabled' ).onChange( animate ); | ||
gui.add( AppState, 'Depth' ).min( 1 ).max( 5 ).step( 1 ).onChange( v => { | ||
|
||
resizeDepth( window.innerWidth, window.innerHeight, renderer.getPixelRatio(), v ); | ||
animate(); | ||
|
||
} ); | ||
|
||
window.addEventListener( 'resize', () => { | ||
|
||
camera.aspect = window.innerWidth / window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
|
||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
resizeLayers( window.innerWidth, window.innerHeight, renderer.getPixelRatio() ); | ||
animate(); | ||
|
||
} ); | ||
|
||
} | ||
|
||
function animate() { | ||
|
||
requestAnimationFrame( () => | ||
AppState.Enabled ? peelDepth( renderer, camera ) : renderer.render( scene, camera ) | ||
); | ||
|
||
} | ||
</script> | ||
</body> | ||
</html> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the past, we've come to the conclusion to not add this kind of shader patching to the examples anymore since it easily breaks when built-in materials are changed.
#15312 also did that and it always felt like a hack. But I assume there is no other way to support depth peeling right? Meaning the technique requires a change in the shader code, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can match
void main() {
and prepend the peeling codes at the beginning of the main instead of appending to the end. It should be safer than just matching}$
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or I can make snapshot of current(r141)
MeshStandardMaterial
fragment shader and embed it usingShaderMaterial
? This way it won't break by any built-in material update.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Um, I'm not yet sure what's best in this context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think
void main() {
match should be practically safe since those four tokens will always exist consecutively according to GLSL specification.But I understand it's a "hack" nevertheless and your reluctance to this approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depth Peeling is simple technique yet its not easily accessible due to lack of examples. So I think it will have benefits at least to some extent for users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what @Mugen87 is suggesting is to add the depth peeling to the renderer itself rather than to create an example or class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LeviPesin Yes, I understood that part. But he asked @mrdoob second opinion and I pryingly added mine 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All in all, please close the PR if you guys think it's against the THREE.JS's policy. I'm happy either way 👍