Skip to content

Commit b42d2a1

Browse files
committed
Updated Stencil Test Case to prevent it from disable after every draw loop
1 parent 11196fd commit b42d2a1

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

src/webgl/p5.RendererGL.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,46 @@ class RendererGL extends Renderer {
427427
this.drawShapeCount = 1;
428428

429429
this.scratchMat3 = new Matrix(3);
430+
431+
this.isStencilTestOn = false; // Track stencil test state
432+
this._userEnabledStencil = false; // Track whether user enabled stencil
433+
// Override WebGL enable function
434+
const prevEnable = this.drawingContext.enable;
435+
this.drawingContext.enable = (key) => {
436+
if (key === this.drawingContext.STENCIL_TEST) {
437+
// When clip() calls enable(), don't mark as user-enabled
438+
if (!this._clipping) {
439+
this._userEnabledStencil = true;
440+
}
441+
this.isStencilTestOn = true;
442+
}
443+
return prevEnable.call(this.drawingContext, key);
444+
};
445+
446+
// Override WebGL disable function
447+
const prevDisable = this.drawingContext.disable;
448+
this.drawingContext.disable = (key) => {
449+
if (key === this.drawingContext.STENCIL_TEST) {
450+
// When pop() disables the stencil test after clip(),
451+
// restore the user's stencil test setting
452+
if (this._clipDepth === this._pushPopDepth) {
453+
this.isStencilTestOn = this._userEnabledStencil;
454+
} else {
455+
this.isStencilTestOn = false;
456+
this._userEnabledStencil = false;
457+
}
458+
}
459+
return prevDisable.call(this.drawingContext, key);
460+
};
461+
462+
// Override WebGL getEnabled function
463+
const prevGetEnabled = this.drawingContext.getEnabled;
464+
this.drawingContext.getEnabled = (key) => {
465+
if (key === this.drawingContext.STENCIL_TEST) {
466+
return this.isStencilTestOn;
467+
}
468+
return prevGetEnabled.call(this.drawingContext, key);
469+
};
430470
}
431471

432472
//////////////////////////////////////////////
@@ -1024,7 +1064,10 @@ class RendererGL extends Renderer {
10241064
//Clear depth every frame
10251065
this.GL.clearStencil(0);
10261066
this.GL.clear(this.GL.DEPTH_BUFFER_BIT | this.GL.STENCIL_BUFFER_BIT);
1027-
this.GL.disable(this.GL.STENCIL_TEST);
1067+
if (!this.isStencilTestOn) {
1068+
this.GL.disable(this.GL.STENCIL_TEST);
1069+
}
1070+
10281071
}
10291072

10301073
/**
@@ -1387,7 +1430,8 @@ class RendererGL extends Renderer {
13871430
this.drawTarget()._isClipApplied = true;
13881431

13891432
const gl = this.GL;
1390-
gl.clearStencil(0);
1433+
this._savedStencilTestState = this._userEnabledStencil;
1434+
gl.clearStencil(0);
13911435
gl.clear(gl.STENCIL_BUFFER_BIT);
13921436
gl.enable(gl.STENCIL_TEST);
13931437
this._stencilTestOn = true;
@@ -1739,6 +1783,11 @@ class RendererGL extends Renderer {
17391783
this._pushPopDepth === this._clipDepths[this._clipDepths.length - 1]
17401784
) {
17411785
this._clearClip();
1786+
if (!this._savedStencilTestState) {
1787+
this.GL.disable(this.GL.STENCIL_TEST);
1788+
}
1789+
1790+
this._userEnabledStencil = this._savedStencilTestState;
17421791
}
17431792
super.pop(...args);
17441793
this._applyStencilTestIfClipping();
@@ -1750,7 +1799,9 @@ class RendererGL extends Renderer {
17501799
this.GL.enable(this.GL.STENCIL_TEST);
17511800
this._stencilTestOn = true;
17521801
} else {
1753-
this.GL.disable(this.GL.STENCIL_TEST);
1802+
if (!this.isStencilTestOn) {
1803+
this.GL.disable(this.GL.STENCIL_TEST);
1804+
}
17541805
this._stencilTestOn = false;
17551806
}
17561807
}

0 commit comments

Comments
 (0)