src/passes/MaskPass.js
import { Pass } from "./Pass.js";
/**
* A mask pass.
*
* This pass requires that the input and output buffers have a stencil buffer.
* You can enable the stencil buffer via the {@link EffectComposer} constructor.
*/
export class MaskPass extends Pass {
/**
* Constructs a new mask pass.
*
* @param {Scene} scene - The scene to render.
* @param {Camera} camera - The camera to use.
*/
constructor(scene, camera) {
super("MaskPass", scene, camera);
this.needsSwap = false;
/**
* Inverse flag.
*
* @type {Boolean}
*/
this.inverse = false;
/**
* Stencil buffer clear flag.
*
* @type {Boolean}
*/
this.clearStencil = true;
}
/**
* Renders the effect.
*
* @param {WebGLRenderer} renderer - The renderer.
* @param {WebGLRenderTarget} inputBuffer - A frame buffer that contains the result of the previous pass.
* @param {WebGLRenderTarget} outputBuffer - A frame buffer that serves as the output render target unless this pass renders to screen.
* @param {Number} [delta] - The time between the last frame and the current one in seconds.
* @param {Boolean} [stencilTest] - Indicates whether a stencil mask is active.
*/
render(renderer, inputBuffer, outputBuffer, delta, stencilTest) {
const context = renderer.context;
const state = renderer.state;
const scene = this.scene;
const camera = this.camera;
const writeValue = this.inverse ? 0 : 1;
const clearValue = 1 - writeValue;
// Don't update color or depth.
state.buffers.color.setMask(false);
state.buffers.depth.setMask(false);
// Lock the buffers.
state.buffers.color.setLocked(true);
state.buffers.depth.setLocked(true);
// Configure the stencil.
state.buffers.stencil.setTest(true);
state.buffers.stencil.setOp(context.REPLACE, context.REPLACE, context.REPLACE);
state.buffers.stencil.setFunc(context.ALWAYS, writeValue, 0xffffffff);
state.buffers.stencil.setClear(clearValue);
// Clear the stencil.
if(this.clearStencil) {
if(this.renderToScreen) {
renderer.setRenderTarget(null);
renderer.clearStencil();
} else {
renderer.setRenderTarget(inputBuffer);
renderer.clearStencil();
renderer.setRenderTarget(outputBuffer);
renderer.clearStencil();
}
}
// Draw the mask.
if(this.renderToScreen) {
renderer.render(scene, camera, null);
} else {
renderer.render(scene, camera, inputBuffer);
renderer.render(scene, camera, outputBuffer);
}
// Unlock the buffers.
state.buffers.color.setLocked(false);
state.buffers.depth.setLocked(false);
// Only render where the stencil is set to 1.
state.buffers.stencil.setFunc(context.EQUAL, 1, 0xffffffff);
state.buffers.stencil.setOp(context.KEEP, context.KEEP, context.KEEP);
}
}