Jump to content

Problems with Texture onLoad callback


JCPalmer
 Share

Recommended Posts

I was trying to take advantage of the probably seldom used callback option when loading textures are complete.  I know about scene.getWaitingItemsCount(), but wanted to see if could have better control for my TOB computer generated source code for when a mesh/meshes are first displayed.  Could be useful for adding meshes once a game begins in a professional way.

I am hand modifying computer built code to nail down all the requirements first.  Added a module internal function to be the callback and properties:

    var waitingMeshes = [];
    var pendingTextures = 0;
    function onTexturesLoaded(){
    	if (--pendingTextures > 0){
    		return;
    	}

    	for (var i = 0, len = waitingMeshes.length; i < len; i++){
    		waitingMeshes[i].setEnabled(true);
    	}
    	waitingMeshes = [];
    }

In each Mesh class I added the lines starting with '++', so the mesh will not initially be enabled the first instance:

    var Voice_sync_female_Body = (function (_super) {
        __extends(Voice_sync_female_Body, _super);
        function Voice_sync_female_Body(name, scene, materialsRootDir, source) {
            _super.call(this, name, scene, null, source, true);

++          this.setEnabled(matLoaded && true);
++          if (!matLoaded) waitingMeshes.push(this); // not present if should not be enabled
            
            if (!materialsRootDir) { materialsRootDir = "./"; }
            defineMaterials(scene, materialsRootDir); //embedded version check
...

In the method defineMaterials, every material which is still not loaded, I bump the pendingTextures++.  It works, but I also had to modify the texture constructor to add the onload arg like:

texture = new B.Texture("skin.png", scene, false, false, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, onTexturesLoaded);

Also have to say ", false, false, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, " because the argument is way to the right.  This is what I get.  Any ideas, please?

Selection_002.jpg

Link to comment
Share on other sites

Well, slept on it.  While getting the texture is the part making everything async, the material still needs to be compile/ bound to mesh once you have all the textures.  Maybe forget about the texture.  Would registering an onBind observable for each material maybe be the callback to use? 

If you liked that image, I made another one a while ago when I was attempting in Blender to merge textures to reduce draw calls that went horribly wrong.  Enjoy!  I have had some experiments, especially in animation dev, that I wish I had saved.  They were just spectacular.

Selection_221.png

Link to comment
Share on other sites

found the problem.  It was how I filled in the extra constructor args to get to the onload callback arg.

", false, true, B.Texture.TRILINEAR_SAMPLINGMODE" worked.  Not sure why .  The typescript signature is :

constructor(
    url: string, 
    scene: Scene, 
    noMipmap?: boolean, 
    invertY?: boolean, 
    samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE,
    onLoad: () => void = null, 
    onError: () => void = null, 
    buffer: any = null, 
    deleteBuffer: boolean = false)

wouldn't the implied default for noMipmap & invertY be false?  Must admit I did not think these were inverted.

Link to comment
Share on other sites

Yes, an "arg = true" syntax is much easier, especially with negative arguments like "noMipmap".  BTW what is the default for that?

I have it in computer built code now. Using isVisible instead of enabled gave exact results.  Any Mesh in a TOB export JS file now pops into being all at the exact same time along with any children.  Very clean.  I also put a hook in to let any hand written base class use the function "grandEntrance" t instead.  This will handle any beam-in, or kung-fu  animation shit you can dream up.

var waitingMeshes = [];
var pendingTextures = 0;
function onTexturesLoaded(){
    if (--pendingTextures > 0) return;
    for (var i = 0, len = waitingMeshes.length; i < len; i++){
        if (typeof waitingMeshes[i].grandEntrance == "function") waitingMeshes[i].grandEntrance();
        else waitingMeshes[i].isVisible = true;
    }
    waitingMeshes = [];
    matLoaded = true;
}

I have just looked up what a Mipmap is.  Pretending anyone but me uses Tower of Babel exporter, would a "Use Mipmap" check box be good?  Do not think it could not be in the .babylon exporter, sorry.

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