package
{
import away3d.cameras.HoverCamera3D;
import away3d.containers.View3D;
import away3d.loaders.Loader3D;
import away3d.loaders.Max3DS;
import away3d.containers.ObjectContainer3D;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import away3d.core.base.Mesh;
[SWF(width="500", height="400", frameRate="30", backgroundColor="#FFFFFF")]
public class MyView extends Sprite
{
private var View:View3D;
private var MyObject:Mesh;
// HoverCam controls
private var camera:HoverCamera3D;
private var lastMouseX:Number;
private var lastMouseY:Number;
private var lastPanAngle:Number;
private var lastTiltAngle:Number;
private var move:Boolean = false;
[Embed(source="obj.3ds",mimeType="application/octet-stream")]
public static var MyObject3DS:Class;
public function MyView()
{
// prep for handling resizing events
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP;
// create a 3D-viewport
camera = new HoverCamera3D({zoom:2, focus:100, distance:250});
View = new View3D({camera:camera,x:250, y:200});
// add viewport to the stage
addChild(View);
camera.panAngle = 45;
camera.tiltAngle = 20;
camera.minTiltAngle = -90;
camera.hover(true);
// Show the model
MyObject = addModel(View);
//var cont:ObjectContainer3D = new ObjectContainer3D([]);
//var ldr:Loader3D = Max3DS::load('obj.3ds', cont);
//View.scene.addChild();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
this.stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
}
private function MouseDown(event:MouseEvent):void
{
lastPanAngle = camera.panAngle;
lastTiltAngle = camera.tiltAngle;
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
move = true;
}
private function MouseUp(event:MouseEvent):void
{
move = false;
}
private function onEnterFrame(e:Event):void
{
// re-render viewport
var cameraSpeed:Number = 0.3; // Approximately same speed as mouse movement.
if (move) {
camera.panAngle = cameraSpeed*(stage.mouseX - lastMouseX) + lastPanAngle;
camera.tiltAngle = cameraSpeed*(stage.mouseY - lastMouseY) + lastTiltAngle;
}
camera.hover();
View.render();
}
private function addModel(View):Mesh
{
var obj:Mesh = Max3DS.parse(MyObject3DS, {ownCanvas:true, material:"white#black", name:"obj", back:"red#blue"});
obj.movePivot((obj.minX+obj.maxX)/2,(obj.minY+obj.maxY)/2,(obj.minZ+obj.maxZ)/2);
obj.x = x;
obj.y = y;
//if(m!=null){ torso.material = m; }
obj.scale(1.5);
View.scene.addChild( obj );
return obj;
}
}
}