Jump to content

Access objects from AssetsManager


skalibran
 Share

Recommended Posts

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 this

var 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 work

Using the

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;};
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

  • 10 months later...

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?

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