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

[Away3D] 部分的にフィルターを適用する方法

copyright (c) 2012 www.romatica.com
@author itoz ( http://www.romatica.com/ )
@since 2012/09/17 1:11:37

[Away3D] 部分的にフィルターを適用する方法

View3Dを複数作成。

View3Dの、
- stage3DProxyを共通にする。
- shareContextをtrueにする。
- 場合によってはカメラなどを共有する。
/**
 * Copyright romatica ( http://wonderfl.net/user/romatica )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cfRC
 */

/**
 * copyright (c) 2012 www.romatica.com
 * @author itoz
 * @since 2012/09/17 1:11:37
 * 
 * [Away3D] 部分的にフィルターを適用する方法
 * 
 * View3Dを複数作成。
 *  
 *  View3Dの、
 *  - stage3DProxyを共通にする。
 *  - shareContextをtrueにする。
 *  - 場合によってはカメラなどを共有する。
 * 
 */
package
{
    import away3d.cameras.Camera3D;
    import away3d.core.managers.Stage3DManager;
    import away3d.core.managers.Stage3DProxy;
    import away3d.events.Stage3DEvent;
    import away3d.filters.BloomFilter3D;
    import away3d.lights.PointLight;
    import away3d.materials.lightpickers.StaticLightPicker;

    import frocessing.color.ColorHSV;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Vector3D;
    import flash.utils.getTimer;

    [SWF(backgroundColor="#FFFFFF", frameRate="60", width="465", height="465")]
    /**
     *  
     */
    public class WonderflAway3DFilter extends Sprite
    {
        private static const ZERO : Vector3D = new Vector3D(0, 0, 0);
        private var stage3DManager : Stage3DManager;
        private var stage3DProxy : Stage3DProxy;
        private var createFlag : Boolean;
        
        private var background3d : BackGroundView3D;
        private var front3d : FrontView3D;
        private var camera : Camera3D;
        
        private var _lightColor0HSV : ColorHSV;
        private var _lightColor0 : PointLight;
        private var _lightColor1HSV : ColorHSV;
        private var _lightColor1 : PointLight;
        private var _lightPicker : StaticLightPicker;
        
        // wonderfl capture
        private var _capture : Bitmap;

        public function WonderflAway3DFilter()
        {
            if (stage) _initialize(null);
            else addEventListener(Event.ADDED_TO_STAGE, _initialize);
        }

        private function _initialize(event : Event) : void
        {
          
            removeEventListener(flash.events.Event.ADDED_TO_STAGE, _initialize);
            initProxies();
        }

        private function initProxies() : void
        {
            // Stage3Dマネージャを取得
            stage3DManager = Stage3DManager.getInstance(stage);
           
            // stage3Dプロキシを取得
            stage3DProxy = stage3DManager.getFreeStage3DProxy();
            stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
        }

        private function onContextCreated(event : Stage3DEvent) : void
        {
            if (createFlag) return;
            createFlag = true;
            stage3DProxy.width = stage.stageWidth;
            stage3DProxy.height = stage.stageHeight;

            // ----------------------------------
            // init away3d
            // ----------------------------------
            initAway3D();

            // ----------------------------------
            // render start
            // ----------------------------------
            stage3DProxy.addEventListener(Event.ENTER_FRAME, update);
            
            // wonderfl capture
            Wonderfl.disable_capture();
             //_capture = addChild(new Bitmap(new BitmapData(465, 465, false, 0x000000))) as Bitmap ;
        }

        /**
         * init Away3D
         */
        private function initAway3D() : void
        {
            // ----------------------------------
            // lights
            // ----------------------------------
            _lightColor0HSV = new ColorHSV(0xcc0000);
            _lightColor0 = new PointLight();
            _lightColor0.ambientColor = _lightColor0HSV.value;
            _lightColor0.ambient = 10;
            _lightColor0.specular = 10;
            _lightColor0.diffuse = 10;

            _lightColor1HSV = new ColorHSV(0x00cc00);
            _lightColor1 = (new PointLight()) ;
            _lightColor1.ambientColor = _lightColor1HSV.value;
            _lightColor1.ambient = 100;
            _lightColor1.specular = 100;
            _lightColor1.diffuse = 100;

            _lightPicker = new StaticLightPicker([_lightColor0, _lightColor1]);
            
            //----------------------------------
            //  ★ background (filter3dを適用する背景)
            //----------------------------------
            background3d = new BackGroundView3D(_lightPicker);
            background3d.stage3DProxy = stage3DProxy;
            background3d.shareContext = true;
            addChild(background3d);
            // filter3d
            background3d.filters3d = [new BloomFilter3D(32, 32)];
            // common camera
            camera = background3d.camera;
            
            // ----------------------------------
            // ★ front
            // ----------------------------------
            front3d = new FrontView3D(_lightPicker);
            front3d.camera = camera;
            front3d.stage3DProxy = stage3DProxy;
            front3d.shareContext = true;
            addChild(front3d);
            
            // stats
//            addChild(new AwayStats(background3d));
        }


        /**
         * update
         */
        private function update(event : Event) : void
        {
            var t : Number = getTimer();
            // ----------------------------------
            //  light
            //----------------------------------
             if (_lightColor0) {
                _lightColor0.x = Math.cos(t / 1024) * 800;
                _lightColor0.z = Math.sin(t / 1024) * 400 + 700;
                _lightColor0HSV.h += 2;
                _lightColor0.ambientColor = _lightColor0HSV.value;
            }

            if (_lightColor1) {
                _lightColor1.x = Math.sin(t / 1024) * 400;
                _lightColor1.y = Math.cos(t / 1024) * 200;
                _lightColor1.z = 1800;
                _lightColor1HSV.h += 2;
                _lightColor1.ambientColor = _lightColor1HSV.value;
            }
            
            //----------------------------------
            //  camera
            //----------------------------------
            camera.x = Math.cos(t / 512) * 100;
            camera.y = Math.sin(t / 512) * 100;
            camera.z = -600;
            camera.lookAt(ZERO);
            
            // ----------------------------------
            // 3D update
            // ----------------------------------
            if (background3d) background3d.update();
            if (front3d) front3d.update();


            // wonderfl capture
            if (_capture) stage3DProxy.context3D.drawToBitmapData(_capture.bitmapData)
            
           
        }

    }
}
import a24.tween.Ease24;
import a24.tween.Tween24;

import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.core.base.Geometry;
import away3d.entities.Mesh;
import away3d.materials.ColorMaterial;
import away3d.materials.lightpickers.LightPickerBase;
import away3d.materials.methods.FogMethod;
import away3d.primitives.CubeGeometry;
import away3d.primitives.WireframeSphere;

import flash.events.Event;
import flash.utils.getTimer;


//--------------------------------------------------------------------------
//
//  FrontView3D 球体部分(フィルタ適用しないView3D)
//
//--------------------------------------------------------------------------

class FrontView3D extends View3D
{
    private static const SPEED : int = 3;
    private var _sphere : WireframeSphere;
    private var _lightPicker : LightPickerBase;

    public function FrontView3D(lightPicker : LightPickerBase = null)
    {
        super();
        _lightPicker = lightPicker;
        if (stage) _initialize(null);
        else addEventListener(Event.ADDED_TO_STAGE, _initialize);
    }

    private function _initialize(event : Event) : void
    {
        removeEventListener(flash.events.Event.ADDED_TO_STAGE, _initialize);
        antiAlias = 4;
        createSphere();
    }

    /**
     * create sphere
     */
    private function createSphere() : void
    {
        // var cubeGeo : SphereGeometry = new SphereGeometry(64, 64);
        // var cubeMat : ColorMaterial = new ColorMaterial(0xffffff);
        // if (_lightPicker) cubeMat.lightPicker = _lightPicker;
        // _sphere = new Mesh(cubeGeo, cubeMat);
        _sphere = new WireframeSphere(128);
        _sphere.color = 0xffffff;
        scene.addChild(_sphere);
    }

    /**
     * update
     */
    public function update() : void
    {
        if (!_sphere) return;
        _sphere.rotationX += (_sphere.rotationX + SPEED > 360 ) ? SPEED - 360 : SPEED ;
        _sphere.rotationY += (_sphere.rotationY + (SPEED * 0.5) > 360 ) ? (SPEED * 0.5) - 360 : (SPEED * 0.5) ;
        _sphere.rotationZ += (_sphere.rotationZ + (SPEED * 0.3) > 360 ) ? (SPEED * 0.3) - 360 : (SPEED * 0.3) ;
        _sphere.z = Math.cos(getTimer() / 1000) * 300;
        render();
    }
}

//--------------------------------------------------------------------------
//
//  BackGroundView3D(フィルタを適用するView3D)
//
//--------------------------------------------------------------------------
class BackGroundView3D extends View3D
{
    private static const CUBE_SIZE : int = 64;
    private static const CUBE_NUM_IN_CIRCLE : int = 16;
    private static const CIRCLE_R : int = 300;
    private static const CIRCLE_NUM : int = 16;
    private static const MIN_DISTANCE : int = -400;
    private static const MAX_DISTANCE : int = 2400;
    private static const DISTANCE : int = (MAX_DISTANCE + Math.abs(MIN_DISTANCE));
    private static const Z_SPEED : Number = 20;
    private static const countMax : int = 60;
    private var count : int = 0;
    private var cubes : Vector.<Mesh>;
    private var circles : Vector.<EffectCircle>;
    private var circleSpan : Number = (DISTANCE / CIRCLE_NUM);
    private var _lightPicker : LightPickerBase;

    public function BackGroundView3D(lightPicker : LightPickerBase = null)
    {
        super();
        _lightPicker = lightPicker;
        if (stage) _initialize(null);
        else addEventListener(Event.ADDED_TO_STAGE, _initialize);
    }

    private function _initialize(event : Event) : void
    {
        removeEventListener(flash.events.Event.ADDED_TO_STAGE, _initialize);
        antiAlias = 2;
        backgroundColor = 0x0;
        camera.lens.far = 2400;
        createCubes();
    }

    /**
     * create cubes
     */
    private function createCubes() : void
    {
        // ----------------------------------
        // material
        // ----------------------------------
        var material : ColorMaterial = new ColorMaterial();
        material.addMethod(new FogMethod(MIN_DISTANCE, MAX_DISTANCE, 0x000000));
        material.lightPicker = _lightPicker;
        material.gloss = 100;

        // ----------------------------------
        // objects
        // ----------------------------------
        circles = new Vector.<EffectCircle>();
        cubes = new Vector.<Mesh>();

        for (var i : int = 0; i < CIRCLE_NUM; i++) {
            // ----------------------------------
            // Circle
            // ----------------------------------
            var circle : EffectCircle = scene.addChild(new EffectCircle()) as EffectCircle;
            circle.z = circleSpan * i;
            circle.id = i;
            circles.push(circle);
            for (var j : int = 0; j < CUBE_NUM_IN_CIRCLE; j++) {
                // ----------------------------------
                // Cube
                // ----------------------------------
                var cubeGeo : Geometry = new CubeGeometry(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
                var cube : Mesh = circle.addChild(new Mesh(cubeGeo, material)) as Mesh;
                var rad : Number = (360 / CUBE_NUM_IN_CIRCLE * j) * (Math.PI / 180);
                cube.x = Math.cos(rad) * CIRCLE_R;
                cube.y = Math.sin(rad) * CIRCLE_R;
                cubes.push(cube);
            }
        }
    }

    public function update() : void
    {
        if (!circles) return;

        var t : Number = getTimer();
        var max : int = circles.length;
        var half : int = int(max * 0.5);
        var cos : Number = Math.cos(t / 36000) * 720 ;
        for (var i : int = 0; i < max; i++) {
            var circle : EffectCircle = circles[i] as EffectCircle;
            if (!circle) break;
            // ----------------------------------
            // Z回転
            // ----------------------------------
            var val : int;
            if (i == half) val = half;
            else if (i < half) val = i % half;
            else val = half - i % half;
            circle.rotationZ = cos * val ;
            // ----------------------------------
            // 手前にZ移動
            // ----------------------------------
            circle.z -= Z_SPEED ;
            if (circle.z < MIN_DISTANCE) circle.z = circle.z + DISTANCE;

            if (count >= countMax) {
                var delay : Number = i * 0.05;
                var scale : Number = 0.75;
                Tween24.serial(
                     Tween24.wait(delay)
                   , Tween24.tween(circle,0.45,Ease24._BackIn).scaleX(scale).scaleY(scale).scaleZ(scale)
                   , Tween24.tween(circle, 0.75, Ease24._BackOut).scaleX(1).scaleY(1).scaleZ(1)
                ).play();
            }
        }

        count = (count + 1 > countMax) ? 0 : count + 1;

        render();
    }
}


class EffectCircle extends ObjectContainer3D
{
    public var id : int = 0;

    public function EffectCircle()
    {
        super();
    }
}