How to clone a mesh to another scene? Trying to cache meshes in memory and share them between scenes

Hello, I had a long forced break to continue with my engine :frowning: , but I’m back now 100% with it :slight_smile:

With the purpose of cache meshes in memory, and clone them to be used in N scenes which can be loaded and unloaded at any time, my question:

How can I clone a mesh with no scene associated to any other scene?

I load the mesh (an all its assets) using BABYLON.SceneLoader.ImportMeshAsync(path, file) with no scene associated. The file is a .babylon file, which could use assets within its same path (textures, etc). I think when I don’t define the scene in ImportMeshAsync, babylon associate the loaded mesh to the last scene, and that would be a problem since I don’t want to associate this mesh to any scene.

Then I want to clone that mesh into any scene or scenes.
Any amount of meshes can be cloned to N scenes.
I don’t see the Mesh.clone method to associate the cloned mesh to any scene.

How could I associate a cloned mesh to a scene?

Is there a better way to cache a mesh and all its assets in memory to be cloned to any amount of scenes?


For now, the better way I’m thinking out is to zip or save somehow the mesh folder content into memory, and use ImportMeshAsync with that folder stored in memory, assigning to it a scene. But I don’t like it beucase in that way I force the user to use a separated folder for each mesh + assets, which could drive to problems.

Is there a way to identify the assets files belonging to a mesh? Then I can store each one of them in memory independently.


2 Likes

If this forum post in 2023 is anything to go by

the best you can do is serialize a mesh, store that into memory, and then parse it back into whatever scene you want, like so

idk if anything has changed since then : /

2 Likes

That simplifies a lot what I have to do.

Thanks! I will check if it is working :slight_smile:

1 Like

@Evgeni_Popov do SerializeMesh serialize it with all its materials, shaders and textures?

Yes, it does, see:

It creates a new scene2 and import the mesh that has been serialized in scene1. This is scene2 which is displayed by the Playground.

2 Likes

Thanks, my concern was mostly about shaders, so I consider they are serialized as well.

Hi,

I’m facing a problem.

I’m loading the mesh from a .babylon file (I export the mesh from Blender), so I load the scene using LoadAsync, then I grab the mesh and I serialize it, but when I use ImportMeshAsync to unserialize, I get an error that the texture cannot be loaded because it doesn’t exists in the root path.


Is there any solution for this?

How can I serialize a mesh and all its assets loaded from a .babylon file with LoadAsync?


Test importing the scene from .babylon file and not serializing the mesh:


Test importing the scene from .babylon file and serializing the mesh:
Since I have to import the mesh to different scenes, I cannot use sceneLoaded for any other purpose than loading the mesh and storing it in memory.

PG does not work because of CORS:

It is a bit tough to follow what you need without a working sample

1 Like

Wow, I enabled cors in a subfolder of the server and it was working in my PC, but you are right, my mobile is throwing same error.

Try again please, it should be working now.

You can see the content here: Index of /test/

You can pass the root url as the second parameter of ImportMeshAsync:

1 Like

Yes but in that way, I wouldn’t be serializing the texture, and the cache system lose a bit of sense, doesn’t it?

The idea is not having to reload assets in a scene switch.

Anyway if there’s no other solution I will do it the way you say, and at least I will cache the .babylon file.

As the texture has already been loaded by the first call to SceneLoader, it will not be loaded again. The correct root url is required so that the full url can be searched for an existing texture in the cache, and because it will be found, the texture will not be reloaded but reused.

You can see it in this PG:

Look at the console log:

image

The first number is the texture identifier, while the second is the internal texture identifier. The texture identifier is different in each scene because a new Texture is created, but the internal texture identifier is the same, meaning that the GPU texture (InternalTexture) is reused in both cases.

As the texture has already been loaded by the first call to SceneLoader , it will not be loaded again.

You are right, but if I unload the scene (dispose it), babylon will remove the texture from memory?

Because what I want is to keep the texture in memory after unloading a scene, so the next scene doesn’t need to load it.


I tested it and babylon seems to keep the texture in memory after the first scene dispose.

What’s the babylon’s criteria to remove the texture from memory?
Memory should be clean after the first scene dispose :thinking:

Texture cache is not cleaned after scene dispose because you may create a new scene afterward, which may benefit from the cache.

If you want to clear the cache, you can call engine.clearInternalTexturesCache().

1 Like

I see. Thanks for letting me know.

One more question:

Am I doing right caching all the rest of assets with the same purpose to use them in afterewards scenes?

  • .babylon files to append meshes or scene blocks.
  • .pngs or .jpgs used to create textures for sprites using BABYLON.Texture.
  • fonts
  • audios

Is it possible to disable that babylon cache so I have the full control over it?

Or maybe I can forget of this feature if babylon handles the cache of totality of assets, and I can implement an automatic cache system in the future using disableOfflineSupportExceptionRules, which I’m not sure that can cover my neccesities.

The aim is to let the users to allow or disallow cache for each asset, that’s why I’m doing this.

E.g.

@Sprite({
  url: './assets/character-spritesheet.png',
  width: 50,
  height: 80,
  cached: true
})
export class MySprite extends SpriteInterface {
  .
  .
  .
}

Ok, job done.
Meshes cached and cloning / instantiating them is working fine :slight_smile:
Thank you guys @Heaust-ops @Evgeni_Popov @Deltakosh

4 Likes