Building a Simple API for Model Creation in Director
- World Cleanup Utilities
- Primitive Creation
- Basic Shader Manipulation
- Using Our API to Quickly Create Content
- Future Considerations
- Summary
Application Programming Interface (API) is the term used to refer to the commands of a programming language that allow you to control the inner workings of the operating system. This term is also used to refer to a group of commands built from a programming language that extend the capabilities of that language. Sometimes this is referred to as a toolkit, but I think the term API is more concise. I will use this term as we use Lingo to build a suite of custom handlers to deal with many common tasks. Among these tasks is the process of creating primitives, as discussed in Chapter 2, "Creating Models from Scratch via Lingo." We know that the creation of a primitive from scratch involves several steps that can become repetitive. Rather than bloating our code by typing these commands over and over, we will build custom handlers to deal with frequently repeated tasks. The elegance of building custom handlers to deal with these tasks is that you can make the handlers as specific as you need them to be.
The handlers that I present in this chapter create the basic primitives, such as boxes and spheres, and some variations on those primitives, such as cones and pyramids. The needs of your project will dictate which of these handlers you require and whether you need to customize them further.
World Cleanup Utilities
Attempting to perform operations on a 3D Castmember before it is fully downloaded will result in a script error. This does not mean that it is not possible to stream in 3D Data. The 3D Castmember contains 3D Dataand your movies may contain several 3D Castmembers. At this point, we are working with 3D Castmembers that are devoid of any Models and are therefore quite small (about 500 bytes). Our main concern is that the 3D Castmembers are fully downloaded and ready for commands to be called on them. You can also work with 3D Castmembers from external casts, loading them as is required. This is a good method of approach for both online and offline work. We will explore streaming in Movies using 3D in Chapter 20, "Download Optimization."
When you're working with generated Models, there are two tasks you should handle at the beginning of any movie: making sure that the 3D Castmembers are fully loaded and resetting the 3D world. This is important in Authoring, Projectors, and Shockwave mode. Although this will become slightly modified when you learn to import Models, the overall schematic is the same.
To ensure that our 3D Castmember is fully downloaded, our efforts must be concentrated on this task from the very beginning of the movie. Recall that the order of events at the instantiation of a movie is as follows:
PrepareMovie() (sets up globals)
BeginSprite() (all Sprites in frame 1 initiate)
PrepareFrame() (occurs before anything is drawn on stage)
StartMovie() (suggest initializing non-3D elements here)
Figure 3.1 illustrates a sample score setup for a movie intended to run online or offline. The CD-ROM accompanying this book contains a file called "basic world setup." The illustration of the score in Figure 3.1 is derived from this file.
Figure 3.1 Example of a possible Score strategy for Movie download and initialization.
This movie contains two custom handlers designed to streamline the task of checking to ensure that the 3D Castmember is fully downloaded. One reason why I leave the first frame empty is because the 3D Castmember sometimes takes a moment to begin. If there is going to be a pause at the very beginning of the movie, you want to be able to control what the users see during that pause. They will see whatever was drawn on stage in the previous frame. If there is no previous frame, they will see the Macromedia load bar. Even if you are going to leave frame 1 empty, controlling the moment when the pause happens will allow you to make the process of beginning the movie more transparent to the user. The Behavior script attached to frame 2 contains an exitframe() handler. This handler calls upon a custom handler called check3Dready() that is used to determine whether the SW3D is ready. Here's the code:
1:on check3Dready(whichSW3D) -- a state of 4 tells you the media is ready 2:if whichSW3D.state = 4 then -- if it is ready, return true (for error checking & debugging) 3:return true 4:else --if it isn't ready return false (for error checking & debugging) 5:return false 6:end if 7:end
This handler returns either true or false, depending on whether the 3D Castmember is ready. This information is used in the exitframe() handler attached to frame 2. The reason for this encapsulation is to create custom handlers that are robust enough to be used in several situations. In this case, check3Dready() is being used to determine whether we loop on a frame or jump to a new marker.
Line 2 of the check3Dready() handler refers to the State property of the 3D Castmember. The State property of a 3D Castmember contains information pertaining to the loading of that Castmember into RAM. Although we are primarily concerned with Castmembers fully downloading, the State property can be used to make sure that CD- or HD-based Director Movies are loaded correctly. The State property has six possible values that it can return, as indicated in Table 3.1.
Table 3.1 Values of 3D Castmember State Property
State Value |
Meaning |
-1 |
Error loading file |
0 |
Not loaded |
1 |
Loading has begun |
2 |
High-priority Models downloading |
3 |
Decompression |
4 |
Media is ready for all 3D commands |
When the State setting of a Castmember is 4, all 3D commands can be run on the Castmember without worry. It is possible to run some commands on 3D Castmembers with a State setting of 2 or 3. However, resetworld() should not be called unless the Castmember's State setting is 4.
NOTE
The priority of Model loading applies to Castmembers that contain Models created externally from Director. Check in Appendix A, "3D Studio MAX W3D Exporter," for information about changing the stream priority of Models.
The check3Dready() custom handler reports false when the 3D Castmember's State setting is not 4. The State property applies to other Castmember types as well as SW3D Castmembers. The State values for 3D Castmembers are unique to 3D Castmembers and do not reflect values for Shockwave Audio or Flash Castmembers.
When the 3D Castmember's State setting is 4, the exitframe() handler sends the Playback Head to the "init" marker. At init, the "call initialize" script contains an enterframe() handler. This handler calls a custom handler called initialize() and then sends the Playback Head to the "run" marker. The reason for all this jumping is to ensure that we have a specific moment that we can refer to when the initialize() handler will be called.
This strategy also allows us to separate the initialization of 3D and non-3D elements in our Movies. For example, although this demo does not contain a Startmovie event, it is a good location to initialize the non-3D portions of our Movie.
NOTE
This strategy can also be helpful if you want to "restart" a movie without stopping at some point by sending the Playback Head to the init marker.
Currently, the only operation of our initialize custom handler is to call yet another custom handler, shown here, called clearworld():
1: on clearworld(whichSW3D) -- last minute double checking to make sure SW3D is ready 2: if check3Dready(whichSW3D) then -- if everything is ready, reset the world to default values 3: whichSW3D.resetworld() -- return true (for error checking & debugging) 4: return true 5: else -- SW3D not ready, return false (for error checking) 6: return false 7: end if 8: end
The clearworld() custom handler is designed to make one final check to see whether the 3D Castmember's State property is ready, and then it calls the resetworld() function. Notice how in line 2 I call the check3Dready() custom handler to find out whether State is set to 4. This is a prime example of the reusability of the custom handlers we are building.
Most of the work of this handler occurs in line 3, where the resetworld() function is called. The resetworld() function is used to reinitialize a 3D Castmember to its default state. This means that after you call resetworld() on a 3D Castmember, it will only contain the defaults of one Camera, one Modelresource, one Shader, one Texture and two Lights.
If your 3D Castmember was created externally from Director, you should not use resetworld(). Rather, the reverttoworlddefaults() command reinitializes the 3D Castmember to its "saved" state. Chapter 9, "Working with Prebuilt Scenes and Behaviors," examines methods of working with 3D Castmembers created outside of Director.
One final precaution that we can take to avoid script errors is to actually turn off the streaming functionality for the Movie. You can accomplish this by editing your Movie Playback properties as noted in Figure 3.2.
Figure 3.2 Turning off the Play While Downloading Movie option from the Modify Movie menu.
You can set the Movie Playback properties from the Modify, Movie menu to make sure that the movie fully downloads before playing. This is another method of ensuring that you or your users will not encounter script errors at the beginning of your movie due to bandwidth. However, this method does not take advantage of the ability to stream your data in dynamically.