| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import { Constants } from "../Engines/constants";
- /**
- * @hidden
- **/
- export class StencilState {
- /** Passed to depthFunction or stencilFunction to specify depth or stencil tests will always pass. i.e. Pixels will be drawn in the order they are drawn */
- public static readonly ALWAYS = Constants.ALWAYS;
- /** Passed to stencilOperation to specify that stencil value must be kept */
- public static readonly KEEP = Constants.KEEP;
- /** Passed to stencilOperation to specify that stencil value must be replaced */
- public static readonly REPLACE = Constants.REPLACE;
- private _isStencilTestDirty = false;
- private _isStencilMaskDirty = false;
- private _isStencilFuncDirty = false;
- private _isStencilOpDirty = false;
- private _stencilTest: boolean;
- private _stencilMask: number;
- private _stencilFunc: number;
- private _stencilFuncRef: number;
- private _stencilFuncMask: number;
- private _stencilOpStencilFail: number;
- private _stencilOpDepthFail: number;
- private _stencilOpStencilDepthPass: number;
- public get isDirty(): boolean {
- return this._isStencilTestDirty || this._isStencilMaskDirty || this._isStencilFuncDirty || this._isStencilOpDirty;
- }
- public get stencilFunc(): number {
- return this._stencilFunc;
- }
- public set stencilFunc(value: number) {
- if (this._stencilFunc === value) {
- return;
- }
- this._stencilFunc = value;
- this._isStencilFuncDirty = true;
- }
- public get stencilFuncRef(): number {
- return this._stencilFuncRef;
- }
- public set stencilFuncRef(value: number) {
- if (this._stencilFuncRef === value) {
- return;
- }
- this._stencilFuncRef = value;
- this._isStencilFuncDirty = true;
- }
- public get stencilFuncMask(): number {
- return this._stencilFuncMask;
- }
- public set stencilFuncMask(value: number) {
- if (this._stencilFuncMask === value) {
- return;
- }
- this._stencilFuncMask = value;
- this._isStencilFuncDirty = true;
- }
- public get stencilOpStencilFail(): number {
- return this._stencilOpStencilFail;
- }
- public set stencilOpStencilFail(value: number) {
- if (this._stencilOpStencilFail === value) {
- return;
- }
- this._stencilOpStencilFail = value;
- this._isStencilOpDirty = true;
- }
- public get stencilOpDepthFail(): number {
- return this._stencilOpDepthFail;
- }
- public set stencilOpDepthFail(value: number) {
- if (this._stencilOpDepthFail === value) {
- return;
- }
- this._stencilOpDepthFail = value;
- this._isStencilOpDirty = true;
- }
- public get stencilOpStencilDepthPass(): number {
- return this._stencilOpStencilDepthPass;
- }
- public set stencilOpStencilDepthPass(value: number) {
- if (this._stencilOpStencilDepthPass === value) {
- return;
- }
- this._stencilOpStencilDepthPass = value;
- this._isStencilOpDirty = true;
- }
- public get stencilMask(): number {
- return this._stencilMask;
- }
- public set stencilMask(value: number) {
- if (this._stencilMask === value) {
- return;
- }
- this._stencilMask = value;
- this._isStencilMaskDirty = true;
- }
- public get stencilTest(): boolean {
- return this._stencilTest;
- }
- public set stencilTest(value: boolean) {
- if (this._stencilTest === value) {
- return;
- }
- this._stencilTest = value;
- this._isStencilTestDirty = true;
- }
- public constructor() {
- this.reset();
- }
- public reset() {
- this._stencilTest = false;
- this._stencilMask = 0xFF;
- this._stencilFunc = StencilState.ALWAYS;
- this._stencilFuncRef = 1;
- this._stencilFuncMask = 0xFF;
- this._stencilOpStencilFail = StencilState.KEEP;
- this._stencilOpDepthFail = StencilState.KEEP;
- this._stencilOpStencilDepthPass = StencilState.REPLACE;
- this._isStencilTestDirty = true;
- this._isStencilMaskDirty = true;
- this._isStencilFuncDirty = true;
- this._isStencilOpDirty = true;
- }
- public apply(gl: WebGLRenderingContext) {
- if (!this.isDirty) {
- return;
- }
- // Stencil test
- if (this._isStencilTestDirty) {
- if (this.stencilTest) {
- gl.enable(gl.STENCIL_TEST);
- } else {
- gl.disable(gl.STENCIL_TEST);
- }
- this._isStencilTestDirty = false;
- }
- // Stencil mask
- if (this._isStencilMaskDirty) {
- gl.stencilMask(this.stencilMask);
- this._isStencilMaskDirty = false;
- }
- // Stencil func
- if (this._isStencilFuncDirty) {
- gl.stencilFunc(this.stencilFunc, this.stencilFuncRef, this.stencilFuncMask);
- this._isStencilFuncDirty = false;
- }
- // Stencil op
- if (this._isStencilOpDirty) {
- gl.stencilOp(this.stencilOpStencilFail, this.stencilOpDepthFail, this.stencilOpStencilDepthPass);
- this._isStencilOpDirty = false;
- }
- }
- }
|