JonathanRev Posted October 20, 2017 Share Posted October 20, 2017 Hello good day team from babylon , i am really glad to have known babylon as well, it is a great tool , i am currently having some issues when using : ground.getHeightAtCoordinates(x, z) , i am currently using the export Toolkit that exports from Unity to Babylon js . i have imported the meshes to my scene using the "assetsManager" this way : var stones = assetsManager.addMeshTask("stones", "", "assets/stones/", "stones.babylon"); stones.onSuccess = function(task) { task.loadedMeshes.forEach(function(mesh) { mesh.isVisible = false; var range = 1024; var count = 40; for (var index = 0; index < count; index++) { var newInstance = mesh.createInstance("j" + index); var x = range / 2 - Math.random() * range; var z = range / 2 - Math.random() * range; var y = ground.getHeightAtCoordinates(x, z) ; // Getting height from ground object newInstance.position = new BABYLON.Vector3(x, y, z); } }); } The idea of using : ground.getHeightAtCoordinates(x, z) was to set each of my meshes in this case "stones" to the ground level on a random location on my scene . The problem starts when i import 1 or more meshes for example a "Palm.babylon or Plant.babylon" that i want to set it in random locations on my scene as well on ground level, then i instanciate and use "var y = ground.getHeightAtCoordinates(x, z) ;" the same way as the stones , but when i load the scene all the meshes doesn't load and i have to reload multiple times so it can appear all the meshes , and sometimes all the meshes never appear . Surprisingly when i replace : ground.getHeightAtCoordinates(x, z) to another high for example : 40 or another number , all the meshes appears correctly , that's why i suspect of getHeightAtCoordinates(x, z) function , Not sure if i am doing the correct importing ? , or it has to be something to Unity exporter? or getHeightAtCoordinates() ? , Anyone have an idea of what could be ocurring ? I wanted to add that if i increment the range of the meshes to be on the scene for example my ground is 1024*1024 and the range of my sheshes is 2048*2048 , all the meshes that are outside the ground appears correctly and the meshes inside the ground doesn't appear , any ideas? maybe it has to be something about memory? Quote Link to comment Share on other sites More sharing options...
jerome Posted October 21, 2017 Share Posted October 21, 2017 In your snippet we don't know anything about how you built your ground. If your ground is created from a heightmap for instance, you must be sure it's already initialized after the heightmap image download by using the callback function parameter of the groundFromHeighMap constructor static method (this is a common error). The best way to solve your problem would be : - to isolate the issue : ex first, not importing anything and using getHeightAtCoordinates(), then if ok, adding the import, etc - to reproduce it in a PG so we could check the code Quote Link to comment Share on other sites More sharing options...
BitOfGold Posted October 22, 2017 Share Posted October 22, 2017 I think the issue is, when x and z is over the edge of the ground. getHeightAtCoordinates does not give you 0 result or something but an error. Does it give an error on the console? Quote Link to comment Share on other sites More sharing options...
JonathanRev Posted October 23, 2017 Author Share Posted October 23, 2017 Good day / night guys , i think i solved it ! @jerome you were totally right , i noticed some of the meshes were loading before the heighmap load completely , that's why i needed to reload many times so all items appears on the scene : This was the way i was creating the ground : var ground = BABYLON.Mesh.CreateGroundFromHeightMap("groundterrain", "img/heightmap.png", 1024, 1024, 100, 5, 80, scene, true); I solved it this way , correct me if i am wrong please , first of all i used the : onReady method , as i told you before , i am using the "assetsManager" , at the beginning , i was doing this : ground.onReady = function () { var stones = assetsManager.addMeshTask("stones", "", "assets/stones/", "stones.babylon"); stones.onSuccess = function(task) { task.loadedMeshes.forEach(function(mesh) { mesh.isVisible = false; var range = 1024; var count = 40; for (var index = 0; index < count; index++) { var newInstance = mesh.createInstance("j" + index); var x = range / 2 - Math.random() * range; var z = range / 2 - Math.random() * range; var y = ground.getHeightAtCoordinates(x, z) ; // Getting height from ground object newInstance.position = new BABYLON.Vector3(x, y, z); } }); } } But i noticed the meshes were not appearing with that way, any ideas why ? So , ... i solved it this way instead , adding the assetsManager.load() inside onReady method: ground.onReady = function () { assetsManager.load(); } Now everything loads completely without issues or delay. Btw as you can see i am using the assetsManager , but i also tried using the SceneLoader.ImportMesh this way: ground.onReady = function () { BABYLON.SceneLoader.ImportMesh("SmallTree1", "assets/SmallTree1/", "SmallTree1.babylon", scene, function (newMeshes) { }); } This method also load perfect too , as you can see in the code above i can include the BABYLON.SceneLoader.ImportMesh inside the onReady method , and in this case it works perfect not like the assetsManager.addMeshTask that were not loaded. what do you think guys about this methods ? , i don't think it will impact on performance . don't it ? , and sorry if this question is noob but which method on importing objects is the best assetsManager or SceneLoader.ImportMesh ? , in which case do you think is the best on using each? , thanks for your support guys ! i am really grateful to have a great comunity on babylon ! 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.