Threejs.org/examples/#w…

  • The Texture of the RenderTarget will be placed on this panel
varying vec2 vUv;

void main() {
        vUv = uv;
        gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
Copy the code
#include <packing>

varying vec2 vUv;
uniform sampler2D tDiffuse;
uniform sampler2D tDepth; / / depth map
uniform float cameraNear;
uniform float cameraFar;


float readDepth( sampler2D depthSampler, vec2 coord ) {
        float fragCoordZ = texture2D( depthSampler, coord ).x;
        float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar ); // Near and FAR restore true depth values
        return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar ); // Change the true depth value to 0-1
        
}
float perspectiveDepthToViewZ(const in float invClipZ, const in float near, const in float far) {
    return (near * far) / ((far - near) * invClipZ - far);
}
float viewZToOrthographicDepth(const in float viewZ, const in float near, const in float far) {
    return (viewZ + near) / (near - far); //
}

void main() {
        //vec3 diffuse = texture2D( tDiffuse, vUv ).rgb;
        float depth = readDepth( tDepth, vUv );

        gl_FragColor.rgb = 1.0 - vec3( depth );
        gl_FragColor.a = 1.0;
}
Copy the code
function setupPost() {
        // Setup post processing stage
        postCamera = new THREE.OrthographicCamera( - 1.1.1, - 1.0.1 );
        postMaterial = new THREE.ShaderMaterial( {
                vertexShader: document.querySelector( '#post-vert' ).textContent.trim(),
                fragmentShader: document.querySelector( '#post-frag' ).textContent.trim(),
                uniforms: {
                        cameraNear: { value: camera.near },
                        cameraFar: { value: camera.far },
                        tDiffuse: { value: null },
                        tDepth: { value: null}}});const postPlane = new THREE.PlaneGeometry( 2.2 );
        const postQuad = new THREE.Mesh( postPlane, postMaterial );
        postScene = new THREE.Scene();
        postScene.add( postQuad );
}

Copy the code
  • The type of format and type
const formats = { DepthFormat: THREE.DepthFormat, DepthStencilFormat: THREE.DepthStencilFormat };
const types = { UnsignedShortType: THREE.UnsignedShortType, UnsignedIntType: THREE.UnsignedIntType, UnsignedInt248Type: THREE.UnsignedInt248Type };
Copy the code
  • Generate WebGLRenderTarget
scene = new THREE.Scene();
function setupRenderTarget() {
        if ( target ) target.dispose();

        const format = parseFloat( params.format );
        const type = parseFloat( params.type );

        target = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight );
        target.texture.format = THREE.RGBFormat;
        target.texture.minFilter = THREE.NearestFilter;
        target.texture.magFilter = THREE.NearestFilter;
        target.texture.generateMipmaps = false;
        
        target.stencilBuffer = ( format === THREE.DepthStencilFormat ) ? true : false;
        target.depthBuffer = true;
        target.depthTexture = new THREE.DepthTexture();
        target.depthTexture.format = format;
        target.depthTexture.type = type;
}
Copy the code
  • render
renderer.setRenderTarget( target );
renderer.render( scene, camera );

// render post FX
postMaterial.uniforms.tDiffuse.value = target.texture;
postMaterial.uniforms.tDepth.value = target.depthTexture;

renderer.setRenderTarget( null );
renderer.render( postScene, postCamera );
Copy the code