package flare.primitives { import flare.core.*; import flare.materials.*; import flash.geom.*; /** * @author Ariel Nehmad */ public class PieChart3D extends Mesh3D { public function PieChart3D( from:Number, to:Number, radius:Number = 20, height:Number = 5, material:Material3D = null ) { if ( !material ) material = new Shader3D( name + "_material" ); var segments:int = 24; var steps:int = 12; var indices:Vector. = new Vector.; var positions:Vector. = new Vector.; var uvs:Vector. = new Vector.; var normals:Vector. = new Vector.; var vertex:Vector. = new Vector.; var normal:Vector. = new Vector.; var matrix:Matrix3D = new Matrix3D; var i:int, j:int; from = from * 360 / 100; to = to * 360 / 100; vertex.push( 0, height, 0 ); vertex.push( radius, height, 0 ); vertex.push( radius, height, 0 ); vertex.push( radius, 0, 0 ); vertex.push( radius, 0, 0 ); vertex.push( 0, 0, 0 ); normal.push( 0, 1, 0 ); normal.push( 0, 1, 0 ); normal.push( 1, 0, 0 ); normal.push( 1, 0, 0 ); normal.push( 0, -1, 0 ); normal.push( 0, -1, 0 ); var length:int = vertex.length / 3; // project vertex positions and normals. var out:Vector. = new Vector.; matrix.identity(); matrix.appendRotation( from, Vector3D.Y_AXIS ); for ( i = 0; i < segments + 1; i++ ) { matrix.transformVectors( vertex, out ); positions = positions.concat( out ); matrix.transformVectors( normal, out ); normals = normals.concat( out ); matrix.appendRotation( (to-from) / segments, Vector3D.Y_AXIS ); } // uvs. for ( i = 0; i < segments + 1; i++ ) for ( j = 0; j < length; j++ ) uvs.push( i / segments, j / (length - 1) ); // build indices. for ( i = 0; i < segments; i++ ) { for ( j = 0; j < length - 1; j++ ) { var id0:uint = ((i + 1) * length) + j; var id1:uint = (i * length) + j; var id2:uint = ((i + 1) * length) + j + 1; var id3:uint = (i * length) + j + 1; indices.push( id1, id2, id0 ); indices.push( id2, id1, id3 ); } } var len:int = positions.length / 3; out = new Vector.; uvs = uvs.concat( Vector.( [ 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ] ) ); vertex = Vector.( [0, height, 0, radius, height, 0, radius, 0, 0, 0, 0, 0] ); normal = Vector.([ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1 ]); matrix.identity(); matrix.appendRotation( from, Vector3D.Y_AXIS ); matrix.transformVectors( vertex, out ); positions = positions.concat( out ); matrix.transformVectors( normal, out ); normals = normals.concat( out ); indices.push( len + 2, len + 1, len ); indices.push( len + 3, len + 2, len ); normal = Vector.([ 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1 ]); matrix.appendRotation( to - from, Vector3D.Y_AXIS ); matrix.transformVectors( vertex, out ); positions = positions.concat( out ); matrix.transformVectors( normal, out ); normals = normals.concat( out ); indices.push( len + 4, len + 5, len + 6 ); indices.push( len + 4, len + 6, len + 7 ); var surf:Surface3D = new Surface3D( "pieChart" ); surf.vertexVector = positions; surf.indexVector = indices; surf.addVertexData( Surface3D.POSITION ); surf.addVertexData( Surface3D.UV0, 2, uvs ); surf.addVertexData( Surface3D.NORMAL, 3, normals ); surf.material = material; surfaces.push( surf ); } } }