Jump to content

Reference Mesh Variable Outside Callback Function


petemac
 Share

Recommended Posts

Hello

 

For the life of me I couldnt figure out how to do this.

I think I tried everything, looked around on the internet and tried different examples people posted but nothing worked for me.

 

What I am trying to do is something like this:

 var m;var s;BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {m = newMeshes[0];s = newMeshes[0].skeleton;});scene.registerBeforeRender(function () {m.position = new BABYLON.Vector3(0, 1, 0);scene.beginAnimation(s, 2, 70, 1.0, true);}

However what I am being forced to do right now is put the registerbeforerender function inside the other to be able to use the newMeshes and skeleton.

Like this:

BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {scene.registerBeforeRender(function () {newMeshes[0].position = new BABYLON.Vector3(0, 1, 0);scene.beginAnimation(newMeshes[0].skeleton, 2, 70, 1.0, true);} });

If I have lots of sceneloader.importmesh then that is a lot of functions inside each other and is kind of messy.

I just figured there is a better way to do this.

Maybe the way I am doing it is the most efficient way?

I dont want the meshes copied to a variable and then have two meshes and skeletons in my scene and manipulate the new ones, that would be more taxing for the computer, wouldnt it?

I am looking to modify the original (newMeshes & skeleton) thru a reference, I guess?

 

I am targetting mobile devices so looking to be most efficient I can.

 

Sorry for poor explanation, still and always learning programming and just trying to wrap my head around how to do this and the best way.

 

Any advice greatly appreciated!

Thank you.

Link to comment
Share on other sites

The quickest solution would be to check if m and s exist ( if(m && s){...} ) inside the before render loop, and only then run the animation.

But why not do it one time after they were loaded? Why would you want to change the mesh's position on each and every frame to the same value?

Link to comment
Share on other sites

Sorry for the poor example, I dont want to change the mesh position and play the animation every frame, it was to illustrate the use of the variables outside the callback function.

I also tried

var m;var s;BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {m = newMeshes[0];s = newMeshes[0].skeleton;});m.position = new BABYLON.Vector3(0, 1, 0);scene.beginAnimation(s, 2, 70, 1.0, true);

It did not work, if I recall it gave me a blank page.

So my guess is m and s did not exist therefore, the way I did it is wrong.

 

However for example I would like to do something like this:

var m;var s;BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {m = newMeshes[0];s = newMeshes[0].skeleton;});scene.registerBeforeRender(function () {if(moving){m.position = new BABYLON.Vector3(0, 1, 0);scene.beginAnimation(s, 2, 70, 1.0, true);}}

Or something like this:

//state machine?//exampleif(keypress){scene.beginAnimation(s, 2, 70, 1.0, true);}
Link to comment
Share on other sites

This variable inspection will work.

If you want to be more efficient, you will have to do it a bit differently. This function-in-a-function is JavaScript :-) I'm not sure what will be better for performance - a single function checking for null variables like I suggested before, or a lot of before-render function registrations. But it's easily tested.

Link to comment
Share on other sites

Just saw your edit.

It all depends what you are trying to do. Single ballgame changes should be made after they are loaded. If you want to run a function on each frame with those vars, you can do it the way I showed before, it the way you did it in your second example.

This is more software design wisdom and less Babylon - you will have this problem always when working with async functions.

Link to comment
Share on other sites

Thank you.

Ive never really had trouble accessing variables I initalized outside a function and set inside a function, only in this specific case with (newmesh and skeleton) in the callback function;

 

For example 

var a;blah();function blah(){a = 1;}print(a);//prints 1

If im not mistaken, " ( if(m && s){...} )" only checks to see if they exist.

What is the correct way to access newmesh and skeleton outside the callback function?

Ive tried the 

var m;var s;BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {m = newMeshes[0];s = newMeshes[0].skeleton;});m.position = new BABYLON.Vector3(0, 1, 0);scene.beginAnimation(s, 2, 70, 1.0, true);

and it did not work.

Does it not work because it hasnt been loaded before I ask for it to change position and animate?

will changing it to this work?:

var m;var s;BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {m = newMeshes[0];s = newMeshes[0].skeleton;});if(m&&s){m.position = new BABYLON.Vector3(0, 1, 0);scene.beginAnimation(s, 2, 70, 1.0, true);}
Link to comment
Share on other sites

I believe I tried that before, however I was probably doing it incorrectly

Do you use the mesh name how it is named in blender?

For example:

BABYLON.SceneLoader.ImportMesh("thischaractername", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {}myMesh = scene.getMeshByName('thischaractername');myMesh.doStuffWithMe;

I just tried this and it did not work:

I got a blank page.

        BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) { scene.beginAnimation(newMeshes[1].skeleton, 2, 70, 1.0, true);        });var myMesh = scene.getMeshByName('HumanMale');myMesh.position = new BABYLON.Vector3(0, 1, 0);

"HumanMale" is the name of the mesh I have exported from blender to a human24b.babylon.

Link to comment
Share on other sites

Using double quotes didnt work either :

myMesh = scene.getMeshByName("HumanMale");myMesh.position = new BABYLON.Vector3(0, 1, 0);

I still  get a blank page.

 

I did this:

myMesh = scene.getMeshByName("HumanMale");//myMesh.position = new BABYLON.Vector3(0, 1, 0);

The page loaded fine.

But of course then I didnt change the position.

Link to comment
Share on other sites

BABYLON.SceneLoader.ImportMesh("thischaractername", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {newMeshes[0].visiblity=true;newMeshes[0].isVisible=true;}

If this doesn't display anything then there's a problem with your handling of ImportMesh. Check the logs and DOM to see that the mesh is loading.




			
		
Link to comment
Share on other sites

        //character        BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) { scene.beginAnimation(newMeshes[1].skeleton, 2, 70, 1.0, true);        });

Above shows my mesh and the animation works.

        //character        BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) { scene.beginAnimation(newMeshes[1].skeleton, 2, 70, 1.0, true);        });myMesh = scene.getMeshByName("HumanMale");myMesh.position = new BABYLON.Vector3(0, 1, 0);

Above my page doesnt load, just get a white screen.

 

The problem lies with accessing the mesh and changing it, what ever and when ever I try something it doesnt load the page.

Ive tried with numerous different models and blender files, attempted many different ways of trying to access and change the mesh outside the callback function.

Always results with the page being white and the rest of my script not executing.

 

This is my blender model showing the naming is correct:

post-16561-0-41773500-1443801172.png

Link to comment
Share on other sites

Shouldn't it be this:

        //character        BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {             scene.beginAnimation(newMeshes[1].skeleton, 2, 70, 1.0, true);             myMesh = scene.getMeshByName("HumanMale");             myMesh.position = new BABYLON.Vector3(0, 1, 0);        });
Link to comment
Share on other sites

But then I cant use myMesh outside of:

 BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) {});

and thats the point, im trying to access and manipulate newMeshes and skeleton outside of that function.

Link to comment
Share on other sites

I thought that might be the problem, is there a onloadfinish function of some sorts I can incorporate to this?

       //example?//function onload(){BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) { scene.beginAnimation(newMeshes[1].skeleton, 2, 70, 1.0, true);        });//}myMesh = scene.getMeshByName("HumanMale");myMesh.position = new BABYLON.Vector3(0, 1, 0);
Link to comment
Share on other sites

  • 2 years later...

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