Papervision3Dを試す

いまさらながらPapervision3Dを使ってみた。

  1. http://blog.papervision3d.org/よりライブラリを入手する。
  2. FlashDevelopにて新規プロジェクトを作成してsrcディレクトリ以下に先ほど入手したライブラリのorg以下を置く。
  3. Main.asを下記のように書き換えるとサンプルのような球体がくるくる回るアニメーションが生成される。

[SWF]http://hirotomium.com/memories/wp-content/uploads/2011/10/test.swf, 730, 300[/SWF]

参考:Papervision3Dのセットアップ&プラクティス – 脳の中の風船


package 
{
	import flash.display.Shader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.special.CompositeMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.primitives.*;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	[SWF(backgroundColor = 0x000000, width = 728, height = 480)]
	
	public class Main extends Sprite 
	{
		private var scene:Scene3D;
		private var viewport:Viewport3D;
		private var camera:Camera3D;
		private var renderer:BasicRenderEngine;	
		private var objSphere:Sphere;		
		private var objPlane:Plane;	
		private var objCube:Cube;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			init3D();
		}
		
		private function init3D():void {			
			//viewport
			viewport = new Viewport3D(0, 9, true);
			viewport.opaqueBackground = 0x000000;
			addChild(viewport);
			
			//render
			renderer = new BasicRenderEngine();
			
			//camera
			camera = new Camera3D();
			camera.z = -300;
			camera.focus = 500;
			camera.zoom = 1;
			
			//scene
			scene = new Scene3D();
			
			//material
			var colorMat:ColorMaterial = new ColorMaterial(0x006699, 1);
			var wireMat:WireframeMaterial = new WireframeMaterial(0x0099cc);
			var compoMat:CompositeMaterial = new CompositeMaterial();
			compoMat.addMaterial(colorMat);
			compoMat.addMaterial(wireMat);
			compoMat.doubleSided = true;
			
			objSphere = new Sphere(compoMat,100,16,12);
			scene.addChild(objSphere);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void {
			objSphere.rotationX += 1;
			objSphere.rotationY += 1;
			objSphere.rotationZ += 1;
			renderer.renderScene(scene, camera, viewport);
		}		
	}
}