Hi Folks,
I’m a programmer for the USAF and my background is in PHP and ASP development. I’ve recently been assigned to a new unit that does flash development so I have about a year’s experience learning ActionScript 3.
Here’s my problem:
I’m writing a 3D surgical instruments application that had a 3D model of a tool. User can pan, tilt, zoom in, zoom out, move left, move right. However, I want to have descriptions of the image to the left of the 3d clipped window. When the user clicks on a description, for example “pointed tounge”, I was the 3d object to rotate and zoom in on that specific part.
The only way I can conceive to do this is to bring the camera to a specific point and calculate the rotation based on that. However, when I use the pan and tilt, I cannot figure out how to get my camera back to where it started. Here is my code:
package
{
import away3d.cameras.*;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import away3d.core.base.Object3D;
import away3d.core.math.Number3D;
import away3d.core.render.Renderer;
import away3d.events.Loader3DEvent;
import away3d.lights.PointLight3D;
import flash.events.MouseEvent;
import away3d.loaders.Loader3D;
import away3d.loaders.Obj;
import away3d.loaders.Max3DS;
import flash.display.Sprite;
import flash.events.Event;
import caurina.transitions.*
import caurina.transitions.properties.*
import away3d.lights.PointLight3D;
public class Globe extends Sprite
{
protected var _scene:Scene3D;
protected var _view:View3D;
protected var _camera:Camera3D;
protected var _loader:Loader3D;
protected var _loaderTank:Loader3D;
protected var _mesh:Object3D;
protected var _light:PointLight3D;
// variables
var move:Boolean = false;
var lastPanAngle:Number;
var lastTiltAngle:Number;
var lastMouseX:Number;
var lastMouseY:Number;
public function Globe()
{
initAway();
}
private function initAway():void
{
_camera = new Camera3D({ zoom:2, focus:200});
_camera.pan(-26);
_camera.tilt(20);
//_camera.hover();
lastPanAngle = -26;
lastTiltAngle = 20;
_scene = new Scene3D();
_view = new View3D( { x:230, y:300, scene:_scene, camera:_camera, renderer:Renderer.CORRECT_Z_ORDER } );
addChild( _view );
_view.clipping.maxX = 200;
_view.clipping.maxY = 80;
_view.clipping.minX = -200;
_view.clipping.minY = -270;
_loader = Max3DS.load( "ST_1.3DS", { bothsides:true } );
_loader.addOnSuccess( onObjLoad );
_light = new PointLight3D( { x:300, y:100, z:100,brightness:250, ambient:.5, diffuse:.5, specular:1 } );
//_scene.addLight(_light);
_scene.addChild(_light);
//_loaderTank = Obj.load( "Instrument_ENT.obj", { scaling:.5, bothsides:true } );
//_loaderTank.addOnSuccess( onObjLoad );
}
private function onObjLoad( e:Loader3DEvent ):void
{
_mesh = e.loader.handle;
_mesh.scaleX = 10;
_mesh.scaleY = 10;
_mesh.scaleZ = 10;
_mesh.rotationX=90;
_mesh.rotationY=-90;
//_mesh.rotationX = 90;
_scene.addChild( _mesh );
//Event Listeners
addEventListener( Event.ENTER_FRAME, onRender );
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
btnLeft.addEventListener(MouseEvent.MOUSE_DOWN, startLeftTurn);
btnRight.addEventListener(MouseEvent.MOUSE_DOWN, startRightTurn);
btnUp.addEventListener(MouseEvent.MOUSE_DOWN, startUpTurn);
btnDown.addEventListener(MouseEvent.MOUSE_DOWN, startDownTurn);
btnZoomIn.addEventListener(MouseEvent.MOUSE_DOWN, startZoomIn);
btnZoomOut.addEventListener(MouseEvent.MOUSE_DOWN, startZoomOut);
btnCenter.addEventListener(MouseEvent.MOUSE_DOWN, CenterCamera);
//Tweener.addTween(_mesh,{rotationY:100,time:100});
}
private function onRender( e:Event ):void
{
//_mesh.rotationY += 5;
_view.render();
}
//Left Turn
private function startLeftTurn(e:MouseEvent):void
{
btnLeft.removeEventListener(MouseEvent.MOUSE_DOWN, startLeftTurn);
btnLeft.addEventListener(MouseEvent.MOUSE_UP, stopLeftTurn);
btnLeft.addEventListener(Event.ENTER_FRAME, turnLeft);
}
private function stopLeftTurn(e:MouseEvent):void
{
btnLeft.addEventListener(MouseEvent.MOUSE_DOWN, startLeftTurn);
btnLeft.removeEventListener(MouseEvent.MOUSE_UP, stopLeftTurn);
btnLeft.removeEventListener(Event.ENTER_FRAME, turnLeft);
}
private function turnLeft(e:Event):void
{
_camera.pan(-2);
lastPanAngle -= 2;
}
function MouseDown(event:MouseEvent):void
{
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
move = true;
}
function MouseUp(event:MouseEvent):void
{
move = false;
}
function onEnterFrame(e:Event):void
{
// rerender viewport
var cameraSpeed:Number = 0.3; // Approximately same speed as mouse movement.
if (move) {
_camera.pan(cameraSpeed*(stage.mouseX - lastMouseX));
lastPanAngle += cameraSpeed*(stage.mouseX - lastMouseX);
_camera.tilt(cameraSpeed*(stage.mouseY - lastMouseY));
lastTiltAngle += cameraSpeed*(stage.mouseY - lastMouseY);
}
//_camera.hover();
_view.render();
//trace(lastPanAngle + "," + lastTiltAngle);
}
public function CenterCamera(e:Event) {
_camera.x = 0;
_camera.y = 0;
_camera.z = 0;
_camera.lookAt(new Number3D(0,0,0));
_camera.pan(-26);
_camera.tilt(20);
_camera.zoom = 2;
_camera.position.normalize(1);
}
}
}