Jump to content

Applying textures to loaded objects


sociofob
 Share

Recommended Posts

Hi,I have hard times with asynchronous mesh loading.

I need to apply textures to meshes right after meshes are ready. I'm importing in this way:

        var loader = new BABYLON.AssetsManager(scene);
        someLoader = loader.addMeshTask("roulette", "", "", "rouletteTable.obj");
        anotherLoader =loader.addMeshTask("chip", "", "", "blackChip.obj");
        someLoader.onSuccess = function (t) {
            t.loadedMeshes.forEach(function (m) {
                tableMeshArr.push(m);
            });
        tableMeshArr[0].material = materialWheel;
        tableMeshArr[1].material = materialRoulette;
        tableMeshArr[2].material = materialBlack;
        
        };
        anotherLoader.onSuccess = function (t) {
            t.loadedMeshes.forEach(function (m) {
                chip=m;
                chip.material=materialChips;
            });
        };
        loader.onFinish = applyObjTextures;
        loader.load();


 var applyObjTextures = function () {
        tableMeshArr[0].material = materialWheel;
        tableMeshArr[1].material = materialRoulette;
        tableMeshArr[2].material = materialBlack;
        chip.material = materialChips;
    };

But it applies only to tableMeshArr[0] mesh.

onFinish  Callback works right after finishing importing first mesh, but I need a callback when all meshes are loaded.

Link to comment
Share on other sites

I supposed, the problem is in applying texture to mesh before texture is loaed.

Obviously the solution here is to wait for loading, checking texture.isReady() time by time until it happens, but, it doesn't work in my case:(

 this.applyMaterials=function(){   
        if(wheelTexture.isReady&&rouletteTexture.isReady){
            blackChipMesh.material=materialChips;
            redChipMesh.material=materialChips;
            blueChipMesh.material=materialChips;
            greenChipMesh.material=materialChips;
            whiteChipMesh.material=materialChips;
            plane.material = materialCarpet;  
            tableMeshes[0].material = materialWheel;
            tableMeshes[1].material = materialRoulette;
            tableMeshes[2].material = materialBlack;
         }
        else
        {
            setTimeout (applyMaterials, 1000);
        }
    }

I call this function onAssetsManager's onFinish callback

var meshLoader = new BABYLON.AssetsManager(scene);
//...
meshLoader.onFinish = onMeshLoadingFinish;
meshLoader.load();

But, I suspect this callback this callback dont work properly.

Also, I have some error message in console, so that can be connected. I get this error although meshes are imported.

Untitled.png

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