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

3D スプライン軌跡の視野

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

package {
    import flash.geom.ColorTransform;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Utils3D;
    import flash.geom.Vector3D;
    import flash.geom.PerspectiveProjection;
    import flash.geom.Matrix3D;
    import flash.events.Event;
    import flash.display.Shape;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            var bitmatData:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,false,0x0)
            addChild(new Bitmap(bitmatData))
            var shape:Shape = new Shape
            var solid:Object = new Object
            solid.verts = new Vector.<Number>
            solid.verts.push(0,1,0,1,0,0,0,0,1,-1,0,0,0,0,-1,0,-1,0)
            solid.indices = new Vector.<int>
            solid.indices.push(0,1,2,0,2,3,0,3,4,0,4,1,5,2,1,5,3,2,5,4,3,5,1,4)            
            var spline:Function = function(p:Vector.<Vector3D>, t:Number):Vector3D {
                var point:Vector3D = new Vector3D
                for each(var v:String in ["x","y","z"]){
                    point[v] =  0.5*(
                                (2*p[1][v]) +
                                (-p[0][v] + p[2][v]) * t +
                                (2*p[0][v] - 5*p[1][v] + 4*p[2][v] - p[3][v]) * Math.pow(t,2) +
                                (-p[0][v] + 3*p[1][v]- 3*p[2][v] + p[3][v]) * Math.pow(t,3) )
                }
                return point
            }                          
            var radius:int = 20            
            var rabbits:Vector.<Vector3D> = new Vector.<Vector3D>
            for (var i:int=0; i<10; i++) {
                rabbits.push(new Vector3D(
                    radius*(Math.random()-0.5),
                    radius*(Math.random()-0.5),
                    radius*(Math.random()-0.5)
                ))
            }            
            var dog:Vector3D = new Vector3D
            var lastDog:Vector3D = new Vector3D
            var pers:PerspectiveProjection = new PerspectiveProjection
            var persMatrix:Matrix3D = pers.toMatrix3D()
            var worldMatrix:Matrix3D = new Matrix3D
            var solidMatrix:Matrix3D = new Matrix3D            
            var split:int = 100
            var count:int = 1
            addEventListener(Event.ENTER_FRAME, function(e:Event):void {
                lastDog = dog.clone()
                dog = spline(rabbits,count/split)
                count++
                if (count>split) {
                    rabbits.shift()
                    rabbits.push(new Vector3D(
                        radius*(Math.random()-0.5),
                        radius*(Math.random()-0.5),
                        radius*(Math.random()-0.5)
                    ))                    
                    count=1
                }
                var colorTransform:ColorTransform = new ColorTransform(0.95,0.95,0.95)
                bitmatData.colorTransform(bitmatData.rect, colorTransform)
                shape.graphics.clear()
                var sightLine:Vector3D = Vector3D.Z_AXIS                
                var diff:Vector3D = dog.subtract(lastDog)
                var axis:Vector3D = sightLine.crossProduct(diff)
                var theta:Number = -Vector3D.angleBetween(sightLine,diff)*180/Math.PI
                with (worldMatrix) {
                    identity()
                    appendTranslation(-dog.x,-dog.y,-dog.z)                
                    appendRotation(theta,axis)
                    append(persMatrix)
                }           
                var n:int = rabbits.length
                while (n--) {
                    with (solidMatrix) {
                        identity()
                        appendTranslation(rabbits[n].x,rabbits[n].y,rabbits[n].z)
                        append(worldMatrix)
                    }
                    if (solidMatrix.transformVector(new Vector3D(0,0,0)).z > 1) {
                        var persVerts:Vector.<Number> = new Vector.<Number>
                        var uvtData:Vector.<Number> = new Vector.<Number>
                        Utils3D.projectVectors(solidMatrix, solid.verts, persVerts, uvtData)
                        persVerts.map(function(item:Number, index:int, vector:Vector.<Number>):void {
                            vector[index] += (index%2==0) ? stage.stageWidth/2 : stage.stageHeight/2
                        })
                        shape.graphics.lineStyle(0,0x0055aa)
                        shape.graphics.drawTriangles(persVerts, solid.indices, uvtData, 'negative')
                    }
                }
                bitmatData.draw(shape)                
            })                 
        }
    }
}