petemac Posted October 2, 2015 Share Posted October 2, 2015 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. Quote Link to comment Share on other sites More sharing options...
RaananW Posted October 2, 2015 Share Posted October 2, 2015 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? Quote Link to comment Share on other sites More sharing options...
petemac Posted October 2, 2015 Author Share Posted October 2, 2015 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 triedvar 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);} Quote Link to comment Share on other sites More sharing options...
RaananW Posted October 2, 2015 Share Posted October 2, 2015 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. Quote Link to comment Share on other sites More sharing options...
RaananW Posted October 2, 2015 Share Posted October 2, 2015 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. Quote Link to comment Share on other sites More sharing options...
petemac Posted October 2, 2015 Author Share Posted October 2, 2015 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 1If 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);} Quote Link to comment Share on other sites More sharing options...
reddozen Posted October 2, 2015 Share Posted October 2, 2015 so if inside "human24b.babylon" your mesh is named "bob_the_builder"...myMesh = scene.getMeshByName("bob_the_builder");myMesh.position = new BABYLON.Vector3(0, 1, 0);scene.beginAnimation(myMesh.skeleton, 2, 70, 1.0, true); Quote Link to comment Share on other sites More sharing options...
petemac Posted October 2, 2015 Author Share Posted October 2, 2015 I believe I tried that before, however I was probably doing it incorrectlyDo 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. Quote Link to comment Share on other sites More sharing options...
petemac Posted October 2, 2015 Author Share Posted October 2, 2015 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. Quote Link to comment Share on other sites More sharing options...
reddozen Posted October 2, 2015 Share Posted October 2, 2015 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. Quote Link to comment Share on other sites More sharing options...
petemac Posted October 2, 2015 Author Share Posted October 2, 2015 //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: Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 2, 2015 Share Posted October 2, 2015 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); }); Quote Link to comment Share on other sites More sharing options...
petemac Posted October 2, 2015 Author Share Posted October 2, 2015 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. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 2, 2015 Share Posted October 2, 2015 Then you have to wait for this function to finish as the load is asynchronous. Quote Link to comment Share on other sites More sharing options...
petemac Posted October 2, 2015 Author Share Posted October 2, 2015 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); Quote Link to comment Share on other sites More sharing options...
reddozen Posted October 2, 2015 Share Posted October 2, 2015 BABYLON.SceneLoader.ImportMesh("", "firstpersongame/", "human24b.babylon", scene, function (newMeshes, skeleton) scene.executeWhenReady( function () { myMesh = scene.getMeshByName("bob_the_builder"); myMesh.position = new BABYLON.Vector3(0, 1, 0); scene.beginAnimation(myMesh.skeleton, 2, 70, 1.0, true); }); jools_n_jops 1 Quote Link to comment Share on other sites More sharing options...
syed samoon Posted November 25, 2017 Share Posted November 25, 2017 Hi petemac, can you got solution for this problem. because i'm also facing the same issue. Quote Link to comment Share on other sites More sharing options...
Sebavan Posted November 25, 2017 Share Posted November 25, 2017 @syed samoon answered in your separate thread with the same question: 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.