PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/core/render/BackgroundImageRenderer.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 151 lines | 129 code | 22 blank | 0 comment | 9 complexity | a8871a6e5e210b7f8e4b8fe43a4b1910 MD5 | raw file
  1package away3d.core.render
  2{
  3	import away3d.core.managers.Stage3DProxy;
  4	import away3d.debug.Debug;
  5	import away3d.textures.Texture2DBase;
  6	
  7	import com.adobe.utils.AGALMiniAssembler;
  8	
  9	import flash.display3D.Context3D;
 10	import flash.display3D.Context3DBlendFactor;
 11	import flash.display3D.Context3DProgramType;
 12	import flash.display3D.Context3DTextureFormat;
 13	import flash.display3D.Context3DVertexBufferFormat;
 14	import flash.display3D.IndexBuffer3D;
 15	import flash.display3D.Program3D;
 16	import flash.display3D.VertexBuffer3D;
 17	
 18	public class BackgroundImageRenderer
 19	{
 20		private var _program3d:Program3D;
 21		private var _texture:Texture2DBase;
 22		private var _indexBuffer:IndexBuffer3D;
 23		private var _vertexBuffer:VertexBuffer3D;
 24		private var _stage3DProxy:Stage3DProxy;
 25		private var _context:Context3D;
 26		
 27		public function BackgroundImageRenderer(stage3DProxy:Stage3DProxy)
 28		{
 29			this.stage3DProxy = stage3DProxy;
 30		}
 31		
 32		public function get stage3DProxy():Stage3DProxy
 33		{
 34			return _stage3DProxy;
 35		}
 36		
 37		public function set stage3DProxy(value:Stage3DProxy):void
 38		{
 39			if (value == _stage3DProxy)
 40				return;
 41			_stage3DProxy = value;
 42			
 43			removeBuffers();
 44		}
 45		
 46		private function removeBuffers():void
 47		{
 48			if (_vertexBuffer) {
 49				_vertexBuffer.dispose();
 50				_vertexBuffer = null;
 51				_program3d.dispose();
 52				_program3d = null;
 53				_indexBuffer.dispose();
 54				_indexBuffer = null;
 55			}
 56		}
 57		
 58		private function getVertexCode():String
 59		{
 60			return "mov op, va0\n" +
 61				"mov v0, va1";
 62		}
 63		
 64		private function getFragmentCode():String
 65		{
 66			var format:String;
 67			switch (_texture.format) {
 68				case Context3DTextureFormat.COMPRESSED:
 69					format = "dxt1,";
 70					break;
 71				case "compressedAlpha":
 72					format = "dxt5,";
 73					break;
 74				default:
 75					format = "";
 76			}
 77			return "tex ft0, v0, fs0 <2d, " + format + "linear>	\n" +
 78				"mov oc, ft0";
 79		}
 80		
 81		public function dispose():void
 82		{
 83			removeBuffers();
 84		}
 85		
 86		public function render():void
 87		{
 88			var context:Context3D = _stage3DProxy.context3D;
 89			
 90			if (context != _context) {
 91				removeBuffers();
 92				_context = context;
 93			}
 94			
 95			if (!context)
 96				return;
 97			
 98			if (!_vertexBuffer)
 99				initBuffers(context);
100			
101			context.setBlendFactors(Context3DBlendFactor.ONE, Context3DBlendFactor.ZERO);
102			context.setProgram(_program3d);
103			context.setTextureAt(0, _texture.getTextureForStage3D(_stage3DProxy));
104			context.setVertexBufferAt(0, _vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
105			context.setVertexBufferAt(1, _vertexBuffer, 2, Context3DVertexBufferFormat.FLOAT_2);
106			context.drawTriangles(_indexBuffer, 0, 2);
107			context.setVertexBufferAt(0, null);
108			context.setVertexBufferAt(1, null);
109			context.setTextureAt(0, null);
110		}
111		
112		private function initBuffers(context:Context3D):void
113		{
114			_vertexBuffer = context.createVertexBuffer(4, 4);
115			_program3d = context.createProgram();
116			_indexBuffer = context.createIndexBuffer(6);
117			_indexBuffer.uploadFromVector(Vector.<uint>([2, 1, 0, 3, 2, 0]), 0, 6);
118			_program3d.upload(new AGALMiniAssembler(Debug.active).assemble(Context3DProgramType.VERTEX, getVertexCode()),
119				new AGALMiniAssembler(Debug.active).assemble(Context3DProgramType.FRAGMENT, getFragmentCode())
120				);
121			
122			var w:Number = 2;
123			var h:Number = 2;
124			var x:Number = -1;
125			var y:Number = 1;
126			
127			if (_stage3DProxy.scissorRect) {
128				x = (_stage3DProxy.scissorRect.x * 2 - _stage3DProxy.viewPort.width) / _stage3DProxy.viewPort.width;
129				y = (_stage3DProxy.scissorRect.y * 2 - _stage3DProxy.viewPort.height) / _stage3DProxy.viewPort.height * -1;
130				w = 2 / (_stage3DProxy.viewPort.width / _stage3DProxy.scissorRect.width);
131				h = 2 / (_stage3DProxy.viewPort.height / _stage3DProxy.scissorRect.height);
132			}
133			
134			_vertexBuffer.uploadFromVector(Vector.<Number>([x, y-h, 0, 1,
135			x+w, y-h, 1, 1,
136			x+w, y, 1, 0,
137			x, y, 0, 0
138			]), 0, 4);
139		}
140		
141		public function get texture():Texture2DBase
142		{
143			return _texture;
144		}
145		
146		public function set texture(value:Texture2DBase):void
147		{
148			_texture = value;
149		}
150	}
151}