Per Face Material

Friday, December 12th, 2008 | examples


source

package
{
	import flash.events.Event;
 
	import org.papervision3d.core.geom.renderables.Triangle3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#ffffff", frameRate="60")]
	public class PerFaceMaterial extends BasicView
	{
		private var sphere:Sphere
 
		public function PerFaceMaterial()
		{
			sphere = new Sphere(null, 400, 30, 20);
 
			for each(var face:Triangle3D in sphere.geometry.faces)
			{
				var color:Number = Math.random() * 0xffffff;
				face.material = new ColorMaterial(color);
			}
 
			scene.addChild(sphere);
			startRendering();
		}
 
 
		override protected function onRenderTick(event:Event=null):void
		{
			sphere.rotationY += .3;
			renderer.renderScene(scene, camera, viewport);
		}
	}
}

Tags: ,

4 Comments to Per Face Material

Hawk
December 14, 2008

That’s really cool. What if I would like to color or add a material to a square, instead of a triangle?

John Lindquist
December 15, 2008

@Hawk - I guess you could just change the color ever other triangle. Something like this:

			var count:int = 0;
			var color:Number = Math.random() * 0xffffff;
			for each(var face:Triangle3D in sphere.geometry.faces)
			{
				if((count % 2) == 0) color = Math.random() * 0xffffff;
				face.material = new ColorMaterial(color);
				count++;
			}
Nate
January 6, 2009

Thanks very much for your excellent tutorials!

I’m trying to extend this to add shaded materials to each face, but I’m getting an error (”Cannot access a property or method of a null object reference”). The one major change is at line 22, from

face.material = new ColorMaterial(color);

to

face.material = new FlatShadeMaterial(light, 0xDDDDDD, color);

I also added “light” as a private PointLight3D and the necessary import statements.

Adding a shaded material to the entire Sphere using its constructor does not result in an error.

I’d be very grateful for any suggestions.

Phil
February 10, 2009

Cool example, thanks.

I’m trying to count the visible triangles in the same way.


override protected function onRenderTick(event:Event=null):void
{
var num:Number;
num = 0;
for each(var face:Triangle3D in sphere.geometry.faces)
{
if(face.visible == true)
{
num++;
}
}
....
}

But num is always 0. Any idea why this does not work?

Leave a comment