Created
October 4, 2011 09:55
-
-
Save wwwins/1261273 to your computer and use it in GitHub Desktop.
away3d demo
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.Camera3D; | |
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 camera:Camera3D; | |
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; | |
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 Camera3D(); | |
camera.z = -1000; | |
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 | |
{ | |
lastRotationX = container.rotationX; | |
lastRotationY = cube.rotationY; | |
lastMouseX = mouseX; | |
lastMouseY = mouseY; | |
move = true; | |
} | |
private function _onEnterFrame(e:Event):void | |
{ | |
if (move) { | |
container.rotationX = (mouseY - lastMouseY)/10 + lastRotationX; | |
if (container.rotationX > 90) | |
container.rotationX = 90; | |
if (container.rotationX < -90) | |
container.rotationX = -90; | |
cube.rotationY = (lastMouseX - mouseX)/10 + lastRotationY; | |
} | |
view.render(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment