Jump to content

Iterate through blender exported scene to freeze all materials?


gaelbeltran
 Share

Recommended Posts

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

 

Link to comment
Share on other sites

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; 
}); 
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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 Onlyonce
https://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.  

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...