Jump to content

importMesh and modify the model outside the callback


Terminator
 Share

Recommended Posts

let's say I am using this code:
 

var board;

BABYLON.SceneLoader.ImportMesh("board","/models/","models.babylon",scene,function(newMeshes){
    board = newMeshes[0];   
});

board.position.x += 400;

board.position.x won't work since importMesh is using a callback function which is something I am aware off, but is there an alternative solution?

what I want is to import a mesh from a file, then save this mesh to a variable then clone this mesh but I don't want that to happen inside the callback function

for example:

var originalMesh = getMeshFromFile(filename_here);  
for (var i= 0; i< 100; ++i) {  
    var newClone = originalMesh.clone("index: " + index);
    newClone.position.y =+ i*2;
    newClone.material = ...
}

 

Link to comment
Share on other sites

Hi people,

Javascript is async (and thank god for that!). If you load an object, it will take some time until it is rendered in the scene. you will only be able to change its position after the callback was executed. 

changing the position before the object was loaded will not work (as you already noticed). you will have to work with callbacks, as this is how the language works :) . you can still have a global reference to the object, and when it is not null do something with it. but then again, what's wrong with doing it after the callback was executed?

Link to comment
Share on other sites

 

@RaananW For sure there's nothing wrong to wait for the callback is executed, the problem is just that I don't want to execute the code inside the callback function.

here is what I am thinking:

1- Import the mesh

2- get a reference about the imported mesh and clone it multiple times

3- manipulate the cloned meshes inside:

scene.registerBeforeRender(function(){

}); // registerBeforeRender()

 

so for me it is just about being able to use the imported mesh outside the callback function scope, (I think the solution is to just define a global variable for the mesh and just keep looping until it is no longer null as you mentioned?)

Link to comment
Share on other sites

once you start understanding the way callbacks are executed, you don't really need global variables. 

You could register the before render loop inside the callback.

For example - you could create an external function that will be called after the callback was executed: http://www.babylonjs-playground.com/#1BUQD5

You could also create a global variable and register the "beforeRender" after the callback was executed - http://www.babylonjs-playground.com/#1BUQD5#1 .

So many ways of achieving it without inspecting constantly if mesh is not null.

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...