Beautifl - Flash Gallery

Thumbnail : Away3D [plane/cube]
Away3D [plane/cube]
ProjectNya 2012-09-26 MIT License

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

////////////////////////////////////////////////////////////////////////////////
// Away3D [plane/cube]
//
// [Away3D] 平面 (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1771
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.system.System;
    import flash.events.Event;
    import flash.geom.Vector3D;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import away3d.Away3D;
    import away3d.containers.View3D;
    import away3d.containers.Scene3D;
    import away3d.cameras.Camera3D;
    import away3d.lights.PointLight;
    import away3d.lights.DirectionalLight;
    import away3d.entities.Mesh;
    import away3d.primitives.SkyBox;
    import away3d.primitives.PlaneGeometry;
    import away3d.primitives.CubeGeometry;
    import away3d.materials.ColorMaterial;
    import away3d.materials.TextureMaterial;
    import away3d.textures.BitmapCubeTexture;
    import away3d.textures.BitmapTexture;
    import away3d.materials.lightpickers.StaticLightPicker;
    import away3d.materials.methods.EnvMapMethod;
    import away3d.materials.methods.SoftShadowMapMethod;
    import away3d.debug.AwayStats;

    [SWF(backgroundColor="#000000", width="600", height="600", frameRate="60")]

    public class Main extends Sprite {
        private var loaders:Array;
        private var data:Array;
        private var loaded:uint = 0;
        private static var basePath:String = "http://www.project-nya.jp/images/wonderfl/environment/";
        private var view:View3D;
        private var scene:Scene3D;
        private var camera:Camera3D;
        private var light:PointLight;
        private var dlight:DirectionalLight;
        private var plane:Mesh;
        private static var max:uint = 50;
        private static var radius:uint = 500;
        private var angle:Number = 90;
        private var degree:Number = 0;
        private static var depression:uint = 30;
        private static var radian:Number = Math.PI/180;
        private static var center:Vector3D = new Vector3D();
        private var stats:AwayStats;
        //private var source:BitmapData = new BitmapData(465, 465, false, 0x000000);

        public function Main() {
            Wonderfl.disable_capture();
            //addChild(new Bitmap(source));
            //
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            System.pauseForGCIfCollectionImminent(1);
            init();
            stats = new AwayStats(view);
            addChild(stats);
            stats.x = 475;
        }

        private function init():void {
            view = new View3D();
            scene = view.scene;
            camera = view.camera;
            addChild(view);
            light = new PointLight();
            scene.addChild(light);
            dlight = new DirectionalLight();
            scene.addChild(dlight);
            setup();
            //
            var imagePaths:Array = new Array();
            imagePaths.push("right.jpg");
            imagePaths.push("left.jpg");
            imagePaths.push("top.jpg");
            imagePaths.push("bottom.jpg");
            imagePaths.push("front.jpg");
            imagePaths.push("back.jpg");
            loaders = new Array();
            data = new Array();
            for (var n:uint = 0; n < 6; n++) {
                var loader:ImageLoader = new ImageLoader();
                loader.id = n;
                loader.addEventListener(ImageLoader.COMPLETE, complete, false, 0, true);
                loader.load(basePath + imagePaths[n], true);
                loaders.push(loader);
            }
            //
            addEventListener(Event.ENTER_FRAME, render, false, 0, true);
        }
        private function complete(evt:Event):void {
            var loader:ImageLoader = ImageLoader(evt.target);
            loader.removeEventListener(ImageLoader.COMPLETE, complete);
            data[loader.id] = loader.content.bitmapData;
            loader = null;
            //
            loaded ++;
            if (loaded > 5) {
                initialize();
            }
        }
        private function setup():void {
            view.backgroundColor = 0x000000;
            view.antiAlias = 4;
            //
            camera.x = 0;
            camera.y = 0;
            camera.z = - radius;
            //
            light.x = 0;
            light.y = 0;
            light.z = 0;
            light.specular = 0.1;
            light.diffuse = 0.9;
            light.ambient = 0.1;
            dlight.direction = new Vector3D(0, -1, 0);
            dlight.x = 0;
            dlight.y = 0;
            dlight.z = 0;
            dlight.specular = 0.9;
            dlight.diffuse = 0.9;
            dlight.ambient = 0.1;
        }
        private function initialize():void {
            var map:BitmapCubeTexture = new BitmapCubeTexture(data[0], data[1], data[2], data[3], data[4], data[5]);
            var sky:SkyBox = new SkyBox(map);
            scene.addChild(sky);
            //
            var geometry:PlaneGeometry = new PlaneGeometry(400, 400, 1, 1, true, true);
            var material:ColorMaterial = new ColorMaterial(0xFFFFFF, 1);
            material.addMethod(new EnvMapMethod(map, 0.9));
            var dlightPicker:StaticLightPicker = new StaticLightPicker([dlight]);
            material.lightPicker = dlightPicker;
            material.shadowMethod = new SoftShadowMapMethod(dlight);
            plane = new Mesh(geometry, material);
            plane.y = -160;
            scene.addChild(plane);
            //
            createCubes();
        }
        private function createCubes():void {
            var geometry:CubeGeometry = new CubeGeometry(20, 20, 20, 1, 1, 1, false);
            var bitmapData:BitmapData = new BitmapData(2, 2, false, 0xFFFFFFFF);
            var texture:BitmapTexture = new BitmapTexture(bitmapData);
            var material:TextureMaterial = new TextureMaterial(texture);
            var lightPicker:StaticLightPicker = new StaticLightPicker([light]);
            material.lightPicker = lightPicker;
            for (var n:uint = 0; n < max; n++) {
                var cube:Mesh = new Mesh(geometry, material);
                cube.x = (Math.random() - 0.5)*200;
                cube.y = (Math.random() - 0.5)*200;
                cube.z = (Math.random() - 0.5)*200;
                cube.rotationX = Math.random()*360;
                cube.rotationY = Math.random()*360;
                cube.rotationZ = Math.random()*360;
                scene.addChild(cube);
                cube.castsShadows = true;
            }
        }
        private function render(evt:Event):void {
            angle += 0.5;
            degree += 1;
            var dip:Number = depression*Math.sin(degree*radian);
            camera.x = radius*Math.cos(angle*radian)*Math.cos(dip*radian);
            camera.y = radius*Math.sin(dip*radian);
            camera.z = radius*Math.sin(angle*radian)*Math.cos(dip*radian);
            camera.lookAt(center);
            view.render();
            //view.renderer.queueSnapshot(source);
        }

    }

}


//////////////////////////////////////////////////
// ImageLoaderクラス
//////////////////////////////////////////////////

import flash.events.EventDispatcher;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.LoaderContext;

class ImageLoader extends EventDispatcher {
    private var loader:Loader;
    public var id:uint;
    private var info:LoaderInfo;
    public var content:Bitmap;
    private var smoothing:Boolean;
    public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
    public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
    public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
    public static const INIT:String = Event.INIT;
    public static const COMPLETE:String = Event.COMPLETE;

    public function ImageLoader() {
        loader = new Loader();
        info = loader.contentLoaderInfo;
    }

    public function load(file:String, s:Boolean = false):void {
        smoothing = s;
        info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
        info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
        info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
        info.addEventListener(Event.INIT, initialize, false, 0, true);
        info.addEventListener(Event.COMPLETE, complete, false, 0, true);
        try {
            //loader.load(new URLRequest(file));
            loader.load(new URLRequest(file), new LoaderContext(true));
        } catch (err:Error) {
            trace(err.message);
        }
    }
    public function unload():void {
        loader.unload();
    }
    private function ioerror(evt:IOErrorEvent):void {
        loader.unload();
        dispatchEvent(new Event(ImageLoader.IO_ERROR));
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        dispatchEvent(new Event(ImageLoader.HTTP_STATUS));
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        dispatchEvent(new Event(ImageLoader.SECURITY_ERROR));
    }
    private function initialize(evt:Event):void {
        if (smoothing) {
            content = Bitmap(info.content);
            content.smoothing = true;
        } else {
            content = Bitmap(info.content);
        }
        dispatchEvent(new Event(ImageLoader.INIT));
    }
    private function complete(evt:Event):void {
        info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
        info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
        info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
        info.removeEventListener(Event.INIT, initialize);
        info.removeEventListener(Event.COMPLETE, complete);
        dispatchEvent(new Event(ImageLoader.COMPLETE));
    }
    public function centerize():void {
        content.x = -uint(content.width*0.5);
        content.y = -uint(content.height*0.5);
    }

}