package { import away3d.containers.View3D; import away3d.events.MouseEvent3D; import away3d.lights.DirectionalLight3D; import away3d.materials.ShadingColorMaterial; import away3d.primitives.Sphere; import flash.display.Sprite; import flash.events.Event; import flash.geom.Vector3D; [SWF(width="500",height="200", backgroundColor="#ffffff")] public class ExShadingColorMaterial extends Sprite { private var view:View3D; private var sp:Sphere; private var changed:Boolean = true; public function ExShadingColorMaterial() { view = new View3D(); view.x = 250; view.y = 100; addChild(view); var light:DirectionalLight3D = new DirectionalLight3D(); light.direction = new Vector3D(500,-300,200); view.scene.addLight(light); // add spheres that increase in complexity for(var i:uint = 1; i<11; i++){ sp = new Sphere({radius:45,segmentsW:i*2,segmentsH:i*2}); sp.x = (i*50)-280; i%2 == 0 ? sp.y = -50 : sp.y = 50; sp.material = new ShadingColorMaterial(0xffffff); view.scene.addChild(sp); sp.addEventListener(MouseEvent3D.MOUSE_DOWN,setColor); } this.addEventListener(Event.ENTER_FRAME,update); } private function update(e:Event):void { // only update if something was changed if(changed){ view.render(); changed = false; } } private function setColor(e:MouseEvent3D):void { // Change material to a new random colored material if(e.currentTarget is Sphere){ var sp:Sphere = e.currentTarget as Sphere; sp.material = new ShadingColorMaterial(Math.random()*Math.pow(8,8)); changed = true; } } } }