flash cs4 openGL 硬件加速研究

flashplayer10以后开始支持硬件加速,目前支持openGL2.0。下面是一个实例,先贴上代码。
/**
tb
*/
package
{
	import flash.display.Loader;
	import flash.display.BitmapData;
	import flash.display.Shader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	 
	public class TestShader extends Sprite
	{
		private var _shape:Shape=new Shape();
		private var _bitmapData:BitmapData;
 		private var _shader:Shader = new Shader();
		private var _sl:URLStream=new URLStream();
                
		public function TestShader()
		{
			this.stage.scaleMode=StageScaleMode.NO_SCALE;
			this.stage.align=StageAlign.TOP_LEFT;
			this.stage.showDefaultContextMenu=false;
			//===========================================
                        
			//加载图片
			var imgloader:Loader=new Loader();
			imgloader.contentLoaderInfo.addEventListener(Event.COMPLETE,imgComplete);
			imgloader.load(new URLRequest("image.jpg"));
		}
                
		/**
		* 加载图片完成
		* @param event
		* 
		*/                
		private function imgComplete(event:Event):void
		{
			 _bitmapData=event.target.content.bitmapData;
			_sl.addEventListener(Event.COMPLETE,shaderLoaded);
			_sl.load(new URLRequest("Crystallize.pbj"));
		}
                
		/**
		* 加载pbj文件完成
		* @param event
		* 
		*/                
		private function shaderLoaded(event:Event):void
		{
			var sls:URLStream= event.target as URLStream;
			if(sls==null){
				throw new Error("GLSL data is empty or load error");
			}
			var byArr:ByteArray=new ByteArray();
			sls.readBytes(byArr);
                        
			//开始着色渲染
			_shader.byteCode = byArr;
			_shader.data.src.input = _bitmapData;
            
            //添加显示
            _shape.x=20;
            _shape.y=20;
            this.addChild(_shape);
			
			//时间间隔
			var time:Timer=new Timer(100);
			time.addEventListener(TimerEvent.TIMER,onTime);
			time.start();
		}
                
		/**
		* 时间间隔,动态改变着色渲染 
		* @param event
		* 
		*/                
		private function onTime(event:TimerEvent):void
		{
			_shader.data.size.value = [Math.sin(event.target.currentCount/100)*50];
                 
			_shape.graphics.clear();
			_shape.graphics.beginShaderFill(_shader);
			_shape.graphics.drawRect(0,0,200,200);
		}
	}
}

你可能感兴趣的:(.net,Flash)