UNPKG

2.86 kBJavaScriptView Raw
1import { ExtensionType, extensions } from "@pixi/extensions";
2import { settings } from "@pixi/settings";
3import { AbstractMaskSystem } from "./AbstractMaskSystem.mjs";
4class StencilSystem extends AbstractMaskSystem {
5 /**
6 * @param renderer - The renderer this System works for.
7 */
8 constructor(renderer) {
9 super(renderer), this.glConst = settings.ADAPTER.getWebGLRenderingContext().STENCIL_TEST;
10 }
11 getStackLength() {
12 const maskData = this.maskStack[this.maskStack.length - 1];
13 return maskData ? maskData._stencilCounter : 0;
14 }
15 /**
16 * Applies the Mask and adds it to the current stencil stack.
17 * @param maskData - The mask data
18 */
19 push(maskData) {
20 const maskObject = maskData.maskObject, { gl } = this.renderer, prevMaskCount = maskData._stencilCounter;
21 prevMaskCount === 0 && (this.renderer.framebuffer.forceStencil(), gl.clearStencil(0), gl.clear(gl.STENCIL_BUFFER_BIT), gl.enable(gl.STENCIL_TEST)), maskData._stencilCounter++;
22 const colorMask = maskData._colorMask;
23 colorMask !== 0 && (maskData._colorMask = 0, gl.colorMask(!1, !1, !1, !1)), gl.stencilFunc(gl.EQUAL, prevMaskCount, 4294967295), gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR), maskObject.renderable = !0, maskObject.render(this.renderer), this.renderer.batch.flush(), maskObject.renderable = !1, colorMask !== 0 && (maskData._colorMask = colorMask, gl.colorMask(
24 (colorMask & 1) !== 0,
25 (colorMask & 2) !== 0,
26 (colorMask & 4) !== 0,
27 (colorMask & 8) !== 0
28 )), this._useCurrent();
29 }
30 /**
31 * Pops stencil mask. MaskData is already removed from stack
32 * @param {PIXI.DisplayObject} maskObject - object of popped mask data
33 */
34 pop(maskObject) {
35 const gl = this.renderer.gl;
36 if (this.getStackLength() === 0)
37 gl.disable(gl.STENCIL_TEST);
38 else {
39 const maskData = this.maskStack.length !== 0 ? this.maskStack[this.maskStack.length - 1] : null, colorMask = maskData ? maskData._colorMask : 15;
40 colorMask !== 0 && (maskData._colorMask = 0, gl.colorMask(!1, !1, !1, !1)), gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR), maskObject.renderable = !0, maskObject.render(this.renderer), this.renderer.batch.flush(), maskObject.renderable = !1, colorMask !== 0 && (maskData._colorMask = colorMask, gl.colorMask(
41 (colorMask & 1) !== 0,
42 (colorMask & 2) !== 0,
43 (colorMask & 4) !== 0,
44 (colorMask & 8) !== 0
45 )), this._useCurrent();
46 }
47 }
48 /**
49 * Setup renderer to use the current stencil data.
50 * @private
51 */
52 _useCurrent() {
53 const gl = this.renderer.gl;
54 gl.stencilFunc(gl.EQUAL, this.getStackLength(), 4294967295), gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
55 }
56}
57StencilSystem.extension = {
58 type: ExtensionType.RendererSystem,
59 name: "stencil"
60};
61extensions.add(StencilSystem);
62export {
63 StencilSystem
64};
65//# sourceMappingURL=StencilSystem.mjs.map