Jump to content

Materials and OBJ loader


Recommended Posts


I am rather new to Babylon. I am using the AssetManger to load OBJ/MTL. I am executing my renderloop in the onFinish callback on the AssetManager. I want to make adjustments to the material created when the MTL-file was loaded (adjust the emissive color). However, it appears that the materials are not yet created when onFinish is called (i.e. they are still undefined). How do I know when the materials have been properly created so that I can make my adjustments?

I have something along the lines of this,

const scene = createScene(); // Setup camera etc.
const loader = new BABYLON.AssetsManager(scene);

const model = loader.addMeshTask("model", "", "source/", "Model.obj");
model.onSuccess = (t) => {
  t.loadedMeshes.forEach((m) => {
    m.position.x = 0;
    m.position.y = 0;
    m.position.z = 0;

loader.onFinish = (task) => {
  // task[0].loadedMeshes[0].material is undefined...
  engine.runRenderLoop(() => {




Link to comment
Share on other sites

Hi bob!  Sorry about the problems.  No errors seen on console network areas... like "404" for the .mtl file?  Wouldn't the texture be loaded separately with a textureTask?  Not sure.  I'm not very experienced with .obj and .mtl loading, but I found this monster playground demo... with all sorts of object-loader crap.  In fact, it might contain the entire obj loader SYSTEM.  I don't know who coded it or what its purpose, but take a look http://www.babylonjs-playground.com/#1S9WC2#5

Material/Texture .isReady might not be activating, but maybe the mtl isn't even loading correctly.  Check your stuff... and I'll keep thinking.  Sorry for the slow and bad reply.  I'll do some studying.

Link to comment
Share on other sites

Thank you for your suggestions Wingnut.

It does not appear to be an issue with fetching the resources from the server. Everything loads nicely, both the OBJ and MTL files are loaded and rendered appropriately. However, I don't know where I should hook in my logic to alter the materials that were automatically created from the MTL file? I would think there would be a way to observe the material state and wait till it is ready (and the textures loaded etc.) and then make modifications to the material properties, but I am not sure how. I have browsed the documentation and currently I am going through the sourcecode of the file loader.

It appears to be the way material loading has been implemented in the _parseSolid method in babylon.objFileLoader.ts where the materials are loaded async while the function returns as soon as the meshes have loaded (leaving the material callback hanging without anyway to know when it is done...). See this file on Github. Can anybody confirm this?

(The code snippet you referred to, Wingnut, appears to be an (early?) implementation of the obj loader).

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.

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.


  • Recently Browsing   0 members

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