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

  • oscar
    Hi, thanks for the info, I'm trying to replace two triangles with BitmapFileMaterial but looks like the material is divided among all teh faces of the sphere, I'm new on papervision so I'm not sure where to start, i was thinking to create the materials on a plane and etract them from those planes where they already could have teh correct division, I'm not sure, could yuo give me some tip to do this? Thanks.
  • this can use in the .DAE model?
  • Phil
    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?
  • Nate
    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.
  • John Lindquist
    @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++;
    }
  • Hawk
    That's really cool. What if I would like to color or add a material to a square, instead of a triangle?
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 ...