Jump to content

Obj file loader:create one mesh with multi-material got wrong


fdeng
 Share

Recommended Posts

Hi everyone,

In the obj file, when an object has multi-material, the obj file loader will create several meshes.

image.png.44991f8000ae962343260610e418a0e6.png

I want to only create one mesh with multi-material, but got the wrong result:

image.png.165b9e8c7e7f1fb10e86b9c9d7ac38e0.png

I attached the obj file and the new obj file loader I modified(objFileLoader_new.js ), can anybody help to have a look why it is wrong. It troubled me a whole day now.

obj.rar

Link to comment
Share on other sites

@kcoley

What I want is not a really a single mesh, but a mesh with BABYLON.MultiMaterial, it will have multiple submesh.

my code is like:

                if (handledMesh.materials.length > 1) {
                    babylonMesh.subMeshes = [];
                    var multimat = new BABYLON.MultiMaterial("multi", scene);
                    babylonMesh.material = multimat;
                    for (var i = 0; i < handledMesh.materials.length; i++) {
                        var submat = materialsFromMTLFile.materials[handledMesh.materials];
                        multimat.subMaterials.push(submat);
                        var submesh = new BABYLON.SubMesh(i, 0, handledMesh.positions.length/3, handledMesh.spans[0], handledMesh.spans[1], babylonMesh);
                    }
                }
                else {
                    babylonMesh.material = materialsFromMTLFile.materials[handledMesh.materials[0]];
                }

You can have a look of my entire code in the attachment in the top post

Link to comment
Share on other sites

@fdeng I took a quick read-through and one thing that seems to be missing is adding the submesh to the your handledMesh array:

Something like this:

handledMesh.subMeshes.push(submesh);

 

This playground may be a good reference in getting MultiMaterials to work with multiple submeshes if you haven't seen this one yet.

https://www.babylonjs-playground.com/#28EUMN#21

 

Let me know if that helps.

Link to comment
Share on other sites

Hi @kcoley

It seems not the reason, when create a submesh like below, 

var submesh = new BABYLON.SubMesh(..., mesh);   

Actually the created submesh will be pushed to subMeshes array of the parent mesh automatically.

You can verify it in  this new PG:

https://www.babylonjs-playground.com/#28EUMN#24

I removed these push actions:
sphere.subMeshes.push(submesh1);
sphere.subMeshes.push(submesh2);
sphere.subMeshes.push(submesh3);
sphere.subMeshes.push(submesh4);
sphere.subMeshes.push(submesh5);
sphere.subMeshes.push(submesh6);
 
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...