Macromedia Director MX 2004: Training from the Source
In this section, we'll walk through the 3D cast member by creating some primitive models and texture mapping them using a bitmap image, as shown here: Director's 3D cast member isn't like other members, in that it is not just a single, discrete, piece of media. A 3D member contains a complete three-dimensional world and can consist of models, shaders, textures, cameras, lightseven animations. When a 3D member is instanced onto the Stage as a sprite, the view of the world you see is through a particular camera in that world. There can be one camera or many, and you can switch between them any time you like. You can even have several 3D sprites on the Stage, all instances of the same 3D member, and have each sprite show a different view. You're even able to create animated particle systems to create effects like smoke, rain, and snow. And while you will normally create your models using an external 3D package, Director offers primitive models such as cubes, spheres, and cylinders. You can even generate meshes directly from Lingo, though that's an advanced topic and beyond the scope of this book. Let's begin by creating a 3D cast member, then making and texturing a primitive sphere. Creating a 3D Sprite
Primitives are simple 3D models and Director offers several different ones such as the plane, sphere, cube, and cylinder, as well as the particle system primitive.
Creating Primitives
Models, within the 3D world, have what is known as a resource. Think of a resource as a cast member, and a model as a sprite or instance of that member. Once a resource has been created you can create many model instances from that single resource. Press Play in the toolbar, then open the Message window. Enter the following: wrld = member("test") This creates a reference to the 3D cast member. spRes = wrld.newModelResource("sphere", #sphere) This line creates a model resource of type sphere and gives it the name "sphere". You can now create a model from the resource: wrld.newModel("sphere", spRes)
This creates a model named sphere, from the model resource referenced by spRes. As soon as you hit Enter, a sphere model appears within the 3D sprite with a default red-and-white checker texture map applied to it: You can increase the size of the sphere by modifying the radius property of the sphere's resource: trace(spRes.radius) -- 25.0000
The sphere's default radius is 25 units. To make it twice as big, increase the radius to 50 units: spRes.radius = 50
Now, let's look at replacing the default texture board pattern with an image of our choosing. Creating a Shader
Before placing a new bitmap image on the sphere model, you need to understand the concept of the shader. A shader defines how the surface of a model responds to light. Its reflectivity, shininess, color, and texture are all affected by the model's shader. There are four different types of shaders that you can create, and each has its own properties: standard, painter, engraver, and newsprint. The standard shader is the most common type and is used when you want to apply a bitmap image to a model. The following image compares the different shader styles: For our purposes, we'll use the standard shader. But feel free to experiment once you know how to create and apply shaders to a model.
Assigning a Texture
Each texture used in your 3D world is stored within the world. Textures are just bitmap images, and you must pay attention to two things when creating textures for use in 3D. First is the resolution of the images. The length and width of any texture map must be a power of 2. That is, 64x64, 128x128, 128x256, 512x512, 512x256, etc. The length and width can be different, but each needs to be a power of 2. If they aren't, Director will scale the image for you, to the nearest power of 2, typically producing less than desirable results. Note The power of 2 rule arises from the way textures are accessed from video memory. By having this requirement the renderer can access specific pixels within the texture, known as texels, using very fast bitshifting operations, instead of slow multipy and divide operations. This requirement came about in order to optimize the texture access speed and was necessary on older graphics cards. Although the rule still applies today, newer generation renderers, such as OpenGL2 will break with this tradition. The second thing to consider is that of your user's video RAM (VRAM). The memory on a video card must be able to accept all your textures, to be able to use hardware acceleration. Director's 3D engine will happily use DirectX or OpenGL for hardware-accelerated rendering, but only if the textures fit in the card's VRAM. If they don't, your scene will still render, but will use a much slower non-hardware accelerated software rendering. Memory is cheap and newer video cards contain 128 MB of RAM and more, so this is less and less a factor. But if you're creating a large 3D scene, it's something to consider. Now, let's import a bitmap image and use it to create a texture on our sphere.
Translating a Model
Translating a model includes moving, rotating, and scaling the model along one or more axes. As you can see from the following image you can translate your models along either the X, Y, or Z axis: To move a model using Lingo, you use the translate command. For instance, to move the sphere up 10 units, you'd translate it by 10 in the Y axis. Entering the following in the Message window would move the sphere up and right by 10 units each: wrld.model("sphere").translate(10, 10, 0)
If you wanted to rotate the sphere 45 degrees around its vertical (Y) axis, you would use: wrld.model("sphere").rotate(0, 45, 0) If you wanted to turn the sphere into a kind of egg shape, you could scale it along its Y axis like so: wrld.model("sphere").scale(1, 2, 1):
This has the effect of scaling the model by two times in Y and not scaling at all in either X or Z. To turn the scaled sphere back into a sphere, you would scale it by one-half in Y, like so: wrld.model("sphere").scale(1, .5, 1)
Before moving on to the next section, let's look at how you might move your camera around the sphere to create an animation. Moving the Camera
The camera can be moved and rotated, like any other object in your 3D world. By moving the camera on a per-frame basis, you create an animation. Using just a single line of Lingo, you can have the scenes camera orbit the sphere.
|