Skip to content
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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@
"webgl_video_kinect",
"webgl_video_panorama_equirectangular",
"webgl_water",
"webgl_water_flowmap"
"webgl_water_flowmap",
"webgl_depth_peeling"
],
"webgl / nodes": [
"webgl_materials_instance_uniform_nodes",
Expand Down
Binary file added examples/screenshots/webgl_depth_peeling.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
272 changes: 272 additions & 0 deletions examples/webgl_depth_peeling.html
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 ) => {
Copy link
Collaborator

@Mugen87 Mugen87 Jun 14, 2022

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so.

Copy link
Contributor Author

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 }$.

Copy link
Contributor Author

@ingun37 ingun37 Jun 14, 2022

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 using ShaderMaterial? This way it won't break by any built-in material update.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@ingun37 ingun37 Jun 18, 2022

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 😅

Copy link
Contributor Author

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 👍


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>