In this tutorial we'll be using the 'box.fla' movie as created in the Controlling the dimensions tutorial.
1. Open your 'box.fla' again, select the 3D Box instance. Add this code to the onLoad handler:
c0 = new Color(this.f0.l);
c1 = new Color(this.f1.l);
c2 = new Color(this.f2.l);
c3 = new Color(this.f3.l);
c4 = new Color(this.f4.l);
c5 = new Color(this.f5.l);
The above code creates six color objects, for each side of the box.
2. Add the following code to the onEnterFrame handler:
// function for calculating the light (specular
+ ambient):
function ang(a) {
return a*a/100+60;
}
// this.v1.pz - Z component of vector which orthogonal to "top" and "bottom"
//
it's equal to scalar product this.v1 vector and specular light vector (front
light)
l1 = ang(this.v1.pz);
l2 = ang(this.v2.pz);
l3 = ang(this.v3.pz);
// reduce brightness depending on calculated light
c0.setTransform({ra:l1, ba:l1, ga:l1});
c1.setTransform({ra:l2, ba:l2, ga:l2});
c2.setTransform({ra:l3, ba:l3, ga:l3});
c3.setTransform({ra:l3, ba:l3, ga:l3});
c4.setTransform({ra:l2, ba:l2, ga:l2});
c5.setTransform({ra:l1, ba:l1, ga:l1});
// this is a trick to hide invisible sides
// it only works for full box without holes and transparent areas
// remove these six lines for other cases
this.f5.l._visible = this.v1.pz<0;
this.f0.l._visible = this.v1.pz>=0;
this.f4.l._visible = this.v2.pz<0;
this.f1.l._visible = this.v2.pz>=0;
this.f2.l._visible = this.v3.pz<0;
this.f3.l._visible = this.v3.pz>=0;
This is a simple, yet effective solution.