Papervision head-first part 2

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

  2. private var light:PointLight3D;
  3. private var phong:PhongShader;
  4. private var phongShaderMat:ShadedMaterial

and instantiate them in our constructor

  2. var mcMat:MovieMaterial = new MovieMaterial(new MCTexture());
  3. light = new PointLight3D();
  4. light.y = 400;
  5. phong = new PhongShader(light, 0xffffff, 0x008800);
  6. 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:

  2. 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

  2. import org.papervision3d.lights.PointLight3D;
  3. import org.papervision3d.objects.primitives.Sphere;
  4. import org.papervision3d.materials.shaders.PhongShader;
  5. 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.


 likes / 0 Comments
Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + 8 =


> <
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec