14 KiB
Phaser 3.80.0 Change Log
Return to the Change Log index.
WebGL Context Restore
Phaser now performs a WebGL context restore.
WebGL can lose context when the browser wants to reclaim resources. This happens most often when a browser tab is inactive. When it happens is beyond the control of web developers.
When context is lost, the game canvas goes blank. A lost context loses all the data that was sent to the WebGL system. It cannot do anything until the browser restores the context, usually when the user returns to their tab.
Phaser now restores this data, with a few exceptions, allowing your games to seamlessly resume. For the most part, your WebGL games should now simply recover when the browser restores the WebGL context.
Key concepts:
WebGLRendereremitsLOSE_WEBGLandRESTORE_WEBGLevents.- Dynamic textures are cleared.
- WebGL objects are now enclosed in wrappers.
- You can test WebGL context restore with WebGL extensions.
- Supporting changes were made to the rendering system.
Events
Phaser.Renderers.WebGL.WebGLRenderer now emits events that you can use to stay informed about context loss.
Phaser.Renderer.Events#LOSE_WEBGLis emitted when context is lost. This would be a good time to pause the game, or otherwise avoid making changes that the player cannot see.Phaser.Renderer.Events#RESTORE_WEBGLis emitted when context is restored. This would be a good time to restore dynamic textures, and start things moving again.
Dynamic Textures
Dynamic textures and render textures are stored on the GPU. When context is lost, the texture is erased. When context is restored, the texture is re-enabled, but it remains blank.
A render texture which is redrawn every frame will naturally redraw itself.
Textures which are drawn once, however, will stay blank. You should listen for the RESTORE_WEBGL event to know when to redraw them.
It is safe to draw while the context is lost, but nothing will be drawn. Snapshots will return blank.
WebGL Object Wrappers
WebGL objects are now wrapped. The wrapper holds the necessary information to automatically recreate the object when context is restored.
This is only relevant if you need deep access to the WebGL renderer.
Wrappers are created and managed by the WebGLRenderer. They have the following qualities:
- A property for the wrapped WebGL object, for use in rendering. This property should only be accessed within a call to WebGL; it may be changed within the wrapper.
createResource(), which will create a new WebGL object from stored information.destroy(), which will remove the object. Do not call this directly; it is managed by theWebGLRenderer.
The following wrappers are available in the Phaser.Renderer.WebGL.Wrappers namespace:
WebGLAttribLocationWrapperWebGLBufferWrapperWebGLFramebufferWrapperWebGLProgramWrapperWebGLTextureWrapperWebGLUniformLocationWrapper
Testing Context Restore
You can test context loss and restore with a built-in WebGL extension. The following code snippet should be useful for testing purposes. It shows how to get the built-in extension, and simulate losing and restoring the context after 1 and 2 seconds respectively.
const webGLLoseContextExtension = game.renderer.getExtension('WEBGL_lose_context');
setTimeout(function () {
webGLLoseContextExtension.loseContext();
setTimeout(function () {
webGLLoseContextExtension.restoreContext();
}, 1000)
}, 1000);
Supporting Changes
Several changes were made to the rendering system to support these improvements. In particular, many properties were changed to use wrappers instead of raw WebGL objects. The full list of changes is as follows:
Phaser.FX.DisplacementglTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapper
BatchChar- Constructor:
textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
- Constructor:
Phaser.GameObjects.ShadervertexBufferproperty type changed fromWebGLBuffertoWebGLBufferWrapperprogramproperty type changed fromWebGLProgramtoWebGLProgramWrapperframebufferproperty type changed fromWebGLFramebuffertoWebGLFramebufferWrapperglTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapper#initUniformsmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
Phaser.GameObjects.TileSpritefillPatternproperty type changed from?(WebGLTexture|CanvasPattern)to?(WebGLTextureWrapper|CanvasPattern)
Phaser.Renderer.WebGL.PipelineManager- Added property
postPipelineInstancesof typePostFXPipeline[] - Added method
removePostPipeline(pipeline: PostFXPipeline) - Added method
restoreContext(), used after context is restored
- Added property
Phaser.Renderer.WebGL.RenderTargetframebufferproperty type changed fromWebGLFramebuffertoWebGLFramebufferWrappertextureparameter type changed fromWebGLTexturetoWebGLTextureWrapper#unbindmethod:- Return type changed from
WebGLFramebuffertoWebGLFramebufferWrapper
- Return type changed from
Phaser.Renderer.WebGL.WebGLPipelinevertexBufferproperty type changed fromWebGLBuffertoWebGLBufferWrapperactiveBufferproperty type changed fromWebGLBuffertoWebGLBufferWrappercurrentTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapperactiveTexturesproperty type changed fromWebGLTexture[]toWebGLTextureWrapper[]#setShadermethod:vertexBufferparameter type changed fromWebGLBuffertoWebGLBufferWrapper
#createBatchmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#addTextureToBatchmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#pushBatchmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
- Added method
restoreContext(), used after context is restored #setVertexBuffermethod:bufferparameter type changed fromWebGLBuffertoWebGLBufferWrapper
#batchQuadmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#batchTrimethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#drawFillRectmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#setTexture2Dmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#bindTexturemethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
Phaser.Renderer.WebGL.WebGLRenderer- Added property
glBufferWrappersof typeWebGLBufferWrapper[] - Added property
glProgramWrappersof typeWebGLProgramWrapper[] - Added property
glTextureWrappersof typeWebGLTextureWrapper[] - Added property
glFramebufferWrappersof typeWebGLFramebufferWrapper[] - Added property
glAttribLocationWrappersof typeWebGLAttribLocationWrapper[] - Added property
glUniformLocationWrappersof typeWebGLUniformLocationWrapper[] - Added property
normalTextureof typeWebGLTextureWrapper currentFramebufferproperty type changed fromWebGLFramebuffertoWebGLFramebufferWrapperfboStackproperty type changed fromWebGLFramebuffer[]toWebGLFramebufferWrapper[]currentProgramproperty type changed fromWebGLProgramtoWebGLProgramWrapperblankTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapperwhiteTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapper- Added method
createTemporaryTextures(), used when booting on MacOS #pushFramebuffermethod:framebufferparameter changed fromWebGLFramebuffertoWebGLFramebufferWrappertextureparameter changed fromWebGLTexturetoWebGLTextureWrapper
#setFramebuffermethod:framebufferparameter changed fromWebGLFramebuffertoWebGLFramebufferWrappertextureparameter changed fromWebGLTexturetoWebGLTextureWrapper
#popFramebuffermethod:- Return type changed from
WebGLFramebuffertoWebGLFramebufferWrapper
- Return type changed from
#setProgrammethod:programparameter changed fromWebGLProgramtoWebGLFramebufferWrapper
#createTextureFromSourcemethod:- Return type changed from
WebGLTexturetoWebGLTextureWrapper
- Return type changed from
#createTexture2Dmethod:widthparameter made optionalheightparameter made optional- Return type changed from
WebGLTexturetoWebGLTextureWrapper
#createFramebuffermethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper- Return type changed from
WebGLFramebuffertoWebGLFramebufferWrapper
#createProgrammethod:- Return type changed from
WebGLProgramtoWebGLProgramWrapper
- Return type changed from
#createVertexBuffermethod:- Return type changed from
WebGLBuffertoWebGLBufferWrapper
- Return type changed from
- Added method
createAttribLocation - Added method
createUniformLocation #createIndexBuffermethod:- Return type changed from
WebGLBuffertoWebGLBufferWrapper
- Return type changed from
#deleteTexturemethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#deleteFramebuffermethod:framebufferparameter type changed fromWebGLFramebuffertoWebGLFramebufferWrapper
#deleteProgrammethod:programparameter type changed fromWebGLProgramtoWebGLProgramWrapper
- Added method
deleteAttribLocation - Added method
deleteUniformLocation #deleteBuffermethod:vertexBufferparameter type changed fromWebGLBuffertoWebGLBufferWrapper
#snapshotFramebuffermethod:framebufferparameter changed fromWebGLFramebuffertoWebGLFramebufferWrapper
#canvasToTexturemethod:dstTextureparameter type changed fromWebGLTexturetoWebGLTextureWrapper- Return type changed from
WebGLTexturetoWebGLTextureWrapper
#createCanvasTexturemethod:- Return type changed from
WebGLTexturetoWebGLTextureWrapper
- Return type changed from
#updateCanvasTexturemethod:dstTextureparameter type changed fromWebGLTexturetoWebGLTextureWrapper- Return type changed from
WebGLTexturetoWebGLTextureWrapper
#createVideoTexturemethod:- Return type changed from
WebGLTexturetoWebGLTextureWrapper
- Return type changed from
#updateCanvasTexturemethod:dstTextureparameter type changed fromWebGLTexturetoWebGLTextureWrapper- Return type changed from
WebGLTexturetoWebGLTextureWrapper
- Added method
createUint8ArrayTexture #setTextureFiltermethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
- Added property
Phaser.Renderers.WebGL.WebGLShaderprogramproperty type changed fromWebGLProgramtoWebGLProgramWrapper- Added method
syncUniformsfor use during context recovery
Phaser.Renderers.WebGL.Pipelines.LightPipeline- Removed property
defaultNormalMap. There is now a default normal map atWebGLRenderer.normalTexture, or the texture key'__NORMAL'. currentNormalMapproperty type changed fromWebGLTexturetoWebGLTextureWrapper#setTexture2Dmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#isNewNormalMapmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrappernormalMapparameter type changed fromWebGLTexturetoWebGLTextureWrapper
#getNormalMapmethod:- Return type changed from
WebGLTexturetoWebGLTextureWrapper
- Return type changed from
#batchTexturemethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
- Removed property
Phaser.Renderers.WebGL.Pipelines.MultiPipeline#batchTexturemethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
Phaser.Renderers.WebGL.Pipelines.PreFXPipelinequadVertexBufferproperty type changed fromWebGLBuffertoWebGLBufferWrapper#batchQuadmethod:textureparameter type changed fromWebGLTexturetoWebGLTextureWrapper
Phaser.Renderers.WebGL.Pipelines.DisplacementFXPipelineglTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapper
WebGLPipelineAttributelocationproperty changed fromnumberto-1|WebGLAttribLocationWrapper
WebGLPipelineBatchEntrytextureproperty changed fromWebGLTexturetoWebGLTextureWrapper
WebGLPipelineUniformsConfig- Added property
setterof type?functionfor use in restoring context
- Added property
Phaser.Textures.DynamicTexture#getWebGLTexturemethod:- Return type changed from
WebGLTexturetoWebGLTextureWrapper
- Return type changed from
Phaser.Textures.FrameglTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapper- Property is now read-only
Phaser.Textures.Texture- Constructor
sourceparameter type options addedWebGLTextureWrapper
- Constructor
Phaser.Textures.TextureManager- A texture with the key
'__NORMAL'is created on boot if the WebGL renderer is being used. This is a 1x1 texture of colour #7f7fff, or a flat normal map. #addGLTexturemethod:glTextureparameter type changed fromWebGLTexturetoWebGLTextureWrapperwidthparameter removedheightparameter removed
#getTextureKeysnow excludes'__NORMAL'as well as'__DEFAULT','__MISSING', and'__WHITE'.- Added method
addUint8Arrayfor creating textures from raw colour data
- A texture with the key
Phaser.Textures.TextureSourceglTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapper
Phaser.Tilemaps.TilesetglTextureproperty type changed fromWebGLTexturetoWebGLTextureWrapper