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

Ribbon roadの焼き直し

なんでだんだん重くなっていくのかは不明
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dAps
 */

// なんでだんだん重くなっていくのかは不明
package {
    import flash.display.Sprite;
    import flash.text.*;
    import flash.events.*;
    import org.papervision3d.view.*;
    import org.papervision3d.scenes.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.materials.special.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.render.*;
    import org.papervision3d.core.math.*;
    import org.papervision3d.core.geom.*;
    import org.papervision3d.core.geom.renderables.*;
    import org.libspark.betweenas3.*;
    import org.libspark.betweenas3.tweens.*;
    import org.libspark.betweenas3.events.*;
    import org.libspark.betweenas3.easing.*;
    
    [SWF(frameRate=60)]
    public class FlashTest extends BasicView {
        private var _cm : ColorMaterial;
        
        private var _lines : Lines3D;
        
        private var _tf : TextField;
        
        private var _p : Array;
        private var _cor : Array;
        
        private var _t : ITween;
        private var _tlap : Object;
        
        public function FlashTest() {
            super(0, 0, true, false, CameraType.FREE);
            
            _tf = new TextField();
            _tf.height = 465;
            _tf.width = 465;
            addChild(_tf);
            
            init();
            init3D();
            
            _tlap = {};
            _cor = [{},{},{},{}];
            onComplete();
            startRendering();
        }
        
        private function onComplete() : void
        {
            for each(var k : String in ["x", "y", "z"]){
                _cor[3][k] = (-_p[0][k] + 3 * _p[1][k] - 3 * _p[2][k] + _p[3][k]) / 6;
                _cor[2][k] = (_p[0][k] - 2 * _p[1][k] + _p[2][k]) / 2;
                _cor[1][k] = (-_p[0][k] + _p[2][k]) / 2;
                _cor[0][k] = (_p[0][k] + 4 * _p[1][k] + _p[2][k]) / 6;
            }
            
            _p.shift();
            var theta : Number = Math.random() * Math.PI * 2;
            var phi : Number = Math.random() * Math.PI * 2;
            _p.push({
                x : Math.cos(theta) * Math.sin(phi) * 500,
                y : Math.sin(theta) * Math.sin(phi) * 500,
                z : Math.cos(phi) * 500
            });
            
            _t = BetweenAS3.tween(_tlap, {t : 1.0}, {t : 0.0}, 0.7);
            _t.onComplete = onComplete;
            _t.play();
        }
        
        private var _vs : Array;
        private var E : int = 300;
        private var _front : Number3D = new Number3D(0, 0, 1);
        private var _X : Number3D = new Number3D(1, 0, 0);
        private var _Y : Number3D = new Number3D(0, 1, 0);
        private var _ys : Array;
        
        override protected function onRenderTick(e : Event = null) : void
        {
            var t : Number = _tlap.t;
            var curPos : Number3D = new Number3D();
            for each(var k : String in ["x", "y", "z"]){
                curPos[k] = ((((_cor[3][k] * t) + _cor[2][k]) * t) + _cor[1][k]) * t + _cor[0][k];
            }
            
            var prevPos : Number3D = _vs[_vs.length - 1];
            var curFront : Number3D = Number3D.sub(curPos, prevPos);
            curFront.normalize();
            
            var curX : Number3D;
            if(Number3D.dot(_front, curFront) < 0.99999){
                var n : Number3D = Number3D.cross(_front, curFront);
                n.normalize();
                var angle : Number = Math.acos(Number3D.dot(_front, curFront));
                var q : Quaternion = Quaternion.createFromAxisAngle(n.x, n.y, n.z, angle);
                var qc : Quaternion = Quaternion.conjugate(q);
            
                _front = curFront;
//                _front = applyQuaternion(_front, q, qc);
                curX = applyQuaternion(_X, q, qc);
                _Y = applyQuaternion(_Y, q, qc);
            }else{
                curX = _X;
            }
            
            var W : Number = 10;
            _lines.addNewLine(2, 
                prevPos.x + _X.x * W, prevPos.y + _X.y * W, prevPos.z + _X.z * W,
                curPos.x + curX.x * W, curPos.y + curX.y * W, curPos.z + curX.z * W
                );
            _lines.addNewLine(2, 
                prevPos.x - _X.x * W, prevPos.y - _X.y * W, prevPos.z - _X.z * W,
                curPos.x - curX.x * W, curPos.y - curX.y * W, curPos.z - curX.z * W
                );
            _lines.addNewLine(2,
                curPos.x - curX.x * W, curPos.y - curX.y * W, curPos.z - curX.z * W,
                curPos.x + curX.x * W, curPos.y + curX.y * W, curPos.z + curX.z * W
                );
            _X = curX;
                
            if(_lines.lines.length >= (E + 5) * 3){
                _lines.lines.shift(); 
                _lines.lines.shift();
                _lines.lines.shift();
            }
            
            _vs.push(curPos.clone());
            _ys.push(_Y.clone());
            if(_vs.length >= E){
                _tf.text = "GO!";
                rotCamera();
            }else{
                _tf.text = "Under construction.. " + int(_vs.length / E * 100) + "%";
            }
            
            super.onRenderTick(e);
        }
        
        private var _frontCamera : Number3D = new Number3D(0, 0, 1);
        
        private function rotCamera() : void
        {
            var pos : Number3D = _vs.shift();
            var Y : Number3D = _ys.shift(); // Yだけはレールからとってこないとだんだんずれてくる
            camera.x = pos.x;
            camera.y = pos.y;
            camera.z = pos.z;
            var curFront : Number3D = Number3D.sub(_vs[5], pos);
            curFront.normalize();
            
            if(Number3D.dot(_frontCamera, curFront) < 0.99999){
                var n : Number3D = Number3D.cross(_frontCamera, curFront);
                n.normalize();
                var angle : Number = Math.acos(Number3D.dot(_frontCamera, curFront));
                var q : Quaternion = Quaternion.createFromAxisAngle(n.x, n.y, n.z, angle);
                var qc : Quaternion = Quaternion.conjugate(q);
            
                // 視線とカメラの上のベクトルを回転
                _frontCamera = applyQuaternion(_frontCamera, q, qc);
            }
            
                var xCamera : Number3D = Number3D.cross(_frontCamera, Y);
                xCamera.normalize();
            
                // 行列をつくってカメラに適用
                var look : Matrix3D = camera.transform;
                look.n11 = xCamera.x * camera.scaleX;
                look.n21 = xCamera.y * camera.scaleX;
                look.n31 = xCamera.z * camera.scaleX;
                look.n12 = -Y.x * camera.scaleY;
                look.n22 = -Y.y * camera.scaleY;
                look.n32 = -Y.z * camera.scaleY;
                look.n13 = _frontCamera.x * camera.scaleZ;
                look.n23 = _frontCamera.y * camera.scaleZ;
                look.n33 = _frontCamera.z * camera.scaleZ;
                camera.copyTransform(look);
                camera.moveUp(10);
                camera.moveBackward(5); 
        }
        
        // クォータニオンのベクトルへの適用
        private function applyQuaternion(src : Number3D, q : Quaternion, qc : Quaternion) : Number3D
        {
            var qSrc : Quaternion = new Quaternion(src.x, src.y, src.z, 0);
            var qDst : Quaternion = Quaternion.multiply(qc, qSrc);
            qDst.mult(q);
            return new Number3D(qDst.x, qDst.y, qDst.z);
        }
        
        private function init3D() : void
        {
            var dm : DisplayObject3D = new DisplayObject3D();
            dm.x = 0;
            dm.y = 0;
            dm.z = 0;
            
            _vs = [Number3D.ZERO, Number3D.ZERO];
            _ys = [new Number3D(0, 1, 0), new Number3D(0, 1, 0)];
            _lines = new Lines3D(new LineMaterial(0x333377));
            scene.addChild(_lines);
            
            camera.x = 0;
            camera.y = 0;
            camera.z = 0;
            var cd : Object = _p[3];
            dm.x = cd.x;
            dm.y = cd.y;
            dm.z = cd.z;
            camera.lookAt(dm);
            camera.moveUp(10); 
            camera.moveBackward(5); 
           
            _cm = new ColorMaterial(0);
        }
        
        private function init() : void
        {
            _p = [];
            
            _p.push({x : 0, y : 0, z : 0});
            _p.push({x : 0, y : 0, z : 0});
            _p.push({x : 0, y : 0, z : 0});
            for(var i : int = 1;i < 2;i++){
                var theta : Number = Math.random() * Math.PI * 2;
                var phi : Number = Math.random() * Math.PI * 2;
                _p.push({
                    x : Math.cos(theta) * Math.sin(phi) * 1000,
                    y : Math.sin(theta) * Math.sin(phi) * 1000,
                    z : Math.cos(phi) * 1000
                });
            }
            
        }
    }
}