O3D makes it very easy to load scenes. A scene, in O3D, is just a collection files which can render an object, or even multiple objects. The scene file normally has the extension .o3dtgz and is essentially just a tgz archive which contains the various files making up the scene.
A scene can contain the shaders, textures and the data making up the meshes as well. This means is that when O3D loads the scene file, it is able to set up everything associated with the models in the scene, and the application does not need to worry about setting up materials or shaders for the models making up the scene. This doesn’t mean that you can’t override these components though, which makes modifying models once they in the scene an easy job to do too.
So anyway, let’s look at the code. The first thing is to include the pack and scene libraries.
o3djs.require('o3djs.pack'); o3djs.require('o3djs.scene');
In the intialisation function we call the loadScene() function, which we will define next. I have used the teapot from the O3D samples as the scene to load, but this could be any valid scene file.
loadScene(g_pack, 'tutorial14/teapot.o3dtgz', g_3dRoot);
Now, we get to the function that does the work. We pass the pack (which in our case is g_pack), the filename of the scene file to load, and g_3DRoot which the scene will be attached to.
The first thing we do here is load the scene using o3djs.scene.loadscene(), which then calls the callback function once the load has completed. In the callback function, if no error occured, we then make a call to o3djs.pack.preparePack() which sets up the pack with the details of the view, so that the camera angle, etc is set up correctly.
function loadScene(pack, fileName, parent) { var scenePath = o3djs.util.getCurrentURI() + fileName; o3djs.scene.loadScene(g_client, pack, parent, scenePath, callback); function callback(pack, parent, exception) { if (exception) { alert('Could not load: ' + fileName + '\n' + exception); return; } o3djs.pack.preparePack(pack, g_viewInfo); } }
One more small change I made in the code from prevous tutorials, is that I moved the primitives that I am creating out of the way a bit, as the scene is loaded at the origin. I could just as easily moved the scene though.
continue reading…