PageRenderTime 92ms CodeModel.GetById 77ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/tools/LightsHelper.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 185 lines | 134 code | 30 blank | 21 comment | 35 complexity | f2b6d21834c98537a0808429dbc6a085 MD5 | raw file
  1package away3d.tools
  2{
  3	import away3d.containers.ObjectContainer3D;
  4	import away3d.core.base.SubMesh;
  5	import away3d.entities.Mesh;
  6	import away3d.lights.LightBase;
  7
  8	/**
  9	* Helper Class for the LightBase objects <code>LightsHelper</code>
 10	* A series of methods to ease work with LightBase objects 
 11	*/
 12	 
 13	public class LightsHelper {
 14		
 15		private static var _lightsArray:Array;
 16		private static var _light:LightBase;
 17		private static var _state:uint;
 18		
 19		/**
 20		* Applys a series of lights to all materials found into an objectcontainer and its children.
 21		* The lights eventually set previously are replaced by the new ones.
 22		* @param	 objectContainer3D 	ObjectContainer3D. The target ObjectContainer3D object to be inspected.
 23		* @param	 lights						Vector.&lt;LightBase&gt;. A series of lights to be set to all materials found during parsing of the target ObjectContainer3D.
 24		*/
 25		public static function addLightsToMaterials(objectContainer3D:ObjectContainer3D, lights:Vector.<LightBase> ):void
 26		{
 27			if(lights.length == 0)
 28				return;
 29				
 30			_lightsArray = [];
 31
 32			for(var i:uint = 0;i<lights.length;++i)
 33				_lightsArray.push(lights[i]);
 34			
 35			_state = 0;
 36			parseContainer(objectContainer3D);
 37			_lightsArray = null;
 38		}
 39		
 40		/**
 41		* Adds one light to all materials found into an objectcontainer and its children.
 42		* The lights eventually set previously on a material are kept unchanged. The new light is added to the lights array of the materials found during parsing.
 43		* @param	 objectContainer3D 	ObjectContainer3D. The target ObjectContainer3D object to be inspected.
 44		* @param	 light							LightBase. The light to add to all materials found during the parsing of the target ObjectContainer3D.
 45		*/
 46		public static function addLightToMaterials(objectContainer3D:ObjectContainer3D, light:LightBase):void
 47		{
 48			parse(objectContainer3D, light, 1);
 49		}
 50		
 51		/**
 52		* Removes a given light from all materials found into an objectcontainer and its children.
 53		* @param	 objectContainer3D 	ObjectContainer3D. The target ObjectContainer3D object to be inspected.
 54		* @param	 light							LightBase. The light to be removed from all materials found during the parsing of the target ObjectContainer3D.
 55		*/
 56		public static function removeLightFromMaterials(objectContainer3D:ObjectContainer3D, light:LightBase):void
 57		{
 58			parse(objectContainer3D, light, 2);
 59		}
 60		
 61		
 62		private static function parse(objectContainer3D:ObjectContainer3D, light:LightBase, id:uint):void
 63		{
 64			_light = light;
 65			if(!_light) return;
 66			_state = id;
 67			parseContainer(objectContainer3D);
 68		}
 69		
 70		private static function parseContainer(objectContainer3D:ObjectContainer3D):void
 71		{
 72			if(objectContainer3D is Mesh && objectContainer3D.numChildren == 0)
 73				parseMesh(Mesh(objectContainer3D));
 74				 
 75			for(var i:uint = 0;i<objectContainer3D.numChildren;++i)
 76				parseContainer(ObjectContainer3D(objectContainer3D.getChildAt(i) ) );
 77		}
 78		
 79		private static function parseMesh(mesh:Mesh):void
 80		{
 81			var hasLight:Boolean;
 82			var i :uint;
 83			var j :uint;
 84			var aLights:Array;
 85			
 86			if(mesh.material){
 87				switch(_state){
 88					case 0:
 89						mesh.material.lights = _lightsArray;
 90						break;
 91						
 92					case 1:
 93						aLights = mesh.material.lights;
 94						if(aLights && aLights.length> 0){
 95							for (i = 0; i<aLights.length; ++i){
 96								if(aLights[i] == _light){
 97									hasLight = true;
 98									break;
 99								}
100							}
101
102							if(!hasLight) {
103								aLights.push(_light);
104								mesh.material.lights = aLights;
105							} else {
106								hasLight = false;
107								break;
108							}
109							
110							
111						} else {
112							mesh.material.lights = [_light];
113						}
114						
115						break;
116						
117					case 2:
118						aLights = mesh.material.lights;
119						if(aLights){
120							for (i = 0; i<aLights.length; ++i){
121								if(aLights[i] == _light){
122									aLights.splice(i, 1);
123									mesh.material.lights = aLights;
124									break;
125								}
126							}
127						}
128				}
129			}
130				
131			var subMeshes:Vector.<SubMesh> = mesh.subMeshes;
132			var subMesh:SubMesh;
133			for (i = 0; i<subMeshes.length; ++i){
134				subMesh = subMeshes[i];
135				if(subMesh.material){
136					switch(_state){
137						case 0:
138							if(subMesh.material.lights !=_lightsArray )
139								subMesh.material.lights = _lightsArray;
140							break;
141							
142						case 1:
143							aLights = subMesh.material.lights;
144							
145							if(aLights && aLights.length> 0){
146								for (j = 0; j<aLights.length; ++j){
147									if(aLights[j] == _light){
148										hasLight = true;
149										break;
150									}
151								}
152								 
153								if(!hasLight){
154									aLights.push(_light);
155									subMesh.material.lights = aLights;
156								} else{
157									hasLight = false;
158									break;
159								}
160							} else {
161								subMesh.material.lights = [_light];
162							}
163							break;
164							
165						case 2:
166							aLights = subMesh.material.lights;
167							if(aLights){
168								for (j = 0; j<aLights.length; ++j){
169									if(aLights[j] == _light){
170										aLights.splice(j, 1);
171										subMesh.material.lights = aLights;
172										break;
173									}
174								}
175							}
176							
177					}
178					
179				}
180				
181			}
182		}
183		
184	}
185}