camera 相關原理可以先參考這兩篇
http://www.flashmagazine.com/Tutorials/detail/away3d_basics_the_cameras/
http://blog.tartiflop.com/2008/08/understanding-zoom-focus-and-field-of-view-in-papervision3d/
away3d 有四種 camera,分別是
camera3D:default camera
TargetCamera3D:automatically look at a specified target object
SpringCam:1st and 3rd person camera
HoverCamera3D:hover round a specified target object
本文介紹(最常用) HoverCamera3D,
利用改變 camera.panAngle 及 camera.tileAngle
來產生旋轉及放大縮小的 3D 效果。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import away3d.cameras.HoverCamera3D; | |
import away3d.containers.ObjectContainer3D; | |
import away3d.containers.Scene3D; | |
import away3d.containers.View3D; | |
import away3d.materials.VideoMaterial; | |
import away3d.primitives.Cube; | |
import away3d.primitives.Trident; | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
public class Main extends Sprite | |
{ | |
private var scene:Scene3D; | |
private var view:View3D; | |
private var material:VideoMaterial; | |
private var cube:Cube; | |
private var container:ObjectContainer3D; | |
private var lastRotationX:Number; | |
private var lastRotationY:Number; | |
private var move:Boolean; | |
private var lastMouseY:Number; | |
private var lastMouseX:Number | |
private var camera:HoverCamera3D;; | |
private var lastPanAngle:Number; | |
private var lastTiltAngle:Number; | |
public function Main():void | |
{ | |
if (stage) | |
init(); | |
else | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(e:Event = null):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
// entry point | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
init3D(); | |
} | |
private function init3D():void | |
{ | |
scene = new Scene3D(); | |
camera = new HoverCamera3D(); | |
camera.z = -1000; // make sure the camera is positioned away from the default 0,0,0 coordinate | |
camera.panAngle = 45; | |
camera.tiltAngle = 5; | |
camera.hover(true); | |
camera.minTiltAngle = -90; | |
view = new View3D(); | |
view.x = stage.stageWidth / 2; | |
view.y = stage.stageHeight / 2; | |
view.scene = scene; | |
view.camera = camera; | |
addChild(view); | |
var axis:Trident = new Trident(250, true); | |
scene.addChild(axis); | |
// 拿 flv 的材質 | |
material = new VideoMaterial(); | |
material.loop = true; | |
material.file = "video/1.flv"; | |
material.smooth = true; | |
cube = new Cube(); | |
cube.material = material; | |
cube.width = 200; | |
cube.height = 200; | |
cube.depth = 200; | |
container = new ObjectContainer3D(cube); | |
scene.addChild(container); | |
addEventListener(Event.ENTER_FRAME, _onEnterFrame); | |
addEventListener(Event.ADDED_TO_STAGE, _onAddedToStage); | |
} | |
private function _onAddedToStage(e:Event):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, _onAddedToStage); | |
view.x = stage.stageWidth / 2; | |
view.y = stage.stageHeight / 2; | |
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); | |
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); | |
} | |
private function onMouseUp(e:MouseEvent):void | |
{ | |
move = false; | |
} | |
private function onMouseDown(e:MouseEvent):void | |
{ | |
lastPanAngle = camera.panAngle; | |
lastTiltAngle = camera.tiltAngle; | |
lastMouseX = mouseX; | |
lastMouseY = mouseY; | |
move = true; | |
} | |
private function _onEnterFrame(e:Event):void | |
{ | |
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(); | |
} | |
} | |
} |
沒有留言:
張貼留言