@@ -429,27 +429,29 @@ class RendererGL extends Renderer {
429
429
this . scratchMat3 = new Matrix ( 3 ) ;
430
430
431
431
this . isStencilTestOn = false ; // Track stencil test state
432
+ this . _userEnabledStencil = false ; // Track whether user enabled stencil
433
+ // Override WebGL enable function
432
434
const prevEnable = this . drawingContext . enable ;
433
435
this . drawingContext . enable = ( key ) => {
434
- if ( key === this . drawingContext . STENCIL_TEST ) {
435
- this . isStencilTestOn = true ;
436
- }
437
- return prevEnable . call ( this . drawingContext , key ) ;
436
+ if ( key === this . drawingContext . STENCIL_TEST ) {
437
+ if ( ! this . _clipping ) {
438
+ this . _userEnabledStencil = true ;
439
+ }
440
+ this . isStencilTestOn = true ;
441
+ }
442
+ return prevEnable . call ( this . drawingContext , key ) ;
438
443
} ;
439
444
440
445
// Override WebGL disable function
441
446
const prevDisable = this . drawingContext . disable ;
442
447
this . drawingContext . disable = ( key ) => {
443
448
if ( key === this . drawingContext . STENCIL_TEST ) {
444
- // When pop() disables the stencil test after clip(),
445
- // restore the user's stencil test setting
446
- // if (this._clipDepth === this._pushPopDepth) {
447
- // this.isStencilTestOn = this._userEnabledStencil;
448
- // } else {
449
- // this.isStencilTestOn = false;
450
- // this._userEnabledStencil = false;
451
- // }
452
- this . isStencilTestOn = false ;
449
+ if ( this . _clipDepth === this . _pushPopDepth ) {
450
+ this . isStencilTestOn = this . _userEnabledStencil ;
451
+ } else {
452
+ this . isStencilTestOn = false ;
453
+ this . _userEnabledStencil = false ;
454
+ }
453
455
}
454
456
return prevDisable . call ( this . drawingContext , key ) ;
455
457
} ;
@@ -1425,7 +1427,9 @@ class RendererGL extends Renderer {
1425
1427
this . drawTarget ( ) . _isClipApplied = true ;
1426
1428
1427
1429
const gl = this . GL ;
1428
- this . isStencilTestOn = this . drawingContext . getEnabled ( this . drawingContext . STENCIL_TEST ) ;
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);
1429
1433
gl . clearStencil ( 0 ) ;
1430
1434
gl . clear ( gl . STENCIL_BUFFER_BIT ) ;
1431
1435
gl . enable ( gl . STENCIL_TEST ) ;
@@ -1778,11 +1782,22 @@ class RendererGL extends Renderer {
1778
1782
this . _pushPopDepth === this . _clipDepths [ this . _clipDepths . length - 1 ]
1779
1783
) {
1780
1784
this . _clearClip ( ) ;
1781
- if ( this . isStencilTestOn ) {
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 ) {
1782
1794
this . GL . enable ( this . GL . STENCIL_TEST ) ;
1783
1795
} else {
1784
1796
this . GL . disable ( this . GL . STENCIL_TEST ) ;
1785
1797
}
1798
+
1799
+ // Reset saved state
1800
+ this . _userEnabledStencil = this . _savedStencilTestState ;
1786
1801
}
1787
1802
super . pop ( ...args ) ;
1788
1803
this . _applyStencilTestIfClipping ( ) ;
0 commit comments