PageRenderTime 183ms CodeModel.GetById 80ms app.highlight 9ms RepoModel.GetById 92ms app.codeStats 0ms

/src/away3d/materials/methods/ColorMatrixMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 103 lines | 66 code | 15 blank | 22 comment | 2 complexity | a82ff0ed89b3bc492d2b959a3ccc4b5d 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	 * ColorMatrixMethod provides a shading method that changes the colour of a material analogous to a ColorMatrixFilter.
 12	 */
 13	public class ColorMatrixMethod extends EffectMethodBase
 14	{
 15		private var _matrix:Array;
 16		
 17		/**
 18		 * Creates a new ColorTransformMethod.
 19		 *
 20		 * @param matrix An array of 20 items for 4 x 5 color transform.
 21		 */
 22		public function ColorMatrixMethod(matrix:Array)
 23		{
 24			super();
 25			if (matrix.length != 20)
 26				throw new Error("Matrix length must be 20!");
 27			
 28			_matrix = matrix;
 29		}
 30		
 31		/**
 32		 * The 4 x 5 matrix to transform the color of the material.
 33		 */
 34		public function get colorMatrix():Array
 35		{
 36			return _matrix;
 37		}
 38		
 39		public function set colorMatrix(value:Array):void
 40		{
 41			_matrix = value;
 42		}
 43		
 44		/**
 45		 * @inheritDoc
 46		 */
 47		override arcane function getFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
 48		{
 49			var code:String = "";
 50			var colorMultReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 51			regCache.getFreeFragmentConstant();
 52			regCache.getFreeFragmentConstant();
 53			regCache.getFreeFragmentConstant();
 54			var colorOffsetReg:ShaderRegisterElement = regCache.getFreeFragmentConstant();
 55			
 56			vo.fragmentConstantsIndex = colorMultReg.index*4;
 57			
 58			code += "m44 " + targetReg + ", " + targetReg + ", " + colorMultReg + "\n" +
 59				"add " + targetReg + ", " + targetReg + ", " + colorOffsetReg + "\n";
 60			
 61			return code;
 62		}
 63		
 64		/**
 65		 * @inheritDoc
 66		 */
 67		override arcane function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
 68		{
 69			var matrix:Array = _matrix;
 70			var index:int = vo.fragmentConstantsIndex;
 71			var data:Vector.<Number> = vo.fragmentData;
 72			// r
 73			data[index] = matrix[0];
 74			data[index + 1] = matrix[1];
 75			data[index + 2] = matrix[2];
 76			data[index + 3] = matrix[3];
 77			
 78			// g
 79			data[index + 4] = matrix[5];
 80			data[index + 5] = matrix[6];
 81			data[index + 6] = matrix[7];
 82			data[index + 7] = matrix[8];
 83			
 84			// b
 85			data[index + 8] = matrix[10];
 86			data[index + 9] = matrix[11];
 87			data[index + 10] = matrix[12];
 88			data[index + 11] = matrix[13];
 89			
 90			// a
 91			data[index + 12] = matrix[15];
 92			data[index + 13] = matrix[16];
 93			data[index + 14] = matrix[17];
 94			data[index + 15] = matrix[18];
 95			
 96			// rgba offset
 97			data[index + 16] = matrix[4];
 98			data[index + 17] = matrix[9];
 99			data[index + 18] = matrix[14];
100			data[index + 19] = matrix[19];
101		}
102	}
103}