UNPKG

3.98 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var color = require('@pixi/color');
6var extensions = require('@pixi/extensions');
7var math = require('@pixi/math');
8
9const tempRect = new math.Rectangle();
10const tempRect2 = new math.Rectangle();
11class RenderTextureSystem {
12 constructor(renderer) {
13 this.renderer = renderer;
14 this.defaultMaskStack = [];
15 this.current = null;
16 this.sourceFrame = new math.Rectangle();
17 this.destinationFrame = new math.Rectangle();
18 this.viewportFrame = new math.Rectangle();
19 }
20 bind(renderTexture = null, sourceFrame, destinationFrame) {
21 const renderer = this.renderer;
22 this.current = renderTexture;
23 let baseTexture;
24 let framebuffer;
25 let resolution;
26 if (renderTexture) {
27 baseTexture = renderTexture.baseTexture;
28 resolution = baseTexture.resolution;
29 if (!sourceFrame) {
30 tempRect.width = renderTexture.frame.width;
31 tempRect.height = renderTexture.frame.height;
32 sourceFrame = tempRect;
33 }
34 if (!destinationFrame) {
35 tempRect2.x = renderTexture.frame.x;
36 tempRect2.y = renderTexture.frame.y;
37 tempRect2.width = sourceFrame.width;
38 tempRect2.height = sourceFrame.height;
39 destinationFrame = tempRect2;
40 }
41 framebuffer = baseTexture.framebuffer;
42 } else {
43 resolution = renderer.resolution;
44 if (!sourceFrame) {
45 tempRect.width = renderer._view.screen.width;
46 tempRect.height = renderer._view.screen.height;
47 sourceFrame = tempRect;
48 }
49 if (!destinationFrame) {
50 destinationFrame = tempRect;
51 destinationFrame.width = sourceFrame.width;
52 destinationFrame.height = sourceFrame.height;
53 }
54 }
55 const viewportFrame = this.viewportFrame;
56 viewportFrame.x = destinationFrame.x * resolution;
57 viewportFrame.y = destinationFrame.y * resolution;
58 viewportFrame.width = destinationFrame.width * resolution;
59 viewportFrame.height = destinationFrame.height * resolution;
60 if (!renderTexture) {
61 viewportFrame.y = renderer.view.height - (viewportFrame.y + viewportFrame.height);
62 }
63 viewportFrame.ceil();
64 this.renderer.framebuffer.bind(framebuffer, viewportFrame);
65 this.renderer.projection.update(destinationFrame, sourceFrame, resolution, !framebuffer);
66 if (renderTexture) {
67 this.renderer.mask.setMaskStack(baseTexture.maskStack);
68 } else {
69 this.renderer.mask.setMaskStack(this.defaultMaskStack);
70 }
71 this.sourceFrame.copyFrom(sourceFrame);
72 this.destinationFrame.copyFrom(destinationFrame);
73 }
74 clear(clearColor, mask) {
75 const fallbackColor = this.current ? this.current.baseTexture.clear : this.renderer.background.backgroundColor;
76 const color$1 = clearColor ? color.Color.shared.setValue(clearColor) : fallbackColor;
77 const destinationFrame = this.destinationFrame;
78 const baseFrame = this.current ? this.current.baseTexture : this.renderer._view.screen;
79 const clearMask = destinationFrame.width !== baseFrame.width || destinationFrame.height !== baseFrame.height;
80 if (clearMask) {
81 let { x, y, width, height } = this.viewportFrame;
82 x = Math.round(x);
83 y = Math.round(y);
84 width = Math.round(width);
85 height = Math.round(height);
86 this.renderer.gl.enable(this.renderer.gl.SCISSOR_TEST);
87 this.renderer.gl.scissor(x, y, width, height);
88 }
89 this.renderer.framebuffer.clear(color$1.red, color$1.green, color$1.blue, color$1.alpha, mask);
90 if (clearMask) {
91 this.renderer.scissor.pop();
92 }
93 }
94 resize() {
95 this.bind(null);
96 }
97 reset() {
98 this.bind(null);
99 }
100 destroy() {
101 this.renderer = null;
102 }
103}
104RenderTextureSystem.extension = {
105 type: extensions.ExtensionType.RendererSystem,
106 name: "renderTexture"
107};
108extensions.extensions.add(RenderTextureSystem);
109
110exports.RenderTextureSystem = RenderTextureSystem;
111//# sourceMappingURL=RenderTextureSystem.js.map