Jump to content

Having position/rotation issue with MergeMesh()


Recommended Posts

So basically, I am importing meshes from a babylon file; they are tiles for a maze.

I clone them to create several rotation-wise clones of these tiles and obtain the full set of tiles.

Then based on the results of a maze generator I clone the tiles of my full set of tiles in the right positions to make my maze.

Here the results before merge (sorry for the ugly materials and textures, I was testing stuffs):

opera_2018-11-23_19-40-35.thumb.png.c883b55aef81d77373501cb9b759b2d9.png

And here is the hierarchy:

image.png.2736bd2448d1cfbc151b8c6419baadcb.png

 

So now, I am going through all the meshes of the maze, and I skip the meshes with 0 vertices while putting the meshes with same material in the same group, then I merge the different group of meshes:

MergeMeshes(){
    let groupedMeshes: IDictionary = {} 
    let children = this.root.getChildMeshes(false);
    
    for (let mesh of children){
        if(mesh.getTotalVertices() == 0)
            continue; //  go to next mesh
        if(mesh.material){
            if(!(mesh.material.name in groupedMeshes) )
                groupedMeshes[mesh.material.name] = []
            groupedMeshes[mesh.material.name].push(mesh);
        }
    }

    for (let key in groupedMeshes){
        let newMesh = BABYLON.Mesh.MergeMeshes(groupedMeshes[key], true);
        if (newMesh)
            newMesh.parent = this.root;
        }

    let exparents = this.root.getChildTransformNodes();
    for(let parent of exparents){
        if(!parent.name.endsWith('merged'))
            parent.dispose();
    }

}

And I get my meshes merged into that:

opera_2018-11-23_19-39-30.png.11fcab73268f03a72c17fa0d9bd4a091.png

But good news, the grouping by material did worked:

image.png.511af301e701a8ba61c07a8aca8093a3.png

 

I cannot find out what I am doing wrong! 😕 my code was working with mock tiles I was generating with code.

Hence I have one main suspect:

- the fact that I clone clones! (I make clones of my tiles to create the full tileset, then I clone these tiles, which can be original meshes or clones, to make the maze)

I believe that when I merge clones of clones, the merge function is taking into account the position of the first clone (which here was [0,0,0]), explaining the results above.

To test my hypothesis, I set the position of the clones of my tileset in a particular fashion to see if the resulting merged mesh will be different / dependent on these new positions.

Here are the positions that I used for the tileset clones (instead of having them all at [0,0,0]) (the top row are original meshes, while the other rows are rotated clones):

image.thumb.png.b1f20ed7991ce06366711fe205c790ff.png 

Now here is the results of the mergeMeshes() function when it tries to merge the same maze as on the first screenshot:

opera_2018-11-23_20-18-53.thumb.png.54fb000a3bc9ea7d39dee0f46999d462.png

 

I think that there is obviously something weird happening when merging clones of clones. It seems that the merge function is mixing the position of the tiles in the tilesets and the position of the tiles in the maze.

 

I think that a simple solution would be to transform the clones in my tileset into real meshes. Any idea of how to transform a clone into a real mesh?

 

Link to post
Share on other sites

Yeah!!

I was so sure there was some function like that and I was playing around without success with the auto-completion yesterday writing things like "unique", "makeUnique".

I am happy to know that I was not crazy after all, gonna have another try today and let you know if it worked.

NOTE for others: It looks like that the issue I have with merging clones of clones happens also when making instances of clones. 

Link to post
Share on other sites

I made it works!! Finally!

So this is definitely because my tile's parents were exported as meshes and not as transform nodes!

I replaced the parent Mesh of my tiles with a TransformNode like so:

if(newtile.getChildMeshes()[0]){
    // NOTE that it works without making the Geometry Unique!
    newtile = newtile.getChildMeshes()[0] //.makeGeometryUnique(); 
}

let root_tile = new BABYLON.TransformNode('root.'+name, newtile.getScene());
newtile.parent = root_tile;

And it worked!

 

Hence, I guess there are two things that might be asked:

- is that normal that the merge works when I use TransformNodes as parent of my tiles but fails (i.e. does weird things) when using Meshes as parent of my tiles?

-  could the Blender exporter export the Empty as TransformNode? It seems to make sense.

Link to post
Share on other sites
On 11/24/2018 at 6:17 PM, Nodragem said:

is that normal that the merge works when I use TransformNodes as parent of my tiles but fails (i.e. does weird things) when using Meshes as parent of my tiles?

I guess it depends of the code and it might also be cloning from the clone as moving to a new transformNode kind of break the clone of clone ?

On 11/24/2018 at 6:17 PM, Nodragem said:

could the Blender exporter export the Empty as TransformNode? It seems to make sense.

Sounds like a nice plan but not sure you would not have the same clone troubles ???

Link to post
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...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...