3D Cameras
Provided by Adam Montandon


Lots of people ask me how to use cameras in 3D. There are loads of different things you can do with cameras, and here is a basic introduction for anybody starting out with 3D lingo.

This tutorial is designed for people who are familiar with how lingo works, and who want to get started manipulating cameras in 3D.

Whenever you create a 3D scene, it has, by default, 1 camera, at position X = 0, Y = 0, Z = 250.

Positions
You can move a camera to an exact X, Y or Z position like this:
(member 1 of castLib 1).camera[1].transform.position.x =
50
Changes the X position of camera 1 to 50

(member 1 of castLib 1).camera[1].transform.position.y = -200
Changes the Y position of camera 1 to 50

(member 1 of castLib 1).Camera[1].transform.position.z = 32.965
Changes the Z position of camera 1 to 32.965

You'll notice that's a different line of code for X, Y and Z positions. How about saving time to do X Y and Z together.

You can use 3D vectors.

Vectors look like this:
vector(x, y, z)

So now you can try:
(member 1 of castLib 1).Camera[1].transform.position = vector(50,-200,32.965)

It does the same as the previous 3 lines of code!

Rotations
We can also rotate the camera like this:
(member 1 of castLib 1).Camera[1].transform.rotation.x = -3
(member 1 of castLib 1).Camera[1].transform.rotation.y = 20
(member 1 of castLib 1).Camera[1].transform.rotation.z = -8
or as a vector:
(member 1 of castLib 1).Camera[1].transform.rotation = vector(-3,20,-8)

Hither and Yon
You can set the Hither and Yon settings in a camera to see how much of the world is drawn. This can help when slower computers are trying to draw large 3D scenes, you may want to clip the viewing distance, so that objects very close up are not rendered or objects far away are not rendered.

Objects CLOSER than Hither will not be rendered:
(member 1 of castLib 1).Camera[1].hither = 1

Objects further away from Yon will not be rendered.
(member 1 of castLib 1).Camera[1].yon = 3.402823

These settings are great if you are making a driving game, and want to avoid "pop-up" in some situations, you can increase the Yon, or if you want to make a "cut away" of a 3D scene, you can increase your Hither.

Field Of View
You can set your field of view like this:
(member 1 of castLib 1).Camera[1].fieldOfView = 34.5160

By changing the field of view you get a "warped" effect, a bit like a fish eye lens. So if you want to make trippy effects of being sucked through dimensions, or 3D warp effects, bulges etc, alter the Field of view.

Projection
The standard projection mode is Perspective. This means objects get smaller the further away you are from them, this is how we see in the real world, you can set it like this:
(member 1 of castLib 1).Camera[1].projection = #perspective

However, Orthographic projection means that things do NOT get smaller the further away they are. You can set it like this:
(member 1 of castLib 1).Camera[1].projection = #orthographic
This mode is good to use when you want something that behaves like its in 3D but looks flat.

Trails
The clearatrender property of the camera tells the camera to clear what it previously drew. Normally this is set to true like this:
(member 1 of castLib 1).Camera[1].colorBuffer.clearAtRender = TRUE

However, if you set it to false like this:
(member 1 of castLib 1).Camera[1].colorBuffer.clearAtRender = FALSE
You get a trails effect, as the camera does not clear the previous 3D rendering. You can make snake like effects or anything with trails, blobs etc with this property.

Background color of the camera.
You can set the color that the camera uses to "clear" with, so by setting an RGB value you can change the color that appears behind your models like this:
(member 1 of castLib 1).Camera[1].colorBuffer.clearValue = rgb( 0, 0, 0 )

Change the rgb(0,0,0) (black) to rgb(255,0,0) for red or whatever RGB number you decide looks best for your scene.

Fog
Fogging effect is normally used to make models in the background "fog out" or fade to the color that you set. For example, if you are making a space game, you could use a black fog, so that spaceships in the distance look darker than ones closer to you. This fog is NOT like the fancy volumetric lighting effects that you see in professional 3D packages, it does NOT make smoke, or fog, or mist or haze or anything like that, it simply blends distant models into a background color.

To turn on fog use:
(member 1 of castLib 1).Camera[1].fog.enabled = True

and to turn it off use:
(member 1 of castLib 1).Camera[1].fog.enabled = False

To set your fogging distances use:
(member 1 of castLib 1).Camera[1].fog.near = 4
(member 1 of castLib 1).Camera[1].fog.far = 1006
fog.near is the point in world units where fogging starts.
Fog.far is the point where fogging is at maximum strength, anything beyond this point is totally covered in fog.

To set the color that your models will fog to use:
(member 1 of castLib 1).Camera[1].fog.color = rgb( 0, 255, 0 )
Changing the RGB value to whatever you like, this one fogs to green.

You have 3 different decay modes to the fog effect:
(member 1 of castLib 1).Camera[1].fog.decayMode = #linear
(member 1 of castLib 1).Camera[1].fog.decayMode = #exponential
(member 1 of castLib 1).Camera[1].fog.decayMode = #exponential2
Macromedia describes the 3 modes as:
#linear: the fog density is linearly interpolated between fog.near and fog.far.
#exponential: fog.far is the saturation point; fog.near is ignored.
#exponential2: fog.near is the saturation point; fog.far is ignored.

Find the best effect that works for you.

Backdrops
Backdrops are used as a 2D image placed at the back of the camera, with all other models rendered on top of it. These are not all that useful in most 3D work, unless you need a static shot, so I wont spend much time on these. To add a new backdrop, it first has to be imported as a texture then use:
(member 1 of castLib 1).Camera[1].addBackdrop((member 1 of castLib 1).texture[1],point(0,0), 0)

You can remove a backdrop like this:
(member 1 of castLib 1).Camera[1].removeBackdrop(1)

Overlays
Overlays work in a similar way to backdrops, except they go on top of everything, appearing at the front of the camera, with all the models rendered behind.

You can add an overlay as long as it is defined first as a texture like this:
(member 1 of castLib 1).Camera[1].addOverlay((member 1 of castLib 1).texture[1],point(0,0), 0)

and remove it like this:
(member 1 of castLib 1).Camera[1].removeOverlay(1)

Adding More Than One Camera
To make a new camera with the name "CloseUp" simply use:
(member 1 of castLib 1).newcamera("CloseUp")

You can change the word CloseUp for any name you want to give your camera.

You can make as many new cameras as you want.

You can replace the code camera[1] for camera[2] to use the second camera you make, or camera[3] for a third camera and so on. To call a camera by name instead of number you can use camera("CloseUp") or whatever name you gave your camera. Never give 2 cameras the same name though!

Deleting Cameras
To remove your camera just use:
(member 1 of castLib 1).deletecamera("CloseUp")

Or, if you want to delete a camera by index number use
(member 1 of castLib 1).deletecamera[2]

Changing from one camera to another
Cameras are actually changed at the sprite level, not at the member.

If your 3D member is in sprite channel 1, and you want to change to camera 4, use this:
sprite(1).Camera = (member 1 of castLib 1).Camera[4]

Or to change it to a camera via its name use:
sprite(1).Camera = (member 1 of castLib 1).Camera("closeup")

There are lots of other interesting things you can do with cameras, and here I have just scraped the surface, but I hope it will give many of you a good introduction to some of the features available with cameras in Director 8.5.

1 comments

  1. Anonymous // 10/22/07, 7:13 AM  

    wah tutorialnya bagus tapi bisa minta sourcenya ga(pening bos)