Back to "flash Basic Theory Class-Catalog"
3D Lighting
This example almost makes our rendering perfect, but it seems to be missing something. Some monotonous. Ok,ok, we see the title already know, the following let us add 3D lighting effect bar.
As with the back, the details of the 3D lighting are also quite complex and require mathematical calculations. I really don't have much room to discuss every beautiful detail, but through the fast web search you can get very much more relevant information, perhaps more than we have seen in our lifetime. Here, I give you a few basic functions that need to be used.
First, you need a light source. One of the simplest light sources has only two properties: position and brightness (brightness). In a more complex 3D system, it can also point to a direction, with color, decay rate (falloff rate), cone area, and so on. But these are beyond the scope of this example.
Let's start by making a light lighting class. It will hold the two attributes we have just said--position and brightness.
package {
public class Light {
public var x:Number;
public var y:Number;
public var z:Number;
private var _brightness:Number;
public function Light(x:Number = -100,
y:Number = -100,
z:Number = -100,
brightness:Number = 1) {
this.x = x;
this.y = y;
this.z = z;
this.brightness = brightness;
}
public function set brightness(b:Number):void {
_brightness = Math.max(b, 0);
_brightness = Math.min(_brightness, 1);
}
public function get brightness():Number {
return _brightness;
}
}
}
You can now create a new default light in the Init method of the main class:
var light:Light = new Light();
Or you can create a light that specifies the location and area:
var light:Light = new Light(100, 200, 300, .5);
Here are two important places to pay attention to. One is the position that is used only to calculate the angle of the light. The brightness of the light will not decay because of distance. So changing x, Y, Z to –1,000,000 or 1 is no different from the brightness of the light that shines on the object.
Only the Brightness property will change the characteristics of the light. We can of course add a function to judge the distance between the light and the object to calculate the brightness value of the light (brightness). It's not going to be hard, it's almost done now, so leave this function to everyone.
Brightness must be a number between 0.0 and 1.0. If this is the case, it will bring some strange results. That's why I created a private property _brightness and allowed access to brightness through public getter and setter. By doing so, we are allowed to validate the values passed in and ensure that the number is in the valid range.
An ideal class should not have public properties, even if they do not require validation, and only private properties are accessible through getter and setter functions. Here I took a shortcut to make the code concise and highlight the principles of animation programming. In this case, however, this extra step is necessary.