Pv3d基本入门-flex下运行

最近比较忙,好久没有写博客了,现在写一下关于pv3d的基本例子,希望对大家有用.

1.下载pv3d包
2.新建工程
3.撰写代码

时间关系先直接贴源码
4.关键类
package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.utils.Base64Decoder;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.cameras.SpringCamera3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	/**
	 * ...
	 * @author Asher LO
	 */
	public class MyPanel extends Sprite
	{
		private var viewPort:Viewport3D;
		private var scene:Scene3D;//场景
		private var camera:SpringCamera3D;//
		private var renderer:BasicRenderEngine;//渲染引擎
		private var plane:Plane;//面板
		private var material:ColorMaterial;//颜色材料
		public function MyPanel() 
		{
			init();
		}
		/**
		 * 初始化的方法
		 */
		private function init():void {
			initPapervision();
			initMaterial();//初始化颜色必须在初始化组件前面
			initPanel();//为组件添加颜色
			initListener();//添加事件
		}
		/**
		 * 初始化pv3d所有的组件
		 */
		private function initPapervision():void {
			//viewPort = new Viewport3D();
			viewPort = new Viewport3D(400,300,false,true);
			//添加ViewPort不然看不到东西
			addChild(viewPort);
			scene=new Scene3D();
			//camera=new Camera3D();
			camera=new SpringCamera3D();
			renderer=new BasicRenderEngine();
			addRenderScene();//渲染
		}
		private function initMaterial():void{
			material=new ColorMaterial(0xcc0000);
			material.interactive=true;
			material.doubleSided=true;//让背部也有颜色
		}
		
		/**
		 * 初始化面板
		 * */
		private function initPanel():void{
			plane=new Plane(material);//添加到面板中
			scene.addChild(plane);//添加到场景
			//addRenderScene();//重新渲染
		}
		private function initListener():void{
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onPlaneClick);
		}
		/**
		 * 加载事件
		 * */
		private function onEnterFrame(e:Event):void{
			plane.yaw(2);
			addRenderScene();
		}
		private function onPlaneClick(e:InteractiveScene3DEvent):void{
			//(e.target as Plane).pitch(20);
			plane.pitch(20);
		}
		private function addRenderScene():void{
			renderer.renderScene(scene,camera,viewPort);
		}
		
	}

}



5.添加组件 Canvas代码

package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.utils.Base64Decoder;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.cameras.SpringCamera3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	/**
	 * ...
	 * @author Asher LO
	 */
	public class MyPanel extends Sprite
	{
		private var viewPort:Viewport3D;
		private var scene:Scene3D;//场景
		private var camera:SpringCamera3D;//
		private var renderer:BasicRenderEngine;//渲染引擎
		private var plane:Plane;//面板
		private var material:ColorMaterial;//颜色材料
		public function MyPanel() 
		{
			init();
		}
		/**
		 * 初始化的方法
		 */
		private function init():void {
			initPapervision();
			initMaterial();//初始化颜色必须在初始化组件前面
			initPanel();//为组件添加颜色
			initListener();//添加事件
		}
		/**
		 * 初始化pv3d所有的组件
		 */
		private function initPapervision():void {
			//viewPort = new Viewport3D();
			viewPort = new Viewport3D(400,300,false,true);
			//添加ViewPort不然看不到东西
			addChild(viewPort);
			scene=new Scene3D();
			//camera=new Camera3D();
			camera=new SpringCamera3D();
			renderer=new BasicRenderEngine();
			addRenderScene();//渲染
		}
		private function initMaterial():void{
			material=new ColorMaterial(0xcc0000);
			material.interactive=true;
			material.doubleSided=true;//让背部也有颜色
		}
		
		/**
		 * 初始化面板
		 * */
		private function initPanel():void{
			plane=new Plane(material);//添加到面板中
			scene.addChild(plane);//添加到场景
			//addRenderScene();//重新渲染
		}
		private function initListener():void{
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onPlaneClick);
		}
		/**
		 * 加载事件
		 * */
		private function onEnterFrame(e:Event):void{
			plane.yaw(2);
			addRenderScene();
		}
		private function onPlaneClick(e:InteractiveScene3DEvent):void{
			//(e.target as Plane).pitch(20);
			plane.pitch(20);
		}
		private function addRenderScene():void{
			renderer.renderScene(scene,camera,viewPort);
		}
		
	}

}


6.添加到App中
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" xmlns:local="*">
	<local:MyPanelView>
		
	</local:MyPanelView>

</mx:Application>

你可能感兴趣的:(xml,Flex,Flash)