flex4.6开发移动stage3d应用(一):环境搭建

两年没有写过博文了,这几日突然有写博客的冲动。

可惜自己的网站域名都失效了,在个人网站恢复开通之前,先在csdn安家,写些技术博客吧。


随着air3.2的发布,已经可以在手机端(android、ios)上利用air开发stage3d相关的应用了。

这里写一下开发环境搭建方法:

1、安装flex 4.6,这个不用说了

2、由于4.6自动的air sdk还是3.1,需要到adobe官网下air的最新sdk(3.2),只要到adobe 官网搜索air sdk即可。

下载后覆盖到flex4.6的sdk目录下,如C:\Program Files\Adobe\Adobe Flash Builder 4.6\sdks\4.6.0

3、新建mobile工程,在app xml里配置directtrue,在compile参数里加上-swf-version 15

这就搞定了,发布测试的时候可以选择真机或者测试机。这里发布一个测试代码,在我的小米和iphone 4s上测试通过。

[SWF(frameRate="60", backgroundColor="#FFFFFF")]
	public class testMobile extends Sprite {
		
		protected var context3D:Context3D;
		protected var program:Program3D;
		protected var vertexbuffer:VertexBuffer3D;
		protected var indexbuffer:IndexBuffer3D;
		
		public function testMobile() {
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			//
			var stage3d:Stage3D = stage.stage3Ds[0];
			
			stage3d.addEventListener(Event.CONTEXT3D_CREATE, initStage3d);
			stage3d.requestContext3D();
			
			addEventListener(Event.ENTER_FRAME, onRender);
		}
		
		private function initStage3d(evt:Event):void {
			context3D = stage.stage3Ds[0].context3D;			
			context3D.configureBackBuffer(800, 600, 1, true);
			
			var vertices:Vector. = Vector.([
				-0.3,-0.3,0, 1, 0, 0, // x, y, z, r, g, b
				-0.3, 0.3, 0, 0, 1, 0,
				0.3, 0.3, 0, 0, 0, 1]);
			
			// Create VertexBuffer3D. 3 vertices, of 6 Numbers each
			vertexbuffer = context3D.createVertexBuffer(3, 6);
			// Upload VertexBuffer3D to GPU. Offset 0, 3 vertices
			vertexbuffer.uploadFromVector(vertices, 0, 3);				
			
			var indices:Vector. = Vector.([0, 1, 2]);
			
			// Create IndexBuffer3D. Total of 3 indices. 1 triangle of 3 vertices
			indexbuffer = context3D.createIndexBuffer(3);			
			// Upload IndexBuffer3D to GPU. Offset 0, count 3
			indexbuffer.uploadFromVector (indices, 0, 3);			
			
			var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
			vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
				"m44 op, va0, vc0\n" + // pos to clipspace
				"mov v0, va1" // copy color
			);			
			
			var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
			fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,
				
				"mov oc, v0"
			);
			
			program = context3D.createProgram();
			program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
		}	
		
		protected function onRender(e:Event):void
		{
			if ( !context3D ) 
				return;
			
			context3D.clear ( 1, 1, 1, 1 );
			
			// vertex position to attribute register 0
			context3D.setVertexBufferAt (0, vertexbuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			// color to attribute register 1
			context3D.setVertexBufferAt(1, vertexbuffer, 3, Context3DVertexBufferFormat.FLOAT_3);
			// assign shader program
			context3D.setProgram(program);
			
			var m:Matrix3D = new Matrix3D();
			m.appendRotation(getTimer()*stage.frameRate/40, Vector3D.Z_AXIS);
			context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
			
			context3D.drawTriangles(indexbuffer);
			
			context3D.present();			
		}
	}

示意图,上面小米,下面iphone 4s



参考资料以及延伸阅读:

1、(译)Flash Player 11.2 和 AIR 3.2 RC版(功能说明,安装流程以及Stage3D移动开发指导)

2、Using Flash Builder 4.5 to package applications for Apple iOS devices

3、Hello Triangle

你可能感兴趣的:(flex4.6开发移动stage3d应用(一):环境搭建)