This part takes off where part 1 left.
In this part, we will add a shader to our awesome cube and make it more like a cube. Let's start by making a MovieClip in the Flash IDE and call it MCTexture (also export it for actionscript with the same class name). My MCTexture is a blue square again, anything would do, even an image.
Let's define some new objects
private var light:PointLight3D; private var phong:PhongShader; private var phongShaderMat:ShadedMaterial
and instantiate them in our constructor
var mcMat:MovieMaterial = new MovieMaterial(new MCTexture()); light = new PointLight3D(); light.y = 400; phong = new PhongShader(light, 0xffffff, 0x008800); phongShaderMat = new ShadedMaterial(mcMat, phong);
We can now remove all the stuff about materials from the previous part and change the line where you make a new cube to:
var sphere:Sphere = new Sphere(phong,200,32,32);
Now, to explanations. Shaders require lights to be rendered since they define how an object's surface behaves under lights (pixel shaders, not vertex shaders) so we created a light and placed it up in there somewhere in space. We also created a new MovieMaterial which is essentially a material that has the movie clip we have created in the Flash IDE in it.
Then we defined the PhongShader and the ShadedMaterial objects. PhongShader's constructor takes in a few parameters, so we pass in the light instance, a light color and an ambient color(0x008800).
Now we need to create a ShadedMaterial instance and put our MovieMaterial and the PhongShader (or any other shader) in it. The last line does just that
The last snippet replaces our boring cube with a sphere (gasp) and gives it te ShadedMaterial as the material parameter, 200 as the radius parameter and 32 for both of the subdivison parameters. All primitive objects can take those parameters as well, you can define width, length and height of a cube, width and length of a plance and subdivision levels on all of them (tip: if your bitmap textures are all skewy, raise the subdivison levels a bit).
Now, if you import these extra packages
import org.papervision3d.lights.PointLight3D; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.materials.shaders.PhongShader; import org.papervision3d.materials.shaders.ShadedMaterial;
and compile, you should get something like this.
There we are, we now have a shaded cube and a light in our scene You can test out other shaders and primitves for mileage. I hope this was helpful.