Author: makc
Date: February 26th 2008
version: 3.0.2

Lighting in Sandy3D v3.0.2

This page briefly covers lighting features new in 3.0.2. Before you read, you might want to refresh 3.0.1 lighting features in your memory looking at LightAttributes tutorial and Gouraud demo.

Phong reflection model

Sandy3D v3.0.2 provides support for Blinn flavor of Phong reflection model with four parameters: ambient, diffuse, specular and gloss exponent.

Let us will start from this simple code that should work fine in 3.0.1:

package
{
	import flash.display.*
	import flash.events.*

	import sandy.core.*
	import sandy.core.scenegraph.*
	import sandy.materials.*
	import sandy.materials.attributes.*
	import sandy.primitive.*

	public class V302_Light extends Sprite
	{
		private var world:Scene3D = new Scene3D ("scene", this as Sprite, new Camera3D (200, 200), new Group ("root"));

		public function V302_Light ()
		{
			// set up light attributes
			var a:LightAttributes = new LightAttributes (true, 0.2);

			// show something
			var s:Sphere = new Sphere ("s", 100, 20, 20);
			var m:ColorMaterial = new ColorMaterial (0xFF, 1, new MaterialAttributes (a));
			m.lightingEnable = true; s.appearance = new Appearance (m);
			var g:Group = new Group ("root"); g.addChild (s); g.addChild (world.camera);
			world.root = g; world.light.setDirection (1, -1, 1); world.render ();
		}
	}
}
This code allows you to alter just one of reflection model parameters, ambient, in light attributes constructor:

// here 0.2 is ambient
var a:LightAttributes = new LightAttributes (true, 0.2);

In 3.0.2 you can also alter three other parameters of reflection, for example:

var a:LightAttributes = new LightAttributes (true, 0.2);
a.diffuse = 0.5;
a.specular = 1;
a.gloss = 5;

Same parameters are available in GouraudAttributes and new PhongAttributes (see below).

Colored light

Light3D class in 3.0.2 has additional property, color. Setting this property changes color of all objects that have one of lighting attributes in their appearance. For example, let's add this code somewhere prior to render() call in the code above:

// full red with slight shade of blue
world.light.color = 0xFF0030;

Smooth shading

Sandy3D v3.0.1 already had smooth shading implemented with GouraudAttributes. The way it works, however, this class will not render highlights in the middle of a polygon correctly. In order to work around that, 3.0.2 provides complementary attribute class, PhongAttributes:

var a:PhongAttributes = new PhongAttributes (true, 0.2, 15);
a.diffuse = 0.5;
a.specular = 1;
a.gloss = 5;

Before you go using this attribute, there are few more things you need to know.

Cel-shading

Another new feature of 3.0.2 lighting is cel-shading, provided by new CelShadeAttribute. By default, this attribute looks like this:

var a:CelShadeAttributes = new CelShadeAttributes ();
 
// swf on the right uses following light vector:
world.light.setDirection (1, -1, 3);

Alternatively, you have an option to provide your own shades map, as explained here.

Light support in sprites

In 3.0.2, sprites can obey scene light, since they posses new material property. Through this property, you can apply lighting attributes to sprites like this:

var s:Sprite2D = new Sprite2D ("s", new MySprite (), 1);
s.material = new Material (new MaterialAttributes (
	new LightAttributes (false, 0.4)
));

Note that since geometry of sprites is unknown to engine, it can simulate ambient reflection only, which is enough for fading sprites together with other objects in the scene, but may make your sprites look dark (since diffuse and specular reflections are 0). To work around this, use increased ambient values for sprites.