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

[PV3D]Plane - tiling

Papervision3D Plane - tiling
Get Adobe Flash player
by pego451 18 Apr 2011
/**
 * Copyright pego451 ( http://wonderfl.net/user/pego451 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cp5t
 */

package  {
    
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.cameras.Camera3D;
    import org.papervision3d.view.Viewport3D;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.events.InteractiveScene3DEvent;
    
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    
    import caurina.transitions.Tweener;
    
    [SWF(width = "465", height = "465", backgroundColor = "0", frameRate = "30")]
    
    public class Main extends Sprite {
        
        private const MAX:uint = 9;
        private const FIELDS_W:uint = 3000;
        
        private var scene:Scene3D;
        private var camera:Camera3D;
        private var viewport:Viewport3D;
        private var render:BasicRenderEngine;
        
        private var isMouseTrace:Boolean = true;
        private var plane_vcta:Vector.<Plane> = new Vector.<Plane>(MAX);
        
        public function Main() {
            // init
            scene = new Scene3D();
            viewport = new Viewport3D(0, 0, true, true);
            addChild(viewport);
            
            initCamera();
            initWall();
            initPlane();
            initRender();
            
            onRenderEvent(true);
        };
        
        //init - camera
        private function initCamera():void {        
            camera = new Camera3D();
            camera.x = 0;
            camera.y = 0;
            camera.z = -5000;
            camera.focus = 100;
            camera.zoom = 10;
            camera.target = null;
        };
        
        //init - plane wall
        private function initWall():void {
            var color_mat:ColorMaterial = new ColorMaterial(0x000000, 1, true);
            
            var wall_plane:Plane = new Plane(color_mat, FIELDS_W, FIELDS_W, 20, 20);
            wall_plane.useOwnContainer = true;
            wall_plane.z = 500;
            
            var wall_do3d:DisplayObject3D = new DisplayObject3D();
            
            scene.addChild(wall_do3d);
            wall_do3d.addChild(wall_plane);
            
            wall_plane.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE, function() { isMouseTrace = true; } );
        };
        
        //init - plane
        private function initPlane():void {
            for (var i:uint = 0; i < MAX; i++) {
                var nRandom = Math.floor(Math.random() * 0xffffffff);
                
                var color_mat:ColorMaterial = new ColorMaterial(nRandom);
                color_mat.interactive = true;
                
                var plane:Plane = new Plane(color_mat, 300, 300, 2, 2);
                
                plane.useOwnContainer = true;
                plane.extra = i;
                plane_vcta[plane.extra] = plane;
                
                var btn:BtnPrimitive = new BtnPrimitive(plane, i, viewport);
                btn.addEventListener(CustomEvent.CUSTOM, monBtnPrimitiveRelease);
                
                scene.addChild(plane);
                
                plane_vcta[i].x = Math.floor(i % 3) * 340 - 340;
                plane_vcta[i].y = Math.floor(i / 3) * 340 -340
            }
            
        };
        
        //init - render
        private function initRender():void {
            render = new BasicRenderEngine();
        };
        
        private function onRenderEvent(b:Boolean):void {
            if (b) addEventListener(Event.ENTER_FRAME, render3D);
            else removeEventListener(Event.ENTER_FRAME, render3D);
        };
        
        private function render3D(evt:Event):void {
            render.renderScene(scene, camera, viewport);
            
            if (isMouseTrace) {
                var num:Number = mouseX / 465;
                onTweenCamera(num);
            }
        };
        
        public function onTweenCamera(percent:Number):void {
            var target_x:Number = 4000 * percent - 2000;
            var target_rot:Number = -40 * percent + 20;
            
            Tweener.addTween(camera, {
                x : target_x,
                y : 0,
                z : -5000,
                rotationY : target_rot,
                time : 2
            });
        };
        
        public function monBtnPrimitiveRelease(e:CustomEvent):void {
            isMouseTrace = false;
            
            var target_pos:DisplayObject3D = new DisplayObject3D();
            target_pos.copyTransform(e._obj);
            target_pos.moveBackward(1000);
            
            Tweener.addTween(camera, {
                x : target_pos.x,
                y : target_pos.y,
                z : target_pos.z,
                rotationX : target_pos.rotationX,
                rotationY : target_pos.rotationY,
                rotationZ : target_pos.rotationZ,
                time : 1.2,
                transition : "easeInOutExpo"
            });
        };
    }
};

//button
import flash.display.Sprite;
import flash.events.Event;
import org.papervision3d.events.InteractiveScene3DEvent;

class BtnPrimitive extends Sprite {
    private var _obj:*;
    private var _id:*;
    private var _vp:*;
    
    public function BtnPrimitive(obj:*, id:*, vp:*):void {            
        _obj = obj;
        _id = id;
        _vp = vp;
            
        _obj.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, monRollOver);
        _obj.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, monRollOut);
        _obj.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE, monRelease);
    };
    
    private function monRelease(e:InteractiveScene3DEvent):void {
        dispatchEvent(new CustomEvent(CustomEvent.CUSTOM, _obj));
    };
    
    private function monRollOver(e:InteractiveScene3DEvent):void {
        _vp.buttonMode = true;
    };
        
    private function monRollOut(e:InteractiveScene3DEvent):void {
        _vp.buttonMode = false;
    };
};

//event - custom
import flash.events.Event;

class CustomEvent extends Event {
    public static const CUSTOM:String = "custom";
    public var _obj:*;
    
    public function CustomEvent(type:String, obj:*) {
        super(type);
        _obj = obj;
    };
    
    public override function clone():Event {
        return new CustomEvent(type, _obj);
    };
    
    public override function toString():String {
        return formatToString("CustomEvent", "type", "bubbles", "cancelable", "eventPhase", "_obj");
    };
    
};