Phong shading (in progress) in AS3

The past week, I’ve mainly spent trying to figure out how to do (fake) Phong shading and environment mapping. In the case of phong, the result can be seen here. A few alternatives that I tried didn’t work out, but after some quick hints from Ralph and lots of calculations, I found a solution that more or less works (at least for me ;) ). Weirdly enough, most of my time went into figuring out how not to show the lighting on the opposite side of the lit object (it’s a normal effect since this shading technique borrows from environment mapping). As you might notice, it’s still not quite right…

In the demo, you can see more or less the same cube and sphere from the Gouraud post. The uv-mapping is still off on the seem where the two horizontal ends of the texture (should) connect, but at least now I know why :) The texture “wraps back” because the U-coordinates are on opposite sides of the texture. I’ll have to figure out how to fix it… If it looks like a familiar problem, any suggestions are welcome :)

Next task: heavy duty optimization and refactoring! Good times :D

8 thoughts on “Phong shading (in progress) in AS3

  1. Pingback: Status update on Wick3d | Der Schmale - David Lenaerts's blog

  2. Hi Promethe,

    It’s using a lightmap (as bitmapdata) representing the lighting values for vertex normal locations, it’s not very different from environment maps, except that it can change orientation. The lightmap is calculated once using specular and diffuse (lambert) calculations. The drawing just occurs on a seperate layer.
    This is an old version of Wick3d, however, and the current version doesn’t have shaders anymore. Slowly working on that tho, and hopefully, when they’re back it will be much faster – and open source of course :)

  3. It means you are using a per polygon drawing approach?
    I’m working on the same think but I use drawTriangles methods… and I have problems with non convex polytopes! The per-triangle approach must be slower… still it’s gonna be necessary to perform vertex shaders i think :(

    Pixel Bender should do the trick, but I did not manage to use it with another (bitmap|color)Fill (I did not try much though…).

    I will look at your code when it’s done to see if there is ideas I could use or just to see how it performs.

  4. It’s per-triangle (the whole model is built out of triangles, I don’t support n-gons for performance reasons). Btw, this demo is still using FP9 (same for the next few updates that I’ll do). I’m not sure Pixel Bender’s going to help much, though. Things like phong shading will be more correct, but the speed impact will be dramatic. I am looking forward to try 3D shading with PB nonetheless, for experimentation’s sake :)

  5. n-gons or not, per-polygon treatment == loop == cost a lot :s
    when rendering a 3000 polygons model, doing a per-polygon algorithm might not be a good idea. I’ll give it a try though…

  6. With spheres for instance you could do a per-object approach, but for more complicated surfaces, I wouldn’t know how you could do the shading if not per poly…

  7. pure convex polytopes such as cubes and spheres do not need a per polygon approach for the same reasons they don’t need z-sorting: backface culling does all the job…

Leave a Reply

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