PageRenderTime 494ms CodeModel.GetById 212ms app.highlight 7ms RepoModel.GetById 152ms app.codeStats 0ms

/src/away3d/materials/methods/EnvMapDiffuseMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 131 lines | 76 code | 20 blank | 35 comment | 2 complexity | f1234a6d2eb976a5704371c1828e848e MD5 | raw file
  1package away3d.materials.methods
  2{
  3	import away3d.arcane;
  4	import away3d.core.managers.CubeTexture3DProxy;
  5	import away3d.core.managers.Stage3DProxy;
  6	import away3d.materials.utils.CubeMap;
  7	import away3d.materials.utils.ShaderRegisterCache;
  8	import away3d.materials.utils.ShaderRegisterElement;
  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 EnvMapDiffuseMethod extends BasicDiffuseMethod
 17	{
 18		private var _cubeTexture : CubeTexture3DProxy;
 19		private var _cubeMapIndex : int;
 20
 21		/**
 22		 * Creates a new EnvMapDiffuseMethod object.
 23		 * @param envMap The cube environment map to use for the diffuse lighting.
 24		 */
 25		public function EnvMapDiffuseMethod(envMap : CubeMap)
 26		{
 27			_cubeTexture = new CubeTexture3DProxy();
 28			_cubeTexture.cubeMap = envMap;
 29		}
 30
 31		arcane override function reset() : void
 32		{
 33			super.reset();
 34			_cubeMapIndex = -1;
 35		}
 36
 37		/**
 38		 * @inheritDoc
 39		 */
 40		override public function dispose(deep : Boolean) : void
 41		{
 42			_cubeTexture.dispose(deep);
 43		}
 44
 45		/**
 46		 * The cube environment map to use for the diffuse lighting.
 47		 */
 48		public function get envMap() : CubeMap
 49		{
 50			return _cubeTexture.cubeMap;
 51		}
 52
 53		public function set envMap(value : CubeMap) : void
 54		{
 55			_cubeTexture.cubeMap = value;
 56		}
 57
 58		/**
 59		 * @inheritDoc
 60		 */
 61		arcane override function set numLights(value : int) : void
 62		{
 63			super.numLights = value;
 64			_needsNormals = true;
 65		}
 66
 67		/**
 68		 * @inheritDoc
 69		 */
 70		arcane override function activate(stage3DProxy : Stage3DProxy) : void
 71		{
 72			super.activate(stage3DProxy);
 73
 74			stage3DProxy.setTextureAt(_cubeMapIndex, _cubeTexture.getTextureForContext(stage3DProxy));
 75		}
 76
 77//		arcane override function deactivate(stage3DProxy : Stage3DProxy) : void
 78//		{
 79//			super.deactivate(stage3DProxy);
 80//
 81//			stage3DProxy.setTextureAt(_cubeMapIndex, null);
 82//		}
 83
 84		/**
 85		 * @inheritDoc
 86		 */
 87		arcane override function getFragmentAGALPreLightingCode(regCache : ShaderRegisterCache) : String
 88		{
 89			return "";
 90		}
 91
 92		/**
 93		 * @inheritDoc
 94		 */
 95		arcane override function getFragmentCodePerLight(lightIndex : int, lightDirReg : ShaderRegisterElement, lightColReg : ShaderRegisterElement, regCache : ShaderRegisterCache) : String
 96		{
 97			return "";
 98		}
 99
100		/**
101		 * @inheritDoc
102		 */
103		arcane override function getFragmentPostLightingCode(regCache : ShaderRegisterCache, targetReg : ShaderRegisterElement) : String
104		{
105			var code : String = "";
106			var cubeMapReg : ShaderRegisterElement = regCache.getFreeTextureReg();
107			var temp : ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
108
109			code += "tex " + temp + ", " + _normalFragmentReg + ", " + cubeMapReg + " <cube,miplinear,linear,clamp>\n" +
110					"add " + temp+".xyz, " + temp+".xyz, " + targetReg+".xyz\n" +
111					"sat " + temp+".xyz, " + temp+".xyz\n";
112
113			_cubeMapIndex = cubeMapReg.index;
114
115            if (_useTexture) {
116				_diffuseInputRegister = regCache.getFreeTextureReg();
117				code += getTexSampleCode(targetReg, _diffuseInputRegister);
118			}
119			else {
120				_diffuseInputRegister = regCache.getFreeFragmentConstant();
121				code += "mov " + targetReg + ", " + _diffuseInputRegister + "\n";
122			}
123
124			_diffuseInputIndex = _diffuseInputRegister.index;
125
126			code += "mul " + targetReg + ", " + targetReg + ", " + temp + " \n";
127
128			return code;
129		}
130	}
131}