import { LinearFilter, RGBFormat, UnsignedByteType, WebGLRenderTarget } from 'three'; import { Pass } from './Pass'; import { CopyMaterial } from '../materials/CopyMaterial'; /** * A pass that renders the result from a previous pass to another render target. */ export class SavePass extends Pass { renderTarget: any; resize: boolean; /** * Constructs a new save pass. * * @param {WebGLRenderTarget} [renderTarget] - A render target. * @param {Boolean} [resize=true] - Whether the render target should adjust to the size of the input buffer. */ constructor (renderTarget, resize = true) { super('SavePass'); this.setFullscreenMaterial(new CopyMaterial()); this.needsSwap = false; /** * The render target. * * @type {WebGLRenderTarget} */ this.renderTarget = renderTarget; if (renderTarget === undefined) { this.renderTarget = new WebGLRenderTarget(1, 1, { minFilter: LinearFilter, magFilter: LinearFilter, stencilBuffer: false, depthBuffer: false }); this.renderTarget.texture.name = 'SavePass.Target'; } /** * Indicates whether the render target should be resized automatically. * * @type {Boolean} */ this.resize = resize; } /** * Saves the input buffer. * * @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} [deltaTime] - 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?, deltaTime?, stencilTest?) { this.getFullscreenMaterial().uniforms.inputBuffer.value = inputBuffer.texture; renderer.setRenderTarget(this.renderToScreen ? null : this.renderTarget); renderer.render(this.scene, this.camera); } /** * Updates the size of this pass. * * @param {Number} width - The width. * @param {Number} height - The height. */ setSize (width, height) { if (this.resize) { const w = Math.max(width, 1); const h = Math.max(height, 1); this.renderTarget.setSize(w, h); } } /** * Performs initialization tasks. * * @param {WebGLRenderer} renderer - A renderer. * @param {Boolean} alpha - Whether the renderer uses the alpha channel. * @param {Number} frameBufferType - The type of the main frame buffers. */ initialize (renderer, alpha, frameBufferType) { if (!alpha && frameBufferType === UnsignedByteType) { this.renderTarget.texture.format = RGBFormat; } if (frameBufferType !== undefined) { this.renderTarget.texture.type = frameBufferType; } } }