gaelbeltran Posted October 17, 2016 Share Posted October 17, 2016 Hello, I was reading that you can specifically freeze materials to improve performance. I'm wondering if there's an easy way to do this to a whole loaded scene. I'm loading the scene like this: BABYLON.SceneLoader.ImportMesh("", "scenes/", "daz2.babylon", scene, function (newMeshes) { // Set the target of the camera to the first imported mesh camera.target = newMeshes[0]; // Move the light with the camera scene.registerBeforeRender(function () { light.position = camera.position; }); return scene; } //scene loader Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted October 17, 2016 Share Posted October 17, 2016 Hi @gaelbeltran I hope this points you in the right direction try to console.log(newMeshes); and look in the console for the correct structure. and as i also commented, use the method to freeze materials which is shown in the docs BABYLON.SceneLoader.ImportMesh("", "scenes/", "daz2.babylon", scene, function (newMeshes) { for(var i = 0, max = newMeshes.length; i < max; i++){ if(!newMeshes[i]) continue; /* NOTE; use console.log & babylon docs to find and use the correct terms. I'm not sure the syntrax "mesh.material.freeze();" is correct. */ newMeshes[i].material.freeze(); //If the current mesh have children/sub-meshes with seperate materials, you should make a secondary loop if(newMeshes[i]._children){ for(var i2 = 0, max2 = newMeshes[i]._children.length; i2 < max2; i2++){ if(!newMeshes[i]._children[i2] || !newMeshes[i]._children[i2].material) continue; newMeshes[i]._children[i2].material.freeze(); } } } // Set the target of the camera to the first imported mesh camera.target = newMeshes[0]; // Move the light with the camera scene.registerBeforeRender(function () { light.position = camera.position; }); } Quote Link to comment Share on other sites More sharing options...
gryff Posted October 17, 2016 Share Posted October 17, 2016 Hi @gaelbeltran : if what you mean by "freezing materials" is setting "checkReadyOnlyOnce" to true for each material and you are using Blender, then you can do it before export by setting it in the data object tab. See attached image. cheers, gryff Quote Link to comment Share on other sites More sharing options...
JCPalmer Posted October 17, 2016 Share Posted October 17, 2016 Yes, material freeze is the same thing as 'Check Ready Only Once'. Would not recommend doing this while you are getting a scene to work though. This is not always possible, and may not be easy to spot what went wrong. Better to leave this for later passes. Doing all materials in a scene with a loop sounds like a bad idea. gaelbeltran 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 17, 2016 Share Posted October 17, 2016 Can't agree more with @JCPalmer. The freeze is powerful for performance but sometimes a material cannot be frozen. Check more about freeze here: http://doc.babylonjs.com/tutorials/Optimizing_your_scene V!nc3r and gaelbeltran 2 Quote Link to comment Share on other sites More sharing options...
Nabroski Posted October 18, 2016 Share Posted October 18, 2016 I also had often to iterate trough my data to change values. And yes, freeze materials before the shaders starts to work in your main app, can cause issues, (e.g no animations on meshes, no loading of a mesh at all). An inexperience user will stuck here for a while. I learn this the hard way. So this is one of my solutions, when you what to avoid using just another loop and in the second line just an other one. maybe its helpful so i leave it here http://babylonjs-playground.com/#2ENZDZ#1 Quote Link to comment Share on other sites More sharing options...
gaelbeltran Posted October 18, 2016 Author Share Posted October 18, 2016 So the best solution is applying this after everything has been loaded and everything is working? Yes, I'll be very careful with this option, I'm just testing things. Btw, how much of a performance improvement are we talking about? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 18, 2016 Share Posted October 18, 2016 It depends. If you have a lot of meshes using the same material this could save a lot of time gaelbeltran 1 Quote Link to comment Share on other sites More sharing options...
Nabroski Posted October 18, 2016 Share Posted October 18, 2016 @gaelbeltran I'm sorry, but for now i'm not so sure anymore if i grab the data in the right place to modify it. http://babylonjs-playground.com/#2ENZDZ#2 Two options OnevryCall and Onlyoncehttps://github.com/BabylonJS/Documentation/blob/fb118c05a8be522f3b4160d099c3823a1ccd2c1c/content/classes/2.4/Material.md#checkreadyoneverycall--boolean Best EDIT: So it should work, but its not! Idk why. v() function is triggered 8 times, but if i ask for the materials in the scene it shows me only 4 materials, okay so, the issue is, be course from 8 meshes 4 meshes share the same material. gaelbeltran 1 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.