forked from: FollowCamera - Alternativa3D
/**
* Copyright Pelisalinet ( http://wonderfl.net/user/Pelisalinet )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cBXr
*/
// forked from Pelisalinet's FollowCamera - Alternativa3D
package {
import flash.text.engine.TextLine;
import flash.display.Loader;
import alternativ5.engine3d.core.Object3D;
import alternativ5.engine3d.loaders.*;
import alternativ5.engine3d.core.Mesh;
import alternativ5.engine3d.materials.TextureMaterialPrecision;
import alternativ5.engine3d.primitives.Box;
import alternativ5.engine3d.primitives.Sphere;
import alternativ5.engine3d.primitives.Plane;
import alternativ5.engine3d.materials.FillMaterial;
import alternativ5.engine3d.materials.WireMaterial;
import alternativ5.engine3d.events.MouseEvent3D
import alternativ5.types.Point3D;
import alternativ5.utils.*;
import alternativ7.engine3d.controllers.SimpleObjectController;
import flash.display.InteractiveObject;
import flash.events.KeyboardEvent;
import flash.geom.Vector3D;
import flash.display.Sprite;
import flash.display.BlendMode;
import flash.utils.describeType;
import flash.events.*;
[SWF(width = 1200, height = 700, frameRate = 60, backgroundColor=0x00000000)]
public class SimpleDemo extends Sprite {
public function SimpleDemo():void {
// テンプレートを作成します
var template:BasicTemplate = new BasicTemplate();
addChild(template);
var nextRot:int = 0;
var nextPos:int = 0;
for(var i:int=0; i < 50; i++){
var starf2:Plane=new Plane(20,150);
starf2.cloneMaterialToAllSurfaces(new FillMaterial(10010010));
starf2.y -= i*(150/2);
starf2.z = 0;
starf2.x = 0;
if(nextRot){
starf2.rotationZ += 80;
starf2.y -= 220;
starf2.x -= 150/2;
nextRot = int(false);
}else{
if(nextPos){
starf2.x -= 250/2;
nextPos = int(false);
}else{
nextPos = int(true);
}
nextRot = int(true);
}
template.scene.root.addChild(starf2);
}
function rando(low:Number=NaN, high:Number=NaN):Number
{
var low:Number = low;
var high:Number = high;
if(isNaN(low))
{
throw new Error("low must be defined");
}
if(isNaN(high))
{
throw new Error("high must be defined");
}
return Math.round(Math.random() * (high - low)) + low;
}
var cube:Box=new Box(10,10,10);
cube.cloneMaterialToAllSurfaces(new FillMaterial(00010100));
cube.z = 5;
template.scene.root.addChild(cube);
var loader:Loader3DS = new Loader3DS();
loader.smooth = true;
loader.precision = TextureMaterialPrecision.HIGH;
loader.load("http://www.pelisali.net/MAXvirtual/house.3ds");
loader.addEventListener(Event.COMPLETE, onLoadingComplete);
function onLoadingComplete(e:Event):void
{
for (var o:* in loader.content.children)
{
var object:Object3D = o;
template.scene.root.addChild(object);
}
}
//var material:FillMaterial = new FillMaterial(0x666666, 1, 0,0x0);
/*var plane:Plane = new Plane(2000, 2000, 4, 4);
//plane.setMaterialToAllFaces(material);
plane.cloneMaterialToAllSurfaces(new FillMaterial(00010000));
template.scene.root.addChild(plane);*/
// SpringCameraController
var cameraContoller:SpringCameraController=new SpringCameraController(this,template.camera);
cameraContoller.target=cube;
template.camera.fov=100*Math.PI/180
cameraContoller.mass = 30;
cameraContoller.damping = 30;
cameraContoller.stiffness = 1;
cameraContoller.positionOffset = new Point3D(0, 70, 0);
cameraContoller.lookOffset = new Point3D(0, 0, 0);
var rot:Number = 0;
var isKey:int;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onEnterRend);
function onEnterRend(k : KeyboardEvent):void{
isKey = k.keyCode;
}
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyOutRend);
function onKeyOutRend(k : KeyboardEvent):void{
isKey = 0;
}
var _speed:int = 5;
template.onPreRender = function():void {
rot += 0.15;
if(isKey == 38){
cube.y += _speed;
}
if(isKey == 40){
cube.y -= _speed;
}
if(isKey == 37){
cube.x -= _speed;
}
if(isKey == 39){
cube.x += _speed;
}
cameraContoller.move();
}
}
}
}
import flash.display.DisplayObject;
import alternativ5.engine3d.controllers.WalkController;
import alternativ5.types.Matrix3D;
import alternativ5.types.Point3D;
import flash.utils.*;
//-------------------------------------------------------------------
//SpringCameraController
//-------------------------------------------------------------------
class SpringCameraController extends WalkController{
private var _camTarget:Object3D
private var _zrot:Number = 0;
public var mass:Number = 40;
public var damping:Number = 4;
public var stiffness:Number = 1;
public var positionOffset:Point3D = new Point3D(0, -50, 5);
public var lookOffset:Point3D = new Point3D(0, 10, 2);
public function SpringCameraController(eventSourceObject:DisplayObject, object:Object3D = null) {
super(eventSourceObject,object)
}
public function set target(t:Object3D):void {
_camTarget=t
}
public function get target():Object3D {
return _camTarget;
}
public function set zrot(n:Number):void {
_zrot = n;
if(_zrot < 0.001) n = 0;
}
public function get zrot():Number {
return _zrot;
}
private var _velocity:Point3D = new Point3D();
private var _dv:Point3D = new Point3D();
private var _stretch:Point3D = new Point3D();
private var _force:Point3D = new Point3D();
private var _acceleration:Point3D = new Point3D();
private var _desiredPosition:Point3D = new Point3D();
private var _lookAtPosition:Point3D = new Point3D();
private var _xPosition:Point3D = new Point3D();
public function move():void {
if(_camTarget != null) {
_desiredPosition = _camTarget.coords.clone();
_desiredPosition.add(positionOffset.clone())
_stretch.x = (coords.x - _desiredPosition.x) * -stiffness;
_stretch.y = (coords.y - _desiredPosition.y) * -stiffness;
_stretch.z = (coords.z - _desiredPosition.z) * -stiffness;
_dv.x = _velocity.x * damping;
_dv.y = _velocity.y * damping;
_dv.z = _velocity.z * damping;
_force.x = _stretch.x - _dv.x;
_force.y = _stretch.y - _dv.y;
_force.z = _stretch.z - _dv.z;
_acceleration.x = _force.x * (1 / mass);
_acceleration.y = _force.y * (1 / mass);
_acceleration.z = _force.z * (1 / mass);
_velocity.add(_acceleration);
_xPosition=coords.clone()
_xPosition.add(_velocity)
coords=_xPosition
_lookAtPosition=_camTarget.coords.clone()
_lookAtPosition.add(lookOffset.clone())
lookAt(_lookAtPosition);
if(Math.abs(_zrot) > 0) object.rotationY = _zrot;
}
}
}
import alternativ5.engine3d.controllers.CameraController;
import alternativ5.engine3d.core.Camera3D;
import alternativ5.engine3d.core.Object3D;
import alternativ5.engine3d.core.Scene3D;
import alternativ5.engine3d.display.View;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
class BasicTemplate extends Sprite{
public var scene:Scene3D;
public var view:View;
public var camera:Camera3D;
public var cameraController:CameraController;
private var _viewWidth:int;
private var _viewHeight:int;
private var _scaleToStage:Boolean;
public function BasicTemplate(viewWidth:int=640, viewHeight:int=480, scaleToStage:Boolean = true) {
_viewWidth = viewWidth;
_viewHeight = viewHeight;
_scaleToStage = scaleToStage;
// Creating scene
scene = new Scene3D();
scene.splitAnalysis = false; // not analysis for performance
scene.root = new Object3D();
// Adding camera
camera = new Camera3D();
camera.z = -1000;
scene.root.addChild(camera);
// camera contoller
cameraController = new CameraController(this);
cameraController.camera = camera;
// set view
view = new View();
view.camera = camera;
addChild(view);
// stage
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
protected function atInit():void {}
private var _onInit:Function = function():void { };
public function get onInit():Function { return _onInit; }
public function set onInit(value:Function):void {
_onInit = value;
}
protected function atPreRender():void {}
private var _onPreRender:Function = function():void{};
public function get onPreRender():Function { return _onPreRender; }
public function set onPreRender(value:Function):void {
_onPreRender = value;
}
protected function atPostRender():void {
}
protected var _onPostRender:Function = function():void{};
public function get onPostRender():Function { return _onPostRender; }
public function set onPostRender(value:Function):void {
_onPostRender = value;
}
public function startRendering():void {
addEventListener(Event.ENTER_FRAME, onRenderTick);
}
public function stopRendering():void {
removeEventListener(Event.ENTER_FRAME, onRenderTick);
}
public function singleRender():void {
onRenderTick();
}
/**
* @private
*/
private function init(e:Event = null):void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.HIGH;
// resize
stage.addEventListener(Event.RESIZE, onResize);
onResize(null);
// render
startRendering();
atInit();
_onInit();
}
/**
* @private
*/
private function onRenderTick(e:Event = null):void {
atPreRender();
_onPreRender();
scene.calculate();
atPostRender();
_onPostRender();
}
/**
* @private
*/
private function onResize(event:Event = null):void {
if (_scaleToStage) {
view.width = stage.stageWidth;
view.height = stage.stageHeight;
view.graphics.clear()
view.graphics.beginFill(0x000000)
view.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight)
}else {
view.width = _viewWidth;
view.height = _viewHeight;
view.graphics.clear();
view.graphics.beginFill(0x000000)
view.graphics.drawRect(0,0,_viewWidth,_viewHeight)
}
}
}