1 | "use strict";
|
2 | var constants = require("@pixi/constants"), extensions = require("@pixi/extensions"), SpriteMaskFilter = require("../filters/spriteMask/SpriteMaskFilter.js"), MaskData = require("./MaskData.js");
|
3 | class MaskSystem {
|
4 | |
5 |
|
6 |
|
7 | constructor(renderer) {
|
8 | this.renderer = renderer, this.enableScissor = !0, this.alphaMaskPool = [], this.maskDataPool = [], this.maskStack = [], this.alphaMaskIndex = 0;
|
9 | }
|
10 | |
11 |
|
12 |
|
13 |
|
14 | setMaskStack(maskStack) {
|
15 | this.maskStack = maskStack, this.renderer.scissor.setMaskStack(maskStack), this.renderer.stencil.setMaskStack(maskStack);
|
16 | }
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | push(target, maskDataOrTarget) {
|
25 | let maskData = maskDataOrTarget;
|
26 | if (!maskData.isMaskData) {
|
27 | const d = this.maskDataPool.pop() || new MaskData.MaskData();
|
28 | d.pooled = !0, d.maskObject = maskDataOrTarget, maskData = d;
|
29 | }
|
30 | const maskAbove = this.maskStack.length !== 0 ? this.maskStack[this.maskStack.length - 1] : null;
|
31 | if (maskData.copyCountersOrReset(maskAbove), maskData._colorMask = maskAbove ? maskAbove._colorMask : 15, maskData.autoDetect && this.detect(maskData), maskData._target = target, maskData.type !== constants.MASK_TYPES.SPRITE && this.maskStack.push(maskData), maskData.enabled)
|
32 | switch (maskData.type) {
|
33 | case constants.MASK_TYPES.SCISSOR:
|
34 | this.renderer.scissor.push(maskData);
|
35 | break;
|
36 | case constants.MASK_TYPES.STENCIL:
|
37 | this.renderer.stencil.push(maskData);
|
38 | break;
|
39 | case constants.MASK_TYPES.SPRITE:
|
40 | maskData.copyCountersOrReset(null), this.pushSpriteMask(maskData);
|
41 | break;
|
42 | case constants.MASK_TYPES.COLOR:
|
43 | this.pushColorMask(maskData);
|
44 | break;
|
45 | default:
|
46 | break;
|
47 | }
|
48 | maskData.type === constants.MASK_TYPES.SPRITE && this.maskStack.push(maskData);
|
49 | }
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | pop(target) {
|
57 | const maskData = this.maskStack.pop();
|
58 | if (!(!maskData || maskData._target !== target)) {
|
59 | if (maskData.enabled)
|
60 | switch (maskData.type) {
|
61 | case constants.MASK_TYPES.SCISSOR:
|
62 | this.renderer.scissor.pop(maskData);
|
63 | break;
|
64 | case constants.MASK_TYPES.STENCIL:
|
65 | this.renderer.stencil.pop(maskData.maskObject);
|
66 | break;
|
67 | case constants.MASK_TYPES.SPRITE:
|
68 | this.popSpriteMask(maskData);
|
69 | break;
|
70 | case constants.MASK_TYPES.COLOR:
|
71 | this.popColorMask(maskData);
|
72 | break;
|
73 | default:
|
74 | break;
|
75 | }
|
76 | if (maskData.reset(), maskData.pooled && this.maskDataPool.push(maskData), this.maskStack.length !== 0) {
|
77 | const maskCurrent = this.maskStack[this.maskStack.length - 1];
|
78 | maskCurrent.type === constants.MASK_TYPES.SPRITE && maskCurrent._filters && (maskCurrent._filters[0].maskSprite = maskCurrent.maskObject);
|
79 | }
|
80 | }
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 | detect(maskData) {
|
87 | const maskObject = maskData.maskObject;
|
88 | maskObject ? maskObject.isSprite ? maskData.type = constants.MASK_TYPES.SPRITE : this.enableScissor && this.renderer.scissor.testScissor(maskData) ? maskData.type = constants.MASK_TYPES.SCISSOR : maskData.type = constants.MASK_TYPES.STENCIL : maskData.type = constants.MASK_TYPES.COLOR;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 | pushSpriteMask(maskData) {
|
95 | const { maskObject } = maskData, target = maskData._target;
|
96 | let alphaMaskFilter = maskData._filters;
|
97 | alphaMaskFilter || (alphaMaskFilter = this.alphaMaskPool[this.alphaMaskIndex], alphaMaskFilter || (alphaMaskFilter = this.alphaMaskPool[this.alphaMaskIndex] = [new SpriteMaskFilter.SpriteMaskFilter()])), alphaMaskFilter[0].resolution = maskData.resolution, alphaMaskFilter[0].multisample = maskData.multisample, alphaMaskFilter[0].maskSprite = maskObject;
|
98 | const stashFilterArea = target.filterArea;
|
99 | target.filterArea = maskObject.getBounds(!0), this.renderer.filter.push(target, alphaMaskFilter), target.filterArea = stashFilterArea, maskData._filters || this.alphaMaskIndex++;
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
105 | popSpriteMask(maskData) {
|
106 | this.renderer.filter.pop(), maskData._filters ? maskData._filters[0].maskSprite = null : (this.alphaMaskIndex--, this.alphaMaskPool[this.alphaMaskIndex][0].maskSprite = null);
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 | pushColorMask(maskData) {
|
113 | const currColorMask = maskData._colorMask, nextColorMask = maskData._colorMask = currColorMask & maskData.colorMask;
|
114 | nextColorMask !== currColorMask && this.renderer.gl.colorMask(
|
115 | (nextColorMask & 1) !== 0,
|
116 | (nextColorMask & 2) !== 0,
|
117 | (nextColorMask & 4) !== 0,
|
118 | (nextColorMask & 8) !== 0
|
119 | );
|
120 | }
|
121 | |
122 |
|
123 |
|
124 |
|
125 | popColorMask(maskData) {
|
126 | const currColorMask = maskData._colorMask, nextColorMask = this.maskStack.length > 0 ? this.maskStack[this.maskStack.length - 1]._colorMask : 15;
|
127 | nextColorMask !== currColorMask && this.renderer.gl.colorMask(
|
128 | (nextColorMask & 1) !== 0,
|
129 | (nextColorMask & 2) !== 0,
|
130 | (nextColorMask & 4) !== 0,
|
131 | (nextColorMask & 8) !== 0
|
132 | );
|
133 | }
|
134 | destroy() {
|
135 | this.renderer = null;
|
136 | }
|
137 | }
|
138 | MaskSystem.extension = {
|
139 | type: extensions.ExtensionType.RendererSystem,
|
140 | name: "mask"
|
141 | };
|
142 | extensions.extensions.add(MaskSystem);
|
143 | exports.MaskSystem = MaskSystem;
|
144 |
|