Erain 3D
advert here

Level: Beginner Version : 1.0 Author : Thomas PFEIFFER Date : May 25th 2006
Reference : Transform your 3D object


Understand lights in Sandy

Sandy's lights are very simple.

Sandy doesn't have advanced light management and there are two reasons for that. First of all, Flash doesn't allow complex light implementation – the flash player is not fast enough – and the mathematical theory behind scene lighting is quite complex, maybe too much for me right now.

Basically in Sandy a light has a direction and a power. That's all :) As I said, it is very simple.

Light direction

Concerning the light direction, you have to be aware that the emitting source is considered to be located at infinity, so far that we are actually receiving a field of parallel light rays, with a constant power. To illustrate, imagine the sun as a light source.

The direction is a vector, and its direction is very important because a light coming from the left wouldn't illuminate faces whose normal vector is pointing to the right.

Light Power

The light power is constant, and doesn't change with direction or time, etc. The higher the power is, more your objects are illuminated. With a very high power, even the faces that are normally hidden ( black! ) will be illuminated. With a low value, the faces will be darker faster.

You may adjust this value depending on your needs.

A default light is available in Sandy. Its direction is parallel to the Z axis, and its power is about 50 (no unit here). But even if a default light is available, your 3D objects WILL NOT be illuminated by default. To enable the light on your objects, you have to call the setLightingEnable() method of your object's skin with a true parameter value. (false to disable)

Code

Now you are certainly impatient to test it yourself, so here is the sample code. The light is coming from the left, with low intensity.

import sandy.core.light.*;
import sandy.core.data.*;
import sandy.core.group.*;
import sandy.primitive.*;
import sandy.view.*;
import sandy.core.*;
import sandy.skin.*;
import sandy.util.*;
import sandy.core.transform.*;
import sandy.events.*;
function init (Void):Void
{
	// screen creation, the object where objects will be displayed.
	var ecran:ClipScreen = new ClipScreen (this.createEmptyMovieClip ('ecran', 1), 600, 600);
	// we create our camera
	var cam:Camera3D = new Camera3D (700, ecran);
	// we add the camera to the world
	World3D.getInstance ().addCamera (cam);
 
        //We create the Light coming from the left with a low intensity (20)
	var l:Light3D = new Light3D (new Vector (1, 0, 0), 20);
	// set the world light 
	World3D.getInstance ().setLight (l);
 
	// we create the root node.
	var bg:Group = new Group ();
	// and set it as the root node of the world.
	World3D.getInstance ().setRootGroup (bg);
	// and we launch the scene creation
	createScene (bg);
	// and now that everything is created, we can launch the world rendering.
	World3D.getInstance ().render ();
}
function createScene (bg:Group):Void
{
	// We create our object. It is a cube of 50 pixels, and with the quad mode, so here four points per face.
	var o:Object3D = new Box (50, 50, 50, 'quad');
	// we create a skin to "dress up " the object a bit, and make it visually better.
	//  SimpleColor skin is used here, it needs the fill color, alpha, and thickness
	var skin:Skin = new MixedSkin (0x00FF00, 100, 0, 1);
             // NEW PART, we enable the light 
             skin.setLightingEnable(true);
	// We apply the skin to the object
	o.setSkin (skin);
	// We create two TransformGroup instances. Each one corresponds to a node of the tree we are going to create.
	var tg1:TransformGroup = new TransformGroup ();
	var tg2:TransformGroup = new TransformGroup ();
	// We also create two instances of Translation objects.
	// Give them an appropriate name, this becomes important very rapidly.
	var translation:Transform3D = new Transform3D ();
	// We create an interpolator to make it rotate linearly for a while
	var ease:Ease = new Ease ();
	ease.linear ();
	var rotint:RotationInterpolator = new RotationInterpolator (ease.create (), 300);
	// we translate the object by 500 pixels on the Z axis.
	translation.translate (0, 0, 500);
	// We apply those transformations to the nodes.
	tg1.setTransform (translation);
	tg2.setTransform (rotint);
	// We add the rotation tansformation to the node which represents the rotation,by setting it as a child.
	// The rotation will be applied first, translation later (higher in the tree)
	tg2.addChild (o);
	// Now we link the two nodes
	// The order here is very important.
	tg1.addChild (tg2);
	// Now we simply link the translation node to the root node, thus completing the tree creation
	bg.addChild (tg1);
}
// We launch the animation creation.
init ();

Sources and demo

Source :
.fla file
Demo :