- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
import { AbstractMaskSystem } from './AbstractMaskSystem';
import type { Renderer } from '../Renderer';
import type { IMaskTarget, MaskData } from './MaskData';
import { settings } from '@pixi/settings';
/**
* System plugin to the renderer to manage stencils (used for masks).
* @memberof PIXI
*/
export class StencilSystem extends AbstractMaskSystem
{
/**
* @param renderer - The renderer this System works for.
*/
constructor(renderer: Renderer)
{
super(renderer);
this.glConst = settings.ADAPTER.getWebGLRenderingContext().STENCIL_TEST;
}
getStackLength(): number
{
const maskData = this.maskStack[this.maskStack.length - 1];
if (maskData)
{
return maskData._stencilCounter;
}
return 0;
}
/**
* Applies the Mask and adds it to the current stencil stack.
* @param maskData - The mask data
*/
push(maskData: MaskData): void
{
const maskObject = maskData.maskObject;
const { gl } = this.renderer;
const prevMaskCount = maskData._stencilCounter;
if (prevMaskCount === 0)
{
// force use stencil texture in current framebuffer
this.renderer.framebuffer.forceStencil();
gl.clearStencil(0);
gl.clear(gl.STENCIL_BUFFER_BIT);
gl.enable(gl.STENCIL_TEST);
}
maskData._stencilCounter++;
const colorMask = maskData._colorMask;
if (colorMask !== 0)
{
maskData._colorMask = 0;
gl.colorMask(false, false, false, false);
}
// Increment the reference stencil value where the new mask overlaps with the old ones.
gl.stencilFunc(gl.EQUAL, prevMaskCount, 0xFFFFFFFF);
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
maskObject.renderable = true;
maskObject.render(this.renderer);
this.renderer.batch.flush();
maskObject.renderable = false;
if (colorMask !== 0)
{
maskData._colorMask = colorMask;
gl.colorMask(
(colorMask & 1) !== 0,
(colorMask & 2) !== 0,
(colorMask & 4) !== 0,
(colorMask & 8) !== 0
);
}
this._useCurrent();
}
/**
* Pops stencil mask. MaskData is already removed from stack
* @param {PIXI.DisplayObject} maskObject - object of popped mask data
*/
pop(maskObject: IMaskTarget): void
{
const gl = this.renderer.gl;
if (this.getStackLength() === 0)
{
// the stack is empty!
gl.disable(gl.STENCIL_TEST);
}
else
{
const maskData = this.maskStack.length !== 0 ? this.maskStack[this.maskStack.length - 1] : null;
const colorMask = maskData ? maskData._colorMask : 0xf;
if (colorMask !== 0)
{
maskData._colorMask = 0;
gl.colorMask(false, false, false, false);
}
// Decrement the reference stencil value where the popped mask overlaps with the other ones
gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR);
maskObject.renderable = true;
maskObject.render(this.renderer);
this.renderer.batch.flush();
maskObject.renderable = false;
if (colorMask !== 0)
{
maskData._colorMask = colorMask;
gl.colorMask(
(colorMask & 0x1) !== 0,
(colorMask & 0x2) !== 0,
(colorMask & 0x4) !== 0,
(colorMask & 0x8) !== 0
);
}
this._useCurrent();
}
}
/**
* Setup renderer to use the current stencil data.
* @private
*/
_useCurrent(): void
{
const gl = this.renderer.gl;
gl.stencilFunc(gl.EQUAL, this.getStackLength(), 0xFFFFFFFF);
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
}
}