Moon3D

Explore the moon in 3D with flash

Moon3D

src/moon3D.mxml


<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	backgroundColor="#000000" backgroundAlpha="1"
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	xmlns:xx="nu.xero.flex.*"
	layout="absolute" 
	pageTitle="Moon3D"
	applicationComplete="init3D()">
	<mx:Script>
		<![CDATA[
		//_______________________________________________________________________
		//                                                                imports			
			import flash.geom.*;
			import flash.events.*;
			import nu.xero.pv3d.obj.*;			
			import org.papervision3d.events.*;
			import org.papervision3d.core.math.*;
			import org.papervision3d.materials.*;
			import org.papervision3d.objects.special.*;
			import org.papervision3d.materials.special.*;
		//_______________________________________________________________________
		//                                                              variables			
			private static const FORWARD:Number3D = new Number3D(0, 0, 1);
			private var moon			:Planet;
			private var space			:ParticleField;
			private var mouseisdown		:Boolean = false;
			private var lastPoint		:Point = new Point();	
		//_______________________________________________________________________
		//                                                          texture embed
			[Embed(source="assets/moonmap4k.jpg")]
			 private var mapData		:Class;
		//_______________________________________________________________________
		//                                                             initilizer			
			private function init3D():void {
				//---camera setup
				canvas.view.cameraAsCamera3D.zoom = 500;
				canvas.view.cameraAsCamera3D.focus = 10;
				
				//---space
				space = new ParticleField(new ParticleMaterial(0xcccccc, 0.75, 1), 2000, 2, 2500, 2500, 2500);
				space.moveForward(5000);
				canvas.view.scene.addChild(space);
				
				//---planet
				var moonmap:BitmapMaterial= new BitmapMaterial((new mapData() as Bitmap).bitmapData);
				moon = new Planet(moonmap);
				moon.material.interactive = true;
				moon.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, hold);
				moon.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE, letgo);
				moon.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE_OUTSIDE, letgo);
				moon.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, mouseOver);
				moon.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, mouseOut);				
				addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
				canvas.view.scene.addChild(moon);
				
				//---rendering
				addEventListener(Event.ENTER_FRAME, loop3D);
				canvas.view.startRendering();
			}
		//_______________________________________________________________________
		//                                                            render loop
			private function loop3D(e:Event):void {
				var thisPoint:Point = new Point(canvas.mouseX, canvas.mouseY);
				if(mouseisdown) {
					var diff:Point = thisPoint.subtract(lastPoint);
					var vector:Number3D = new Number3D(diff.x, diff.y, 0);
					
					var rotAxis:Number3D = Number3D.cross(vector, FORWARD);
					rotAxis.normalize();
					
					var dist:Number = Point.distance(thisPoint, lastPoint);
					var rotMatrix:Matrix3D = Matrix3D.rotationMatrix(rotAxis.x, -rotAxis.y, rotAxis.z, dist/250);
					
					moon.transform.calculateMultiply3x3(rotMatrix, moon.transform);
				}
				lastPoint = thisPoint;
			}
		//_______________________________________________________________________
		//                                             interactive scene handlers
			private function hold(e:InteractiveScene3DEvent):void {
				mouseisdown = true;
			}
			private function letgo(e:InteractiveScene3DEvent):void {
				mouseisdown = false;
			}
			private function mouseOver(e:InteractiveScene3DEvent):void {
				this.buttonMode = true;
			}
			private function mouseOut(e:InteractiveScene3DEvent):void {
				this.buttonMode = false;
			}
			private function onWheel(e:MouseEvent):void {
				if(e.delta < 0) {
					theZoom.value -= 30;
				} else {
					theZoom.value += 30;
				}
				pov();
			}
		//_______________________________________________________________________
		//                                                   camera point of view
			private function pov():void {
				canvas.view.cameraAsCamera3D.zoom = theZoom.value;
			}
		]]>
	</mx:Script>
	<mx:Style>
		Panel {
			borderStyle: solid;
			borderColor: #ffffff;
			cornerRadius: 0;
			backgroundColor: #000000;
			dropShadowEnabled: false;
			titleStyleName: "panelTitle";
		}
		.panelTitle {
			textAlign: center;
			letterSpacing: 1;
			color: #ffffff;
			fontWeight: normal;
		}	
	</mx:Style>
	<xx:CanvasView3D id="canvas" interactivity="true" top="0" bottom="0" left="0" right="0"/>
	<mx:Panel width="195" height="62" layout="absolute" title="Moon3D" right="0" bottom="0">
		<mx:HSlider x="10" y="-6" minimum="300" maximum="1700" value="500" id="theZoom" change="pov()" liveDragging="true"/>
	</mx:Panel>
	<xx:FlexFPS left="0" top="0" right="0" horizontalCenter="0"/>
</mx:Application>

Download

raw zip tar