Jump to content

Force compilation of shaders


GameMonetize
 Share

Recommended Posts

Thanks, I actually found that I need to get all the simple materials of a mesh and its descendants.  I have this coded so far in QI.Mesh, since I cannot bring myself to switch to 3.1 Alpha on day one.  I can move to BABYLON.Mesh after I get one kink out.

/**
 * Get Every material in use by the mesh & it's children.  This is primarily for compileMaterials(),
 * but needs to be broken out, so it can be called recursively.
 * 
 * @param repo - This is an array of dictionaries with entries of a mesh & material, initialized when missing.
 * @param mesh - An argument of the mesh to operate on, default this.  Needed because function is implemented at QI.Mesh.
 * @returns an array of dictionaries with entries of a mesh & material.
 */
public getEverySimpleMaterial(repo? : Array<{mesh: BABYLON.Mesh, mat: BABYLON.Material}>, mesh : BABYLON.Mesh = this) : Array<{mesh: BABYLON.Mesh, mat: BABYLON.Material}> {
    if (!repo) repo = new Array<{mesh: BABYLON.Mesh, mat: BABYLON.Material}>(0);
    
    // take care of this mesh
    if (mesh.material instanceof BABYLON.MultiMaterial) {
        var subMaterials = (<BABYLON.MultiMaterial> mesh.material).subMaterials;
        for (var i = 0, len = subMaterials.length; i < len; i++) {
            repo.push( {mesh : mesh, mat: subMaterials[i]} );
        }
    } else repo.push( {mesh : mesh, mat: mesh.material} );
    
    // take care of children (those which are not QI.Mesh, can only go one level)
    var children = mesh.getChildMeshes();
    for (var i = 0, len = children.length; i < len; i++) {
        if (children[i] instanceof QI.Mesh)
            (<QI.Mesh> children[i]).getEverySimpleMaterial(repo);
        else
            (<QI.Mesh> mesh).getEverySimpleMaterial(repo, <BABYLON.Mesh> children[i]);
    }            
    return repo;
}

/**
 * Ensure that all materials for this mesh & it's children are actively forced to compile
 */
public compileMaterials(completionCallback : () => void) : void {
    var everyMatSet = this.getEverySimpleMaterial();
    console.log("Total number of materials " + everyMatSet.length);
    var compiledMaterials = 0;
    var nMaterials = everyMatSet.length;
    
    // the callback to forceCompilation 
    var callback = function(material : BABYLON.Material) : void {
        console.log(material.name);
        if (++compiledMaterials  < nMaterials) return;
        completionCallback();     
    };
    
    // force compile each mesh & material set
    for (var i = 0; i < nMaterials; i++) {
        console.log("queued " + everyMatSet[i].mat.name);
        everyMatSet[i].mat.forceCompilation(everyMatSet[i].mesh, callback);
    }
}

That kink is one of my child meshes is colorShader for LinesMesh (Hair).  Do I need to do a test for StandardMaterial?

Link to comment
Share on other sites

I implemented the forced compiling of even ShaderMaterials, but just do not block for it.  https://github.com/BabylonJS/Extensions/commit/d4d6f914b7dc8b7d36c98ac0732d9f4a1905b374

When I had a simple merged head & hair, I had been getting pretty good sync with any GrandEntrance sound effect, but as the number of meshes increase for full scale characters (clothes, shoes) the sound was getting ahead.  This closed some of that.  The effects also needed tweeking.

Let me know if you want me to move this, or steal it yourself.

Link to comment
Share on other sites

For those defining all their meshes before the first render (but mesh do you pass to forceCompilation).  I am attempting to dynamically add meshes to a scene that is already rendering meshes.  I retrieve the geo (inside .js files) & buffer the data for texture files asyc ahead of time to reduce the slow down.  There is going a brief hiccup though.  That is kind of disguised using these animations to initially display meshes.  Do not want either the animation to not be smooth or be behind the audio.

I am fine it this was just in my code.  Would have marked the topic [solved], if it were mine.  I am perfectly positioned for async shader compiling!

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...