package geometry.basics { import away3d.core.base.SubGeometry; import away3d.entities.Mesh; import away3d.primitives.Sphere; import flash.events.Event; import flash.events.MouseEvent; /** * @author Nicolas Barradeau * http://en.nicoptere.net */ public class E_Deformation extends BaseScene { private var subGeometry:SubGeometry; private var vertices:Vector.; private var origins:Vector.; private var mouseDown:Boolean; private var force:Number = 0; public function E_Deformation() { setup(); WHITE.lights = lights; addChild( GlobalTextField.instance ); _trace( 'press mouse to deform mesh\nthen release at some point :)' ); //create a Primitive mesh var mesh:Mesh = new Sphere( WHITE, 150, 64, 48 ); addMesh( mesh ); //by default a subgeometry is created and added to the geometry object of the Mesh //we access it like this subGeometry = mesh.geometry.subGeometries[ 0 ]; //we can then collect the vertices like this: vertices = subGeometry.vertexData.concat(); //and store a copy of the original vertices locations origins = vertices.concat(); //to inifinity and beyond! stage.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown ); stage.addEventListener( MouseEvent.MOUSE_UP, onMouseDown ); addEventListener( Event.ENTER_FRAME, update ); } private function onMouseDown(e:MouseEvent):void { mouseDown = e.type == MouseEvent.MOUSE_DOWN ? true : false; //cancels the force on MouseUp if ( !mouseDown ) force = 0; } private function update(e:Event):void { var i:int; //when mouse is clicked if ( mouseDown ) { //we move each vertex a bit in all directions ( XYZ ) by the amount of 'force' for ( i = 0; i < vertices.length; i++) { vertices[ i ] += ( Math.random() - .5 ) * force; } //and we slightly increase the force force += .1; } else //mouse not clicked { //we ease the vertices towards their original location for ( i = 0; i < vertices.length; i++) { vertices[ i ] += ( origins[ i ] - vertices[ i ] ) * .1; } } //in any case, we update the subGeometry to see the result. subGeometry.updateVertexData( vertices ); } } }