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

frocessing IsoPyramid3D

Base model Icosahedron (to check) for frocessing
(class IsoPyramid3D)
using IsoTriangle3Ds for faceSet
seems to be ok but Mathematics seems not so sharp to me
(I'm forced to have this ' - 1' somewhere around line 228 to correct the sticking of the shapes together).
Help welcomed 8
Get Adobe Flash player
by sazaam 16 Mar 2011
    Embed
// sorry for the lack of structure in all this   - ; -

package 
{
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
    import frocessing.display.F5MovieClip3DBmp;
    import frocessing.f3d.models.F3DPlane;
    import frocessing.geom.FNumber3D;
    import gs.easing.Quad;
    import gs.TweenLite;
    
    /**
     * ...
     * @author saz
     */
    [SWF(frameRate=24, width=465, height=465, backgroundColor=0x2a2a2a)]
    public class TestIsoPyramid3D extends F5MovieClip3DBmp 
    {
        
        private const BMP:String = "http://assets.wonderfl.net/images/related_images/5/50/5076/507626853fa40bc565596061e9b5f0b0eddde148" ;
        
        private var stage_width:Number  = 465;
        private var stage_height:Number = 465;
        private var pyramids:Vector.<IsoPyramid3D>;
        private var __moving:Boolean;
        private var __viewCoords3D:FNumber3D = new FNumber3D(0,0,0) ;
        private var __angle:Number = 0 ;
        private var __coordsMouse:Point;
        private var __coordsNewMouse:Point;
        
        public function TestIsoPyramid3D() 
        {
            super() ;
            stage.scaleMode = 'noScale' ;
            trace('instanciated')
        }
         private function onLoadComplete(e:Event):void{
            e.target.removeEventListener(e.type, arguments.callee) ;
            e.target.content.smoothing = true ;
            var tex:BitmapData = e.target.content.bitmapData ;
            
            setTexture(tex) ;
        }
         private function setTexture(tex:BitmapData):void {
            var front:BitmapData = tex.clone() ;
            var invertedFront:BitmapData = new DirectionBitmapData(front, DirectionBitmapData.X_AXIS_INVERTED) ;
            var reflect:BitmapData = new Reflect(invertedFront, false) ;
            var l:int = pyramids.length ;
            for (var i:int = 0 ; i < l ; i++ ) {
                var t:IsoPyramid3D = pyramids[i] ;
                t.setTexture(front) ;
                t.userData.reflect.setTexture(reflect) ;
            }
        }
        public function setup():void 
        {
            initPyramids() ;
             
            size(stage_width, stage_height) ;
            colorMode(RGB, 255, 255, 255, 1) ;
            background(0,0,0,0) ;
            noStroke() ;
            noFill() ;
            
            var l:Loader = new Loader;
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete) ;
            l.load(new URLRequest(BMP), new LoaderContext(true)) ;
            
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageDown) ;
            stage.addEventListener(MouseEvent.MOUSE_UP, onStageDown) ;
            stage.addEventListener(MouseEvent.MOUSE_WHEEL, onStageWheel) ;
            
            tweenPyramids(1) ;
        }
        
        private function onStageWheel(e:MouseEvent):void 
        {
            tweenPyramids(e.delta / 3) ;
        }
        
        private function tweenPyramids(n:int):void 
        {
            var l:int = pyramids.length ;
            for (var i:int = 0 ; i < l ; i++ ) {
                var t:IsoPyramid3D = pyramids[i] ;
                TweenLite.killTweensOf(t) ;
                TweenLite.killTweensOf(t.userData.reflect) ;
                
                var d:Number = __angle + radians(n * 120) ;
                TweenLite.to(t, .5, { ease:Quad.easeOut, rotationY: d } ) ;
                TweenLite.to(t.userData.reflect, .5, { ease:Quad.easeOut, rotationY: -d +Math.PI } ) ;
                trace(d)
                trace(-d + Math.PI)
            }
            __angle = d ;
        }
        
        private function onStageDown(e:MouseEvent):void 
        {
            if (e.type == MouseEvent.MOUSE_DOWN) {
                __coordsMouse = new Point(e.stageX, e.stageY) ;
                __moving = true ;
            }else {
                __moving = false ;
            }
        }
        
        public function draw():void
        {
            background(0, 0, 0, 0) ;
            pushMatrix();
            __coordsNewMouse = new Point(stage.mouseX, stage.mouseY) ;
            
            translate(stage_width / 2, stage_height / 2) ;
            if (__moving && !__coordsNewMouse.equals(__coordsMouse)) {
                var degX:Number = (__coordsMouse.x -__coordsNewMouse.x) / stage.stageWidth * 180  ;
                var angleY:Number = (degX / 180 * Math.PI) ;
                __viewCoords3D.y += angleY ;
                
                var degY:Number = (__coordsMouse.y - __coordsNewMouse.y) / stage.stageHeight * 180  ;
                var angleX:Number = (degY / 180 * Math.PI) ;
                __viewCoords3D.x += angleX ;
                __coordsMouse = __coordsNewMouse ;
            }
            rotateY(__viewCoords3D.y) ;
            rotateX(__viewCoords3D.x) ;
            renderPyramids() ;
            
            
            
            
            //renderCenterPlane() ;
            popMatrix() ;
        }
        
        private function renderCenterPlane():void 
        {
            var p:F3DPlane = new F3DPlane(30, 30) ;
            p.setColor(0xFFFFFF) ;
            model(p) ;
        }

        public function renderPyramids():void
        {
            var l:int = pyramids.length ;

            for (var i:int = 0 ; i < l ; i++ ) {
                var t3:IsoPyramid3D = pyramids[i] ;
                model(t3) ;
                model(t3.userData.reflect) ;
            }
        }
        public function initPyramids():void
        {
            var rad:Number = 200 ;
            const TOTAL:Number = 3 ;
            pyramids = new Vector.<IsoPyramid3D>() ;
            
            for (var i:int = 0 ;  i < TOTAL ; i++ ) {
                var p3:IsoPyramid3D = new IsoPyramid3D(100, 1) ;
                
                var r3:IsoPyramid3D = new IsoPyramid3D(100, 1) ;
                r3.y = 65 ;
                r3.yow(radians(180)) ;
                
                p3.userData.reflect = r3 ;
                
                var baseAngle:Number = 90 ;
                p3.x = Math.cos(radians(baseAngle + (360 / TOTAL*i))) * rad ;
                p3.z = Math.sin(radians(baseAngle + (360 / TOTAL*i))) * rad/2 ;
                
                r3.x = Math.cos(radians(baseAngle + (360 / TOTAL*i))) * rad ;
                r3.z = Math.sin(radians(baseAngle + (360 / TOTAL*i))) * rad/2 ;
                
                pyramids.push(p3) ;
            }
        }
    }

}

import flash.display.BitmapData;
import flash.geom.Matrix;
import frocessing.core.canvas.ICanvas3D;
import frocessing.f3d.*;
import frocessing.math.FMath;

/**
 * ...
 * @author saz
 */

class IsoPyramid3D extends F3DModel 
{
    public var front:IsoTriangle3D;
    public var bottom:IsoTriangle3D;
    public var left:IsoTriangle3D;
    public var right:IsoTriangle3D;
    
    public function IsoPyramid3D(size:Number = 50, segment:int = 2) 
    {
        super();
        backFaceCulling = false;
        
        initModel( size , segment);
    }
    
    /** @private */
    private function initModel( size:Number, seg:int):void
    {
        var numSize:int = 3 ;
        
        var deg:Number = 180 / numSize / Math.PI ;
        var percent:Number = deg / 90 ;
        var angleRoll:Number = deg /180 * Math.PI ;
        var newSize:Number = size - (size * percent) ;
        var midSize:Number = newSize >> 1 ;
        var ht:Number = int(Math.sqrt((newSize * newSize) - (midSize * midSize))) ;
        var g:Number = 2 / 3 ;
        var remain:Number = 1 / 3 ;
        var rayonInnerDisk:Number = (size * g)/2 ;
        var rayonBase:Number = (size / 4 ) -1 ;
        var baseAngle:Number = 90 ;
        var unitAngle:Number = 360 / 3 ;
        
        var frontZ:Number = rayonBase * (1-percent) ;
        var rightX:Number = Math.cos(FMath.radians(baseAngle + (unitAngle))) * rayonBase * (1-percent);
        var rightZ:Number = Math.sin(FMath.radians(baseAngle + (unitAngle))) * rayonBase * (1-percent);
        var leftX:Number = Math.cos(FMath.radians(baseAngle + (360 - unitAngle))) * rayonBase * (1-percent);
        var leftZ:Number = Math.sin(FMath.radians(baseAngle + (360 - unitAngle))) * rayonBase * (1-percent);
        
        front = new IsoTriangle3D( size, seg );
        bottom = new IsoTriangle3D( size, seg );
        left = new IsoTriangle3D( size, seg );
        right = new IsoTriangle3D( size, seg );
        front.backFaceCulling = true;
        bottom.backFaceCulling = true;
        left.backFaceCulling = true;
        right.backFaceCulling = true;
        
        front.pitch(FMath.radians(180)) ;
        front.roll(-angleRoll) ;
        front.translate( 0, 0, frontZ);
        
        // yaw  = Z rotate
        // roll  = X rotate
        // pitch  = Y rotate
        
        left.pitch(FMath.radians(60)) ;
        left.roll(-angleRoll) ;
        left.translate( -leftX, 0, leftZ ) ;
         right.pitch(FMath.radians(-60)) ;
        right.roll(-angleRoll) ;
        right.translate( -rightX, 0, rightZ ) ;
        bottom.roll( Math.PI / 2 ) ;
        bottom.translate( 0, (rayonInnerDisk * (1-percent)), 0) ;
    }
    
    
    //--------------------------------------------------------------------------------------------------- MATERIAL
    /**
     * 
     */
    public function setColors( front:uint, right:uint, left:uint, bottom:uint , alpha:Number = 1):void
    {
        this.front.setColor( front , alpha) ;
        this.bottom.setColor( bottom , alpha) ;
        this.left.setColor( left , alpha) ;
        this.right.setColor( right , alpha) ;
    }
    
    override public function setColor(color:uint, alpha:Number = 1):void
    {
        setColors(color, color, color, color, alpha) ;
    }
    /**
     * 
     */
    public function setTextures( front:BitmapData, right:BitmapData, left:BitmapData, bottom:BitmapData = null):void
    {
        this.front.setTexture( invertXTexture(front)) ;
        this.left.setTexture(invertXTexture(left)) ;
        this.right.setTexture(invertXTexture(right)) ;
        if (!Boolean(bottom as BitmapData)) {
            this.bottom.setColor(0x0) ;
        }else {
            this.bottom.setTexture( invertXTexture(bottom)) ;
        }
    }
    
    private function invertXTexture(tex:BitmapData):BitmapData
    {
        var temp:BitmapData = new BitmapData(tex.width, tex.height, true, 0x0) ;
        temp.draw(tex, new Matrix( -1, 0, 0, 1, tex.width, 0), null, null, null, true) ;
        return temp ;
    }
    
    override public function setTexture( front:BitmapData, bottom:BitmapData = null):void
    {
        setTextures(front, front, front, bottom) ;
    }
    
    //---------------------------------------------------------------------------------------------------
    /** @inheritDoc */
    override public function draw( g:ICanvas3D ):void
    {
        if ( visible ) {
            front.draw(g);
            bottom.draw(g);
            left.draw(g);
            right.draw(g);
        }
    }
    
    /** @inheritDoc */
    override public function updateTransform( m11_:Number, m12_:Number, m13_:Number,
                                              m21_:Number, m22_:Number, m23_:Number,
                                              m31_:Number, m32_:Number, m33_:Number,
                                              m41_:Number, m42_:Number, m43_:Number ):void
    {
        super.updateTransform( m11_, m12_, m13_, m21_, m22_, m23_, m31_, m32_, m33_, m41_, m42_, m43_ );
        front.updateTransform( m11, m12, m13, m21, m22, m23, m31, m32, m33, m41, m42, m43 );
        bottom.updateTransform( m11, m12, m13, m21, m22, m23, m31, m32, m33, m41, m42, m43 );
        left.updateTransform( m11, m12, m13, m21, m22, m23, m31, m32, m33, m41, m42, m43 );
        right.updateTransform( m11, m12, m13, m21, m22, m23, m31, m32, m33, m41, m42, m43 );
    }
}

import frocessing.f3d.F3DModel;

/**
 * ...
 * @author saz
 */

class IsoTriangle3D extends F3DModel
{
    public function IsoTriangle3D(size:Number = 50, segment:int = 2)
    {
        super();
        backFaceCulling = false;
        initModel( size , segment);
    }
    
    /** @private */
    private function initModel( size:Number, seg:int):void
    {
        beginVertex(TRIANGLES) ;
        
        var total:int = seg + 1 ;
        var hh:Number = 0 ;
        var midSize:Number = size * .5 ;
        hh = int(Math.sqrt((size*size) - (midSize*midSize))) ;
        var diff:Number = size - hh ;
        var midW:Number = size * .5 ;
        var midH:Number = hh * .5 ;
        var divX:Number = size / (total) ;
        var divY:Number = hh / (total) ;
        
        var startX:Number = - midW ;
        var startY:Number = - (hh - diff) / 2 ;
        var startIndY:Number = (size - hh)/2 / size;
        var unitX:Number = 1/ (total*2) ;
        
         for ( var i:int = 0 ; i <= seg; i++ ) {
            var curX:Number =  midW + ( - i * divX * .5) ;
            var curX2:Number =  midW + ( - i * divX) ;
            var curY:Number =  divY * i ;
            
            for ( var j:int = 0; j <= i ; j ++ ) {
                var xx:Number = curX + divX * j ;
                var yy:Number = curY ;
                var indX:Number = xx / size - unitX ;
                var indY:Number = yy / hh ;
                
                var result0:Number = indX + (divX  / size / 2) ;
                var result1:Number = indX + (divX  / size) ;
                var result2:Number = indX ;
                
                if (j != 0) {
                    addVertex(startX + xx - divX, startY + (yy - divY / 2), 0,
                            result1 - unitX*3, indY - startIndY) ;
                    addVertex(startX + xx, startY + (yy - divY / 2), 0,
                            result1 - unitX, indY - startIndY) ;
                    addVertex(startX + (xx - divX / 2), startY + (yy + divY / 2), 0,
                            result1 - unitX*2, indY + (divY / hh) - startIndY) ;
                }
                addVertex(startX + xx, startY + (yy - divY / 2), 0, 
                         result0, indY - startIndY) ;
                addVertex(startX + (xx + divX / 2), startY + (yy + divY / 2), 0, 
                        result1, indY + (divY / hh) - startIndY) ;
                addVertex(startX + (xx - divX / 2), startY + (yy + divY / 2), 0, 
                        result2, indY + (divY / hh) - startIndY) ;
            }
        }
        endVertex() ;
    }
    
    public function toString():String 
    {
        return '[object IsoTriangle3D] >> ( x:'+x+', y:'+y+', z:'+z+')' ;
    }
}


import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.IBitmapDrawable;
import flash.geom.Matrix;
import flash.geom.Rectangle;

/**
 * ...
 * @author saz
 */
class DirectionBitmapData extends BitmapData 
{
    private var __dims:Rectangle;
    private var __source:IBitmapDrawable;
    static public const NORMAL:int = -1
    static public const X_AXIS_INVERTED:int = 1 ;
    static public const Y_AXIS_INVERTED:int = 2 ;
    
    
    public function DirectionBitmapData(src:IBitmapDrawable, mode:int = -1) 
    {
        __source = src ;
        __dims = new Rectangle(0,0,BitmapData(src).width, BitmapData(src).height) ;
        super(__dims.width, __dims.height, true, 0xFF6600) ;
        drawSource(mode) ;
    }
    public function drawSource(mode:int = -1):void 
    {
        var m:Matrix ; 
        switch(mode) {
            case X_AXIS_INVERTED:
                m = new Matrix(-1, 0, 0, 1, __dims.width, 0) ;
            break;
            case Y_AXIS_INVERTED:
                m = new Matrix(1, 0, 0, -1, 0, __dims.height) ;
            break;
            default :
                return draw(source, null, null, null, null, true) ;
            break ;
        }
        var temp:BitmapData = this.clone() ;
        temp.draw(__source, null, null, null, null, true) ;
        draw(temp, m) ;
    }
    public function toBitmap(pixelSnapping:String, smoothing:Boolean):Bitmap
    {
        return new Bitmap(this, pixelSnapping, smoothing) ; 
    }
    
    public function get source():IBitmapDrawable { return __source }
    public function set source(value:IBitmapDrawable):void { __source = value }
    
    public function get dimensions():Rectangle { return __dims }
    public function set dimensions(value:Rectangle):void { __dims = value }
    
}

import flash.display.BitmapData;
import flash.display.GradientType;
import flash.display.IBitmapDrawable;
import flash.display.Shape;
import flash.geom.Matrix;
import flash.geom.Rectangle;

/**
 * ...
 * @author saz
 */
class Reflect extends DirectionBitmapData 
{
    public function Reflect(src:IBitmapDrawable, invert:Boolean = true, mode:Number = -1)
    {
        if (mode == -1 && Boolean(invert)) mode = Y_AXIS_INVERTED ;
        super(src, mode) ;
        init(mode) ;
    }
    
    private function init(mode:int):void 
    {
        drawReflectGradient(mode) ;
    }
    
    private function drawReflectGradient(mode:int):void 
    {
        var matrix:Matrix = new Matrix() ;
        var colors:Array = [0xFFFFFF, 0x0] ;
        var alphas:Array = [.25, 0] ;
        var ratios:Array = [0, 255] ;
        
        var rotation:Number ;
        switch(mode) {
            case X_AXIS_INVERTED :
                trace('yo') ;
                colors = [0xFFFFFF, 0x0] ;
                alphas = [.25, 0] ;
                ratios = [0, 255] ;
                rotation = 0 ;
            break;
            case Y_AXIS_INVERTED:
                colors = [0xFFFFFF, 0x0] ;
                alphas = [.25, 0] ;
                ratios = [0, 255] ;
                rotation = Math.PI * .5 ;
            break;
            default:
                rotation = -Math.PI * .5 ;
            break;
        }
        
        var sh:Shape = new Shape() ;
        
        matrix.createGradientBox(dimensions.width, dimensions.height, rotation, 0, 0) ;
        sh.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix) ;
        sh.graphics.drawRect(0, 0, dimensions.width, dimensions.height) ;
        sh.graphics.endFill() ;
        
        draw(sh, null, null, 'alpha' ,null,true) ;
    }
}