Skip to content

Instantly share code, notes, and snippets.

@SelStrom
Last active August 8, 2016 16:09
Show Gist options
  • Save SelStrom/3bd01457ad61df13f7b9549edf988194 to your computer and use it in GitHub Desktop.
Save SelStrom/3bd01457ad61df13f7b9549edf988194 to your computer and use it in GitHub Desktop.
package {
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.core.managers.Stage3DManager;
import away3d.core.managers.Stage3DProxy;
import away3d.debug.AwayStats;
import away3d.entities.Mesh;
import away3d.events.Stage3DEvent;
import away3d.materials.TextureMaterial;
import away3d.primitives.PlaneGeometry;
import away3d.textures.BitmapTexture;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display3D.Context3DProfile;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.utils.Timer;
/**
* ...
* @author Shatalov Andrey
*/
public class Main extends Sprite {
private var _view : View3D;
private var _stage3DProxy : Stage3DProxy;
protected var _awayStats : AwayStats;
public function Main() {
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e : Event = null) : void {
removeEventListener(Event.ADDED_TO_STAGE, init);
setupStage();
initializeStats();
initialize3D();
}
private function initializeStats() : void {
addChild(_awayStats = new AwayStats());
_awayStats.visible = true;
}
protected function tryPositionAwayStats() : void {
if (_awayStats)
_awayStats.x = stage.stageWidth - _awayStats.width;
}
private function setupStage() : void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
private function initialize3D() : void {
_stage3DProxy = Stage3DManager.getInstance(stage).getFreeStage3DProxy(false, Context3DProfile.BASELINE_EXTENDED);
_stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
_stage3DProxy.antiAlias = 8;
_stage3DProxy.color = 0;
_stage3DProxy.addEventListener(flash.events.Event.ENTER_FRAME, onEnterFrame);
_view = new View3D();
_view.stage3DProxy = _stage3DProxy;
_view.shareContext = true;
_view.layeredView = true;
this.addChild(_view);
_awayStats.registerView(_view);
new CameraController(_view.camera, stage);
}
private function onContextCreated(event : Stage3DEvent) : void {
stage.addEventListener(flash.events.Event.RESIZE, onResize);
onResize();
createPlane();
}
private function onEnterFrame(e : flash.events.Event) : void {
_view.render();
}
private function onResize(event : flash.events.Event = null) : void {
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
tryPositionAwayStats();
}
private var _bitmapData : BitmapData;
private var _texture : BitmapTexture;
private var _material : TextureMaterial;
private function createPlane() : void {
_bitmapData = new BitmapData(256, 256, true, 0x33aaaaaa);
_texture = new BitmapTexture(_bitmapData);
_material = new TextureMaterial(_texture);
var geometry : PlaneGeometry = new PlaneGeometry(256, 256);
var mesh : Mesh = new Mesh(geometry, _material);
_view.scene.addChild( mesh);
var timer : Timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, updatePlain);
timer.start();
}
private function updatePlain(e:Event) : void {
trace("updating");
//
var x : uint = int(Math.random() * _bitmapData.width);
var y : uint = int(Math.random() * _bitmapData.height);
var color : uint = (Math.random() * 0xff * 0x1000000) + uint(Math.random() * 0xffffff);
_bitmapData.setPixel32(x, y, color);
_texture.invalidateContent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment