Jump to content

Recommended Posts

Hi Guys,

I am new to Babylon and been given a task to apply pbr Material to obj files after they are imported in an already existing babylon project.

I cant figure it out how to do this and I was thinking if any of you lads could help me on this. This project contains two js files, one called render and other one is loadfile. I dont know how and in which class to define PBR material , adjust the PBR attributes and where to force the loaded obj files to use the defined PBR material. I would be grateful if you help me on this.



Link to comment
Share on other sites

Hi - Welcome to the forum!  What you are asking is going to depend on a couple of factors.

1. Version of BabylonJS  OBJ loader
2. If your OBJ files are already loading a Material.

So, if you look at the OBJ loader latest version, the parseSolid() method waits until all the materials are loaded before returning (blocking):

I'm assuming you are on an older version, since it's an existing project and this is a really new change.

What this means for working with an OBJ file with material using an older version of BabylonJS can be quite confusing, because if you assign a material in your function initmesh( mesh ) then it will be overwritten when the MTL file loads asynchronously.  You can see if your OBJ file is loading materials from the OBJ file text contents, but you can also see in the network tab if a .MTL file is being loaded.  I'm pretty sure that if you set a material in initmesh(mesh) and the MTL file loads on older versions that the PBR material you assign in initMesh() will get overwritten - at least on slower connections.

So, 3 options that I can see:
1. Upgrade OBJ Loader. Set PBR material in initMesh function.
2. Remove materials from OBJ files.  You should do this anyway to save bandwidth, if the materials aren't needed.  For any OBJ loader version.  Set PBR Material in initMesh.
3. Write a hacky looping setTimeout() from initMesh() that checks if your mesh has a material assigned.  This is really not a good option - especially if the MTL file failed to load it would never complete.  This option is more to understand the async nature of Materials loading.

You will need to figure out a way to see if the meshes should have a PBR material on them.  You can use mesh.name or some other way.  Then follow this playground (PG):

Link to comment
Share on other sites

You could have a giant switch statement in your initmesh, but maybe better to flow something through from the original caller.  The other problem with initMesh is you don't know which OBJ file, so you could have the same 'name' on a mesh from different OBJ files.  You'll perhaps want to start with on your onSuccess() callback declaration:

task.onSuccess = function(task) { task.loadedMeshes.forEach( initmesh, this );};

I think I would change that a bit to something like - make sure the correct 'this' context is going through and you can use ES6 arrow functions:

task.onSuccess = (task) => {
  task.loadedMeshes.forEach(loadedMesh => initMesh(loadedMesh, arrayPBR))
Then you could do something like:
loadobj( "P_MedDensitySideText", "Side Text" , ["pbr_mesh_name_1", "pbr_mesh_name_2")

Add a param to initMesh for the mesh names (mesh.name in array) to add the PBR material.  Something like that maybe.

Link to comment
Share on other sites

Not sure if this will answer any questions, but this is how people done it, back in the good old days: http://www.html5gamedevs.com/topic/24210-textures-not-loaded/
they call me - the M a s t e r of cloning.

Links are broken, was hosting stuff on Gdrive, and it quit some of the service (or something)



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