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: , ,

  • 1010013548

    you can use getMaterialByName method and getChildByName to control it

    你可以给那个面的材质命名!然后getMaterialByName 得到材质使其具有交互性 用getChildByName得到该面对象

  • 周杰

    Now. There is a *. dae file, which has a model of how to get the model of a face. Or which part. And then interact with the surface and the mouse?
    现在.有一个*.dae文件,里面有一个模型,该如何获得这个模型的一个面.或者其中的一部份.然后用这个面与鼠标交互?

  • king

    can you teach me how can I add a BitmapMaterial on the mesh?

    kingg64@hotmail.com

  • Alex

    When creating a plane or another primitive, papervision creates the UV for each triangle in the primitive. For this example however, one has to manually compute the UV array. Being new to this kind of stuff i can't seem to understand how this is done so i kind of need some help on this one.

    Regards,
    Alex.

  • I don't know why I read Trianglesaurus Rex...

  • Kyle

    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 } );
    }
    }

    }

    }

  • John Lindquist

    @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.

  • 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.

  • JB

    Is it possible to use quaterFaces?

    Tried
    triangleMesh3d.quarterFaces();

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

  • Soenke

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

blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

December 2008
M T W T F S S
« Nov   Jan »
1234567
891011121314
15161718192021
22232425262728
293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...