PV3D入门程序

package {
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	[SWF(width = '800',height = '800' ,backgroundColor = '#ffffff',frameRate ='40')]
	public class Main extends Sprite
	{
		/**
		 * 视窗
		 * */
		private var viewport:Viewport3D;
		/**
		 * 场景
		 * */
		private var scene:Scene3D;
		/**
		 * 照相机
		 * */
		private var camera:Camera3D;
		/**
		 * 有材质球面
		 * */
		private var sphere:Sphere;
		/**
		 * 渲染引擎
		 * */
		private var renderer:BasicRenderEngine;
		/**
		 * 立方体
		 * */
		private var cube:Cube;
		
		
		/**
		 * 构造函数
		 * */
		public function Main()
		{
			stage.frameRate = 40;
			this.initPapervision3D();
			this.createCube();
			this.createSphere();
		}
		
		/**
		 * 初始化3D模型
		 * */
		private function initPapervision3D():void
		{
			viewport = new Viewport3D(800,800,false,false,true,true);
			addChild(viewport);
			scene = new Scene3D();
			camera = new Camera3D();
			renderer = new BasicRenderEngine();
		}
		
		/**
		 * 创建球模型
		 * */
		private function createSphere():void
		{
			sphere = new Sphere();
			scene.addChild(sphere);
			this.addEventListener(Event.ENTER_FRAME,getSphereRender);
		}
		
		/**
		 * 调用球体渲染模型
		 * */
		private function getSphereRender(e:Event):void
		{
			sphere.localRotationX +=1;
			renderer.renderScene(scene,camera,viewport);
		}
		
		/**
		 * 创建一个立方体的模型
		 * */
		private function createCube():void
		{
			var allM:WireframeMaterial = new WireframeMaterial();
			var m:MaterialsList = new MaterialsList();
			m.addMaterial(allM,"all");
			
			var w:Number = 300;
			var d:Number = 500;
			var h:Number = 700;
			
			var sS:int = 2;
			var sT:int = 3;
			var sH:int = 4;
			
			cube = new Cube(m,w,d,h,sS,sT,sH);
			scene.addChild(cube);
			
			this.addEventListener(Event.ENTER_FRAME,render);
		}
		
		/**
		 * 调用一个监听事件
		 * */		
		private function render(e:Event):void
		{
			//偏航
			cube.yaw(5);
			//
			cube.pitch(1);
			//角度
			cube.roll(1);
			//视角向前
			cube.moveForward(0);
			//视角向后
			cube.moveBackward(1);
			//视角向左
			cube.moveLeft(0);
			//视角向右
			cube.moveRight(2);
			//视角向上
			cube.moveUp(0);
			//视角向下
			cube.moveDown(0);
			renderer.renderScene(scene,camera,viewport);
			
		}
		
	}
	
}


package {
	import flash.events.Event;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;

	public class PV3D extends BasicView
	{
		/**
		 * 球体
		 * */
		private var sphere:Sphere;
		
		public function PV3D()
		{
			
			stage.frameRate = 40;
			
			sphere = new Sphere;
			scene.addChild(sphere);
			startRendering();
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			sphere.localRotationX +=1;
			super.onRenderTick();
		}
	}
}
 

你可能感兴趣的:(Flash)