@@ -427,6 +427,46 @@ class RendererGL extends Renderer {
427
427
this . drawShapeCount = 1 ;
428
428
429
429
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
+ } ;
430
470
}
431
471
432
472
//////////////////////////////////////////////
@@ -1024,7 +1064,10 @@ class RendererGL extends Renderer {
1024
1064
//Clear depth every frame
1025
1065
this . GL . clearStencil ( 0 ) ;
1026
1066
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
+
1028
1071
}
1029
1072
1030
1073
/**
@@ -1387,7 +1430,8 @@ class RendererGL extends Renderer {
1387
1430
this . drawTarget ( ) . _isClipApplied = true ;
1388
1431
1389
1432
const gl = this . GL ;
1390
- gl . clearStencil ( 0 ) ;
1433
+ this . _savedStencilTestState = this . _userEnabledStencil ;
1434
+ gl . clearStencil ( 0 ) ;
1391
1435
gl . clear ( gl . STENCIL_BUFFER_BIT ) ;
1392
1436
gl . enable ( gl . STENCIL_TEST ) ;
1393
1437
this . _stencilTestOn = true ;
@@ -1739,6 +1783,11 @@ class RendererGL extends Renderer {
1739
1783
this . _pushPopDepth === this . _clipDepths [ this . _clipDepths . length - 1 ]
1740
1784
) {
1741
1785
this . _clearClip ( ) ;
1786
+ if ( ! this . _savedStencilTestState ) {
1787
+ this . GL . disable ( this . GL . STENCIL_TEST ) ;
1788
+ }
1789
+
1790
+ this . _userEnabledStencil = this . _savedStencilTestState ;
1742
1791
}
1743
1792
super . pop ( ...args ) ;
1744
1793
this . _applyStencilTestIfClipping ( ) ;
@@ -1750,7 +1799,9 @@ class RendererGL extends Renderer {
1750
1799
this . GL . enable ( this . GL . STENCIL_TEST ) ;
1751
1800
this . _stencilTestOn = true ;
1752
1801
} else {
1753
- this . GL . disable ( this . GL . STENCIL_TEST ) ;
1802
+ if ( ! this . isStencilTestOn ) {
1803
+ this . GL . disable ( this . GL . STENCIL_TEST ) ;
1804
+ }
1754
1805
this . _stencilTestOn = false ;
1755
1806
}
1756
1807
}
0 commit comments