PageRenderTime 26ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 12ms app.codeStats 0ms

/src/away3d/primitives/WireframeSphere.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 102 lines | 80 code | 8 blank | 14 comment | 10 complexity | 824db737e6caaab9db44061c9cacc730 MD5 | raw file
  1package away3d.primitives
  2{
  3	import flash.geom.Vector3D;
  4	
  5	/**
  6	 * A WireframeSphere primitive mesh
  7	 */
  8	public class WireframeSphere extends WireframePrimitiveBase
  9	{
 10		private var _segmentsW:uint;
 11		private var _segmentsH:uint;
 12		private var _radius:Number;
 13		
 14		/**
 15		 * Creates a new WireframeSphere object.
 16		 * @param radius The radius of the sphere.
 17		 * @param segmentsW Defines the number of horizontal segments that make up the sphere.
 18		 * @param segmentsH Defines the number of vertical segments that make up the sphere.
 19		 * @param color The colour of the wireframe lines
 20		 * @param thickness The thickness of the wireframe lines
 21		 */
 22		public function WireframeSphere(radius:Number = 50, segmentsW:uint = 16, segmentsH:uint = 12, color:uint = 0xFFFFFF, thickness:Number = 1)
 23		{
 24			super(color, thickness);
 25			
 26			_radius = radius;
 27			_segmentsW = segmentsW;
 28			_segmentsH = segmentsH;
 29		}
 30		
 31		/**
 32		 * @inheritDoc
 33		 */
 34		override protected function buildGeometry():void
 35		{
 36			var vertices:Vector.<Number> = new Vector.<Number>();
 37			var v0:Vector3D = new Vector3D();
 38			var v1:Vector3D = new Vector3D();
 39			var i:uint, j:uint;
 40			var numVerts:uint = 0;
 41			var index:int;
 42			
 43			for (j = 0; j <= _segmentsH; ++j) {
 44				var horangle:Number = Math.PI*j/_segmentsH;
 45				var z:Number = -_radius*Math.cos(horangle);
 46				var ringradius:Number = _radius*Math.sin(horangle);
 47				
 48				for (i = 0; i <= _segmentsW; ++i) {
 49					var verangle:Number = 2*Math.PI*i/_segmentsW;
 50					var x:Number = ringradius*Math.cos(verangle);
 51					var y:Number = ringradius*Math.sin(verangle);
 52					vertices[numVerts++] = x;
 53					vertices[numVerts++] = -z;
 54					vertices[numVerts++] = y;
 55				}
 56			}
 57			
 58			for (j = 1; j <= _segmentsH; ++j) {
 59				for (i = 1; i <= _segmentsW; ++i) {
 60					var a:int = ((_segmentsW + 1)*j + i)*3;
 61					var b:int = ((_segmentsW + 1)*j + i - 1)*3;
 62					var c:int = ((_segmentsW + 1)*(j - 1) + i - 1)*3;
 63					var d:int = ((_segmentsW + 1)*(j - 1) + i)*3;
 64					
 65					if (j == _segmentsH) {
 66						v0.x = vertices[c];
 67						v0.y = vertices[c + 1];
 68						v0.z = vertices[c + 2];
 69						v1.x = vertices[d];
 70						v1.y = vertices[d + 1];
 71						v1.z = vertices[d + 2];
 72						updateOrAddSegment(index++, v0, v1);
 73						v0.x = vertices[a];
 74						v0.y = vertices[a + 1];
 75						v0.z = vertices[a + 2];
 76						updateOrAddSegment(index++, v0, v1);
 77					} else if (j == 1) {
 78						v1.x = vertices[b];
 79						v1.y = vertices[b + 1];
 80						v1.z = vertices[b + 2];
 81						v0.x = vertices[c];
 82						v0.y = vertices[c + 1];
 83						v0.z = vertices[c + 2];
 84						updateOrAddSegment(index++, v0, v1);
 85					} else {
 86						v1.x = vertices[b];
 87						v1.y = vertices[b + 1];
 88						v1.z = vertices[b + 2];
 89						v0.x = vertices[c];
 90						v0.y = vertices[c + 1];
 91						v0.z = vertices[c + 2];
 92						updateOrAddSegment(index++, v0, v1);
 93						v1.x = vertices[d];
 94						v1.y = vertices[d + 1];
 95						v1.z = vertices[d + 2];
 96						updateOrAddSegment(index++, v0, v1);
 97					}
 98				}
 99			}
100		}
101	}
102}