Director 8.5: More About Primitives
June 7, 2001
by Gary Rosenzweig
Last week I showed you how to make a rotating sphere with the new Shockwave 3D cast member type. A sphere is one type of primitive that you can create in Lingo without modeling it first in an expensive 3D program. There are other types of primitives. This week, lets create a box, a cylinder and a plane.
This animated image shows the end result of the 3D project in this article.
The behavior we'll create will be attached to a 3D sprite. Create a 3D member and place it in the score like last week. The behavior will start by resetting the 3D member and then calling individual handlers to create each primitive.
property pScene, pSphere, pBox, pCylinder, pPlane
on beginSprite me
pScene = sprite (me.spriteNum).member
-- reset the world when done
pScene.resetWorld ()
-- create box, cylinder and plane
createBox (me)
createCylinder (me)
createPlane (me)
end
The createBox handler will start by making a box model resource just like we did with the sphere last week. Boxes don't have a radius property like spheres, but instead have width, height, and length properties. We'll make a box that is 20 units long on each edge.
on createBox me
-- create a box resource
boxResource = pScene.newModelResource ("BoxResource", #box)
boxResource.width = 20
boxResource.height = 20
boxResource.length = 20
Now that the resource has been created, we'll make a model from it. The property pBox will store a reference to this model.
-- create new model
pBox = pScene.newModel ("Box", boxResource)
Applying a texture to the box is a little more complex than a sphere because a box has 6 faces. We'll create a texture from the member "texture-blue" and then create a shader called "box shader" and assign this texture to it.
Then we will loop through all of the items in the box's shaderList and assign the shader to each one. This will make a nice blue box.
-- assign a texture to all 6 faces
pScene.newtexture ("box texture", #fromCastMember, member ("texture-blue"))
pScene.newshader ("box shader", #standard)
pScene.shader ("box shader").texture = pScene.texture ("box texture")
repeat with i = 1 to pBox.shaderList.count
pBox.shaderList[i] = pScene.shader ("box shader")
end repeat
Since we are going to create more than one primitive here, it is probably a good idea to move the box away from the center of the world so we can make room for the next model. By using the translate command, we can push the box -30 units along the x-axis.
-- move into position
pBox.translate (-30, 0, 0)
end
Creating a cylinder is just as easy. Cylinders have the properties height, topRadius and bottomRadius. They also have three faces that we need to shade: the top, bottom and the body of the cylinder. We'll move the cylinder 30 units to the right when done.
on createCylinder me
-- create a cylinder resource
cylinderResource = pScene.newModelResource ("CylinderResource",#cylinder)
cylinderResource.height = 20
cylinderResource.topRadius = 5
cylinderResource.bottomRadius = 5
-- create new model
pCylinder = pScene.newModel ("Cylinder", cylinderResource)
-- assign a texture to all 3 faces
pScene.newtexture ("cylinder texture",#fromCastMember, member ("texture-green"))
pScene.newshader ("cylinder shader", #standard)
pScene.shader ("cylinder shader").texture = pScene.texture ("cylinder texture")
repeat with i = 1 to pCylinder.shaderList.count
pCylinder.shaderList[i] = pScene.shader ("cylinder shader")
end repeat
-- move into position
pCylinder.translate (30, 0, 0)
end
Since cylinders have a topRadius and a bottomRadius property, you can easily set them to different values and create a cone-like object rather than a plain cylinder. Try it by changing the topRadius property to 2 instead of 5.
The final primitive that we will create is a plane.
Planes are strange because they have no thickness to them at all. They are completely flat surfaces with a front and a back face. You can set the width and length properties of a plane, but not a height.
The following handler creates a plane that is 100x100 units. It shades only one side of the plane, since we will never see the other side in this example.
Planes are created facing the camera. In order to make this plane a floor of sorts, we will have to rotate it 90 degrees along the x-axis. This we'll put the plane perpendicular to the screen. Since it has no thickness it will disappear, sort of like looking at a piece of paper edge-on. So we will lower the plane by 40 pixels so that it is slightly below eye level and visible again.
on createPlane me
-- create a plane resource
planeResource = pScene.newModelResource ("PlaneResource", #plane)
planeResource.length = 100
planeResource.width = 100
-- create new model
pPlane = pScene.newModel ("Plane", planeResource)
-- assign a texture to the sphere
pScene.newtexture ("plane texture",#fromCastMember,member ("texture-purple"))
pScene.newshader ("plane shader", #standard)
pScene.shader ("plane shader").texture = pScene.texture ("plane texture")
pPlane.shaderList[1] = pScene.shader ("plane shader")
-- move plane into position
pPlane.translate (0, -40, 0)
-- rotate plane so it is parallel to ground
pPlane.rotate (90, 0, 0)
end
The example last week showed the sphere spinning. We can do the same here by spinning these primitives. We can spin the box and the cylinder in two directions at once to make them tumble. The plane we'll just spin along one axis.
on exitFrame me
-- make box and cylinder tumble
pBox.rotate (1, 1, 0)
pCylinder.rotate (1, 1, 0)
-- make plane spin
pPlane.rotate (0, 0, 1)
end
Here is the finished product. You can experiment with assigning different textures to different faces of the models, positioning them differently, and changing their size properties as well.
Here is the finished movie. You'll need Shockwave 8.5 to see it in your browser.Next week, we'll create a primitive and learn how to make it move with the arrow keys.
A sample Director 8.5 movie is available for download in Mac or Windows format.
Gary Rosenzweig is the author of six books on Macromedia Director and Flash, including his latest, Flash 5 ActionScript for Fun & Games (http://garyrosenzweig.com/books/book6.html). He also publishes an email newsletter for Flash and Director developers called the Developer Dispatch. You can subscribe to this newsletter at http://developerdispatch.com.
Copyright 1997-2024, Director Online. Article content copyright by respective authors.