Hagop Posted September 3, 2015 Share Posted September 3, 2015 ok I am trying to create a loop using javascript for function in order to import the same mesh multiple times but with different XYZ coordinates.The outcome of my code below shows only one instance of the mesh.What am i doing wrong ? Here is the code: <body> <canvas id="renderCanvas"></canvas> <script> window.addEventListener('DOMContentLoaded', function(){ // get the canvas DOM element var canvas = document.getElementById('renderCanvas'); // load the 3D engine var engine = new BABYLON.Engine(canvas, true); var createScene = function () { var scene = new BABYLON.Scene(engine); //Adding a light var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene); //Adding an Arc Rotate Camera var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene); camera.attachControl(canvas, false); for (i=0; i<5; i++) { BABYLON.SceneLoader.ImportMesh("", "", "pantene_low.babylon", scene, function (newMeshes) { // Set the target of the camera to the first imported mesh if(i==4) { camera.target = newMeshes[0]; } newMeshes[0].position.x = i+10; }); } // Move the light with the camera scene.registerBeforeRender(function () { light.position = camera.position; }); return scene; } // call the createScene function var scene = createScene(); // run the render loop engine.runRenderLoop(function(){ scene.render(); }); // the canvas/window resize event handler window.addEventListener('resize', function(){ engine.resize(); }); }); </script></body> Quote Link to comment Share on other sites More sharing options...
adam Posted September 3, 2015 Share Posted September 3, 2015 My guess is that it could be a scope issue. Add console.log(i); after newMeshes[0].position.x = i+10; to see what the value of i is. Or maybe the other meshes are completely out of view. Quote Link to comment Share on other sites More sharing options...
ChrisR Posted September 4, 2015 Share Posted September 4, 2015 The problem is that the variable i has already incremented to 5 before each of the models has completed loading. Then when the models load they using i=5.. add a counter outside that only gets updated when the mesh is loaded and has used the counter: var GlobalCounter = 0; for (i=0; i<5; i++) { BABYLON.SceneLoader.ImportMesh("", "", "pantene_low.babylon", scene, function (newMeshes) { // Set the target of the camera to the first imported mesh if(GlobalCounter ==4) { camera.target = newMeshes[0]; } newMeshes[0].position.x = GlobalCounter +10; }); GlobalCounter ++; } Quote Link to comment Share on other sites More sharing options...
Temechon Posted September 4, 2015 Share Posted September 4, 2015 Hello, Can I ask why you want to import the same mesh several times ? You can only import it once, and clone it wherever you want. Quote Link to comment Share on other sites More sharing options...
Hagop Posted September 4, 2015 Author Share Posted September 4, 2015 TemechonThe reason I am importing the same mesh several times, is that I want to test the memory limit of the "system" on low internet connections and low ram (ie: 2GB RAM). The reason being we want to create a virtual 3D store with thousands of products (www.ardzan.com). Anyhow, how does cloning work? should I assume that cloning a products several times would use less GPU memory than loading the the mesh the same number of times?Thanks Quote Link to comment Share on other sites More sharing options...
Temechon Posted September 4, 2015 Share Posted September 4, 2015 Indeed, cloning will use less GPU memory than loading several times the same model, and it won't use the bandwidth (as you don't have to load it another time).You win in total number of draw call (better performance) and in memory: it's a win-win!In order to use clone, you can do something like this: http://www.babylonjs-playground.com/#GK7FK#2 I challenge you to do faster by loading 10 times the same model Atep 1 Quote Link to comment Share on other sites More sharing options...
reddozen Posted September 6, 2015 Share Posted September 6, 2015 And instances will use even less than cloning. Quote Link to comment Share on other sites More sharing options...
Hagop Posted September 7, 2015 Author Share Posted September 7, 2015 Reddozen & Temechon Than you for your feedback.Since I will be loading thousands of products into the scene each with unique texture and mesh, cloning and instances is not helpful for my scenario, right? Quote Link to comment Share on other sites More sharing options...
Hagop Posted September 7, 2015 Author Share Posted September 7, 2015 ChrisR Same problem with GlobalCounter ++; It increases before the meshes are loaded.This is how I solved the problem var createScene = function () { var scene = new BABYLON.Scene(engine); //Adding a light var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene); //Adding an Arc Rotate Camera var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene); camera.attachControl(canvas, false); var loadMeshes = function(x) { BABYLON.SceneLoader.ImportMesh("", "scenes/", "pantene_low.babylon", scene, function (newMeshes) { // Set the target of the camera to the first imported mesh //camera.target = newMeshes[0]; //alert("i " +i); newMeshes[0].position = new BABYLON.Vector3(x, 0, 0); }); } for (i=1;i<4;i++) { var x = i*10; var loadedMeshes = loadMeshes(x); } // Move the light with the camera scene.registerBeforeRender(function () { light.position = camera.position; }); return scene; }//end createScene 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.