here’s the class that loads everything (I’m sorry it’s a bit of a mess..)
package
{
import away3d.cameras.HoverCamera3D;
import away3d.cameras.SpringCam;
import away3d.cameras.TargetCamera3D;
import away3d.core.clip.FrustumClipping;
import away3d.core.render.Renderer;
import away3d.core.utils.Cast;
import away3d.lights.PointLight3D;
import planets.Planet1;
import com.greensock.*;
import com.greensock.easing.*;
import away3d.primitives.Plane;
import away3d.primitives.Sphere;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import away3d.events.MouseEvent3D;
///stars
import away3d.materials.TransformBitmapMaterial;
import away3d.primitives.Skybox;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Vector3D;
import away3d.lights.DirectionalLight3D;
public class CameraDemo extends Away3DTemplate
{
protected var sphereCam:Sphere = new Sphere({radius: .1});
protected var hoverCamera:HoverCamera3D;
protected var lastStageX:Number;
protected var lastStageY:Number;
protected var mouseButtonDown:Boolean;
public var planet1:Planet1 = new Planet1();
public var planet2:Planet1 = new Planet1();
//protected var light:DirectionalLight3D;
public function CameraDemo()
{
super();
}
protected override function initEngine():void
{
super.initEngine();
view.renderer = Renderer.CORRECT_Z_ORDER;
view.clipping = new FrustumClipping();
}
protected override function initScene():void
{
super.initScene();
makeSphereCam();
makeStars();
loadPlanes();
var light:DirectionalLight3D = new DirectionalLight3D({color:0xFFFFFF, ambient:.5, diffuse:.3, specular:.1, x:500, direction: new Vector3D(-1,-1,0)});
// scene.addLight(light);
}
protected function loadPlanes():void {
scene.addChild(planet1);
//scene.addChild(planet2);
//planet2.z = 1000;
//planet2.y = 100;
//planet2.x = 300;
planet1.addEventListener(MouseEvent3D.MOUSE_DOWN, planetClick);
//planet2.addEventListener(MouseEvent3D.MOUSE_DOWN, planetClick);
}
protected function planetClick(event:MouseEvent3D):void {
trace("click!" + event.currentTarget.z);
TweenLite.to(sphereCam, 4, {x:event.currentTarget.x, y:event.currentTarget.y, z:event.currentTarget.z});
}
protected function makeSphereCam() : void {
scene.addChild(sphereCam);
addHoverCamera();
}
protected override function initListeners():void
{
super.initListeners();
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(Event.RESIZE, resizeHandler);
}
protected override function onEnterFrame(event:Event):void
{
super.onEnterFrame(event);
//planet1.roll(1);
//planet1.pitch(1);
hoverCamera.hover();
//trace(sphereCam.x + " " + sphereCam.y + " " + sphereCam.z );
}
protected function onMouseDown(event:MouseEvent):void
{
mouseButtonDown = true;
lastStageX = event.stageX;
lastStageY = event.stageY;
}
protected function onMouseUp(event:MouseEvent):void
{
mouseButtonDown = false;
}
protected function addHoverCamera():void
{
hoverCamera = new HoverCamera3D(
{
target: sphereCam,
distance: 100,
mintiltangle: 5,
tiltAngle: 45
}
);
view.camera = hoverCamera;
hoverCamera.zoom = 8 * (stage.stageHeight / 768);
}
protected function onMouseMove(event:MouseEvent):void
{
if (hoverCamera != null && mouseButtonDown == true)
{
var pan:int = (event.stageX - lastStageX);
var tilt:int = (event.stageY - lastStageY);
hoverCamera.panAngle += pan;
hoverCamera.tiltAngle += tilt;
lastStageX = event.stageX;
lastStageY = event.stageY;
}
}
private function resizeHandler(event:Event):void{
view.x = (stage.stageWidth / 2) ;
view.y = (stage.stageHeight / 2) ;
hoverCamera.zoom = 8 * (stage.stageHeight / 768);
}
protected function makeStars():void {
//////////////////////stars
// create a temporary container-sprite to add stars into
var mc:Sprite = new Sprite();
mc.graphics.beginFill(0);
mc.graphics.drawRect(0,0,2048,2048);
mc.graphics.endFill();
// add 2000 star-sprites to the container
var s:Sprite, r:Number;
for( var i:uint=0; i<500; i++ ) {
r = Math.random()*Math.random() * 2;
s = new Sprite();
s.graphics.beginFill(0xFFFFFF,0.9);
s.graphics.drawCircle(0,0,r);
s.graphics.endFill();
s.x = r + (mc.width - 2*r)*Math.random();
s.y = r + (mc.height - 2*r)*Math.random();
mc.addChild(s);
}
// render the container-sprite to a bitmap
var bmd:BitmapData = new BitmapData(mc.width, mc.height, false, 0);
bmd.draw(mc);
// create a 2x2 tiled material from the bitmap
var mat:TransformBitmapMaterial = new TransformBitmapMaterial(bmd, {
smooth: true, precision:0,
repeat: true, scaleX: 0.5, scaleY: 0.5 });
// create a skybox using that material on all 6 sides
var box:Skybox = new Skybox( mat,mat,mat,mat,mat,mat );
// and finally add it to the scene
scene.addChild(box);
}
}
}