PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/materials/methods/FogMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 137 lines | 91 code | 16 blank | 30 comment | 0 complexity | 567b76ce68a26979c23202e3afa473b4 MD5 | raw file
  1package away3d.materials.methods
  2{
  3	import away3d.arcane;
  4	import away3d.core.managers.Stage3DProxy;
  5	import away3d.materials.compilation.ShaderRegisterCache;
  6	import away3d.materials.compilation.ShaderRegisterElement;
  7	
  8	use namespace arcane;
  9
 10	/**
 11	 * FogMethod provides a method to add distance-based fog to a material.
 12	 */
 13	public class FogMethod extends EffectMethodBase
 14	{
 15		private var _minDistance:Number = 0;
 16		private var _maxDistance:Number = 1000;
 17		private var _fogColor:uint;
 18		private var _fogR:Number;
 19		private var _fogG:Number;
 20		private var _fogB:Number;
 21
 22		/**
 23		 * Creates a new FogMethod object.
 24		 * @param minDistance The distance from which the fog starts appearing.
 25		 * @param maxDistance The distance at which the fog is densest.
 26		 * @param fogColor The colour of the fog.
 27		 */
 28		public function FogMethod(minDistance:Number, maxDistance:Number, fogColor:uint = 0x808080)
 29		{
 30			super();
 31			this.minDistance = minDistance;
 32			this.maxDistance = maxDistance;
 33			this.fogColor = fogColor;
 34		}
 35
 36		/**
 37		 * @inheritDoc
 38		 */
 39		override arcane function initVO(vo:MethodVO):void
 40		{
 41			vo.needsProjection = true;
 42		}
 43
 44		/**
 45		 * @inheritDoc
 46		 */
 47		override arcane function initConstants(vo:MethodVO):void
 48		{
 49			var data:Vector.<Number> = vo.fragmentData;
 50			var index:int = vo.fragmentConstantsIndex;
 51			data[index + 3] = 1;
 52			data[index + 6] = 0;
 53			data[index + 7] = 0;
 54		}
 55
 56		/**
 57		 * The distance from which the fog starts appearing.
 58		 */
 59		public function get minDistance():Number
 60		{
 61			return _minDistance;
 62		}
 63		
 64		public function set minDistance(value:Number):void
 65		{
 66			_minDistance = value;
 67		}
 68
 69		/**
 70		 * The distance at which the fog is densest.
 71		 */
 72		public function get maxDistance():Number
 73		{
 74			return _maxDistance;
 75		}
 76		
 77		public function set maxDistance(value:Number):void
 78		{
 79			_maxDistance = value;
 80		}
 81
 82		/**
 83		 * The colour of the fog.
 84		 */
 85		public function get fogColor():uint
 86		{
 87			return _fogColor;
 88		}
 89		
 90		public function set fogColor(value:uint):void
 91		{
 92			_fogColor = value;
 93			_fogR = ((value >> 16) & 0xff)/0xff;
 94			_fogG = ((value >> 8) & 0xff)/0xff;
 95			_fogB = (value & 0xff)/0xff;
 96		}
 97
 98		/**
 99		 * @inheritDoc
100		 */
101		arcane override function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
102		{
103			var data:Vector.<Number> = vo.fragmentData;
104			var index:int = vo.fragmentConstantsIndex;
105			data[index] = _fogR;
106			data[index + 1] = _fogG;
107			data[index + 2] = _fogB;
108			data[index + 4] = _minDistance;
109			data[index + 5] = 1/(_maxDistance - _minDistance);
110		}
111
112		/**
113		 * @inheritDoc
114		 */
115		arcane override function getFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
116		{
117			var fogColor:ShaderRegisterElement = regCache.getFreeFragmentConstant();
118			var fogData:ShaderRegisterElement = regCache.getFreeFragmentConstant();
119			var temp:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
120			regCache.addFragmentTempUsages(temp, 1);
121			var temp2:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
122			var code:String = "";
123			vo.fragmentConstantsIndex = fogColor.index*4;
124			
125			code += "sub " + temp2 + ".w, " + _sharedRegisters.projectionFragment + ".z, " + fogData + ".x          \n" +
126				"mul " + temp2 + ".w, " + temp2 + ".w, " + fogData + ".y					\n" +
127				"sat " + temp2 + ".w, " + temp2 + ".w										\n" +
128				"sub " + temp + ", " + fogColor + ", " + targetReg + "\n" + 			// (fogColor- col)
129				"mul " + temp + ", " + temp + ", " + temp2 + ".w					\n" +			// (fogColor- col)*fogRatio
130				"add " + targetReg + ", " + targetReg + ", " + temp + "\n"; // fogRatio*(fogColor- col) + col
131			
132			regCache.removeFragmentTempUsage(temp);
133			
134			return code;
135		}
136	}
137}