With Babylon.Mesh.MergeMeshes Meshes Broken

hello Everyone,

Again I would like to have your help with MergeMeshes. The problem is when I try to merge my Different Material Meshes it adds on an extra material meshes and is displayes as shown below

without mergeMeshes would look like this as show below (which actually as i want) but i want to use MergeMeshes and get the exact same

the code below without MergeMeshes:

var rearWall = this.wallFactory.createWall(scene,EWallType.eInsideWallDoor, room.presentation.length, room.presentation.width );
var frontWall = this.wallFactory.createWall(scene, EWallType.eOusideWallWindow2, room.presentation.length, room.presentation.width);
var rightWall = this.wallFactory.createWall(scene, EWallType.eInsideWallSimple, room.presentation.length, room.presentation.width );
var leftWall = this.wallFactory.createWall(scene, EWallType.eOutsideWallWindow1, room.presentation.length, room.presentation.width );
var floor = this.wallFactory.createWall(scene, EWallType.eFloor, room.presentation.length, room.presentation.width);
var ceiling = this.wallFactory.createWall(scene, EWallType.eCeiling, room.presentation.length, room.presentation.width );

which has data for each Mesh

the code below is with MergeMeshes:
var rearWall = this.wallFactory.createWall(scene,EWallType.eInsideWallDoor, room.presentation.length, room.presentation.width );
var frontWall = this.wallFactory.createWall(scene, EWallType.eOusideWallWindow2, room.presentation.length, room.presentation.width);
var rightWall = this.wallFactory.createWall(scene, EWallType.eInsideWallSimple, room.presentation.length, room.presentation.width );
var leftWall = this.wallFactory.createWall(scene, EWallType.eOutsideWallWindow1, room.presentation.length, room.presentation.width );
var floor = this.wallFactory.createWall(scene, EWallType.eFloor, room.presentation.length, room.presentation.width);
var ceiling = this.wallFactory.createWall(scene, EWallType.eCeiling, room.presentation.length, room.presentation.width );

      var pilot = this.BABYLON.Mesh.MergeMeshes([rearWall, frontWall, rightWall, leftWall, floor, ceiling]);

pilot.position = new this.BABYLON.Vector3(room.presentation.x, room.presentation.y, 0);

"i want to merge all my meshes as i want treat them as a single mesh"

Also, is there perchance a better way to create a bunch of simple meshes, apply a specific material to each of them and then ‘merging’ them into 1 bigger BABYLON.Mesh without losing their individual texture/material? MergeMeshes destroys the materials of the children objects, which is quite bad :/.

Hey, please create a pg here https://playground.babylonjs.com/, where to expose the issue. I’m almost sure this is not related to the materials, but is impossible to say from 2 images and 10 lines of code…

1 Like

Hi,

Its completely in Angular with json-server running in the backend, just creating a PG will not be sufficient for what im builiding the project according to my requirements

you can clone this and have look at the code , then ul get a rough idea what exactly im trying to do

@MarianG it would be help full that if you have look at the code and help me out ,if u have any questions please do ask me i explain you what it exactly is.

Thanks in Advance

Please make a playground which shows the error, not your entire project.
Thanks.

2 Likes

Hey @alphaShash, hard to say, but I’ll try to help you a little

  1. Try to add this option

    sideOrientation: BABYLON.Mesh.DOUBLESIDE
    

on each ExtrudePolygon function
2. Try to merge all the time the objects with the same material. I mean if you gave 6 wall which have 2 different materials, try to merge them based on material, in 2 different objects and not only one.
If you want to merge different meshes with different materials in one mesh all, then you have to use multimaterial. Take a look at this pg
https://playground.babylonjs.com/#INZ0Z0#61-merge 2 materials wirthout multimat
https://playground.babylonjs.com/#INZ0Z0#62-merge 2 materials with multimat.
From what I saw I think youll need this :wink:
Cheers!

1 Like

@MarianG Thank you for the help, i tried this option but still its not working.
I figured it out the problem is from this piece of code which comes after merging all the meshes

mesh.position = new this.BABYLON.Vector3(room.presentation.x, room.presentation.y, 0);

with the position, where the position of the complete room (in the end looks like a cube) can be set only according to x and y axis which is being fetched from JSON file

{
“nummer”: “1.2”,
“presentation”: {
“x”:0,
“y”:0,
“length”: 5,
“width”:2,
“walls”: {
“north”: “eInsideWallSimple”,
“south”: “eOutsideWallWindow1”,
“east”: “eInsideWallDoor”,
“west”: “eOusideWallWindow2”,
“floor”: “eFloor”,
“ceiling”: “eCeiling”
}
}
},

if i comment this piece of code it works all fine but then the problem would be it will not work according to this JSON data

then i cant set the position of a complete room (in the end looks like a cube) according to this JSON x and y axis position, as i will be requiring n number of rooms.

Basically the idea is when we copy again the above json data with a different x and y axis position it should display another complete room in a different position.

I hope you got my point, what im trying to do here.

Thanks in advance :slight_smile:

1 Like

Why not creating a simple repro in the playground, this is your best chance to involve the entire community and unleash its power :slight_smile:

ok can u plz guide me how to upload extra files like json file.

Thanks in advance.

You do no need that in the PG, the only important thing is to demo the broken behavior. If you add all your code in the PG it is similar to looking at your sources. You need to only hard code creating the differennt pieces and calling merge mesh on them.

This should be all good.

Sure Thank you i will create, but my problem has i have mentioned before if i dont call from Json file for positions, MergeMeshes works totally fine. When i call this MergeMesh position from JSON file only then its broken.

But ill create a play ground

Instead of the JSON file in the PG use the data in an object that is like one that would come from the JSON file and build your room from that.

1 Like

Hey!@ MarianG
I have the same problem as he @ alphaShash, I need to merge my subgrids into one grid, but it seems a bit troublesome
image
https://www.babylonjs-playground.com/#4AJ16M#180

Hi. You can parse each material and merge all the meshes by material.
Something like this: https://www.babylonjs-playground.com/#4AJ16M#181
Or you can merge all of them as a single mesh with multimaterial, like here
https://playground.babylonjs.com/#INZ0Z0#62

2 Likes

Just to add to this thread… BABYLON.Mesh.MergeMeshes() has a parameter for preserving mesh material.


https://playground.babylonjs.com/#INZ0Z0#59
https://doc.babylonjs.com/how_to/how_to_merge_meshes