In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: 追尾カメラ

/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/4X5B
 */

// forked from uwi's 追尾カメラ
package {
    import org.papervision3d.view.BasicView;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import net.hires.debug.Stats;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.core.math.*;
    import org.papervision3d.core.proto.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.materials.special.*;
    import org.papervision3d.objects.special.*;
    import com.bit101.components.*;
    import flash.geom.*;
    
    [SWF(backgroundColor="0x000000", frameRate="60")]
    import flash.accessibility.Accessibility;
    public class PV3D extends BasicView {
        private var _tf : TextField;
        private var _pp : PaperPlane;
        
        private var _XX : Number;
        private var _YY : Number;
        private var _ZZ : Number;
        private var _t : Number;
        
        private var _next : DisplayObject3D;
        private var _ppup : Number3D;
        private var _prevDir : Number3D = null;
        
        private var _cameras : Array;
        
        public function PV3D() {
            super(0, 0, true, false);
            
            graphics.beginFill(0x000000);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            
            scene.addChild(new ParticleField(new ParticleMaterial(0xffff99, 0.8, 0), 3000, 2, 1300, 1300, 1300));
            
            var wm : MaterialObject3D = new WireframeMaterial(0xffffff, 0.5);
            wm.doubleSided = true;
            
            _pp = new PaperPlane(wm);
            scene.addChild(_pp);
            _XX = 1 + Math.random();
            _YY = 1 + Math.random();
            _ZZ = 1 + Math.random();
            _t = Math.random() * 100;
            
            _next = new DisplayObject3D();
            
            // カメラ
            _cameras = [];
            
            var tc : TrackingCamera = new TrackingCamera(_pp, new Number3D(0, 1, 0));
            tc.x = 0; tc.y = 0; tc.z = 0;
            _cameras.push(tc);
            
            var rc : RidingCamera = new RidingCamera(_pp, new Number3D(0, 1, 0));
            _cameras.push(rc);
            
            var sc : OperableSphereCamera = new OperableSphereCamera(
            		new Number3D(0, 0, 0),
            		1000, 
            		new Number3D(0, 0, 1),
            		new Number3D(0, 1, 0),
            		stage,
            		0.005,
            		0.005
            		);
            _cameras.push(sc);
            
            _ppup = null;
            
            _tf = new TextField();
            addChild(_tf);
            _tf.textColor = 0xffffff;
            _tf.width = 100;
            _tf.height = 50;
//            addChild(new Stats());

            _modeCamera = -1;
            changeMode();
            
            startRendering();
            
            var btn : PushButton = new PushButton(this, 365, 0, "change", function(e : MouseEvent) : void { changeMode(); });
        }
        
        private var _modeCamera : int;
        
        private function changeMode() : void
        {
            _modeCamera = (_modeCamera + 1) % _cameras.length;
            _camera = _cameras[_modeCamera];
            _tf.text = ["TrackingCamera", "RidingCamera", "SphereCamera"][_modeCamera];
        }
        
        override protected function onRenderTick(e : Event = null) : void
        {
            _pp.x = 500 * Math.cos(_XX * _t);
            _pp.y = 500 * Math.cos(_YY * _t);
            _pp.z = 500 * Math.cos(_ZZ * _t);
            
            _next.x = 500 * Math.cos(_XX * (_t + 0.01));
            _next.y = 500 * Math.cos(_YY * (_t + 0.01));
            _next.z = 500 * Math.cos(_ZZ * (_t + 0.01));
            
            var curDir : Number3D = Number3D.sub(_next.position, _pp.position);
            curDir.normalize();
            if(_ppup == null){
                var x : Number3D = Number3D.cross(new Number3D(0, 1, 0), curDir);
                x.normalize();
                _ppup = Number3D.cross(x, curDir);
            }
            if(_prevDir != null){
                // 飛行機ヨー用
                var X : Number3D = Number3D.cross(_ppup, _prevDir);
                
                var n : Number3D = Number3D.cross(curDir, _prevDir);
//                if(n.moduloSquared > 0.00000001){
                if(n.moduloSquared != 0){
                    n.normalize();
                    var angle : Number = Math.acos(Number3D.dot(_prevDir, curDir));
                    PV3DUtils.rotate(_ppup, n, angle);
                }
                
                var airup : Number3D = _ppup.clone();
                var dx : Number = Number3D.dot(curDir, X);
                PV3DUtils.rotate(airup, curDir, dx * 10);
                _pp.lookAt(_next, airup);
            }else{
                _pp.lookAt(_next, _ppup);
            }
                
            _prevDir = curDir;
            
            TrackingCamera(_cameras[0]).track();
            
            // 3frameほど調整しないといけない
            if(ttt < 3){
                RidingCamera(_cameras[1])._up = _ppup.clone();
                ttt++;
            }
            RidingCamera(_cameras[1]).move(60, 230);
            
            _t += 0.01;
            super.onRenderTick(e);
        }
        
        private var ttt : uint = 0;
        
        private function tr(...o : Array) : void
        {
            _tf.appendText(o + "\n");
            _tf.scrollV = _tf.maxScrollV;
        }
    }
}

import org.papervision3d.objects.*;
import org.papervision3d.core.math.*;

class PV3DUtils
{
    public static function rotate(x : Number3D, axis : Number3D, angle : Number) : Number3D
    {
        var nCos:Number	= Math.cos(angle);
        var nSin:Number	= Math.sin(angle);
        var scos:Number	= 1 - nCos;

        var sxy	:Number = axis.x * axis.y * scos;
        var syz	:Number = axis.y * axis.z * scos;
        var sxz	:Number = axis.x * axis.z * scos;
        var sz	:Number = nSin * axis.z;
        var sy	:Number = nSin * axis.y;
        var sx	:Number = nSin * axis.x;

        var nx : Number = (nCos + axis.x * axis.x * scos) * x.x + (-sz + sxy) * x.y + (sy + sxz) * x.z;
        var ny : Number = (sz + sxy) * x.x + (nCos + axis.y * axis.y * scos) * x.y + (-sx + syz) * x.z;
        var nz : Number = (-sy + sxz) * x.x + (sx + syz) * x.y + (nCos + axis.z * axis.z * scos) * x.z;

        x.x = nx; x.y = ny; x.z = nz;
        return x;
    }
    
    public static function rotateMulti(xs : Array, axis : Number3D, angle : Number) : void
    {
        var nCos:Number	= Math.cos(angle);
        var nSin:Number	= Math.sin(angle);
        var scos:Number	= 1 - nCos;

        var sxy	:Number = axis.x * axis.y * scos;
        var syz	:Number = axis.y * axis.z * scos;
        var sxz	:Number = axis.x * axis.z * scos;
        var sz	:Number = nSin * axis.z;
        var sy	:Number = nSin * axis.y;
        var sx	:Number = nSin * axis.x;

        for each(var x : Number3D in xs){
            var nx : Number = (nCos + axis.x * axis.x * scos) * x.x + (-sz + sxy) * x.y + (sy + sxz) * x.z;
            var ny : Number = (sz + sxy) * x.x + (nCos + axis.y * axis.y * scos) * x.y + (-sx + syz) * x.z;
            var nz : Number = (-sy + sxz) * x.x + (sx + syz) * x.y + (nCos + axis.z * axis.z * scos) * x.z;
	        x.x = nx; x.y = ny; x.z = nz;
        }
    }
    
    public static function setLookAt(d : DisplayObject3D, front : Number3D, up : Number3D) : void
    {
        var X : Number3D = Number3D.cross(front, up);
        var look : Matrix3D = d.transform;
        look.n11 = X.x; look.n21 = X.y; look.n31 = X.z;
        look.n12 = -up.x; look.n22 = -up.y; look.n32 = -up.z;
        look.n13 = front.x; look.n23 = front.y; look.n33 = front.z;
    }    
}

import org.papervision3d.cameras.*;

class QCamera3D extends Camera3D
{
    public var _up : Number3D; // カメラの上の向きの単位ベクトル
    protected var _front : Number3D;
    private var _prevDir : Number3D;
    
    public function QCamera3D(up : Number3D, front : Number3D= null)
    {
        super();
        _up = null;
        init(up, front);
    }
    
    // prevDirからcurDirへ向ける回転を_upにかけるだけ。カメラ自体に操作はしない
    public function rotate(curDir : Number3D) : void
    {
        if(_prevDir != null){
            var n : Number3D = Number3D.cross(curDir, _prevDir);
//            if(n.moduloSquared > 0.00000001){
            if(n.moduloSquared != 0){
                n.normalize();
                var angle : Number = Math.acos(Number3D.dot(_prevDir, curDir));
                if(_front != null){
                		PV3DUtils.rotate(_front, n, angle);
                }
           		PV3DUtils.rotate(_up, n, angle);
            }
        }
        _prevDir = curDir.clone();
    }
    
    // カメラを_frontのほうへ向ける
    public function head() : void
    {
        if(_front != null){
        		var Z : Number3D = _front.clone();
            Z.normalize();
            var X : Number3D = Number3D.cross(Z, _up);
            X.normalize();
            var Y : Number3D = Number3D.cross(Z, X);
            Y.normalize();

            var look : Matrix3D = this.transform;
            look.n11 = X.x*this.scaleX; look.n21 = X.y*this.scaleY; look.n31 = X.z*this.scaleZ;
            look.n12 = -Y.x*this.scaleX; look.n22 = -Y.y*this.scaleY; look.n32 = -Y.z*this.scaleZ;
            look.n13 = Z.x*this.scaleX; look.n23 = Z.y*this.scaleY; look.n33 = Z.z*this.scaleZ;
        }
    }
    
    public function init(up : Number3D = null, front : Number3D = null) : void
    {
        if(up != null){
            _up = up.clone();
            _up.normalize();
        }
        if(front != null){
            _front = front.clone();
            _front.normalize();
        }else{
            _front = null;
        }
        _prevDir = null;
    }
}

// 球面上を動き、球の中心を見るカメラ
class SphereCamera extends QCamera3D
{
    private var _O : Number3D; // 球の中心
    private var _R : Number; // 球の半径
    
    // O : 中心座標
    // R : 半径
    // front : カメラ正面の向きを表す単位ベクトル
    // up : カメラ上の向きを表す単位ベクトル
    public function SphereCamera(O : Number3D, R : Number, front : Number3D, up : Number3D) : void
    {
        super(up, front);
        _O = O;
        _R = R;
        move();
    }
    
    public function move(x : Number = 0, y : Number = 0) : void
    {
        if(x != 0 || y != 0){
        		var X : Number3D = Number3D.cross(_up, _front);
        		var axis : Number3D = new Number3D(X.x * -y + _up.x * x, X.y * -y + _up.y * x, X.z * -y + _up.z * x);
	          var angle : Number = axis.modulo;
          axis.normalize();
          PV3DUtils.rotate(_front, axis, angle);
          PV3DUtils.rotate(_up, axis, angle);
	        this.x = _front.x * -_R + _O.x;
	        this.y = _front.y * -_R + _O.y;
	        this.z = _front.z * -_R + _O.z;
	        head();
        }
    }
}

import flash.display.*;
import flash.events.*;

class OperableSphereCamera extends SphereCamera
{
    private var _dx : Number;
    private var _dy : Number;
    private var _stage : DisplayObject;
    private var _prevX : Number = -1;
    private var _prevY : Number = -1;
    private var _down : Boolean = false;

    // O : 中心座標
    // R : 半径
    // front : カメラ正面の向きを表す単位ベクトル
    // up : カメラ上の向きを表す単位ベクトル
    // stage : マウスイベントをlistenするDisplayObject
    // dx : カメラ横への移動角度単位
    // dy : カメラ上への移動角度単位
    public function OperableSphereCamera(O : Number3D, R : Number, front : Number3D, up : Number3D, stage : DisplayObject, dx : Number, dy : Number) : void
    {
      super(O, R, front, up);
      _dx = dx;
      _dy = dy;
      _stage = stage;
      addCallback();
    }
    
    public function addCallback() : void
    {
      _stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
      _stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
      _stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    public function removeCallback() : void
    {
      _stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
      _stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
      _stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    private function onMouseDown(e : MouseEvent) : void { _down = true; }
    private function onMouseUp(e : MouseEvent) : void { _down = false; }
    
    private function onEnterFrame(e : Event) : void
    {
      if(_down){
        if(_prevX != -1){
          this.move((_stage.mouseX - _prevX) * _dx, (_stage.mouseY - _prevY) * _dy);
        }
        _prevX = _stage.mouseX;
        _prevY = _stage.mouseY;
      }else{
        _prevX = -1;
        _prevY = -1;
      }
    }
}

// 固定視点から継続的にターゲッティングするカメラ
class TrackingCamera extends QCamera3D
{
    private var _targ : DisplayObject3D;
    
    // targ : 目標
    // up : カメラ上の方向の単位ベクトル
    public function TrackingCamera(targ : DisplayObject3D, up : Number3D)
    {
        super(up);
        _targ = targ;
    }
    
    // 現在の向きから、対象の方向にカメラを向ける
    public function track() : void
    {
        var curDir : Number3D = Number3D.sub(_targ.position, this.position);
        curDir.normalize();
        rotate(curDir);
        this.lookAt(_targ, _up);
    }
}

// ゲットライド!
class RidingCamera extends QCamera3D
{
    private var _ride : DisplayObject3D;
    private var _prevPos : Number3D;
    
    	// ride : 対象
    // up : カメラ上の方向の単位ベクトル
    // front : カメラ正面の方向の単位ベクトル
    public function RidingCamera(ride : DisplayObject3D, up : Number3D, front : Number3D = null)
    {
        super(up, front);
        _ride = ride;
        _prevPos = _ride.position.clone();
    }

    // 対象の後ろ側にback, 上側にup進んだところにカメラを置き、対象の進行方向を見る。
    public function move(up : Number = 0, back : Number = 0) : void
    {
        var curDir : Number3D = new Number3D(_ride.x - _prevPos.x, _ride.y - _prevPos.y, _ride.z - _prevPos.z);
        if(curDir.moduloSquared < 0.0001){
          return;
        }
        
        curDir.normalize();
        if(_front == null){
            _front = curDir.clone();
        }
        rotate(curDir);
        
        var curPos : Number3D = _ride.position.clone();
        var temp : Number3D;
        temp = _up.clone();
        temp.multiplyEq(up);
        curPos.plusEq(temp);
        temp = _front.clone();
        temp.multiplyEq(-back);
        curPos.plusEq(temp);
        this.position = curPos;
        
        head();
        _prevPos.copyFrom(_ride.position);
    }
}