skalibran Posted September 13, 2015 Share Posted September 13, 2015 Hey there, it's me again! Yeah! ... ... ... Well, now that I'm aware of the assets manager (thanks to Temechon) I wonder how I can access the models.For example I have thisvar balloon = assetsManager.addMeshTask("balloon", "", "", "balloon-dae.babylon");Using the OnSuccess-method I can place it one time in my 3D-Space:balloon.onSuccess = function (task) { task.loadedMeshes[0].position = new BABYLON.Vector3(0, 0, 10);}But how can I access my balloon like any in-code created object?balloon.position.y = 3 // does not seem to workUsing thescene.registerBeforeRender(function () { balloon.position.y += 0.1; });does not seem to work either. I guess it is a general understanding problem. What did I wrong? Thanks! Quote Link to comment Share on other sites More sharing options...
Gugis Posted September 13, 2015 Share Posted September 13, 2015 Well you can create "library" array and push meshes to it in success callback.meshTask.onSuccess = function (task) { for(var n in task.loadedMeshes) { LIBRARY.push(task.loadedMeshes[n]); }} Assign LIBRARY variable in global scope so it could be accessed from anywhere in your script. Or you can use scene.getMeshByName() function Temechon 1 Quote Link to comment Share on other sites More sharing options...
skalibran Posted September 13, 2015 Author Share Posted September 13, 2015 Hey, thanks for the answer! Unfortunetaly, I can't get both examples to work. Can you go a bit into detail, please? Thanks! Quote Link to comment Share on other sites More sharing options...
Temechon Posted September 13, 2015 Share Posted September 13, 2015 Create a global variable called LIBRARY, at the beginning of your script:var LIBRARY = {};When a model is loaded, just use the code sent by Gugis:var balloon = assetsManager.addMeshTask("balloon", "", "", "balloon-dae.babylon");ballon.onSuccess = function(task) { LIBRARY['balloon'] = task.loadedMeshes[0]);};When the loader is finished, just called your render loop and update your balloon position:assetsManager.onFinish = function (tasks) { engine.runRenderLoop(function () { scene.render(); }); scene.registerBeforeRender(function () { LIBRARY['balloon'].position.y += 0.1; }); LIBRARY['balloon'].position.x = 0;}; Quote Link to comment Share on other sites More sharing options...
skalibran Posted September 13, 2015 Author Share Posted September 13, 2015 As soon as I add ballon.onSuccess = function(task) { LIBRARY['balloon'] = task.loadedMeshes[0]);};the scene does not load anymore (whitescreen).Removing the ) (unexpected token) will result in a "Uncaught ReferenceError: ballon is not defined" I guess 3D objects hate me. Quote Link to comment Share on other sites More sharing options...
Temechon Posted September 13, 2015 Share Posted September 13, 2015 Can you share your code somewhere ? Quote Link to comment Share on other sites More sharing options...
skalibran Posted September 13, 2015 Author Share Posted September 13, 2015 sure.<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html" charset="utf-8"/> <title>Babylon - Getting Started</title> <script src="babylon.js"></script> <style> html, body { overflow: hidden; width : 100%; height : 100%; margin : 0; padding : 0; } #renderCanvas { width : 100%; height : 100%; touch-action: none; } </style></head><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 LIBRARY = {}; // createScene function that creates and return the scene var createScene = function(){ // create a basic BJS Scene object var scene = new BABYLON.Scene(engine); // create a FreeCamera, and set its position to (x:0, y:5, z:-10) var camera = new BABYLON.FreeCamera('camera1', new BABYLON.Vector3(0, 5,-10), scene); // target the camera to scene origin camera.setTarget(BABYLON.Vector3.Zero()); // attach the camera to the canvas camera.attachControl(canvas, false); // create a basic light, aiming 0,1,0 - meaning, to the sky var light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0,1,0), scene); // create a built-in "ground" shape; its constructor takes the same 5 params as the sphere's one var ground = new BABYLON.Mesh.CreateGround('ground1', 6, 6, 2, scene); ground.visibility = false; var assetsManager = new BABYLON.AssetsManager(scene); var balloon = assetsManager.addMeshTask("balloon", "", "", "balloon-dae.babylon"); var arrM; ballon.onSuccess = function(task) { LIBRARY['balloon'] = task.loadedMeshes[0]; }; assetsManager.load(); assetsManager.onFinish = function (tasks) { scene.registerBeforeRender(function () { }); // run the render loop engine.runRenderLoop(function(){ scene.render(); }); } // return the created scene return scene; } // call the createScene function var scene = createScene(); scene.registerBeforeRender(function () { }); // the canvas/window resize event handler window.addEventListener('resize', function(){ engine.resize(); }); }); </script></body></html> Quote Link to comment Share on other sites More sharing options...
Temechon Posted September 13, 2015 Share Posted September 13, 2015 Try to do this:assetsManager.onFinish = function (tasks) { scene.registerBeforeRender(function () { }); // run the render loop engine.runRenderLoop(function(){ scene.render(); });}assetsManager.load();return scene; Quote Link to comment Share on other sites More sharing options...
dbawel Posted September 14, 2015 Share Posted September 14, 2015 As Gugis mentioned, I believe you must first use the "scene.getMeshByName() function" to set the mesh variable. Quote Link to comment Share on other sites More sharing options...
skalibran Posted September 14, 2015 Author Share Posted September 14, 2015 Finally it's working, thanks to Temechons last post. Only scene.getMeshByName('balloon').position.y = xyz; does not work. Quote Link to comment Share on other sites More sharing options...
jellix Posted July 20, 2016 Share Posted July 20, 2016 Hi, I'm alway getting an error message within my development IDE: 'loadedMeshes' does not exist on type 'IAssetTask'. But with console.log(task.loadedMeshes[0]), it shows the first element of the array. Does anybody know the error? Quote Link to comment Share on other sites More sharing options...
jellix Posted July 20, 2016 Share Posted July 20, 2016 I've solved it. The trick was to give the type to the variabe within the callback-function linke this: meshTask.onSuccess = function (task:BABYLON.MeshAssetTask) { faljse and GameMonetize 2 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.