heyzxz Posted March 23, 2017 Share Posted March 23, 2017 Hi again, Well as it shows in the doc, most of the BJS objects (meshes, materials, textures...) need a 'scene' parameter in their constructor function, I guess the main purpose of this design is making the memory management clearer and easier, so the scene takes fully control of the memory management for all stuffs, when you leave a scene, all you need is simply calling a 'scene.dispose' and it will clear/release everything for you, right? But I have some questions about this: 1. Does this mean when designing a BJS game, it's NOT recommended to share objects(especially model objects ) with multiple scenes? For example, player's game properties, a gun. What is the correct/recommended way to manage it? Re-instantiate the gun in each scene? or holding the gun(model) out of scene and share it with other scenes? 2. How to deal with the later-loading objects? I mean when calling 'scene.dispose', what if there are still something in the loading?? does the 'scene.dispose' takes care of them? It looks not... Please see this PG: http://www.babylonjs-playground.com/#280KRP#1, there is a texture still in loading when calling the 'scene.dispose', then, you'll see some webgl warnings and also you'll find the onload callback for the texture is triggered (which shouldn't be)... A similar example: What if dispose the scene when there is a 'BABYLON.SceneLoader.ImportMesh...' NOT finished... How do I deal with these cases? Thank you! Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted March 23, 2017 Share Posted March 23, 2017 Hey! You are right The scene oriented approach is here to help managing resources. 1. My recommendation would be to have a "holding" scene where your objects belong and when you want to use them in a specific scene you can then use addMesh/removeMesh to move your mesh (You will also have to do it for the materials) 1'. You can also think about a big scene where you have hidden objects that can be used only when needed 2.As you found out this is not supported by scene.dispose but I will fix it for next commit (Checking if scene is disposed in the importMesh callback) Quote Link to comment Share on other sites More sharing options...
heyzxz Posted March 23, 2017 Author Share Posted March 23, 2017 1. Good idea! Make sense for me ! 2. Thank you! Quote Link to comment Share on other sites More sharing options...
heyzxz Posted March 30, 2017 Author Share Posted March 30, 2017 On 3/24/2017 at 0:41 AM, Deltakosh said: Hey! You are right The scene oriented approach is here to help managing resources. 1. My recommendation would be to have a "holding" scene where your objects belong and when you want to use them in a specific scene you can then use addMesh/removeMesh to move your mesh (You will also have to do it for the materials) 1'. You can also think about a big scene where you have hidden objects that can be used only when needed 2.As you found out this is not supported by scene.dispose but I will fix it for next commit (Checking if scene is disposed in the importMesh callback) Hi again @Deltakosh, I followed your #1 suggestion: create a holding scene to hold all the shared stuffs (game properties), and then use addMesh/removeMesh to move them to others scenes, but unfortunately I got another problem The rendering for materials (checked for StandardMaterial, PBRMaterial, but I guess other things such as Meshes, Textures..may still have this problem) are based on their own scenes, which means if an object belongs to SceneA, then I add it to SceneB( by using 'addMesh' ), when rendering this object in the sceneB, it still access SceneA's properties ( object.getScene(), object.getScene().activeCamera... ) to do its render, which is wrong in my case. And I didn't find a 'public way' to change its '_scene' property. https://github.com/BabylonJS/Babylon.js/blob/master/src/Materials/babylon.pbrMaterial.ts#L1334 https://github.com/BabylonJS/Babylon.js/blob/master/src/Materials/babylon.shaderMaterial.ts#L250 Any ideas ? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted March 31, 2017 Share Posted March 31, 2017 Oh yeah right :(. But you can still update mesh._scene (even if this is a bit hacky) it should work Quote Link to comment Share on other sites More sharing options...
heyzxz Posted April 1, 2017 Author Share Posted April 1, 2017 Yeah that's hacky... I think I probably won't do this for now as I'm targeting on a long-term project, hacky ways may bring potential risks in the future... So I'm now giving up caching shared BJS objects and choose to create/dispose them in every scene... But I really like your 'holding scene' idea, and I hope BJS can think about it and some day can provide an official way to support it. Because it's a great way to keep the memory more stable, which can lead to much less GC rounds of the browsers than frequently creating/disposing things (as we know the GC round somethings is the killer for the performance) Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 3, 2017 Share Posted April 3, 2017 Actually we could do an experiment: does it work when you change the mesh_scene? Because you may also need to change the material._scene in the same way. And if your mesh is referenced by a light, shadows, or an instance then everyone has to move. The more I think about i, the more I think we should not do it And what about having only one scene and playing with mesh.clone? It is acceptable in your case? Quote Link to comment Share on other sites More sharing options...
heyzxz Posted April 3, 2017 Author Share Posted April 3, 2017 22 minutes ago, Deltakosh said: Actually we could do an experiment: does it work when you change the mesh_scene? Because you may also need to change the material._scene in the same way. And if your mesh is referenced by a light, shadows, or an instance then everyone has to move. Yes, agree. 23 minutes ago, Deltakosh said: And what about having only one scene and playing with mesh.clone? It is acceptable in your case? Not very sure what do you mean by 'mesh.clone' do you mean just use only one scene and create(may be clone if possible)/dispose game stuffs in that scene? Yes, I'm currently doing this way Thank you. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 4, 2017 Share Posted April 4, 2017 Yes this is what I meant Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.