@@ -428,41 +428,37 @@ class RendererGL extends Renderer {
428
428
429
429
this . scratchMat3 = new Matrix ( 3 ) ;
430
430
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
+
433
437
// Override WebGL enable function
434
- const prevEnable = this . drawingContext . enable ;
435
438
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 ;
441
444
}
442
- return prevEnable . call ( this . drawingContext , key ) ;
445
+ }
446
+ return this . _internalEnable . call ( this . drawingContext , key ) ;
443
447
} ;
444
448
445
449
// Override WebGL disable function
446
- const prevDisable = this . drawingContext . disable ;
447
450
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 ;
464
459
}
465
- return prevGetEnabled . call ( this . drawingContext , key ) ;
460
+ }
461
+ return this . _internalDisable . call ( this . drawingContext , key ) ;
466
462
} ;
467
463
}
468
464
@@ -1061,8 +1057,8 @@ class RendererGL extends Renderer {
1061
1057
//Clear depth every frame
1062
1058
this . GL . clearStencil ( 0 ) ;
1063
1059
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 ) ;
1066
1062
}
1067
1063
1068
1064
}
@@ -1427,9 +1423,6 @@ class RendererGL extends Renderer {
1427
1423
this . drawTarget ( ) . _isClipApplied = true ;
1428
1424
1429
1425
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);
1433
1426
gl . clearStencil ( 0 ) ;
1434
1427
gl . clear ( gl . STENCIL_BUFFER_BIT ) ;
1435
1428
gl . enable ( gl . STENCIL_TEST ) ;
@@ -1782,22 +1775,12 @@ class RendererGL extends Renderer {
1782
1775
this . _pushPopDepth === this . _clipDepths [ this . _clipDepths . length - 1 ]
1783
1776
) {
1784
1777
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 ) ;
1797
1780
}
1798
1781
1799
1782
// Reset saved state
1800
- this . _userEnabledStencil = this . _savedStencilTestState ;
1783
+ // this._userEnabledStencil = this._savedStencilTestState;
1801
1784
}
1802
1785
super . pop ( ...args ) ;
1803
1786
this . _applyStencilTestIfClipping ( ) ;
@@ -1809,9 +1792,9 @@ class RendererGL extends Renderer {
1809
1792
this . GL . enable ( this . GL . STENCIL_TEST ) ;
1810
1793
this . _stencilTestOn = true ;
1811
1794
} 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
+ }
1815
1798
this . _stencilTestOn = false ;
1816
1799
}
1817
1800
}
0 commit comments