Jump to content

Animate rotation of an imported mesh


spritefire
 Share

Recommended Posts

Just trying to relearn things in babylon that I was using in three.js
Can't seem to find the answer anywhere (although I could be looking in the wrong direction or might be so simple I am over looking it).

I have a single mesh that I have exported from 3ds max. No animations just mesh and a texture. I have turned off the camera control and positioned the mesh nicely. Now I just want the mesh to rotate on a horizontal axis using babylon.
 

var container = document.getElementById('canvas');var engine, scene, camera;var internalMesh;function init(){ engine = new BABYLON.Engine(container, true);scene = new BABYLON.Scene(engine); BABYLON.SceneLoader.ImportMesh("", "", "desiren.babylon", scene, function (newMeshes){internalMesh = scene.getMeshByName("desiren_full");//internalMesh = newMeshes[0]; // Both ways find MeshinternalMesh.position = new BABYLON.Vector3.Zero;scene.clearColor = new BABYLON.Color4(1,0,0,0);    engine.runRenderLoop(function() {    scene.render();    internalMesh.rotation.x += 0.05;    });alert(internalMesh); // does return object}); camera = new BABYLON.ArcRotateCamera("Camera", Math.PI/2*3, Math.PI/2, 300, new BABYLON.Vector3(0, 0, 0), scene);var light = new BABYLON.HemisphericLight("Hemi0", new BABYLON.Vector3(0, 2, 0), scene);light.diffuse = new BABYLON.Color3(1, 1, 1);light.specular = new BABYLON.Color3(1, 1, 1);light.groundColor = new BABYLON.Color3(0, 0, 0);window.addEventListener("resize", function () { engine.resize(); }); // Resize engine }init();

Also, does the runRenderLoop function need to be called within the same function of the imported scene or is there another way of accessing the mesh outside of function?

I know this should be simple but I can't seem to find the answer anywhere. 
 

Link to comment
Share on other sites

Hi Spritefire and Temechon!

   Temechon (or anyone else), do you have a moment to expound on your answer?

Let's look at http://playground.babylonjs.com/?16

I happen to know that the skull's .name is 'test'.

So then I modify the scene's rbr func... to...

    scene.registerBeforeRender(function () {        light.position = camera.position;	scene.getMeshByName("test").rotation.y += 0.05;    });

Then hit run.  It doesn't work.

Then hit save.  Now it works (it once did, anyway).

Or does it?  http://playground.babylonjs.com/#NCF3T  (now no, but the rotation worked good when I made this save.  weird)

Can you explain to us... why this is?  I am seeing all sorts of inconsistencies.

Should scene.executeWhenReady() be involved somehow?  Know? 

Thanks either way.  Maybe it's something with the playground?

Link to comment
Share on other sites

I personally like to attach my meshes to my scene, so instead of declaring the internalMesh as a global I would do this instead:

scene.internalMesh = scene.getMeshByName("desiren_full");

then at the end of your scene creation I would create the registerBeforeRender and  call the runRenderLoop:

scene.registerBeforeRender(function () {	scene.internalMesh.rotation.y += 0.05;});engine.runRenderLoop(function() {    if (scene.isReady()) {        scene.render();    }});

Also if you look at the wiki (https://github.com/babylonjs/babylon.js/wiki) it is really valuable. As you expand out you will start finding things like the ActionManager and Animation functions that are really powerful. One thing I am discovering that as you expand, you don't really want to use the renderLoop for logic decisions. I actually created my own loop for logic creation using setTimeout and calling it recursively. Something like this:

var logicLoopId; //to identify the loop if you need to stop itvar logicLoop = function () {   logicLoopId = setTimeout(function () {      //logic   }, 10);   logicLoop();};//then call it in your code when you are ready to start itlogicLoop();
Link to comment
Share on other sites

Thanks guys! 

I have learnt a ton and have discovered that the issue isn't related to coding at all. I have used what everyone has said and I have re-written the code and have it working perfectly if I use the skull.babylon file.

 

However, if I create a simple box in 3DS Max and export the file and place that babylon file into the code it will not rotate. 

If I export a basic mesh shape (the default teapot shape) from 3ds max using the 3ds exporter (v0.9 and v0.14 latest) and use that babylon file I am unable to get the mesh to rotate. 

 

Here is the max archive and also the code that is working fine with the skull:

 

        var canvas = document.getElementById("renderCanvas");        var engine = new BABYLON.Engine(canvas, true);        var createScene = function () {            var scene = new BABYLON.Scene(engine);            var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene);            var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI/2*3, Math.PI/2, 100, new BABYLON.Vector3(0, 0, 0), scene);            //var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 400, BABYLON.Vector3.Zero(), scene);            //camera.attachControl(canvas, false);            BABYLON.SceneLoader.ImportMesh("", "", "desiren.babylon", scene, function (newMeshes) {                    camera.target = newMeshes[0];                    scene.clearColor = new BABYLON.Color4(1,0,0,0);                    scene.registerBeforeRender(function () {                        scene.getMeshByName("test").rotation.y += 0.05;                    });            });                        return scene;        }                var scene = createScene();        engine.runRenderLoop(function () {            scene.render();        });        window.addEventListener("resize", function () { engine.resize(); }); // Resize engine 

http://spritefire.com/files/teapot.zip

 

http://spritefire.com/files/desiren.babylon

Link to comment
Share on other sites

Exported objects from 3dsmax has a rotationQuaternion set by default.

Try this : 

BABYLON.SceneLoader.ImportMesh("", "", "desiren.babylon", scene, function (newMeshes) {      camera.target = newMeshes[0];      scene.clearColor = new BABYLON.Color4(1,0,0,0);      scene.getMeshByName("test").rotationQuaternion = null;      scene.registerBeforeRender(function () {             scene.getMeshByName("test").rotation.y += 0.05;      });});
Link to comment
Share on other sites

 

Exported objects from 3dsmax has a rotationQuaternion set by default.

Try this : 

BABYLON.SceneLoader.ImportMesh("", "", "desiren.babylon", scene, function (newMeshes) {      camera.target = newMeshes[0];      scene.clearColor = new BABYLON.Color4(1,0,0,0);      scene.getMeshByName("test").rotationQuaternion = null;      scene.registerBeforeRender(function () {             scene.getMeshByName("test").rotation.y += 0.05;      });});

Perfect!

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