Jump to content

Merge meshes


juanmajr93
 Share

Recommended Posts

Hi, I would like merge all meshes of obj file. This is my code, and shows me and error when I use the function MergeMeshes()...

 

var t = 0;
  edificios_texto.forEach(function() {
  var loader = new BABYLON.AssetsManager(scene);
  var edificio = loader.addMeshTask(t, "", "http://localhost:8080/modelos/"+edificios_texto[t]+"/",edificios_texto[t]+".obj");
  var mergeEdificio = [];
  edificio.onSuccess = function (task) {
     var k=0;
     task.loadedMeshes.forEach(function(b) {	
        b.computeWorldMatrix(true);
        mergeEdificio[k] = b
        k++;
					
     });
  }
BABYLON.Mesh.MergeMeshes(mergeEdificio);
loader.load();

 

Link to comment
Share on other sites

:)  Cool convo.  @juanmajr93 - don't forget about .parent (instead-of merge).  You can set your mesh.parent = commonMesh ( commonMesh would be an extra plane or box... possibly with .visibility = 0)

Now you have "connected" all your mesh (to commonMesh), yet NOT merged any.  Each slave-mesh you connected (parented)... can have different material.  Yet all will rotate, position, and scale... commonly... simply by doing those things to commonMesh.  You will still be able to rotate, position, and scale each INDIVIDUAL mesh, too,

CommonMesh is sometimes called master, parent, handle, gizmo, etc.  A common mesh that all other mesh parent-to.  *shrug* 

I love parenting feature.  Very very handy.

Link to comment
Share on other sites

How can I do it Wingnut? I have many meshes (25 for each building). You say me that one of them was commonMesh and the rest were connected to this parent so I had only one mesh for each building... GREAT!!

I understand you but I dont know how?¿? At the first post I have shown my code, could you help me?

Thanks

Link to comment
Share on other sites

Hi J.

  I'm not sure how.  :)

Actually, I have some experience with AFTER-IMPORT parenting-to-commonMesh.  (not much experience, though)  :)

http://www.babylonjs-playground.com/#M0UJB#3

The pretend building walls (the 4 discrete-material colored boxes)... retain their OWN material. 

And, they retain their own scaling, rotation, and position... IF the parent is at position 0,0,0, rotation 0,0,0, and scaling 1,1,1... when you set parent.  The parent must have no position, rotation, or non-default scaling... at the time of parenting (in order to keep building parts oriented same as in modeler). 

After parenting all 25 parts to commonMesh parent, then you can go crazy... positioning, rotating, and scaling parent.  Entire building (all parts) will follow and stay proportioned.

So, you would do successful import of all 25 mesh (1 building). Then...

 edificio.onSuccess = function (task) {
    var after_import_parent_mesh = BABYLON.Mesh.CreateBox("commonMesh", 2, scene);
    after_import_parent_mesh.visibility = .2; // set 0 for invisible

    task.loadedMeshes.forEach(function(b) {	
        b.computeWorldMatrix(true);
        b.parent = after_import_parent_mesh;
     });
  }

(I hope I didn't say anything incorrect.  I make mistakes often.)

Some might call this "late parenting".  It is done late in the loading sequence.  Early parenting might be done inside the modeling software.

I have little experience in parenting to a commonMesh while using modeling software.  I don't use modeling software often.  I don't know how well "early parenting" travels through export->import.  Sorry.   Others may comment.

If you want to import... say... 10 buildings in same file (250 total parts), you might want to specially name each building part.  Example: b08_west_wall, b04_east_wall, etc.  THEN, after import, you will need to use custom code to set parent of all building b08 parts... to b08 commonMesh.  Same with building b04... each is parented to the commonMesh for building 04.  You would do this by using JS match and substring operations... on each imported mesh.name property.

Quite a bit more "late" after-import work/code, but it can be done.  Perhaps it is wiser to put each building into a separate .babylon file, and import all 10 .babylon files.  Test and experiment, and ask more questions if you wish.  Good luck.

Link to comment
Share on other sites

Thanks @Wingnut by all your advices. The last of them is very interesting. You say me that put each building into a separate .babylon file and import it instead of load all obj models in my scene. However, I dont know how can use .babylon file and how this model can be loaded into a separate .babylon file. Maybe BLENDER could export model to .babylon file. Finally, do you consider that by this way I could get a faster load of my scene?

 

JMJR

Link to comment
Share on other sites

My pleasure.

I think for fastest performance... while in Blender, make entire building be a single model (combine? group? convert-to-editable-mesh?)  You should still see many different materials on building... all looking proper.  But only one "mesh".  Not sure how to do that.

Then, use unwrap UVs map or something similar.  Create a single texture (an atlas)... that contains ALL the textures needed for that single building.

Now export that single model and single big texture atlas.  Fast load, all texture in-place, all should be good.

BUT... if you want to allow webGL user to change carpet, change kitchen linoleum, change building siding material, etc... you are in trouble (if you use single mesh method).  Also, if you want to change a door, or rotate a wall (to reveal a secret room behind a wall)... that will be much more difficult (because building is one big model, not many parts).

Yes, you gain speed and reduce bytes when you use Blender "compacting" into single mesh, but you lost some future user/programmer adjust-ability.

So, it is difficult for me to determine which methods are best for you.  I don't know what features/power you plan for self/users.  Try ALL ways... and compare speeds. :)

Link to comment
Share on other sites

Hi, @Wingnut the application that I am developing is only to be shown so there arent any transformation of different parts of mesh. I have done many things with blender and the result is better however I must create a single texture to each model to simplify more...( I would like to get only 1 mesh to each building)

Do you know any way to create this single texture? and after I will try to use spstexture as @jerome said me...

Link to comment
Share on other sites

Hi again, I got it a unique texture to all parts of my building. I have made with blender. However, I have the same problem. I have the same numbre of meshes and if I try to merge them again the model is loaded without textures. Like you said me I consider that I am using an 1 material so I dont understand this. I will share you the obj file and mtl with texture. Please if you could test it because it is so important getting for each building an unique mesh.

 

edificio.rar

Link to comment
Share on other sites

Yep, looking good.  Only one mesh, right?  No mesh merging needed, if a single mesh.

Does your .babylon file have a section called UVS?  Those tell the texture HOW to wrap back-onto the single mesh.

No errors on console? 

Are you loading .obj/.mtl file (with BJS objectLoader)... or are you loading .babylon file?  Best to export from Blender... to .babylon file... I think.

I am not very good at this part.  I hope others will help, soon.  :)  Keep experimenting and do forum searches... might help.

Link to comment
Share on other sites

  • 2 weeks later...

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