Using Custom Primitives in Papervision

January 2, 2009


I’ve been building a spherical 3D memory game for the book and found the geodesic sphere and curved plane primitives very useful. These two primitives are not part of the standard Papervision primitive set and are therefore referred to as custom primitives.

You can download  the geodesic sphere primitive from panosalado (a great open source pano project) and the curved plane is created here by me. Here is a little background on the primitives.

Geodesic Sphere

The geodesic sphere primitive was created by Gabriel Putnam and can be downloaded from the open source Panosalado project found on Google Code. It starts with an octahedron and then builds triangle resolution by breaking the edges.
Geodesic Spheres handle image distortion better than the “normal” sphere in Papervision because all of their triangles are more uniform. However, there is still very significant distortion at the poles, but arguably less and the equator is definitely less distorted. Its advantage over using a Sphere primitive is that its vertices are evenly spaced.

Curved Plane

Using the geodesic sphere from above you can wrap a series of planes around it to create a 3D memory game. The positions of the planes can be positioned using the geometry.vertices method demonstrated in the previous blog post. But just using square planes looks a little disjointed as shown on the left of the image below. For a better visual appeal, you want to wrap the planes around the geodesic sphere so that their curvature matches the geo-sphere. This can be accomplished by creating a custom curved plane primitive shown on the right side of the image below. 


Creating a Curved Plane that Matches the Geodesic Sphere

Creating a Curved Plane that Matches the Geodesic Sphere


It’s pretty easy to create a curved plane. Follow this procedure:

• Open up the Plane primitive found in org/papervision3d/objects/primitives and rename it CurvedPlaneXY. Then rename the class and constructor CurvedPlaneXY as well.
• Next add parabolic curvature for both the x and y iteration across the width and height of the plane as shown in the code below;

for( var ix:int = 0; ix < gridX + 1; ix++ )
{for( var iy:int = 0; iy < gridY1; iy++ )
{var x :Number = ix * iW – textureX;
var y :Number = iy * iH – textureY;
var z :Number = -(myCurvatureX*x*x+myCurvatureY*y*y)/((width*width+height*height)/2);
vertices.push( new Vertex3D( x, y, z ) );

This is pretty easy to do and greatly enhances the visual appeal of the wrapped geo-sphere.

You can find the code that wraps the geodesic sphere in curved planes, the geodesic sphere primitive and the curved plane primitives by clicking the more button below;

Read the rest of this entry »