DepthTexture disables stencil buffer
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
}
}
</script>
</body>
</html>
JavaScript
import * as THREE from 'three';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { TexturePass } from 'three/addons/postprocessing/TexturePass.js';
import { ClearPass } from 'three/addons/postprocessing/ClearPass.js';
import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
let camera, composer, renderer;
let box, torus;
init();
async function init() {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 10;
const scene1 = new THREE.Scene();
box = new THREE.Mesh( new THREE.BoxGeometry( 4, 4, 4 ) );
scene1.add( box );
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0xe0e0e0 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.autoClear = false;
document.body.appendChild( renderer.domElement );
//
const clearPass = new ClearPass();
const clearMaskPass = new ClearMaskPass();
const maskPass1 = new MaskPass( scene1, camera );
const texture1 = new THREE.TextureLoader().load("https://madeio.net/dev.png");
const texturePass1 = new TexturePass( texture1 );
const outputPass = new OutputPass();
const parameters = {
stencilBuffer: true
};
const renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, parameters );
renderTarget.depthTexture = new THREE.DepthTexture(window.innerWidth, window.innerHeight);
composer = new EffectComposer( renderer, renderTarget );
composer.addPass( clearPass );
composer.addPass( maskPass1 );
composer.addPass( texturePass1 );
composer.addPass( clearMaskPass );
composer.addPass( outputPass );
window.addEventListener( 'resize', onWindowResize );
animate();
}
function onWindowResize() {
const width = window.innerWidth;
...