Loading Textures and Sounds at Runtime
Summary
By loading images and sounds at run time, both the file size on disk and the file size in memory can be drastically reduced for Virtools-based games and presentations. In this example, we take a composition containing one panoramic view, which occupies about 10 megabytes of memory on disk, and enhance it so that it displays a series of 70 panoramic images and plays a sound track of 21 songs, while occupying only about 200 kilobytes of memory on disk.
Panorama Object Load StudioOverview
What is Virtools
Virtools Dev allows you to build video games, virtual worlds and interactive presentations that can be easily deployed on the World Wide Web. When a project is being developed in Virtools, it is called a composition, and the working file has a .cmo extension. Compositions that are exported to the “Virtools Player” (id est the “3D Life Player”), a file with the extension .vmo is produced which can be run from a web page using the HTML < object > tag. By default, both the Virtools composition and web player files save texture files (such as Targa .tga, DirectX .dds, JPEG .jpg, or Portable Graphics Network .png) and sound files (such as Fraunhofer .mp3 or good ol’ Wave .wav) as raw data within the file itself according to Virtools’ proprietary algorithms. This method simplifies the directory structure for the project, but tends to produce large file sizes, especially when high-quality or large-size asset files are used.
Problem Solved
Fortunately, the engineering team at Virtools has thought of everything, and is just waiting for us to learn the basics before the maelstrom of Virtools development power is fully unleashed. Somewhere hidden within the Virtools Dev package is every conceivable tool a game developer would want for developing beautiful, robust, web-based games and virtual interactions.
Game Dev Best Practices
One of the most fundamental techniques in optimizing the delivery of game content is the serialized loading of game assets at run time. Simply put, once the game has been initialized, it continues to pull itself up by its boot straps by loading in images and sounds one at a time or as they are needed. Simple “loading screen” animations are used to provide feedback to the user, assuring them that the game has not crashed and will begin momentarily. In addition to loading textures and sounds at runtime, Virtools can also load complete “objects” at run time, duplicate and modify objects as need, and even construct entire 3D models at run time from code.
Virtools Composition: CMO
Games, virtual worlds and interactions are built as “compositions” within Virtools. A composition is equivalent to a document in Flash or a project in objective C. Depending on the preferences set on the Options menu, the images used as textures in a composition can be saved within the composition in a “raw” format that relies on Virtools own compression algorithm, or they can be saved using a specific file format and compression rate. For example, a picture can be stored within a composition in JPEG format at 50% compression. If you do this, the image will suffer from pixelization and will not look its best, but the image file will remain fairly small on disk. The main advantage of working with a composition is the WYSIWYG layout of about a dozen “managers” that Virtools provides. Some of the most important managers are the “Schematic” layout, which allows you to build and edit scripts visually using graphical building blocks, the “Level Manager” which gives a line-item inventory of the assets in a composition, the Hierarchy Manager, which allows you to observe and set parent-child relationships, and the Attribute Manager, which is an interface for attributes worn by objects. Although it is technically possible to run a CMO within a Web browser, the recommended practice is to create a Web Player VMO for deployment to the Web.
Virtools Web Player: VMO
It is possible to make both desktop applications (.exe) and 3D Life Player applications (.vmo) from a Virtools composition (.cmo). The process of building a VMO from a CMO is similar to the Flash process of building a .swf file from a .fla file. In the case of Virtools Dev, select “Export to Virtools Player” from the File menu. Select a destination folder and name the 3D Life Player file in the “Save As” dialog box. To create a Web page (HTML) with an imbedded VMO from your composition, select “Create Web Page” from the file menu. In the “Create a Web Page” dialog box, set destination folder, page title and Virtools player window size, and then click on Ok.
Virtools Script: NMS
The Virtools Schematic interfaces allow designers and developers to work with graphically oriented building blocks that effectively replace the need to read or write lines of programming code. (It is worth mentioning that Virtools offers the schematic interface for rapid prototype development. Once concepts and algorithms have been worked out in the schematic interface, they can be encapsulated into custom building blocks (the Run VSL building block), or replaced with runtime DLLs written in objective C.) Scripts can be applied to nearly any “object” within Virtools, including 3D objects, meshes, frames (another convenient abstraction), materials, textures… even the composition itself (referred to in Virtools as the ‘level’). Scripts are also swappable (in our example, we take a script originally written for the level and apply it to the canvas that displays the panorama), savable, and can be attached to objects programmatically at run time.
To save a Virtools script to the hard drive, right click on the script in the schematic view and select “Save As…” from the context menu. Select a destination directory and give the script a name. In the “Behavior Information” dialog box that follows, you have the option of entering the script’s author, version and description.
To load a Virtools script onto an object, right click on the name of the object in the “Level Manager” and select “Load Script…” from the context menu. Using the “Load File” dialog box that appears, select the script and press “Ok”. The script will then be loaded into the composition and attached to the object. An expansion triangle appears to the left of the object in the “Level Manager” view pointing right. Click on the triangle; it will point down, and show the script as a child of the object.
Virtools Object: NMO
A Queer Object Orientation
The Virtoos 3D Object is a data abstraction which associates a model with its meshes, materials, textures, scripts, and other real time 3D and 2D assets. For example, a “Car” object might have a “Car Body” mesh, a “Dented Car Body Mesh”, a “Car Body Paint” material, a “Car Window” material, a “Car Decal” texture, a “Car Script”, and perhaps a “Blow Up Car Script”. Even though these assets exist within the Virtools Level Manager as separate line items, they can be saved to disk as a single file. To do this, select all of the assets that you want to go into the Virtools object file (.nmo), right click, and then select “Save As…” from the context menu. You will be given the normal opportunity to find a directory for the object and choose a file name. This object file can then be imported back into a composition. To manually load an object, select “Import File” from the Resources menu. To load an object programmatically while the composition is running, use the “Object Load” building block.
Level as Object
The contents of an NMO file are not limited to one 3D Object, or even assets that are strictly related to each other. Because the Virtools object is an abstraction, it is implemented mostly for the convenience of the composition author. It is possible to save an entire composition as a single Virtools object, in the sense of an NMO. The caveat is that objects loaded into scenes cannot have a “level” script. (The level script will not appear when the NMO is loaded.) In our example, the script that controls the loading of images and sounds is removed from the “level” and attached to the plane object that acts as a canvas for the panoramic images. Thus, by creating a Virtools Object from an entire composition, the “Panorama_Object_09.nmo” object behaves in the same way within the “Object_Load_Studio_02_Web.cmo” composition as the entire “Panorama_09_Web.cmo” composition does on its own. (In terms of the 3D Life web Player, “Panorama_Object_09.nmo” behaves in the same way within “Object_Load_Studio_02.vmo” as the entire “Panorama_08_Web.vmo” does on its own.
Making it Work on the Web
- Upload the assets to a specific directory on the server (the panorama pictures to the images folder, the sound track mp3s to the sounds folder).
- Build the Panorama composition that loads pictures and sounds at run-time.
- Produce a Panorama VMO to test the code
- Produce a Panorama object (NMO)
- Upload the panorama object (.nmo) to the directory on the web server where the VMO will go.
- Build the Object Loader Studio composition that loads the Panorama object at run-time.
- Upload the Object Loader Studio VMO to the directory on the web server where the panorama object is located.
- Build a web page that will properly feature the Object Loader Studio VMO, and optionally include JavaScript with the page that will install the 3D Life Player on the client browser.
Summary
Virtools offers a number of techniques for optimizing and streaming real time 3D and 2D applications. Many of these techniques are embodied in the graphically oriented development building blocks Web Download, Texture Load, Sprite Load, Sound Load, Object Load, and Movie Load. Loading game assets programmatically has a number of manifest advantages over including all assets in a single file. Textures, videos, audio files and Virtools objects can be loaded into the memory of the client’s computer only before they are needed, and then deleted or replaced once they are no longer required. Load screen animations can be created to provide critical feedback to the user, whether the time to load objects is long or short. Game assets can be prioritized, so that less important assets are only be loaded if the current system performance and available resources are above a certain minimum set by the developer. Textures and meshes of a higher quality can be used, especially for feature assets that occupy centre-stage. Perhaps most importantly, loading multi-media assets into a presentation at runtime removes the practical limit on the scale of Web-based 3D and 2D application