@@ -27,12 +27,30 @@ var WebGLRenderer = new Class({
2727
2828 function WebGLRenderer ( game )
2929 {
30+ var _this = this ;
3031 this . game = game ;
3132 this . type = CONST . WEBGL ;
3233 this . width = game . config . width * game . config . resolution ;
3334 this . height = game . config . height * game . config . resolution ;
3435 this . resolution = game . config . resolution ;
3536 this . view = game . canvas ;
37+ this . view . addEventListener ( 'webglcontextlost' , function ( evt ) {
38+ var renderers = _this . rendererArray ;
39+ for ( var index = 0 ; index < renderers . length ; ++ index )
40+ {
41+ renderers [ index ] . destroy ( ) ;
42+ }
43+ _this . contextLost = true ;
44+ evt . preventDefault ( ) ;
45+ } , false ) ;
46+
47+ this . view . addEventListener ( 'webglcontextrestored' , function ( evt ) {
48+ _this . rendererArray . length = 0 ;
49+ _this . resourceManager . shaderCache = { } ;
50+ _this . resourceManager . shaderCount = 0 ;
51+ _this . contextLost = false ;
52+ _this . init ( ) ;
53+ } , false ) ;
3654
3755 // All of these settings will be able to be controlled via the Game Config
3856 this . config = {
@@ -91,8 +109,10 @@ var WebGLRenderer = new Class({
91109 this . contextLost = true ;
92110 throw new Error ( 'This browser does not support WebGL. Try using the Canvas renderer.' ) ;
93111 }
94-
95112 var gl = this . gl ;
113+
114+ this . lostContext = this . getExtension ( 'WEBGL_lose_context' ) ;
115+
96116 var color = this . game . config . backgroundColor ;
97117
98118 this . resourceManager = new ResourceManager ( gl ) ;
@@ -326,14 +346,13 @@ var WebGLRenderer = new Class({
326346 // Call at the start of the render loop
327347 preRender : function ( )
328348 {
329- this . setRenderTarget ( null ) ;
330-
331349 // No point rendering if our context has been blown up!
332350 if ( this . contextLost )
333351 {
334352 return ;
335353 }
336354
355+ this . setRenderTarget ( null ) ;
337356 // Add Pre-render hook
338357
339358 var gl = this . gl ;
@@ -358,6 +377,7 @@ var WebGLRenderer = new Class({
358377 */
359378 render : function ( scene , children , interpolationPercentage , camera )
360379 {
380+ if ( this . contextLost ) return ;
361381 var gl = this . gl ;
362382 var quadBatch = this . quadBatch ;
363383
@@ -479,6 +499,8 @@ var WebGLRenderer = new Class({
479499 // Called at the end of the render loop (tidy things up, etc)
480500 postRender : function ( )
481501 {
502+ if ( this . contextLost ) return ;
503+
482504 this . currentRenderer . flush ( ) ;
483505
484506 if ( this . snapshotCallback )
@@ -612,6 +634,10 @@ var WebGLRenderer = new Class({
612634
613635 destroy : function ( )
614636 {
637+ if ( this . lostContext )
638+ {
639+ this . lostContext . loseContext ( ) ;
640+ }
615641 this . gl = null ;
616642 }
617643
0 commit comments