Jump to content

Moving and Rotating a Complex Model


ProfessorF
 Share

Recommended Posts

Sorry this is long, but the problem is kind of confusing.  Okay, so I have a neighborhood with many houses.  In unity3d or Maya, it looks something like this:

 

Bb3ISWlIQAAxdRy.jpg

 

But importing the neighborhood with all the houses is huge and causes problems. So I want to import the neighborhood separate, and then import / duplicate the houses.  Here is ONE neighborhood and ONE house BUT THE HOUSE IS NOT POSITIONED YET:

 

                BABYLON.SceneLoader.ImportMesh("", "neighborhood_terrain/", "neighborhood_terrain.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    newMeshes[0].position = new BABYLON.Vector3(0, 0, 0);  
                });
                BABYLON.SceneLoader.ImportMesh("", "house_A/", "house_A.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    newMeshes[0].position = new BABYLON.Vector3(0, 0, 0);
                });
 
post-5799-0-58446700-1387506567.png

 

The correct position for the house is (-431.1,0,-29.5)

The correct rotation is Math.PI/2

 

You would expect that this code works:

                BABYLON.SceneLoader.ImportMesh("", "neighborhood_terrain/", "neighborhood_terrain.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    newMeshes[0].position = new BABYLON.Vector3(0, 0, 0);  
                });
                BABYLON.SceneLoader.ImportMesh("", "house_A/", "house_A.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    newMeshes[0].position = new BABYLON.Vector3(-431.1, 0, -29.5);
                    newMeshes[0].rotation.y = Math.PI / 2;
                });
 
But it does not:
 
post-5799-0-96260000-1387506884.png
 

So I decided to transform all the meshes in a loop:

 

                BABYLON.SceneLoader.ImportMesh("", "house_A/", "house_A.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    //newMeshes[0].position = new BABYLON.Vector3(-431.1, 0, -29.5);
                    //newMeshes[0].rotation.y = Math.PI / 2;
                    for (j = 1; j < newMeshes.length; j++) {
                                newMeshes[j].position.x = -431.1;
                                newMeshes[j].position.y = 0;
                                newMeshes[j].position.z = -29.5;
                                newMeshes[j].rotation.y = Math.PI / 2;
                    }
 
                });
 

But this actually places the model TWICE AS FAR as it should be:

 

post-5799-0-51605500-1387507174.png

 

I decide to transform only child meshes, not parent meshes:

 

                BABYLON.SceneLoader.ImportMesh("", "house_A/", "house_A.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    //newMeshes[0].position = new BABYLON.Vector3(-431.1, 0, -29.5);
                    //newMeshes[0].rotation.y = Math.PI / 2;
                    for (j = 1; j < newMeshes.length; j++) {
                        if (newMeshes[j].subMeshes.length==0) {
                                newMeshes[j].position.x = -431.1;
                                newMeshes[j].position.y = 0;
                                newMeshes[j].position.z = -29.5;
                                newMeshes[j].rotation.y = Math.PI / 2;
                            }
                    }
                });
 

This Works!!!

 

post-5799-0-92317900-1387507470.png

 

A closeup:

 

post-5799-0-10934600-1387507641.png

 

So my question is: Is this really the proper way to move / rotate a complex mesh?  Is there an easier way?

 

The file was generated in Maya, so this will be a common "problem" for a lot of 3D modelers who use Maya.

Link to comment
Share on other sites

Finally, this is how I did five House A's, using Clone on the *parent* meshes

 

                BABYLON.SceneLoader.ImportMesh("", "neighborhood_terrain/", "neighborhood_terrain.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    newMeshes[0].position = new BABYLON.Vector3(0, 0, 0);  
                });
                BABYLON.SceneLoader.ImportMesh("", "house_A/", "house_A.babylon", scene, function (newMeshes, particleSystems, skeletons) {
                    var housedata = [ // These are in Right-Hand Coordinates, So don't forget to convert
                        [-431.1,0, 29.5, -90],
                        [-44.1, 0, 191.7, 0],
                        [-304.1, 0, 122.3, 90],
                        [-255.7, 0, 209.4, 270],
                        [-416.2, 0, 268.8, 58.9]                        
                    ];
 
                    for (var i = 0; i < housedata.length; i++) {
                        harray = housedata;
                        if (i == 0) {
                            for (j = 1; j < newMeshes.length; j++) {                               
                                if (newMeshes[j].subMeshes.length == 0) { // don't Clone the original mesh, just position
                                    newMeshes[j].position.x = harray[0];
                                    newMeshes[j].position.y = harray[1];
                                    newMeshes[j].position.z = -harray[2]; // for R->L coordinate
                                    newMeshes[j].rotation.y = -harray[3]*Math.PI/180;
                                }
                            }
                        } else {
                            for (j = 1; j < newMeshes.length; j++) {
                                if (newMeshes[j].subMeshes.length != 0) { // can't dup child meshes, dup parents
                                    c = newMeshes[j].clone(); // name?
                                    c.position.x = harray[0];
                                    c.position.y = harray[1];
                                    c.position.z = -harray[2];
                                    c.rotation.y = -harray[3] * Math.PI / 180;
                                }
                            }
                        }
                    }
                });
 
post-5799-0-21665200-1387509524.png
Link to comment
Share on other sites

I've had similar problems with my scenes. If you have any translations or rotations on your meshes before you export them from a 3D program, Babylon will assume that the in editor location is "0,0,0" so any additional translations after cloning will go from there.

 

Since I've been working on exporting my UV coordinates etc I haven't had time to revisit my own issues, but I've found it difficult to translate coordinates systems / scale from one format to Babylon.

 

Exactly what deltakosh is saying....

Link to comment
Share on other sites

Alright!  Thanks for the tips DeltaKosh & RedDozen. The neighborhood loaded fine without any stray meshes this time:

 

post-5799-0-24087900-1387580583.png

 

The steps were:

 

1. Load mesh

2. Create array of x,y,z,rot

3. Call cloneModel function

 

               
 BABYLON.SceneLoader.ImportMesh("", "house_A/", "house_A.babylon", scene, function (newMeshes, particleSystems, skeletons) {  // STEP 1                    var housedata = [ // STEP 2 These are in Right-Hand Coordinates, So don't forget to convert                        [-431.1,0, 29.5, -90],                        [-44.1, 0, 191.7, 0],                        [-304.1, 0, 122.3, 90],                        [-255.7, 0, 209.4, 270],                        [-416.2, 0, 268.8, 58.9]                                            ];                    cloneModel(housedata, newMeshes); // STEP 3                });    function cloneModel(data, meshes) {        for (var i = 0; i < data.length; i++) {            var harray = data[i];            if (i == 0) {                for (var j = 1; j < meshes.length; j++) {  // JUST MOVE THE FIRST MODEL                    if (meshes[j].subMeshes.length != 0) { // MOVE PARENTS                        meshes[j].position.x = harray[0];                        meshes[j].position.y = harray[1];                        meshes[j].position.z = -harray[2]; // for R->L coordinate                        meshes[j].rotation.y = -harray[3] * Math.PI / 180;                    }                    else { // ZERO OUT CHILDREN per DeltaKosh                        meshes[j].position.x = 0;                        meshes[j].position.y = 0;                        meshes[j].position.z = 0;                         meshes[j].rotation.y = 0;                    }                }            } else {                for (var j = 1; j < meshes.length; j++) { // CLONE THE REST OF THE MODELS                    if (meshes[j].subMeshes.length != 0) { // can't dup child meshes, dup parents                        var c = meshes[j].clone(); // name?                        c.position.x = harray[0];                        c.position.y = harray[1];                        c.position.z = -harray[2];                        c.rotation.y = -harray[3] * Math.PI / 180;                    }                }            }        }    }
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...