away3d example
found at http://polygeek.com/
♥4 |
Line 165 |
Modified 2012-01-04 23:51:03 |
MIT License
archived:2017-03-08 18:10:11
ActionScript3 source code
/**
* Copyright tjoen ( http://wonderfl.net/user/tjoen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/A8oz
*/
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="550" height="550" applicationComplete="init();" backgroundColor="0x000000">
<fx:Script>
<![CDATA[
import away3d.cameras.Camera3D;
import away3d.containers.View3D;
import away3d.materials.WireframeMaterial;
import away3d.modifiers.HeightMapModifier;
import away3d.primitives.Plane;
private var _camera3D : Camera3D;
private var _view3D : View3D;
private var _tic : Timer;
private var _lastKey : uint;
private var _isMouseDown : Boolean = false;
private var _isUpKeyDown : Boolean = false;
private var _isDownKeyDown : Boolean = false;
private var _isRightKeyDown : Boolean = false;
private var _isLeftKeyDown : Boolean = false;
private var _isPgUpDown : Boolean = false;
private var _isPgDwDown : Boolean = false;
private function init():void {
_camera3D = new Camera3D();
_camera3D.z = 0;
_camera3D.y = -80;
// app-width/height = 550. Center the view.
_view3D = new View3D( { camera:_camera3D, x:550/2, y:550/2 } );
surface.addChild( _view3D );
// add the terrain
makeTerrain();
// set up timer
_tic = new Timer( 33 );
_tic.addEventListener( TimerEvent.TIMER, onTic );
_tic.start();
// Setup keyboard and mouse events
this.stage.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
this.stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp );
this.stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
this.stage.addEventListener( KeyboardEvent.KEY_UP, onKeyUp );
}
private function onTic( e:TimerEvent ):void {
// Tilt the camera up/down based on mouse position.
_camera3D.rotationX += -( mouseY - 275 ) * 0.005;
// Keep the _camera3D.rotationX within a 15 to -30 range.
if( _camera3D.rotationX > 5 ) {
_camera3D.rotationX = 5;
} else if( _camera3D.rotationX < -30 ) {
_camera3D.rotationX = -30;
}
// spin the camera around based on mouse position.
_camera3D.rotationY += ( mouseX - 275 ) * 0.005;
// If the mouse is down then movie in space.
if( _isMouseDown ) {
var xComponent:Number = Math.sin( _camera3D.rotationY * Math.PI / 180 ) * 10;
var zComponent:Number = Math.cos( _camera3D.rotationY * Math.PI / 180 ) * 10;
_camera3D.position = new Vector3D( _camera3D.x + xComponent, _camera3D.y, _camera3D.z + zComponent );
}
// Handle the keyboard events to walk around in space.
walk();
_camera3D.update();
_view3D.render();
}
private function onMouseDown( e:MouseEvent ):void {
_isMouseDown = true;
}
private function onMouseUp( e:MouseEvent ):void {
_isMouseDown = false;
}
private function onKeyDown( e:KeyboardEvent ):void {
switch ( e.keyCode ) {
case Keyboard.UP :
_isUpKeyDown = true;
break;
case Keyboard.DOWN :
_isDownKeyDown = true;
break;
case Keyboard.RIGHT :
_isRightKeyDown = true;
break;
case Keyboard.LEFT :
_isLeftKeyDown = true;
break;
case Keyboard.PAGE_UP :
_isPgUpDown = true;
break;
case Keyboard.PAGE_DOWN :
_isPgDwDown = true;
break;
}
}
private function onKeyUp( e:KeyboardEvent ):void {
switch ( e.keyCode ) {
case Keyboard.UP :
_isUpKeyDown = false;
break;
case Keyboard.DOWN :
_isDownKeyDown = false;
break;
case Keyboard.RIGHT :
_isRightKeyDown = false;
break;
case Keyboard.LEFT :
_isLeftKeyDown = false;
break;
case Keyboard.PAGE_UP :
_isPgUpDown = false;
break;
case Keyboard.PAGE_DOWN :
_isPgDwDown = false;
break;
}
}
private function walk():void {
// These variables help us move forward/backward or left/right based on the direction the _camera3D is pointed.
// Multiplying by 10 is arbitrary and sets the speed which which we move. It would be simple enough to set
// the multiplier as a variable to simulate acceleration.
var xComponent : Number = Math.sin( _camera3D.rotationY * Math.PI / 180 ) * 10;
var zComponent : Number = Math.cos( _camera3D.rotationY * Math.PI / 180 ) * 10;
if( _isUpKeyDown ) {
_camera3D.position = new Vector3D(
_camera3D.x + xComponent,
_camera3D.y,
_camera3D.z + zComponent );
}
if( _isDownKeyDown ) {
_camera3D.position = new Vector3D(
_camera3D.x - xComponent,
_camera3D.y,
_camera3D.z - zComponent );
}
if( _isLeftKeyDown ) {
_camera3D.position = new Vector3D(
_camera3D.x - zComponent,
_camera3D.y,
_camera3D.z + xComponent );
}
if( _isRightKeyDown ) {
_camera3D.position = new Vector3D(
_camera3D.x + zComponent,
_camera3D.y,
_camera3D.z - xComponent );
}
if( _isPgUpDown ) {
_camera3D.position = new Vector3D(
_camera3D.x, _camera3D.y + 10, _camera3D.z );
}
if( _isPgDwDown ) {
_camera3D.position = new Vector3D(
_camera3D.x, _camera3D.y - 10, _camera3D.z );
}
}
private function makeTerrain():void {
// Create a bitmap that is filled with Perlin Noise that will get translated into
// the HeightMapModifier to add bumps to the plane.
var bmp : BitmapData = new BitmapData( 4000, 4000, false );
bmp.perlinNoise( 20, 20, 2, 0, true, true, 7, true );
// Simple WireframeMaterial to give the plane that early 80s video game look.
var material : WireframeMaterial = new WireframeMaterial( 0xFFFFFF );
material.wireAlpha = 0.5;
// Plane
var plane : Plane = new Plane();
plane.width = 4000;
plane.height = 4000;
plane.segmentsW = 50;
plane.segmentsH = 50;
plane.material = material;
// Modifier
var modifier : HeightMapModifier = new HeightMapModifier( plane, bmp );
modifier.scale = 0.9;
modifier.execute();
_view3D.scene.addChild( plane );
}
]]>
</fx:Script>
<s:SpriteVisualElement id="surface" />
<s:VGroup horizontalAlign="center" top="10" width="100%">
<s:Label text="Click with the mouse to move forward in that direction." color="0xFFFFFF" />
<s:Label text="Use forward/back and left/right arrows as well. Pg.Up/Down controls altitude." color="0xFFFFFF" />
</s:VGroup>
</s:Application>