Skip to content

Instantly share code, notes, and snippets.

@gonchar gonchar/gist:3386564
Created Aug 18, 2012

Embed
What would you like to do?
Eclipse(FDT, FlashBuilder) Useful Alternativa3D Start Template with orbitCamera controller
package ${enclosing_package} {
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.Object3D;
import alternativa.engine3d.core.Resource;
import alternativa.engine3d.core.View;
import alternativa.engine3d.materials.FillMaterial;
import alternativa.engine3d.objects.WireFrame;
import alternativa.engine3d.primitives.Box;
import flash.display.Sprite;
import flash.display.Stage3D;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Vector3D;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class ${enclosing_type} extends Sprite {
private var camera : Camera3D;
private var stage3D : Stage3D;
private var rootContainer : Object3D;
private var cameraContainer : SmartCameraContainer;
private var wire : WireFrame;
private var wireContainer : Object3D;
public function ${enclosing_type}() {
if (stage) {
onAdded(null);
} else {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
}
private function onAdded(event : Event) : void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
// request Stage3D
stage3D = stage.stage3Ds[0];
stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
stage3D.requestContext3D();
}
private function onContextCreate(e : Event) : void {
// init stage
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.HIGH;
stage.frameRate = 60;
// create textinfo
createGpuInfoField();
showGpuInfo = true;
// main container
rootContainer = new Object3D();
// camera
setupCameraView();
// wire
addWireXY(500, 500);
// your scene
setupScene();
// upload all resoruces
for each (var resource:Resource in rootContainer.getResources(true)) {
if (resource.isUploaded) continue;
resource.upload(stage3D.context3D);
}
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(Event.RESIZE, stageUpdate);
}
// --------------------------------------------------------------------------
//
// DO YOUR SCENE HERE
//
// --------------------------------------------------------------------------
private function setupScene() : void {
var box : Box = new Box();
box.setMaterialToAllSurfaces(new FillMaterial());
rootContainer.addChild(box);
}
private function setupCameraView() : void {
var viewContainer : Sprite = new Sprite();
addChild(viewContainer);
camera = new Camera3D(1, 50000);
camera.view = new View(stage.stageWidth, stage.stageHeight, false, 0x393939, 1, 8);
viewContainer.addChild(camera.view);
viewContainer.addChild(camera.diagram);
camera.view.hideLogo();
camera.diagramAlign = StageAlign.TOP_RIGHT;
cameraContainer = new SmartCameraContainer(camera, viewContainer);
setCameraPositionDefault();
rootContainer.addChild(cameraContainer);
}
protected function setCameraPositionDefault() : void {
cameraContainer.x = 0;
cameraContainer.y = 0;
cameraContainer.z = 0;
cameraContainer.rotationX = -0.529976651502621;
cameraContainer.rotationY = 0;
cameraContainer.rotationZ = 1.1548630468789403;
}
private function addWireXY(sizeX : Number, sizeY : Number) : void {
var i : int;
var lines : Vector.<Vector3D> = new Vector.<Vector3D>();
var size : Number = 10;
var length : int = sizeX / size;
for (i = 0; i <= length; i++) {
lines.push(new Vector3D(i * size, 0, 0));
lines.push(new Vector3D(i * size, sizeY, 0));
}
length = sizeY / size;
for (i = 0; i <= length; i++) {
lines.push(new Vector3D(0, i * size, 0));
lines.push(new Vector3D(sizeX, i * size, 0));
}
wireContainer = new Object3D();
rootContainer.addChild(wireContainer);
wire = WireFrame.createLinesList(lines, 0x4b4b4b, 0.8, 0.5);
wireContainer.addChild(wire);
wire.x = -sizeX / 2;
wire.y = -sizeY / 2;
}
private function onEnterFrame(e : Event) : void {
camera.render(stage3D);
}
private function stageUpdate(e : Event) : void {
camera.view.height = stage.stageHeight;
camera.view.width = stage.stageWidth;
if (_showGpuInfo) {
gpuInfoField.x = 0;
gpuInfoField.y = stage.stageHeight - gpuInfoField.height - 3;
}
}
// --------------------------------------------------------------------------
//
// GPU INFO FIELD
//
// --------------------------------------------------------------------------
private var _showGpuInfo : Boolean;
private var gpuInfoField : TextField;
private function createGpuInfoField() : void {
gpuInfoField = new TextField();
gpuInfoField.appendText("Driver info: " + stage3D.context3D.driverInfo + "\n");
gpuInfoField.appendText("Enable Error Checking: " + stage3D.context3D.enableErrorChecking.toString());
gpuInfoField.setTextFormat(new TextFormat("_sans", 14, 0xffffff));
gpuInfoField.autoSize = TextFieldAutoSize.LEFT;
gpuInfoField.selectable = false;
gpuInfoField.mouseEnabled = false;
}
public function get showGpuInfo() : Boolean {
return _showGpuInfo;
}
public function set showGpuInfo(value : Boolean) : void {
_showGpuInfo = value;
if (_showGpuInfo) {
if (!contains(gpuInfoField)) {
addChild(gpuInfoField);
}
gpuInfoField.x = 0;
gpuInfoField.y = stage.stageHeight - gpuInfoField.height - 3;
} else {
if (contains(gpuInfoField)) {
removeChild(gpuInfoField);
}
}
}
}
}
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.Object3D;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.MouseEvent;
class SmartCameraContainer extends Object3D {
public static const PERSPECTIVE_PROJECTION : String = "perspective";
public static const ORTHOGONAL_PROJECTION : String = "orthogonal";
private var stage : Stage;
private var tempX : Number;
private var tempY : Number;
private var tempRZ : Number;
private var tempRX : Number;
private var camera : Camera3D;
private var _projection : String = PERSPECTIVE_PROJECTION;
public function SmartCameraContainer(camera : Camera3D, vp : Sprite) {
this.camera = camera;
stage = vp.stage;
addChild(camera);
camera.rotationX = -Math.PI / 2;
camera.y = -322;
rotationX = -0.529976651502621;
rotationY = 0;
rotationZ = 1.1548630468789403;
vp.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
}
private function onMouseDown(event : MouseEvent) : void {
tempX = stage.mouseX;
tempY = stage.mouseY;
tempRX = rotationX;
tempRZ = rotationZ;
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseMove(event : MouseEvent) : void {
var factor : Number = 200;
var newRz : Number = tempRZ + (tempX - stage.mouseX) / factor;
var newRx : Number = tempRX + (tempY - stage.mouseY) / factor;
rotationX = newRx;
rotationZ = newRz;
}
private function onMouseUp(event : MouseEvent) : void {
tempX = stage.mouseX;
tempY = stage.mouseY;
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseWheel(event : MouseEvent) : void {
var forceShift : Number = 8;
if (event.shiftKey) {
forceShift = 30;
}
var newCameraY : Number = camera.y + event.delta * forceShift;
if (newCameraY > -5) newCameraY = -5;
if (newCameraY < -1350) newCameraY = -1350;
camera.y = newCameraY;
}
public function get projection() : String {
return _projection;
}
public function set projection(value : String) : void {
if (_projection != value) {
_projection = value;
if (_projection == PERSPECTIVE_PROJECTION) {
camera.orthographic = false;
} else {
camera.orthographic = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.