Da Fish in Sea

These are the voyages of Captain Observant

3D Heart With Lighting

| Comments

For the latest 25 lines contest, I wanted to do something with a Valentine’s Day theme. I decided to try modeling a heart, and apply some lighting to it. Either of these would by themselves would be challenging to do in under 25 lines, and although I was able to achieve the result I was looking for, I could not quite get it down to 25 lines in time, and had to cut the animation. So I wanted to present it here as I wanted it to be seen. In retrospect it is probably not all that exciting, but I’m pretty happy that my idea for doing lighting worked.

The lighting works like this: you have a BitmapData object which is used for the texture map. You have the uvtData which specifies which points on the texture match up with which vertices on the model. So what I did is calculate the angle between the lighting angle (represented by a Vector3D object) and the normals of each triangle in the mesh. You get the normal of a triangle by finding the vector which is perpendicular to two of the lines in the triangle. Then you draw onto the BitmapData with the calculated shading color, using the uvt coordinates for that tringle in the mesh. So there’s a feedback between the position of the object and the BitmapData object used to skin it. Amazingly I was able to do this with just a few calls to some of the 3D api methods, and no hairy trigonometry was required. I will be doing another example of the lighting technique soon, with more details - just consider this a proof of concept at this point ;)

flash 10 required

Da Code

Heart.as (GitHub)