Skip to content

Commit bc4b5f8

Browse files
committed
Added internal functions and some logic changes
1 parent 38abe63 commit bc4b5f8

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

src/webgl/p5.RendererGL.js

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -428,41 +428,37 @@ class RendererGL extends Renderer {
428428

429429
this.scratchMat3 = new Matrix(3);
430430

431-
this.isStencilTestOn = false; // Track stencil test state
432-
this._userEnabledStencil = false; // Track whether user enabled stencil
431+
this._userEnabledStencil = false;
432+
this.isStencilTestOn = false;
433+
// Store original methods for internal use
434+
this._internalEnable = this.drawingContext.enable;
435+
this._internalDisable = this.drawingContext.disable;
436+
433437
// Override WebGL enable function
434-
const prevEnable = this.drawingContext.enable;
435438
this.drawingContext.enable = (key) => {
436-
if (key === this.drawingContext.STENCIL_TEST) {
437-
if (!this._clipping) {
438-
this._userEnabledStencil = true;
439-
}
440-
this.isStencilTestOn = true;
439+
// When enable is called with STENCIL_TEST
440+
if (key === this.drawingContext.STENCIL_TEST) {
441+
// If not during clip(), mark as user-enabled
442+
if (!this._clipping) {
443+
this._userEnabledStencil = true;
441444
}
442-
return prevEnable.call(this.drawingContext, key);
445+
}
446+
return this._internalEnable.call(this.drawingContext, key);
443447
};
444448

445449
// Override WebGL disable function
446-
const prevDisable = this.drawingContext.disable;
447450
this.drawingContext.disable = (key) => {
448-
if (key === this.drawingContext.STENCIL_TEST) {
449-
if (this._clipDepth === this._pushPopDepth) {
450-
this.isStencilTestOn = this._userEnabledStencil;
451-
} else {
452-
this.isStencilTestOn = false;
453-
this._userEnabledStencil = false;
454-
}
455-
}
456-
return prevDisable.call(this.drawingContext, key);
457-
};
458-
459-
// Override WebGL getEnabled function
460-
const prevGetEnabled = this.drawingContext.getEnabled;
461-
this.drawingContext.getEnabled = (key) => {
462-
if (key === this.drawingContext.STENCIL_TEST) {
463-
return this.isStencilTestOn;
451+
if (key === this.drawingContext.STENCIL_TEST) {
452+
// When pop() disables the stencil test after clip(),
453+
// preserve the user's stencil test setting
454+
if (this._clipDepth === this._pushPopDepth) {
455+
// Don't change user setting here, just use internal disable
456+
} else {
457+
// User explicitly disabled stencil test
458+
this._userEnabledStencil = false;
464459
}
465-
return prevGetEnabled.call(this.drawingContext, key);
460+
}
461+
return this._internalDisable.call(this.drawingContext, key);
466462
};
467463
}
468464

@@ -1061,8 +1057,8 @@ class RendererGL extends Renderer {
10611057
//Clear depth every frame
10621058
this.GL.clearStencil(0);
10631059
this.GL.clear(this.GL.DEPTH_BUFFER_BIT | this.GL.STENCIL_BUFFER_BIT);
1064-
if (!this.isStencilTestOn) {
1065-
this.GL.disable(this.GL.STENCIL_TEST);
1060+
if (!this._userEnabledStencil) {
1061+
this._internalDisable.call(this.GL, this.GL.STENCIL_TEST);
10661062
}
10671063

10681064
}
@@ -1427,9 +1423,6 @@ class RendererGL extends Renderer {
14271423
this.drawTarget()._isClipApplied = true;
14281424

14291425
const gl = this.GL;
1430-
this._savedStencilTestState = this._userEnabledStencil;
1431-
this._preClipStencilState = this.drawingContext.getEnabled(this.drawingContext.STENCIL_TEST);
1432-
// this.isStencilTestOn = this.drawingContext.getEnabled(this.drawingContext.STENCIL_TEST);
14331426
gl.clearStencil(0);
14341427
gl.clear(gl.STENCIL_BUFFER_BIT);
14351428
gl.enable(gl.STENCIL_TEST);
@@ -1782,22 +1775,12 @@ class RendererGL extends Renderer {
17821775
this._pushPopDepth === this._clipDepths[this._clipDepths.length - 1]
17831776
) {
17841777
this._clearClip();
1785-
// if (this.isStencilTestOn) {
1786-
// this.GL.enable(this.GL.STENCIL_TEST);
1787-
// } else {
1788-
// this.GL.disable(this.GL.STENCIL_TEST);
1789-
// }
1790-
// if (!this._savedStencilTestState) {
1791-
// this.GL.disable(this.GL.STENCIL_TEST);
1792-
// }
1793-
if (this._preClipStencilState) {
1794-
this.GL.enable(this.GL.STENCIL_TEST);
1795-
} else {
1796-
this.GL.disable(this.GL.STENCIL_TEST);
1778+
if (!this._userEnabledStencil) {
1779+
this._internalDisable.call(this.GL, this.GL.STENCIL_TEST);
17971780
}
17981781

17991782
// Reset saved state
1800-
this._userEnabledStencil = this._savedStencilTestState;
1783+
// this._userEnabledStencil = this._savedStencilTestState;
18011784
}
18021785
super.pop(...args);
18031786
this._applyStencilTestIfClipping();
@@ -1809,9 +1792,9 @@ class RendererGL extends Renderer {
18091792
this.GL.enable(this.GL.STENCIL_TEST);
18101793
this._stencilTestOn = true;
18111794
} else {
1812-
if (!this.isStencilTestOn) {
1813-
this.GL.disable(this.GL.STENCIL_TEST);
1814-
}
1795+
if (!this._userEnabledStencil) {
1796+
this._internalDisable.call(this.GL, this.GL.STENCIL_TEST);
1797+
}
18151798
this._stencilTestOn = false;
18161799
}
18171800
}

0 commit comments

Comments
 (0)