Is DynamicTexture serialization more memory efficient or not?

Hey folks,

Just wondering, I have following 2 ways to apply a dynamic texture to a PBR material:

Approach #1

  • Create a new DynamicTexture, build it the way I want
  • Assign it to a PBRMaterial
  • Render it…
  • When I am done rendering, dispose this dynamic texture

Approach #2

  • Create a new DynamicTexture, build it the way I want
  • Serialize the dynamic texture using the approach mentioned in this thread: Dispose canvas under DynamicTexture - #6 by RaananW
  • Create a new regular Texture with serialized texture url from previous step
  • Dispose the original DynamicTexture
  • Assign new regular Texture to a PBRMaterial
  • Render it…
  • When I am done rendering, dispose the regular Texture

By comparing these 2 processes. Which one is more memory efficient? Including overall memory usage as well as peak memory usage?

Feels like serializations seams more efficient since it has the less usage of html5 canvas? Is it possible for different browsers (for example, Chrome vs Safari), the answer could be different?

Many thanks!
Tawibox

I’d say approach 2 would be more CPU efficient, but I’m not sure about memory… I think this needs to be tested in practice and it’s very possible that the results would differ browser by browser. I made a test here: Dynamic Texture Example | Babylon.js Playground (babylonjs.com) but I’m not sure if this would be the best way to test, maybe @Evgeni_Popov has an idea?

1 Like

Peak memory will be the same as you use a dynamic texture in both cases. In case 2, as you dispose the dynamic texture, you will save a little bit of memory, but it will probably won’t make a big difference with case 1. If you are really very tighly limited by memory, you can use case 2, else case 1 is ok and is easier to setup.

@carolhmj @Evgeni_Popov These all make sense.

I was also thinking of doing the serialization process offline and store the serialized textures on server. But found the serialized texture string is quite big in size. My original source textures for dynamic texture is ~200KB but the serialized string file is ~5MB. So this offline serializing idea might not a good one in practice.

Thank you so much for the helps!
Tawibox

1 Like