Per Face Material
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); } } }
4 Comments to Per Face Material
That’s really cool. What if I would like to color or add a material to a square, instead of a triangle?
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++; }
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.
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
Search
Recent Posts
- MorphController - Mighty Morphing Papervision3D
- End dump
- Test if a plane is within the view of the camera (aka testing if culled)
- Materials Reference
- Perlin Blob
- Dynamic Text on a Plane
- Maya Texture Baking
- Creating a Custom VectorVision Font
- Workshop video and example dump
- 3D Math Book Recommendation
- Heading to New York. brb ;)
- Launching Flex4.org
- Flex 4 Layouts and Groups
- Flex 4 States
- How to click on stuff in Papervision3D - Viewport, ViewportLayers, InteractiveScene3DEvent, Mouse3D, and MovieMaterial Buttons
Recent Comments
- alexxcz on MorphController - Mighty Morphing Papervision3D
- andre venancio on 3D Math Book Recommendation
- DS on Dynamic Text on a Plane
- John Lindquist on Maya Texture Baking
- Itai on Maya Texture Baking
- Javier on End dump
- samBrown on End dump
- tf on archive
- Pan on requests
- Martin Lindelöf on requests


December 14, 2008