UNPKG

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