Skip to content

Instantly share code, notes, and snippets.

@wwwins
Created October 4, 2011 10:05
Show Gist options
  • Save wwwins/1261286 to your computer and use it in GitHub Desktop.
Save wwwins/1261286 to your computer and use it in GitHub Desktop.
away3d demo HoverCamera3D
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment