UNPKG

4.57 kBJavaScriptView Raw
1"use strict";
2var extensions = require("@pixi/extensions"), math = require("@pixi/math"), settings = require("@pixi/settings"), AbstractMaskSystem = require("./AbstractMaskSystem.js");
3const tempMatrix = new math.Matrix(), rectPool = [], _ScissorSystem = class _ScissorSystem2 extends AbstractMaskSystem.AbstractMaskSystem {
4 /**
5 * @param {PIXI.Renderer} renderer - The renderer this System works for.
6 */
7 constructor(renderer) {
8 super(renderer), this.glConst = settings.settings.ADAPTER.getWebGLRenderingContext().SCISSOR_TEST;
9 }
10 getStackLength() {
11 const maskData = this.maskStack[this.maskStack.length - 1];
12 return maskData ? maskData._scissorCounter : 0;
13 }
14 /**
15 * evaluates _boundsTransformed, _scissorRect for MaskData
16 * @param maskData
17 */
18 calcScissorRect(maskData) {
19 if (maskData._scissorRectLocal)
20 return;
21 const prevData = maskData._scissorRect, { maskObject } = maskData, { renderer } = this, renderTextureSystem = renderer.renderTexture, rect = maskObject.getBounds(!0, rectPool.pop() ?? new math.Rectangle());
22 this.roundFrameToPixels(
23 rect,
24 renderTextureSystem.current ? renderTextureSystem.current.resolution : renderer.resolution,
25 renderTextureSystem.sourceFrame,
26 renderTextureSystem.destinationFrame,
27 renderer.projection.transform
28 ), prevData && rect.fit(prevData), maskData._scissorRectLocal = rect;
29 }
30 static isMatrixRotated(matrix) {
31 if (!matrix)
32 return !1;
33 const { a, b, c, d } = matrix;
34 return (Math.abs(b) > 1e-4 || Math.abs(c) > 1e-4) && (Math.abs(a) > 1e-4 || Math.abs(d) > 1e-4);
35 }
36 /**
37 * Test, whether the object can be scissor mask with current renderer projection.
38 * Calls "calcScissorRect()" if its true.
39 * @param maskData - mask data
40 * @returns whether Whether the object can be scissor mask
41 */
42 testScissor(maskData) {
43 const { maskObject } = maskData;
44 if (!maskObject.isFastRect || !maskObject.isFastRect() || _ScissorSystem2.isMatrixRotated(maskObject.worldTransform) || _ScissorSystem2.isMatrixRotated(this.renderer.projection.transform))
45 return !1;
46 this.calcScissorRect(maskData);
47 const rect = maskData._scissorRectLocal;
48 return rect.width > 0 && rect.height > 0;
49 }
50 roundFrameToPixels(frame, resolution, bindingSourceFrame, bindingDestinationFrame, transform) {
51 _ScissorSystem2.isMatrixRotated(transform) || (transform = transform ? tempMatrix.copyFrom(transform) : tempMatrix.identity(), transform.translate(-bindingSourceFrame.x, -bindingSourceFrame.y).scale(
52 bindingDestinationFrame.width / bindingSourceFrame.width,
53 bindingDestinationFrame.height / bindingSourceFrame.height
54 ).translate(bindingDestinationFrame.x, bindingDestinationFrame.y), this.renderer.filter.transformAABB(transform, frame), frame.fit(bindingDestinationFrame), frame.x = Math.round(frame.x * resolution), frame.y = Math.round(frame.y * resolution), frame.width = Math.round(frame.width * resolution), frame.height = Math.round(frame.height * resolution));
55 }
56 /**
57 * Applies the Mask and adds it to the current stencil stack.
58 * @author alvin
59 * @param maskData - The mask data.
60 */
61 push(maskData) {
62 maskData._scissorRectLocal || this.calcScissorRect(maskData);
63 const { gl } = this.renderer;
64 maskData._scissorRect || gl.enable(gl.SCISSOR_TEST), maskData._scissorCounter++, maskData._scissorRect = maskData._scissorRectLocal, this._useCurrent();
65 }
66 /**
67 * This should be called after a mask is popped off the mask stack. It will rebind the scissor box to be latest with the
68 * last mask in the stack.
69 *
70 * This can also be called when you directly modify the scissor box and want to restore PixiJS state.
71 * @param maskData - The mask data.
72 */
73 pop(maskData) {
74 const { gl } = this.renderer;
75 maskData && rectPool.push(maskData._scissorRectLocal), this.getStackLength() > 0 ? this._useCurrent() : gl.disable(gl.SCISSOR_TEST);
76 }
77 /**
78 * Setup renderer to use the current scissor data.
79 * @private
80 */
81 _useCurrent() {
82 const rect = this.maskStack[this.maskStack.length - 1]._scissorRect;
83 let y;
84 this.renderer.renderTexture.current ? y = rect.y : y = this.renderer.height - rect.height - rect.y, this.renderer.gl.scissor(rect.x, y, rect.width, rect.height);
85 }
86};
87_ScissorSystem.extension = {
88 type: extensions.ExtensionType.RendererSystem,
89 name: "scissor"
90};
91let ScissorSystem = _ScissorSystem;
92extensions.extensions.add(ScissorSystem);
93exports.ScissorSystem = ScissorSystem;
94//# sourceMappingURL=ScissorSystem.js.map