Acknowledgment
Many thanks to Ralph Hauwert who wrote PV3D 2.0. As I work through his many classes, I really get the feeling that I am standing on the shoulders of a giant. I got to meet him in LA, where I took his PV3D RMI Certification class. I flew 1500 miles from Kentucky to take his course, and it was worth 100 times every dime I spent to get there.
Intro
If you want a sunrise you’ve got to have brightness control. The PV3D point light source only has a distance parameter: used primarily to calculate the angle of the triangle normals – no brightness factor. But adding brightness is all too easy, and in this post we’ll show you how to do it!
We’re going to get right down to the nitty-gritty here, so to learn more about how lights works in Papervision3D (normals, cross products, dot products, etc…) buy the book. There, I explain it all!
YouTube
Discussion
If I had any doubt as to where the physics should go, working with light totally solidified it for me. Physics goes into the DisplayObject3D class! And in this case, we just add a simple brightness parameter as shown below:
public function get brightness():Number
{
return this._brightness;
}
public function set brightness(value:Number):void
{
this._brightness = value;
}
And since my light is a display object it immediately inherits the brightness. Ha! You thought this was going to be hard – it isn’t!
Updating Shaders
Now all you have to do is update all the shaders with your new brightness parameter and boom – you’re done. In Papervision3D there are 5 shaders: flat, cell, gouraud, phong, and environment. And we explain each one in detail in the book.
So open up the Shaders which are located in the
org/papervison3d/materials/shadermaterials
and make the following modifications:
Flat
Open up the FlatShadeMaterial and multiply the light brightness by the zd factor.
zAngle = zd*0xff*light.brightness;
Gouraud
Open up the GouraudShadeMaterial and multiply the light brightness by the po, p1, and p2 factors.
p0 *= 127*light.brightness;
p1 *= 127*light.brightness;
p2 *= 127*light.brightness;
Cell, Phong, Environment (three-in-one)
Open up the EnvMapMaterial class and make the following changes:
It seems like a lot, but all you do is multiply the dot product function by (2-light.brightness) .
The changes are in bold below:
p0 = lightmapHalfwidth*(face3D.v0.normal.x * lightMatrix.n11 + face3D.v0.normal.y * lightMatrix.n12 + face3D.v0.normal.z * lightMatrix.n13)*(2-light.brightness) +lightmapHalfwidth;
q0 = lightmapHalfheight*(face3D.v0.normal.x * lightMatrix.n21 + face3D.v0.normal.y * lightMatrix.n22 + face3D.v0.normal.z * lightMatrix.n23)*(2-light.brightness)+lightmapHalfheight;
p1 = lightmapHalfwidth*(face3D.v1.normal.x * lightMatrix.n11 + face3D.v1.normal.y * lightMatrix.n12 + face3D.v1.normal.z * lightMatrix.n13)*(2-light.brightness)+lightmapHalfwidth;
q1 = lightmapHalfheight*(face3D.v1.normal.x * lightMatrix.n21 + face3D.v1.normal.y * lightMatrix.n22 + face3D.v1.normal.z * lightMatrix.n23)*(2-light.brightness)+lightmapHalfheight;
p2 = lightmapHalfwidth*(face3D.v2.normal.x * lightMatrix.n11 + face3D.v2.normal.y * lightMatrix.n12 + face3D.v2.normal.z * lightMatrix.n13)*(2-light.brightness)+lightmapHalfwidth;
q2 = lightmapHalfheight*(face3D.v2.normal.x * lightMatrix.n21 + face3D.v2.normal.y * lightMatrix.n22 + face3D.v2.normal.z * lightMatrix.n23)*(2-light.brightness)+lightmapHalfheight;
That’s it. Just run your shaders like you normally do adjusting your brightness in your animation loop and TaDah! instant rising sun!
Big Note
Keep your brightness value between 0 or 1 or your program will freak, and in the down-loadable source above I renamed the Shader material classes adding a “Brightness” name at the end of them. You don’t have to do this. I did this as a precautionary measure… if I mess up (and I do), I can always go back to the original class.
To see the wrapper file for the shaders download the source above or click the more button below:

Posted by Mike Lively