Created
September 23, 2012 01:48
-
-
Save jonathantorres/3768500 to your computer and use it in GitHub Desktop.
Some basic primitives 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 flash.events.Event; | |
import away3d.containers.View3D; | |
import away3d.cameras.Camera3D; | |
import away3d.containers.Scene3D; | |
import flash.display.Sprite; | |
/** | |
* @author Jonathan Torres | |
*/ | |
public class AwayTemplate extends Sprite | |
{ | |
protected var scene : Scene3D; | |
protected var camera : Camera3D; | |
protected var view : View3D; | |
public function AwayTemplate() { } | |
protected function initEngine() : void | |
{ | |
view = new View3D(); | |
addChild(view); | |
camera = view.camera; | |
scene = view.scene; | |
} | |
protected function initListeners() : void | |
{ | |
addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
} | |
protected function onEnterFrame(event : Event) : void | |
{ | |
view.render(); | |
} | |
} | |
} |
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.core.base.Geometry; | |
import away3d.entities.Mesh; | |
import away3d.materials.TextureMaterial; | |
import away3d.primitives.CapsuleGeometry; | |
import away3d.primitives.ConeGeometry; | |
import away3d.primitives.CubeGeometry; | |
import away3d.primitives.CylinderGeometry; | |
import away3d.primitives.PlaneGeometry; | |
import away3d.primitives.RegularPolygonGeometry; | |
import away3d.primitives.SphereGeometry; | |
import away3d.primitives.TorusGeometry; | |
import away3d.textures.BitmapTexture; | |
import flash.display.Bitmap; | |
import flash.events.Event; | |
import flash.events.KeyboardEvent; | |
import flash.ui.Keyboard; | |
/** | |
* @author Jonathan Torres | |
*/ | |
[SWF(backgroundColor="#000000", frameRate="31", width="800", height="600")] | |
public class BasicPrimitives extends AwayTemplate | |
{ | |
[Embed(source="../../../../assets/material.jpeg")] | |
private var Material : Class; | |
private var _primitive : Mesh; | |
private var _texture : TextureMaterial; | |
private var _currentGeometry : Geometry; | |
private var _shapes : Array; | |
private var _geometryIndex : int; | |
public function BasicPrimitives() | |
{ | |
super(); | |
initEngine(); | |
camera.z = -300; | |
_shapes = new Array(new CapsuleGeometry(), | |
new ConeGeometry(), | |
new CubeGeometry(), | |
new CylinderGeometry(), | |
new PlaneGeometry(), | |
new RegularPolygonGeometry(), | |
new SphereGeometry(), | |
new TorusGeometry()); | |
_geometryIndex = 0; | |
_currentGeometry = _shapes[_geometryIndex]; | |
_texture = new TextureMaterial(new BitmapTexture(Bitmap(new Material()).bitmapData)); | |
_texture.bothSides = true; | |
_primitive = new Mesh(_currentGeometry, _texture); | |
scene.addChild(_primitive); | |
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); | |
initListeners(); | |
} | |
private function onKeyUp(event : KeyboardEvent) : void | |
{ | |
switch(event.keyCode) { | |
case Keyboard.RIGHT : | |
if (_geometryIndex >= _shapes.length - 1) | |
_geometryIndex = 0; | |
else | |
_geometryIndex++; | |
updateGeometry(_geometryIndex); | |
break; | |
case Keyboard.LEFT : | |
if (_geometryIndex <= 0) | |
_geometryIndex = _shapes.length - 1; | |
else | |
_geometryIndex--; | |
updateGeometry(_geometryIndex); | |
break; | |
} | |
} | |
private function updateGeometry(index : int) : void | |
{ | |
_currentGeometry = _shapes[index]; | |
_primitive.geometry = _currentGeometry; | |
} | |
override protected function onEnterFrame(event : Event) : void | |
{ | |
_primitive.rotationX++; | |
_primitive.rotationY++; | |
_primitive.rotationZ++; | |
super.onEnterFrame(event); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment