PageRenderTime 354ms CodeModel.GetById 191ms app.highlight 5ms RepoModel.GetById 156ms app.codeStats 0ms

/src/away3d/materials/methods/EnvMapAmbientMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 88 lines | 51 code | 14 blank | 23 comment | 0 complexity | 253201f57d0c91a4a49b9330e6b2c683 MD5 | raw file
 1package away3d.materials.methods
 2{
 3	import away3d.arcane;
 4	import away3d.core.managers.Stage3DProxy;
 5	import away3d.materials.methods.MethodVO;
 6	import away3d.materials.compilation.ShaderRegisterCache;
 7	import away3d.materials.compilation.ShaderRegisterElement;
 8	import away3d.textures.CubeTextureBase;
 9	
10	use namespace arcane;
11	
12	/**
13	 * EnvMapDiffuseMethod provides a diffuse shading method that uses a diffuse irradiance environment map to
14	 * approximate global lighting rather than lights.
15	 */
16	public class EnvMapAmbientMethod extends BasicAmbientMethod
17	{
18		private var _cubeTexture:CubeTextureBase;
19		
20		/**
21		 * Creates a new EnvMapDiffuseMethod object.
22		 * @param envMap The cube environment map to use for the diffuse lighting.
23		 */
24		public function EnvMapAmbientMethod(envMap:CubeTextureBase)
25		{
26			super();
27			_cubeTexture = envMap;
28		}
29
30		/**
31		 * @inheritDoc
32		 */
33		override arcane function initVO(vo:MethodVO):void
34		{
35			super.initVO(vo);
36			vo.needsNormals = true;
37		}
38		
39		/**
40		 * @inheritDoc
41		 */
42		override public function dispose():void
43		{
44		}
45		
46		/**
47		 * The cube environment map to use for the diffuse lighting.
48		 */
49		public function get envMap():CubeTextureBase
50		{
51			return _cubeTexture;
52		}
53		
54		public function set envMap(value:CubeTextureBase):void
55		{
56			_cubeTexture = value;
57		}
58		
59		/**
60		 * @inheritDoc
61		 */
62		arcane override function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
63		{
64			super.activate(vo, stage3DProxy);
65			
66			stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _cubeTexture.getTextureForStage3D(stage3DProxy));
67		}
68		
69		/**
70		 * @inheritDoc
71		 */
72		arcane override function getFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
73		{
74			var code:String = "";
75			var cubeMapReg:ShaderRegisterElement = regCache.getFreeTextureReg();
76			vo.texturesIndex = cubeMapReg.index;
77			
78			code += getTexCubeSampleCode(vo, targetReg, cubeMapReg, _cubeTexture, _sharedRegisters.normalFragment);
79			
80			_ambientInputRegister = regCache.getFreeFragmentConstant();
81			vo.fragmentConstantsIndex = _ambientInputRegister.index;
82			
83			code += "add " + targetReg + ".xyz, " + targetReg + ".xyz, " + _ambientInputRegister + ".xyz\n";
84			
85			return code;
86		}
87	}
88}