1 | import { ExtensionType, extensions } from "@pixi/extensions";
|
2 | import { settings } from "@pixi/settings";
|
3 | import { AbstractMaskSystem } from "./AbstractMaskSystem.mjs";
|
4 | class StencilSystem extends AbstractMaskSystem {
|
5 | |
6 |
|
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 |
|
17 |
|
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 |
|
32 |
|
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 |
|
50 |
|
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 | }
|
57 | StencilSystem.extension = {
|
58 | type: ExtensionType.RendererSystem,
|
59 | name: "stencil"
|
60 | };
|
61 | extensions.add(StencilSystem);
|
62 | export {
|
63 | StencilSystem
|
64 | };
|
65 |
|