UNPKG

4.5 kBJavaScriptView Raw
1import { Color } from "@pixi/color";
2import { ExtensionType, extensions } from "@pixi/extensions";
3import { Rectangle } from "@pixi/math";
4const tempRect = new Rectangle(), tempRect2 = new Rectangle();
5class RenderTextureSystem {
6 /**
7 * @param renderer - The renderer this System works for.
8 */
9 constructor(renderer) {
10 this.renderer = renderer, this.defaultMaskStack = [], this.current = null, this.sourceFrame = new Rectangle(), this.destinationFrame = new Rectangle(), this.viewportFrame = new Rectangle();
11 }
12 contextChange() {
13 const attributes = this.renderer?.gl.getContextAttributes();
14 this._rendererPremultipliedAlpha = !!(attributes && attributes.alpha && attributes.premultipliedAlpha);
15 }
16 /**
17 * Bind the current render texture.
18 * @param renderTexture - RenderTexture to bind, by default its `null` - the screen.
19 * @param sourceFrame - Part of world that is mapped to the renderTexture.
20 * @param destinationFrame - Part of renderTexture, by default it has the same size as sourceFrame.
21 */
22 bind(renderTexture = null, sourceFrame, destinationFrame) {
23 const renderer = this.renderer;
24 this.current = renderTexture;
25 let baseTexture, framebuffer, resolution;
26 renderTexture ? (baseTexture = renderTexture.baseTexture, resolution = baseTexture.resolution, sourceFrame || (tempRect.width = renderTexture.frame.width, tempRect.height = renderTexture.frame.height, sourceFrame = tempRect), destinationFrame || (tempRect2.x = renderTexture.frame.x, tempRect2.y = renderTexture.frame.y, tempRect2.width = sourceFrame.width, tempRect2.height = sourceFrame.height, destinationFrame = tempRect2), framebuffer = baseTexture.framebuffer) : (resolution = renderer.resolution, sourceFrame || (tempRect.width = renderer._view.screen.width, tempRect.height = renderer._view.screen.height, sourceFrame = tempRect), destinationFrame || (destinationFrame = tempRect, destinationFrame.width = sourceFrame.width, destinationFrame.height = sourceFrame.height));
27 const viewportFrame = this.viewportFrame;
28 viewportFrame.x = destinationFrame.x * resolution, viewportFrame.y = destinationFrame.y * resolution, viewportFrame.width = destinationFrame.width * resolution, viewportFrame.height = destinationFrame.height * resolution, renderTexture || (viewportFrame.y = renderer.view.height - (viewportFrame.y + viewportFrame.height)), viewportFrame.ceil(), this.renderer.framebuffer.bind(framebuffer, viewportFrame), this.renderer.projection.update(destinationFrame, sourceFrame, resolution, !framebuffer), renderTexture ? this.renderer.mask.setMaskStack(baseTexture.maskStack) : this.renderer.mask.setMaskStack(this.defaultMaskStack), this.sourceFrame.copyFrom(sourceFrame), this.destinationFrame.copyFrom(destinationFrame);
29 }
30 /**
31 * Erases the render texture and fills the drawing area with a colour.
32 * @param clearColor - The color as rgba, default to use the renderer backgroundColor
33 * @param [mask=BUFFER_BITS.COLOR | BUFFER_BITS.DEPTH] - Bitwise OR of masks
34 * that indicate the buffers to be cleared, by default COLOR and DEPTH buffers.
35 */
36 clear(clearColor, mask) {
37 const fallbackColor = this.current ? this.current.baseTexture.clear : this.renderer.background.backgroundColor, color = Color.shared.setValue(clearColor || fallbackColor);
38 (this.current && this.current.baseTexture.alphaMode > 0 || !this.current && this._rendererPremultipliedAlpha) && color.premultiply(color.alpha);
39 const destinationFrame = this.destinationFrame, baseFrame = this.current ? this.current.baseTexture : this.renderer._view.screen, clearMask = destinationFrame.width !== baseFrame.width || destinationFrame.height !== baseFrame.height;
40 if (clearMask) {
41 let { x, y, width, height } = this.viewportFrame;
42 x = Math.round(x), y = Math.round(y), width = Math.round(width), height = Math.round(height), this.renderer.gl.enable(this.renderer.gl.SCISSOR_TEST), this.renderer.gl.scissor(x, y, width, height);
43 }
44 this.renderer.framebuffer.clear(color.red, color.green, color.blue, color.alpha, mask), clearMask && this.renderer.scissor.pop();
45 }
46 resize() {
47 this.bind(null);
48 }
49 /** Resets render-texture state. */
50 reset() {
51 this.bind(null);
52 }
53 destroy() {
54 this.renderer = null;
55 }
56}
57RenderTextureSystem.extension = {
58 type: ExtensionType.RendererSystem,
59 name: "renderTexture"
60};
61extensions.add(RenderTextureSystem);
62export {
63 RenderTextureSystem
64};
65//# sourceMappingURL=RenderTextureSystem.mjs.map