Monday, September 29, 2008

Papervision3D - Now Featuring Frustum Clipping

Yes, its true. Clipping has now been added to the Papervision3D trunk (2.0). For those of you unfamilier with the concept of clipping, it solves a very common and very annoying problem in 3D Engines - and does a few other things too. The problem I'm referring to is when a face is culled when part of it extends past the camera. You have probably noticed it in racing games or other various demos where the camera gets too close to an object and triangles start disappearing. The previous solution was trying various things with zoom levels and being more restrictive with camera position to avoid the problem altogether. Clipping (namely Near plane clipping) resolves this issue by cutting a face into new faces that *don't* pass the camera's fov. Thus, no more disappearing faces.

Something else clipping helps with is not spending time drawing triangles that are outside the viewing frustum. When you use full frustum clipping, faces are clipped to the sides of the screen. Nothing is drawn "outside" the viewable area. In addition, it removes the need to even project vertices that aren't visible to the camera (don't get too excited, it slows things back down by having to cut faces).

Use

So, using it is way to easy. There is really only one class you have to know. FrustumClipping. This class takes 1 parameter which defines which planes you want to clip against. You can use multiple planes to clip against by using a bitwise OR (I). The possible planes you can use are ALL, NEAR, TOP, BOTTOM, LEFT, RIGHT. I didn't include a far plane since it would just add unwanted computations in most instances. If you want it let me know its easy to add now.

So, to set the clipping, you simply assign it to the RenderEngine's clipping property:

Actionscript:
  1. renderer.clipping = new FrustumClipping(FrustumClipping.ALL);

Now, all objects will be clipped to the planes you specified. You can disable on object being clipped by setting DisplayObject3D.useClipping = false; When that parameter is set, clipping will not be tested and you won't have any performance change.

Features/Information

  • Set camera.useCulling. When this value is turned on, you get 2 benefits. 1) Objects outside the frustum aren't considered, but 2) ONLY objects that actually cross a frustum plane will be clipped on. Meaning, objects in the scene that don't touch the edge of the screen are ignored and not even tested.
  • Full Frustum clipping is a costly procedure and often unnecessary. If you just need general good clipping, and don't want the overhead, only use the near clipping plane: new FrustumClipping(FrustumClipping.NEAR); I will be working on improving overall performance of the clipping but for now just be aware.
  • Papervision's render pipeline now has the following flow: culling->clipping->projection->draw. FrustumClipping only projects and draws the vertices that remain after the clipping test. If you do not use clipping, all vertices will be bulk projected (as before).
  • Use less faces when using clipping! If you can get away with it, lower the face count as much as possible. Clipping will add faces where it needs them, but the more faces that have to be tested the slower the performance.
  • Set useClipping = false for all objects that don't leave the screen! (doesn't matter if you set culling though).

Demo and Source

View the Demo

and

Get the Demo



post signature