Director Tutorials

 

Lighting Lingo

In the next set of tutorials, we will explore specific elements of controlling a 3D environment. Each will build on the tutorial before, and we will end up with a sophisticated 3D environment, of which you will have total control.

The Lingo is explained primarily in comments in the scripts. Occasionally, additional information is provided outside the script.

Turning on the Lights - 3D Lighting

Before you start, you will need to download the following Director movie - 3Denv.dir, which contains a shockwave 3D cast member created in 3DS Max.

1. Create a pause frame behavior in frame 2 of the scripting channel.

2.  Create a new behavior attached to the 3D sprite with the following code:

property p3Dmember -- reference to 3D cast member

on beginSprite me
  -- create a reference to the 3D member
  p3Dmember = sprite(me.spriteNum).member

  -- reset 3D member to its original condition
  p3Dmember.resetWorld() 

end

on mouseUp
  letThereBeLight
end

In the beginSprite handler are standard 3D Lingo statements that you may use in any Shockwave 3D application. We create a property variable, p3Dmember, that references the 3D cast member and we use resetWold() to reset the 3D scene to its original state.

In the mouseUp handler, letThereBeLight calls a custom message to create a majestic light source (well you be the judge of how great). I have grouped together the lighting code in its own custom handler for organisational purposes. It would work just as well if all these statements were simply included in the mouseUp handler.

2.  OK, to the lighting Lingo. Continue writing the following code elements into the same script:

on letThereBeLight

    -- create a new point light
    p3Dmember.newLight("Bulb Light", #point)

The newLight command creates a light with a unique name, in our case it's Bulb Light (the first parameter of the command). We set the type of light to #point (second parameter). The light type can be #ambient, #directional, #point, or #spot.
#ambient - generalised light in the 3D world.
#directional - light from a specific direction.
#point - light source like a light bulb.
#spot - a spotlight effect.

For more information on light types, look at The make-up of Shockwave 3D Casts Technote.

    -- define light color and intensity
    p3Dmember.light("Bulb Light").color = \
rgb
(255,0,255)

The light colour property ranges from rgb(255,255,255), which is pure white to rgb(0,0,0), which is no light at all. The default setting is rgb(191,191,191).

    p3Dmember.light("Bulb Light").specular = 1

The specular property defines whether or not the light produces highlights on surface. It is either TRUE (1) or FALSE (0). The property is ignored for ambient light.

    -- move the position of the light to x=0 y=0 z=100
    p3Dmember.light("Bulb Light").transform.position = \
vector
(0,0,100)

end

3. Rewind and play the movie. Press the mouse button a few times.
The first time the mouse button is pressed, the light turns on. The second time it is pressed, you get an error message. This is because Director attempts to create a second light with the same name as the first. In a 3D world, every object must have a unique name.

4. Place the following statement before the statement with the newLight command:

  if
voidP(p3Dmember.light("Bulb Light")) then

Using the voidP() function, we can check if the expression in brackets has a value or is void. If Bulb Light does not yet exist, the function will return as TRUE and so the next line of code will be activated, which is the creation of the light. If the light already exists, then Director will ignore the subsequent statements (until it sees end if).

Place the following after the last lighting statement.
  end if

3. Rewind and play the movie. Press the mouse button a few times.
You can download the completed movie from here