Understanding how a TriangleMesh3D works

24 June 2009

Papervision3D, News

Understanding how a TriangleMesh3D works

In Papervision3D, the TriangleMesh3D class is a geometry class used to build renderable objects. There are several types of geometry objects, such as: Particles, Lines3D, Pixels, and meshes conformed by triangles,  called triangleMesh3D objects (See the org.papervision3d.core.geom path). In this post I’ll focus the analysis for understanding how to build a personalized TriangleMesh3D object.

We remember that the more easy way to create custom triangle3DMeshes is using ASCollada (exporting your 3D model from Maya, 3DsMax, etc, to Pv3d). This post is focused on creating a triangleMesh3D at runtime.

The primitive objects (Plane, Cylinder, Sphere)  are extensions of TriangleMesh3D. The construction of another type of TriangleMesh3D object have similar procedures to primitive objects creation.

The basic sintaxis to create a TriangleMesh3D instance is:
var triangleMesh3D = new TriangleMesh3D(material, vertices, faces)
Where the parameters represent:

-material:MaterialObject3D, the material to be mapped into triangles3D.

-vertices:Array, an array of Vertex3D instances that conform the mesh vertices.

-faces:Array, an array of Triangle3D instances that represent the triangles to be rendered.

The vertices array and the faces array represent the TriangleMesh3D geometry.

triangleMesh3D.vertices
triangleMesh3D.faces

The orthogonal space.
Papervision3D use a right-handed triad. That means that the positive axis can be represented by the right-hand rule. This is a graphic which the directions of the positive axis can be appreciated.

xyz_orthogonal_system_pv3d

Creating the vertices array

Each position of the vertices array store a instance of Vertex3D. A vertex3D object lets you create the 3D vertices. A vertice is composed of x, y and z values that represent the position in the orthogonal space.

The sintax to creat a Vertex3D objects is:
var x:Number = 10;
var y:Number = 20;
var z:Number = -15;
var vertex3D = new Vertex3D(x,y,z)

Where the x,y and z variables represent the position in the 3D space.

The set of vertex3D is stored in the vertices array.

A Vertex3D object allow to store others properties (vertex3D.extra).

When a vertex3D object is asigned to another variable, it adquires the same characteristics than the process of array element´s asignation. Then, if you want to create a copy of a vertex3D, you need to create a new instance of this vertex3D.

For example:

var vertexA:Vertex3D = new Vertex3D(1,2,3);
trace(vertexA) //x:1, y:2, z:3;
var vertexB:Vertex3D = vertexA;
vertexB.x = -5;
trace(vertexB) //x:-5, y:2, z:3;

trace(vertexA) //x:-5, y:2, z:3;

See also:

var vertexA:Vertex3D = new Vertex3D(1,2,3);
trace(vertexA) //x:1, y:2, z:3;
var vertexB:Vertex3D = vertexA.vertex3DInstance;
vertexB.x = -5;
trace(vertexB) //x:-5, y:2, z:3;
trace(vertexA) //x:1, y:2, z:3;

Creating the faces array

The faces array have the Triangle3D objects stored, that represent the triangles which conform
the mesh. The sintax to create a Triangle3D is:
var triangle3D:Triangle3D = new Triangle3D(do3d, vertices, material, uv)
Where
do3d:DisplayObject3D, this is the instance that contain the TriangleMesh3D
vertices: An array of length 3. Each position of the array stores a vertice (vertex3D) of the vertices array. Theses vertices represent the absolute position of the triangle3D vertices (v0,v1,v2).
material: The Material3D object to be used in the triangle3D (BitmapMaterial, ColorMaterial, PhongMaterial, etc) is referenced here.

uv: An array of length 3. This array store NumberUV objects that represent the UV mapping of the triangle3D.

The connected faces represent the set of triangle3Ds that are connected to the vertex3D object. Each vertex3D object that belongs to the mesh have at least one triangle3D connected.

After initializing a triangleMesh3D (with the triangleMesh3D.geometry.ready = true property) each vertex3D in the mesh store a Number3D object in the triangleMesh3D.normal property. The normal Number3D represents the normal vector to the surface in the vertice. It is calculated in relation to the triangle3D normals that are stored in the connected faces. The normal have modulo 1.

UV Mapping

Mathematically, a NumberUV represents the projection in the xy plane (by default) of the versor (vector with modulo=1) of the triangle3D vertices. If you move the triangleMesh3D.vertices[k] x, y or z positions, you’ll see a distortion in the texture, this is because you need to update the UV mapping of the triangle3D. You can use the triangleMesh3D.projectTexture() method. This method isn´t fast if it is used in a loop because it creates a lot of new variables and uses dictionary, you can create your own UV mapping update method, for example:

uvAx = Math.abs( (a.x - menU) / (mayU - menU + 0.000001) );
uvAy = Math.abs( (a.y - menV) / (mayV - menV + 0.000001) );
uvBx = Math.abs( (b.x - menU) / (mayU - menU + 0.000001) );
uvBy = Math.abs( (b.y - menV) / (mayV - menV + 0.000001) );
uvCx = Math.abs( (c.x - menU) / (mayU - menU + 0.000001) );
uvCy = Math.abs( (c.y - menV) / (mayV - menV + 0.000001) );
uvDx = Math.abs( (d.x - menU) / (mayU - menU + 0.000001) );

uvDy = Math.abs( (d.y - menV) / (mayV - menV + 0.000001) );

uvA = new NumberUV(uvAx, uvAy);
uvB = new NumberUV(uvBx, uvBy);
uvC = new NumberUV(uvCx, uvCy);
uvD = new NumberUV(uvDx, uvDy);
faces.push( new Triangle3D(this, [a,c,b], null, [uvA,uvC,uvB]) );
faces.push( new Triangle3D(this, [d,b,c], null, [uvD,uvB,uvC]) );

in were a, b, c and d are the vertices of two connected triangles, mayU is the greater X value, mayV is the greater X value, menU is the smallest X value, menV is the smallest Y value, and 0.000001 is used to solve the indetermination of division by zero. This is a code fragment of a dinamic triangleMesh3D.

The secuence of the uv array parameters that are stored in a Triangle3D must be governed by the right hand rule (I’m not sure why pv3d uses the two types of triad rules, it could be better to use only the left hand rule). These graphic can help you  understand the secuence of the uv0, uv1, and uv2 NumberUVs that correspond to v0, v1 and v2 vertex3D’s of a triangle3D.

mapeado2UVmapping

Some experiments of vertices manipulation are done only with wireframe materials or with color material. A way to do vertices manipulations without a distortion in the texture of the 3D objects in meshes that have bitmapMaterials or shade materials is to update the uv mapping of the faces.

PD: This post needs a good revision of the english. My native language is spanish. I created the english version of this blog to exercise my english writting. ;)

rzbknd75ht

Leave a Reply