PageRenderTime 276ms CodeModel.GetById 141ms app.highlight 11ms RepoModel.GetById 120ms app.codeStats 0ms

/src/away3d/filters/tasks/Filter3DRadialBlurTask.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 183 lines | 137 code | 27 blank | 19 comment | 1 complexity | 1e8c8db0685508b2309df1fd7f7227d7 MD5 | raw file
  1package away3d.filters.tasks
  2{
  3	import away3d.arcane;
  4	import away3d.cameras.Camera3D;
  5	import away3d.core.managers.Stage3DProxy;
  6	
  7	import flash.display3D.Context3D;
  8	
  9	import flash.display3D.Context3DProgramType;
 10	
 11	import flash.display3D.textures.Texture;
 12	
 13	use namespace arcane;
 14	
 15	public class Filter3DRadialBlurTask extends Filter3DTaskBase
 16	{
 17		private static const LAYERS:int = 15;
 18		
 19		private var _data:Vector.<Number>;
 20		
 21		private var _intensity:Number = 1.0;
 22		private var _glowGamma:Number = 1.0;
 23		private var _blurStart:Number = 1.0;
 24		private var _blurWidth:Number = -0.3;
 25		private var _cx:Number = 0.5;
 26		private var _cy:Number = 0.5;
 27		
 28		public function Filter3DRadialBlurTask(intensity:Number = 1.0, glowGamma:Number = 1.0, blurStart:Number = 1.0, blurWidth:Number = -0.3, cx:Number = 0.5, cy:Number = 0.5)
 29		{
 30			super();
 31			_intensity = intensity;
 32			_glowGamma = glowGamma;
 33			_blurStart = blurStart;
 34			_blurWidth = blurWidth;
 35			_cx = cx;
 36			_cy = cy;
 37			_data = Vector.<Number>([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, LAYERS, LAYERS - 1]);
 38			resetUniforms();
 39		}
 40		
 41		private function resetUniforms():void
 42		{
 43			_data[0] = _intensity;
 44			_data[1] = _glowGamma;
 45			_data[2] = _blurStart;
 46			_data[3] = _blurWidth;
 47			_data[4] = _cx;
 48			_data[5] = _cy;
 49		}
 50		
 51		override protected function getVertexCode():String
 52		{
 53			return "mov op, va0\n" +
 54				"mov vt0, vc2.xxxy\n" +
 55				"sub vt0.xy, va1.xy, vc1.xy \n" +
 56				"mov v0, vt0";
 57		}
 58		
 59		override protected function getFragmentCode():String
 60		{
 61			var code:String;
 62			
 63			code =
 64				//half4 blurred = 0,0,0,0; = ft1
 65				"mov ft1, fc2.xxxx \n" +
 66				// float2 ctrPt = float2(CX,CY); -> ft2
 67				"mov ft2.xy, fc1.xy \n" +
 68				// ft3.x -> counter = 0;
 69				"mov ft3.x, fc2.x \n";
 70			
 71			// Y-Axis
 72			for (var i:int = 0; i <= LAYERS; i++) {
 73				// float scale = BlurStart + BlurWidth*(i/(float) (nsamples-1)); -> ft4
 74				// ft4.x = (i/(float) (nsamples-1))
 75				code += "div ft4.x, ft3.x, fc2.w\n";
 76				// ft4.x *= Blurwidth;
 77				code += "mul ft4.x, ft4.x, fc0.w \n";
 78				// ft4.x += BlurStart;
 79				code += "add ft4.x, ft4.x, fc0.z \n";
 80				// blurred += tex2D(tex, IN.UV.xy*scale + ctrPt );
 81				code += "mov ft5.xy ,v0.xy\n";
 82				code += "mul ft5.xy, ft5.xy, ft4.xx \n";
 83				code += "add ft5.xy, ft5.xy, fc1.xy \n";
 84				code += "tex ft5, ft5.xy, fs0<2d, clamp, linear>\n";
 85				code += "add ft1, ft1, ft5 \n";
 86				// inc counter by one
 87				code += "add ft3.x, ft3.x, fc2.y \n";
 88			}
 89			/*     blurred /= nsamples;
 90			 blurred.rgb = pow(blurred.rgb,GlowGamma);
 91			 blurred.rgb *= Intensity;
 92			 blurred.rgb = saturate(blurred.rgb);
 93			 */
 94			code += "div ft1, ft1, fc2.z\n";
 95			code += "pow ft1.xyz, ft1.xyz, fc0.y\n";
 96			code += "mul ft1.xyz, ft1.xyz, fc0.x\n";
 97			code += "sat ft1.xyz, ft1.xyz \n";
 98			// var origTex = tex2D(tex, IN.UV.xy + ctrPt );
 99			code += "add ft0.xy, v0.xy, fc1.xy \n";
100			code += "tex ft6, ft0.xy, fs0<2d,clamp, linear>\n";
101			// var newC = origTex.rgb + blurred.rgb;
102			code += "add ft1.xyz, ft1.xyz, ft6.xyz \n";
103			// return newC
104			code += "mov oc, ft1\n";
105			
106			//trace(code);
107			return code;
108		}
109		
110		public function get intensity():Number
111		{
112			return _intensity;
113		}
114		
115		public function set intensity(intensity:Number):void
116		{
117			_intensity = intensity;
118			resetUniforms();
119		}
120		
121		public function get glowGamma():Number
122		{
123			return _glowGamma;
124		}
125		
126		public function set glowGamma(glowGamma:Number):void
127		{
128			_glowGamma = glowGamma;
129			resetUniforms();
130		}
131		
132		public function get blurStart():Number
133		{
134			return _blurStart;
135		}
136		
137		public function set blurStart(blurStart:Number):void
138		{
139			_blurStart = blurStart;
140			resetUniforms();
141		}
142		
143		public function get blurWidth():Number
144		{
145			return _blurWidth;
146		}
147		
148		public function set blurWidth(blurWidth:Number):void
149		{
150			_blurWidth = blurWidth;
151			resetUniforms();
152		}
153		
154		public function get cx():Number
155		{
156			return _cx;
157		}
158		
159		public function set cx(cx:Number):void
160		{
161			_cx = cx;
162			resetUniforms();
163		}
164		
165		public function get cy():Number
166		{
167			return _cy;
168		}
169		
170		public function set cy(cy:Number):void
171		{
172			_cy = cy;
173			resetUniforms();
174		}
175		
176		override public function activate(stage3DProxy:Stage3DProxy, camera3D:Camera3D, depthTexture:Texture):void
177		{
178			var context:Context3D = stage3DProxy._context3D;
179			context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 0, _data, 3);
180			context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, _data, 3);
181		}
182	}
183}