TriangleMesh3D - Create your own mesh

Saturday, December 13th, 2008 | examples, how it works

This shows you how to make little baby triangles. If you put enough of these together with the proper coordinates and rotations, you could make a Tyrannosaurs Rex!


source

package
{
	import flash.events.Event;
 
	import org.papervision3d.core.geom.TriangleMesh3D;
	import org.papervision3d.core.geom.renderables.Triangle3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class TriangleMesh3DExample extends BasicView
	{
		private var triangleMesh3d:TriangleMesh3D;
 
		public function TriangleMesh3DExample()
		{
			var material:ColorMaterial = new ColorMaterial(0xcc0000);
			material.doubleSided = true;
 
			//all the 200's are points of a triangle
			var vertex3D_1:Vertex3D = new Vertex3D(-200, -200, 0);
			var vertex3D_2:Vertex3D = new Vertex3D(200, -200, 0);
			var vertex3D_3:Vertex3D = new Vertex3D(-200, 200, 0);
 
			var triangleVertices:Array = [vertex3D_1, vertex3D_2, vertex3D_3];
 
			//use null because we haven't created its parent mesh yet			
			var triangle3d:Triangle3D = new Triangle3D(null, triangleVertices, material);
			var triangleFaces:Array = [triangle3d];
 
			triangleMesh3d = new TriangleMesh3D(material, triangleVertices, triangleFaces, null);
			//for the triangle to be "renderable", it needs to know who its daddy is
			//this is the same property as the "null" parameter a few lines above
			triangle3d.instance = triangleMesh3d;
 
			scene.addChild(triangleMesh3d);
			startRendering();
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			triangleMesh3d.rotationY = -viewport.containerSprite.mouseX / 2;
			super.onRenderTick(event);
		}
	}
}

Tags: , ,

6 Comments to TriangleMesh3D - Create your own mesh

Soenke
December 17, 2008

Hi,
I tried this example with a GouraudMaterial and a light but the triangle is shaped black. can you tell me why?

JB
January 4, 2009

Is it possible to use quaterFaces?

Tried
triangleMesh3d.quarterFaces();

but that throws an error
papervision3d\core\geom\TriangleMesh3D.as:285]

trix
January 6, 2009

Is it possible to apply a BitmapAssetMaterial on the TriangleMesh3D? I tried but I get an error #1009
BitmapColorMaterial works fine as well as the ColorMaterial in your example.

ERROR: MaterialObject3D: transformUV() uv not found!
TypeError: Error #1009: Cannot access a property or method of a null object reference.

John Lindquist
January 6, 2009

@trix - I didn’t create the UV in this example (to keep it simple), so it won’t know how to map the bitmap on the object. I’ll put it on my list to posts to make.

Kyle
February 26, 2009

Hi John,

I’ve taken this tutorial further and created a bunch of faces that will tween to become a sphere.

Earlier on this site, I requested a way to explode faces and I think I found a way to do a reverse explosion. I don’t think it’s the most efficient way but I’d like to know your thoughts.

Here is the code that I’m using. It might not work if you just copy and paste. I’m using FlashDevelop with a preloader. This is the code from Main.as.

package
{
import flash.display.Sprite;
import flash.events.Event;

// import papervision
import org.papervision3d.view.BasicView;
import org.papervision3d.core.geom.TriangleMesh3D;
import org.papervision3d.core.geom.renderables.Triangle3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.core.math.NumberUV;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.objects.primitives.Sphere;

// import tweener
import caurina.transitions.Tweener;

/**
* …
* @author DefaultUser (Tools -> Custom Arguments…)
*/
public class Main extends BasicView
{
private var allTriangles:Array = new Array();

public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point

// create a sphere so we can grab all of its faces
var sphere:Sphere = new Sphere(null, 100, 15, 15);
var sphereFaces:Array = sphere.geometry.faces;

// create a ColorMaterial that we’ll add to each triangle we create
var material:ColorMaterial = new ColorMaterial(0xCC0000);
material.doubleSided = true;

// loop through each of the faces in the sphere
for each (var sphereTriangle:Triangle3D in sphereFaces)
{
// add all of vertices for the triangle in the an array
var sphereTriangleVertices:Array = sphereTriangle.vertices;

// create an array that will later contain the vertices for the triangle we’ll create
var triangleVertices:Array = new Array();

// loop through all of the vertices
for each (var sphereTriangleVertex3D:Vertex3D in sphereTriangleVertices)
{
// create a new Vertex3D and set its vertices
var vertex3D:Vertex3D = new Vertex3D(sphereTriangleVertex3D.x, sphereTriangleVertex3D.y, sphereTriangleVertex3D.z);

// push the newly created Vertex3D to the triangleVertices array
triangleVertices.push(vertex3D);
}

// create a new Triangle3D
// we set the uv from the triangle in the sphere so we can add listeners later if we want
var triangle3D:Triangle3D = new Triangle3D(null, triangleVertices, material, sphereTriangle.uv);

// create an array that will contain the triangle faces
var triangleFaces:Array = [triangle3D];

// create a new TriangleMesh3D
var triangleMesh3D:TriangleMesh3D = new TriangleMesh3D(material, triangleVertices, triangleFaces, null);

// create the triangle3D’s dad
triangle3D.instance = triangleMesh3D;

// position the triangleMesh3D randomly
triangleMesh3D.x = Math.random() * 1500 - 750;
triangleMesh3D.y = Math.random() * 1500 - 750;
triangleMesh3D.z = Math.random() * 1000 - 500;
triangleMesh3D.rotationY = Math.random() * 360 - (360 / 2);

// add the triangleMesh3D to the allTriangles array
allTriangles.push(triangleMesh3D);

// add the triangleMesh3D to the stage
scene.addChild(triangleMesh3D);
}

// render the scene
startRendering();

// tween each triangle to it’s original position
for each (var triangle:TriangleMesh3D in allTriangles)
{
Tweener.addTween(triangle, { x:0, y:0, z:0, rotationY:0, time:2, delay:2 } );
}
}

}

}

Tomas
February 26, 2009

I don’t know why I read Trianglesaurus Rex…

Leave a comment