1 | import { Color } from "@pixi/color";
|
2 | import { ExtensionType, extensions } from "@pixi/extensions";
|
3 | import { Rectangle } from "@pixi/math";
|
4 | const tempRect = new Rectangle(), tempRect2 = new Rectangle();
|
5 | class RenderTextureSystem {
|
6 | |
7 |
|
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 |
|
18 |
|
19 |
|
20 |
|
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 |
|
32 |
|
33 |
|
34 |
|
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 |
|
50 | reset() {
|
51 | this.bind(null);
|
52 | }
|
53 | destroy() {
|
54 | this.renderer = null;
|
55 | }
|
56 | }
|
57 | RenderTextureSystem.extension = {
|
58 | type: ExtensionType.RendererSystem,
|
59 | name: "renderTexture"
|
60 | };
|
61 | extensions.add(RenderTextureSystem);
|
62 | export {
|
63 | RenderTextureSystem
|
64 | };
|
65 |
|