@@ -32,6 +32,7 @@ import {
3232 type CoreWebGlExtensions ,
3333 getWebGlParameters ,
3434 getWebGlExtensions ,
35+ type WebGlColor ,
3536} from './internal/RendererUtils.js' ;
3637import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js' ;
3738import { Texture , TextureType } from '../../textures/Texture.js' ;
@@ -86,6 +87,11 @@ export class WebGlCoreRenderer extends CoreRenderer {
8687 defaultShader : WebGlCoreShader ;
8788 quadBufferCollection : BufferCollection ;
8889
90+ clearColor : WebGlColor = {
91+ raw : 0x00000000 ,
92+ normalized : [ 0 , 0 , 0 , 0 ] ,
93+ } ;
94+
8995 /**
9096 * White pixel texture used by default when no texture is specified.
9197 */
@@ -127,10 +133,10 @@ export class WebGlCoreRenderer extends CoreRenderer {
127133 options . contextSpy ,
128134 ) ;
129135 const glw = ( this . glw = new WebGlContextWrapper ( gl ) ) ;
130-
131- const color = getNormalizedRgbaComponents ( clearColor ) ;
132136 glw . viewport ( 0 , 0 , canvas . width , canvas . height ) ;
133- glw . clearColor ( color [ 0 ] ! , color [ 1 ] ! , color [ 2 ] ! , color [ 3 ] ! ) ;
137+
138+ this . updateClearColor ( clearColor ) ;
139+
134140 glw . setBlend ( true ) ;
135141 glw . blendFunc ( glw . ONE , glw . ONE_MINUS_SRC_ALPHA ) ;
136142
@@ -754,9 +760,9 @@ export class WebGlCoreRenderer extends CoreRenderer {
754760 node . hasRTTupdates = false ;
755761 }
756762
757- const color = getNormalizedRgbaComponents ( this . stage . options . clearColor ) ;
763+ const clearColor = this . clearColor . normalized ;
758764 // Restore the default clear color
759- glw . clearColor ( color [ 0 ] ! , color [ 1 ] ! , color [ 2 ] ! , color [ 3 ] ! ) ;
765+ glw . clearColor ( clearColor [ 0 ] , clearColor [ 1 ] , clearColor [ 2 ] , clearColor [ 3 ] ) ;
760766
761767 // Bind the default framebuffer
762768 glw . bindFramebuffer ( null ) ;
@@ -784,4 +790,28 @@ export class WebGlCoreRenderer extends CoreRenderer {
784790 override getDefShaderCtr ( ) : BaseShaderController {
785791 return this . defShaderCtrl ;
786792 }
793+
794+ /**
795+ * Updates the WebGL context's clear color and clears the color buffer.
796+ *
797+ * @param color - The color to set as the clear color, represented as a 32-bit integer.
798+ */
799+ updateClearColor ( color : number ) {
800+ if ( this . clearColor . raw === color ) {
801+ return ;
802+ }
803+ const glw = this . glw ;
804+ const normalizedColor = getNormalizedRgbaComponents ( color ) ;
805+ glw . clearColor (
806+ normalizedColor [ 0 ] ,
807+ normalizedColor [ 1 ] ,
808+ normalizedColor [ 2 ] ,
809+ normalizedColor [ 3 ] ,
810+ ) ;
811+ this . clearColor = {
812+ raw : color ,
813+ normalized : normalizedColor ,
814+ } ;
815+ glw . clear ( ) ;
816+ }
787817}
0 commit comments