06-22-09

Flash 10 drawTriangles() with 3D light source headache.

The AS3.0 drawing API was update with the release of Flash Player 10. That’s no news, everyone knows that.
Well I had not ever used any of the new stuff in the API, shame on me!

Anyway! There is a cool new function I wanted to try out: drawTriangles(). How cool is that ? Plot all your points out, make you commands and indices vectors and let Flash do the rest.
Ok so it gets even better to know that you can map a bitmap to the triangles by passing in a uvtData vector to the drawTriangles function. Sweet.

Obviously I was thinking: plot points, drawTriangles, make a sphere or 3d object and then add some lighting. Well no!! Because if you use the drawTriangles function you don’t have that much control over how each triangle will be rendered, and you can’t really change each triangle’s color depending on the light source. Or at least I don’t know how to do it.
So I basicaly had to end up coding my own version of the drawTriangles function so I could have control over the color of each triangle.

All of that brings us to computing the light source on each triangle and then rendering it.
I’m not going to get into the details of rendering lights, google will do a far better job than me. But the main concept is this:

  • Given a light source at x, y, z, get the vector going from the light source to the current triangle
  • Now get the normal vector to the triangle
  • Calculate the angle between those two vectors and get a scalar
  • Use that scalar to change the color values of the triangle
  • Draw the triangle

That’s all there is to it.
So, here it finaly is, a 3D sphere with some lighting, all that, no thanks to drawTriangles!
It’s rendering about 1000 triangles each frame.
You can get the source here. This is one of most unsexy code ever! You have been warned

Oh and don’t forget to double click the stage :)

2 Comments on “Flash 10 drawTriangles() with 3D light source headache.”

  • Andrew Fitzgerald

    That’s sick! Good work man, it looks like you’ve got a lot of experiments here I’ll have to check out ;)

    Cheers!

    06-24-09 » 9:01 am »

  • 3D sound plance | signalsondisplay

    [...] Flash 10 drawTriangles() with 3D light source headache. [...]

    06-29-09 » 6:17 pm »

Leave a Comment