BABYLON.SceneSerializer.SerializeAsync usage

I need to use the Texture.ForceSerializeBuffers flag for serializing RawTextures and then want to use
SceneSerializer.SerializeAsync as it is recommended. However, for other, regular url textures I get the exception below in the console, probably because the texture is not ready, and texture.getSize() returns 0,0. Here is a PG:

https://playground.babylonjs.com/#20OAV9#6271

SceneSerializer.Serialize generates the same error.

I actually would not need the base64 encoded texture for those but I do not know how to avoid generating those, per texture.

How would one use SerializeAsync properly ?

Thanks.

babylon.js:16 Uncaught (in promise) DOMException: Failed to execute 'createImageData' on 'CanvasRenderingContext2D': The source width is zero or not a number.
    at r (https://preview.babylonjs.com/babylon.js:16:942395)
    at o (https://preview.babylonjs.com/babylon.js:16:942805)
    at t.serialize (https://preview.babylonjs.com/babylon.js:16:107944)
    at Function.e.Serialize (https://preview.babylonjs.com/babylon.js:16:5044)
    at t.serialize (https://preview.babylonjs.com/babylon.js:16:495268)
    at Function.e._Serialize (https://preview.babylonjs.com/babylon.js:16:3773858)
    at Function.e.SerializeAsync (https://preview.babylonjs.com/babylon.js:16:3775326)
    at createScene (<anonymous>:38:45)
    at window.initFunction (<anonymous>:58:16)

Cc @sebavan to have a deeper look

1 Like

I ll try to fix it a bit later today :slight_smile:

1 Like

Can you try in the latest preview to set _forceSerialize on your raw textures only to true ?

Without the global forceSerialize.

The current exception as you said comes from the inbetween loading textures.

Thanks for looking into this. It looks like the PG is using the latest preview ? The version is v5.0.0-alpha.63 but does not change for nightlies, I think.

https://playground.babylonjs.com/#20OAV9#6283
uses _forceSerialize=true rather than global but gets the same exception in the console.

I think I understand what I was missing. The SerializeAsync function is probably not intended to wait for the scene to become ready before serializing although I thought that would be the main reason why serializing should be done asynchronously. Is that about right ? I can use scene.whenReadyAsync() instead before serializing.

( What is the reasoning that _forceSerialize is marked with an underscore (‘private’) ? )

_forceSerialize is currently only internal as it has lots of gotchas depending on the texture type so to use with extra care :slight_smile:

About the SerializeAsync it is async mainly for the read texture part which is async due to no sync way of doing so in WebGPU. It is definitely not intented to wait for readiness of the scene.

1 Like

Hi @andreasplesch just checking in, do you have any more questions about it? :smiley:

Thanks. I think I ended up using scene.whenReadyAsync().