Created
September 25, 2012 04:38
-
-
Save jonathantorres/3780010 to your computer and use it in GitHub Desktop.
Point Light & HoverCamera in Away3D
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 com.jonathantorres.practices | |
{ | |
import away3d.cameras.Camera3D; | |
import away3d.controllers.HoverController; | |
import away3d.debug.AwayStats; | |
import away3d.entities.Mesh; | |
import away3d.lights.PointLight; | |
import away3d.materials.ColorMaterial; | |
import away3d.materials.lightpickers.StaticLightPicker; | |
import away3d.primitives.CubeGeometry; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
/** | |
* @author Jonathan Torres | |
*/ | |
[SWF(backgroundColor="#000000", frameRate="31", width="800", height="600")] | |
public class HoverCamera extends AwayTemplate | |
{ | |
private var _cube : Mesh; | |
private var _cubeMaterial : ColorMaterial; | |
private var _camera : Camera3D; | |
private var _cameraController : HoverController; | |
private var _pointLight : PointLight; | |
private var _lightPicker : StaticLightPicker; | |
private var _isMouseDown : Boolean; | |
private var _lastPanAngle : Number; | |
private var _lastTiltAngle : Number; | |
private var _lastMouseX : Number; | |
private var _lastMouseY : Number; | |
public function HoverCamera() | |
{ | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.align = StageAlign.TOP_LEFT; | |
super(); | |
initEngine(); | |
view.antiAlias = 4; | |
_camera = view.camera; | |
_isMouseDown = false; | |
_pointLight = new PointLight(); | |
scene.addChild(_pointLight); | |
_lightPicker = new StaticLightPicker([_pointLight]); | |
_cubeMaterial = new ColorMaterial(0xFF0000); | |
_cubeMaterial.lightPicker = _lightPicker; | |
_cube = new Mesh(new CubeGeometry(200, 200, 200, 50, 50, 50), _cubeMaterial); | |
scene.addChild(_cube); | |
_cameraController = new HoverController(_camera, _cube); | |
_cameraController.panAngle = 45; | |
_cameraController.tiltAngle = 30; | |
_cameraController.distance = 300; | |
addChild(new AwayStats(view)); | |
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown); | |
stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp); | |
initListeners(); | |
} | |
private function onStageMouseUp(event : MouseEvent) : void | |
{ | |
_isMouseDown = false; | |
} | |
private function onStageMouseDown(event : MouseEvent) : void | |
{ | |
_lastPanAngle = _cameraController.panAngle; | |
_lastTiltAngle = _cameraController.tiltAngle; | |
_lastMouseX = stage.mouseX; | |
_lastMouseY = stage.mouseY; | |
_isMouseDown = true; | |
} | |
override protected function onEnterFrame(event : Event) : void | |
{ | |
if (_isMouseDown) { | |
_cameraController.panAngle = 0.5 * (stage.mouseX - _lastMouseX) + _lastPanAngle; | |
_cameraController.tiltAngle = 0.5 * (stage.mouseY - _lastMouseY) + _lastTiltAngle; | |
} | |
_pointLight.position = camera.position; | |
super.onEnterFrame(event); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment