PageRenderTime 73ms CodeModel.GetById 40ms app.highlight 12ms RepoModel.GetById 18ms app.codeStats 0ms

/src/away3d/primitives/SkyBox.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 253 lines | 169 code | 37 blank | 47 comment | 0 complexity | 51f54f095e97e63434985779201b5d1c MD5 | raw file
  1package away3d.primitives
  2{
  3	
  4	import away3d.animators.IAnimator;
  5	import away3d.arcane;
  6	import away3d.bounds.BoundingVolumeBase;
  7	import away3d.bounds.NullBounds;
  8	import away3d.cameras.Camera3D;
  9	import away3d.core.base.IRenderable;
 10	import away3d.core.base.SubGeometry;
 11	import away3d.core.managers.Stage3DProxy;
 12	import away3d.core.partition.EntityNode;
 13	import away3d.core.partition.SkyBoxNode;
 14	import away3d.entities.Entity;
 15	import away3d.errors.AbstractMethodError;
 16	import away3d.library.assets.AssetType;
 17	import away3d.materials.MaterialBase;
 18	import away3d.materials.SkyBoxMaterial;
 19	import away3d.textures.CubeTextureBase;
 20	
 21	import flash.display3D.IndexBuffer3D;
 22	import flash.geom.Matrix;
 23	import flash.geom.Matrix3D;
 24	
 25	use namespace arcane;
 26	
 27	/**
 28	 * A SkyBox class is used to render a sky in the scene. It's always considered static and 'at infinity', and as
 29	 * such it's always centered at the camera's position and sized to exactly fit within the camera's frustum, ensuring
 30	 * the sky box is always as large as possible without being clipped.
 31	 */
 32	public class SkyBox extends Entity implements IRenderable
 33	{
 34		// todo: remove SubGeometry, use a simple single buffer with offsets
 35		private var _geometry:SubGeometry;
 36		private var _material:SkyBoxMaterial;
 37		private var _uvTransform:Matrix = new Matrix();
 38		private var _animator:IAnimator;
 39		
 40		public function get animator():IAnimator
 41		{
 42			return _animator;
 43		}
 44		
 45		override protected function getDefaultBoundingVolume():BoundingVolumeBase
 46		{
 47			return new NullBounds();
 48		}
 49		
 50		/**
 51		 * Create a new SkyBox object.
 52		 * @param cubeMap The CubeMap to use for the sky box's texture.
 53		 */
 54		public function SkyBox(cubeMap:CubeTextureBase)
 55		{
 56			super();
 57			_material = new SkyBoxMaterial(cubeMap);
 58			_material.addOwner(this);
 59			_geometry = new SubGeometry();
 60			buildGeometry(_geometry);
 61		}
 62		
 63		/**
 64		 * @inheritDoc
 65		 */
 66		public function activateVertexBuffer(index:int, stage3DProxy:Stage3DProxy):void
 67		{
 68			_geometry.activateVertexBuffer(index, stage3DProxy);
 69		}
 70		
 71		/**
 72		 * @inheritDoc
 73		 */
 74		public function activateUVBuffer(index:int, stage3DProxy:Stage3DProxy):void
 75		{
 76		}
 77		
 78		/**
 79		 * @inheritDoc
 80		 */
 81		public function activateVertexNormalBuffer(index:int, stage3DProxy:Stage3DProxy):void
 82		{
 83		}
 84		
 85		/**
 86		 * @inheritDoc
 87		 */
 88		public function activateVertexTangentBuffer(index:int, stage3DProxy:Stage3DProxy):void
 89		{
 90		}
 91		
 92		public function activateSecondaryUVBuffer(index:int, stage3DProxy:Stage3DProxy):void
 93		{
 94		}
 95		
 96		/**
 97		 * @inheritDoc
 98		 */
 99		public function getIndexBuffer(stage3DProxy:Stage3DProxy):IndexBuffer3D
100		{
101			return _geometry.getIndexBuffer(stage3DProxy);
102		}
103		
104		/**
105		 * The amount of triangles that comprise the SkyBox geometry.
106		 */
107		public function get numTriangles():uint
108		{
109			return _geometry.numTriangles;
110		}
111		
112		/**
113		 * The entity that that initially provided the IRenderable to the render pipeline.
114		 */
115		public function get sourceEntity():Entity
116		{
117			return null;
118		}
119		
120		/**
121		 * The material with which to render the object.
122		 */
123		public function get material():MaterialBase
124		{
125			return _material;
126		}
127		
128		public function set material(value:MaterialBase):void
129		{
130			throw new AbstractMethodError("Unsupported method!");
131		}
132		
133		public override function get assetType():String
134		{
135			return AssetType.SKYBOX;
136		}
137		
138		/**
139		 * @inheritDoc
140		 */
141		override protected function invalidateBounds():void
142		{
143			// dead end
144		}
145		
146		/**
147		 * @inheritDoc
148		 */
149		override protected function createEntityPartitionNode():EntityNode
150		{
151			return new SkyBoxNode(this);
152		}
153		
154		/**
155		 * @inheritDoc
156		 */
157		override protected function updateBounds():void
158		{
159			_boundsInvalid = false;
160		}
161		
162		/**
163		 * Builds the geometry that forms the SkyBox
164		 */
165		private function buildGeometry(target:SubGeometry):void
166		{
167			var vertices:Vector.<Number> = new <Number>[
168				-1, 1, -1, 1, 1, -1,
169				1, 1, 1, -1, 1, 1,
170				-1, -1, -1, 1, -1, -1,
171				1, -1, 1, -1, -1, 1
172				];
173			vertices.fixed = true;
174			
175			var indices:Vector.<uint> = new <uint>[
176				0, 1, 2, 2, 3, 0,
177				6, 5, 4, 4, 7, 6,
178				2, 6, 7, 7, 3, 2,
179				4, 5, 1, 1, 0, 4,
180				4, 0, 3, 3, 7, 4,
181				2, 1, 5, 5, 6, 2
182				];
183			
184			target.updateVertexData(vertices);
185			target.updateIndexData(indices);
186		}
187		
188		public function get castsShadows():Boolean
189		{
190			return false;
191		}
192		
193		public function get uvTransform():Matrix
194		{
195			return _uvTransform;
196		}
197		
198		public function get vertexData():Vector.<Number>
199		{
200			return _geometry.vertexData;
201		}
202		
203		public function get indexData():Vector.<uint>
204		{
205			return _geometry.indexData;
206		}
207		
208		public function get UVData():Vector.<Number>
209		{
210			return _geometry.UVData;
211		}
212		
213		public function get numVertices():uint
214		{
215			return _geometry.numVertices;
216		}
217		
218		public function get vertexStride():uint
219		{
220			return _geometry.vertexStride;
221		}
222		
223		public function get vertexNormalData():Vector.<Number>
224		{
225			return _geometry.vertexNormalData;
226		}
227		
228		public function get vertexTangentData():Vector.<Number>
229		{
230			return _geometry.vertexTangentData;
231		}
232		
233		public function get vertexOffset():int
234		{
235			return _geometry.vertexOffset;
236		}
237		
238		public function get vertexNormalOffset():int
239		{
240			return _geometry.vertexNormalOffset;
241		}
242		
243		public function get vertexTangentOffset():int
244		{
245			return _geometry.vertexTangentOffset;
246		}
247		
248		public function getRenderSceneTransform(camera:Camera3D):Matrix3D
249		{
250			return _sceneTransform;
251		}
252	}
253}