forked from: PV3Dその5 Planeを球体っぽく並べるのをPV3Dで。

by 1031no forked from PV3Dその5 Planeを球体っぽく並べるのをPV3Dで。 (diff: 2)
前作ったのをPV3Dで。
画面をクリックするとPlaneが球体に配置されます。
♥0 | Line 105 | Modified 2010-05-19 10:32:56 | MIT License
play

ActionScript3 source code

/**
 * Copyright 1031no ( http://wonderfl.net/user/1031no )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wIJW
 */

// forked from sake's PV3Dその5 Planeを球体っぽく並べるのをPV3Dで。
/*
    前作ったのをPV3Dで。
    画面をクリックするとPlaneが球体に配置されます。
*/

package
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	[SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="40")]
	public class PV3D_sample06 extends Sprite
	{
		private var container:Sprite;
		private var viewport:Viewport3D;
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var renderer:BasicRenderEngine;
		private var rootNode:DisplayObject3D;
		private var sphereNode:DisplayObject3D;
		private var flug:int;
		private var planeN:int;
		private const radius:int = 50;
		private const planeH:int = 5;
		private const planeW:int = 5;

		public function PV3D_sample06()
		{
			container=new Sprite();
			addChild(container);
			container.x=stage.stageWidth / 2;
			container.y=stage.stageHeight / 2;

			viewport=new Viewport3D(0, 0, true, true);
			scene=new Scene3D();
			camera=new Camera3D();
			renderer=new BasicRenderEngine();
			rootNode=scene.addChild(new DisplayObject3D("rootNode"));
			sphereNode=rootNode.addChild(new DisplayObject3D("sphereNode"));

			addChild(viewport);
			camera.y=50;
			camera.zoom=200;
			camera.focus=50;
			camera.target=DisplayObject3D.ZERO;
			viewport.containerSprite.buttonMode = true;

			var H:int=(2 * radius * Math.PI) / 2 / planeH;
			var sheeta1:Number;
			var sheeta2:Number=90;
			
			flug = planeN = 0;

			for(var i:int=0; i < H; i++)
			{
				sheeta1=0;
				var pn:int=Math.floor((2 * radius * Math.cos(sheeta2 * Math.PI / 180) * Math.PI) / planeW);
				for(var j:int=0; j < pn; j++)
				{
					var mat:ColorMaterial=new ColorMaterial(Math.random()*0xFFFFFF, 0.3);
					var plane:Plane=new Plane(mat, planeW, planeH);
					plane.material.doubleSided=true;
					var rx:int=-sheeta2;
					var ry:int=sheeta1;
					var xx:int=radius * Math.cos(sheeta2 * Math.PI / 180) * Math.sin(sheeta1 * Math.PI / 180);
					var yy:int=radius * Math.sin(sheeta2 * Math.PI / 180);
					var zz:int=radius * Math.cos(sheeta2 * Math.PI / 180) * Math.cos(sheeta1 * Math.PI / 180);
					plane.extra={x:xx, y:yy, z:zz, rx:rx, ry:ry};
					sphereNode.addChild(plane, "plane" + planeN.toString());
					sheeta1+=360 / pn;
					planeN++;
				}
				sheeta2-=180 / H;
			}

			for(var i:int=0; i < planeN; i++)
			{
				var plane:Plane = sphereNode.getChildByName("plane" + i.toString()) as Plane;
				plane.x=Math.floor(Math.random() * 600) - 300;
				plane.y=Math.floor(Math.random() * 600) - 300;
				plane.z=Math.floor(Math.random() * 600) - 300;
				plane.rotationX=Math.random() * 360;
				plane.rotationY=Math.random() * 360;
				plane.rotationZ=Math.random() * 360;
			}
			
			stage.addEventListener(MouseEvent.CLICK, mouseClick);
			addEventListener(Event.ENTER_FRAME, onFrame);
		}

		private function mouseClick(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.CLICK, mouseClick);
			
			for(var i:int=0; i<planeN; i++)
			{
				var plane:Plane = sphereNode.getChildByName("plane" + i.toString()) as Plane;
				Tweener.addTween(plane, {x:plane.extra.x, y:plane.extra.y, z:plane.extra.z, rotationX:plane.extra.rx, rotationY:plane.extra.ry, rotationZ:0, time:5});
			}
			
			flug = 1;
		}

		private function onFrame(e:Event):void
		{
			if (flug == 1)
			{
				sphereNode.rotationX += 1.5;
				sphereNode.rotationY += 1.5;
			}
			
			renderer.renderScene(scene, camera, viewport);
		}
	}
}