@@ -998,6 +998,22 @@ var WebGLRenderer = new Class({
998998 return this . currentPipeline ;
999999 } ,
10001000
1001+ /**
1002+ * Is there an active stencil mask?
1003+ *
1004+ * @method Phaser.Renderer.WebGL.WebGLRenderer#hasActiveStencilMask
1005+ * @since 3.17.0
1006+ *
1007+ * @return {boolean } `true` if there is an active stencil mask, otherwise `false`.
1008+ */
1009+ hasActiveStencilMask : function ( )
1010+ {
1011+ var mask = this . currentMask ;
1012+ var camMask = this . currentCameraMask ;
1013+
1014+ return ( ( mask && mask . isStencil ) || ( camMask && camMask . isStencil ) ) ;
1015+ } ,
1016+
10011017 /**
10021018 * Use this to reset the gl context to the state that Phaser requires to continue rendering.
10031019 * Calling this will:
@@ -1024,14 +1040,15 @@ var WebGLRenderer = new Class({
10241040 gl . disable ( gl . DEPTH_TEST ) ;
10251041 gl . disable ( gl . CULL_FACE ) ;
10261042
1027- if ( ! this . currentMask && ! this . currentCameraMask )
1043+ if ( this . hasActiveStencilMask ( ) )
10281044 {
1029- gl . disable ( gl . STENCIL_TEST ) ;
1030- gl . clear ( gl . DEPTH_BUFFER_BIT | gl . STENCIL_BUFFER_BIT ) ;
1045+ gl . clear ( gl . DEPTH_BUFFER_BIT ) ;
10311046 }
10321047 else
10331048 {
1034- gl . clear ( gl . DEPTH_BUFFER_BIT ) ;
1049+ // If there wasn't a stencil mask set before this call, we can disable it safely
1050+ gl . disable ( gl . STENCIL_TEST ) ;
1051+ gl . clear ( gl . DEPTH_BUFFER_BIT | gl . STENCIL_BUFFER_BIT ) ;
10351052 }
10361053
10371054 gl . viewport ( 0 , 0 , this . width , this . height ) ;
@@ -1849,9 +1866,9 @@ var WebGLRenderer = new Class({
18491866
18501867 if ( camera . mask )
18511868 {
1852- camera . mask . postRenderWebGL ( this , camera . _maskCamera ) ;
1853-
18541869 this . currentCameraMask = null ;
1870+
1871+ camera . mask . postRenderWebGL ( this , camera . _maskCamera ) ;
18551872 }
18561873 } ,
18571874
@@ -1955,22 +1972,28 @@ var WebGLRenderer = new Class({
19551972
19561973 var mask = child . mask ;
19571974
1958- if ( this . currentMask && this . currentMask !== mask )
1959- {
1960- this . currentMask . postRenderWebGL ( this ) ;
1961- }
1962-
1963- if ( mask && mask !== this . currentMask )
1975+ if ( mask !== this . currentMask )
19641976 {
1965- mask . preRenderWebGL ( this , child , camera ) ;
1977+ if ( this . currentMask )
1978+ {
1979+ // Render out the previously set mask
1980+ this . currentMask . postRenderWebGL ( this , camera ) ;
1981+ }
1982+
1983+ if ( mask )
1984+ {
1985+ // Set-up the new mask
1986+ mask . preRenderWebGL ( this , child , camera ) ;
1987+ }
19661988 }
19671989
19681990 child . renderWebGL ( this , child , interpolationPercentage , camera ) ;
19691991 }
19701992
19711993 if ( this . currentMask )
19721994 {
1973- this . currentMask . postRenderWebGL ( this ) ;
1995+ // Render out the previously set mask, if it was the last item in the display list
1996+ this . currentMask . postRenderWebGL ( this , camera ) ;
19741997 }
19751998
19761999 this . setBlendMode ( CONST . BlendModes . NORMAL ) ;
0 commit comments