ProfessorF Posted December 20, 2013 Share Posted December 20, 2013 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: 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); }); 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: 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: 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!!! A closeup: 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. Quote Link to comment Share on other sites More sharing options...
ProfessorF Posted December 20, 2013 Author Share Posted December 20, 2013 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; } } } } }); Quote Link to comment Share on other sites More sharing options...
ProfessorF Posted December 20, 2013 Author Share Posted December 20, 2013 Sorry, for the long post, but is this the way to do it? I don't want to place the trees, bushes, stop signs, etcs, and then find out that this is the wrong (or long) way to do it!!! Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 20, 2013 Share Posted December 20, 2013 The thing I can suspect here is that children objects have already a position and rotation. So that, they are offseted from their parent. Could you try to set the right position on the parent AND set the children to 0,0,0 ? GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
reddozen Posted December 20, 2013 Share Posted December 20, 2013 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.... Quote Link to comment Share on other sites More sharing options...
ProfessorF Posted December 20, 2013 Author Share Posted December 20, 2013 Alright! Thanks for the tips DeltaKosh & RedDozen. The neighborhood loaded fine without any stray meshes this time: The steps were: 1. Load mesh2. Create array of x,y,z,rot3. 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; } } } } } GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Dad72 Posted December 21, 2013 Share Posted December 21, 2013 @ProfessorF : Could you put your code in the tag [/ code]. It would make the code more enjoyable to read. Thank you in advance. Quote Link to comment Share on other sites More sharing options...
ProfessorF Posted December 21, 2013 Author Share Posted December 21, 2013 oh sorry about that. Didn't know that. Thank you, will do. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 21, 2013 Share Posted December 21, 2013 done Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.