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

[Alternativa3D] Collada Load Test (Octcat)

copyright (c) 2012 www.romatica.com
 @author itoz ( http://www.romatica.com/ )
  
   [MODEL DATA] 
   https://github.com/itoz/Octcat3DModel
/**
 * Copyright romatica ( http://wonderfl.net/user/romatica )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9W83
 */

/**
 * copyright (c) 2012 www.romatica.com
 * @author itoz ( http://www.romatica.com/ )
 * 
 *  [MODEL DATA] 
 *  https://github.com/itoz/Octcat3DModel
 *  
 */
package
{
    import alternativa.engine3d.core.Camera3D;
    import alternativa.engine3d.core.Object3D;
    import alternativa.engine3d.core.Resource;
    import alternativa.engine3d.core.View;
    import alternativa.engine3d.lights.AmbientLight;
    import alternativa.engine3d.lights.DirectionalLight;
    import alternativa.engine3d.loaders.ParserCollada;
    import alternativa.engine3d.materials.EnvironmentMaterial;
    import alternativa.engine3d.materials.StandardMaterial;
    import alternativa.engine3d.objects.Mesh;
    import alternativa.engine3d.primitives.Plane;
    import alternativa.engine3d.resources.BitmapCubeTextureResource;
    import alternativa.engine3d.resources.BitmapTextureResource;
    import alternativa.engine3d.resources.TextureResource;

    import jp.progression.data.getResourceById;

    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.Stage3D;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.utils.getTimer;

    [SWF(backgroundColor="#ffffff", width="465", height="465", frameRate="60")]
    public class Octcat_Wonderfl extends Sprite
    {
        private static var radius : uint = 500;
        private var stage3D : Stage3D;
        private var scene : Object3D;
        private var camera : Camera3D;
        private var centerObj : Object3D = new Object3D();
        private var container : Object3D;
        private var environmentMap : BitmapCubeTextureResource;
        private var environmentMaterial : EnvironmentMaterial;
        
        public function Octcat_Wonderfl()
        {
            Wonderfl.disable_capture();
            if (stage) _initialize(null);
            else addEventListener(Event.ADDED_TO_STAGE, _initialize);
        }

        private function _initialize(event : Event) : void
        {
            removeEventListener(Event.ADDED_TO_STAGE, _initialize);
            //
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            // ----------------------------------
            // Assets Load Start
            // ----------------------------------
            var assetsLoader:AssetsLoader = AssetsLoader.getInstance();
            assetsLoader.addEventListener(Event.COMPLETE, onTexturesLoadComplete_Handler);
            assetsLoader.loadStart();
        }

        private function onTexturesLoadComplete_Handler(event : Event) : void
        {
            scene = new Object3D();
            camera = scene.addChild(new Camera3D(1, 1000)) as Camera3D;
            camera.view = addChild(new View(465, 465, true, 0x000000, 1, 4)) as View;
            camera.view.backgroundColor = 0xffffff;
            camera.x = - radius;
            camera.y = 0;
            camera.z = 0;
            // addChild(camera.diagram);
            //
            stage3D = stage.stage3Ds[0];
            stage3D.addEventListener(Event.CONTEXT3D_CREATE, onCreateContext_Handler, false, 0, true);
            stage3D.requestContext3D();
        }


        private function onCreateContext_Handler(evt : Event) : void
        {
            stage3D.removeEventListener(Event.CONTEXT3D_CREATE, onCreateContext_Handler);
            createObject();
            addEventListener(Event.ENTER_FRAME, render, false, 0, true);
        }

        private function createObject() : void
        {
            // --------------------------------------------------------------------------
            //
            // container
            //
            // --------------------------------------------------------------------------
            container = scene.addChild(new Object3D())as Object3D;
            container.scaleX = container.scaleY = container.scaleZ = 75;
            container.z = -140;
            
            // --------------------------------------------------------------------------
            //
            // light
            //
            // --------------------------------------------------------------------------

            // 環境光
            scene.addChild(new AmbientLight(0xd8d8d8))  as AmbientLight;

            // ディレクショナルライト
            var dLight:DirectionalLight = scene.addChild( new DirectionalLight(0xe3e3e3)) as DirectionalLight;
            dLight.rotationX = Math.PI / 2;
            dLight.rotationY = Math.PI / 2;
            dLight.rotationZ = Math.PI / 2;
            
            // --------------------------------------------------------------------------
            //
            // 土台
            //
            // --------------------------------------------------------------------------
            
            // environment
             environmentMap = new BitmapCubeTextureResource(
                  getResourceById(AssetsLoader.ENV_LEFT).data as BitmapData
                , getResourceById(AssetsLoader.ENV_RIGHT).data as BitmapData
                , getResourceById(AssetsLoader.ENV_BACK).data as BitmapData
                , getResourceById(AssetsLoader.ENV_FRONT).data as BitmapData
                , getResourceById(AssetsLoader.ENV_BOTTOM).data as BitmapData
                , getResourceById(AssetsLoader.ENV_TOP).data as BitmapData
            );
            var diffuseMap : TextureResource = new BitmapTextureResource(getResourceById(AssetsLoader.GROUND_TEXTURE).data as BitmapData);
            var normalMap : TextureResource = new BitmapTextureResource(getResourceById(AssetsLoader.GROUND_BUMP).data as BitmapData);
            var reflectionMap:TextureResource = new BitmapTextureResource(getResourceById(AssetsLoader.GROUND_REFRECTION).data as BitmapData);
            var opacityMap:TextureResource = new BitmapTextureResource(getResourceById(AssetsLoader.GROUND_OPACITY).data as BitmapData);
            environmentMaterial = new EnvironmentMaterial(
                                     diffuseMap
                                    , environmentMap
                                    , normalMap
                                    , reflectionMap
                                    , null//lightMap
                                    , opacityMap
                                    , 1
                                    );
            environmentMaterial.reflection = 0.3;
            environmentMaterial.alphaThreshold = 1;//## これ大事    
            
            //土台
            container.addChild(new Plane(4, 4, 16, 16, true, false, environmentMaterial, environmentMaterial)) as Plane;

            // --------------------------------------------------------------------------
            //
            // model
            //
            // --------------------------------------------------------------------------

            // モデルマテリアル
            var material : StandardMaterial = new StandardMaterial(
                new BitmapTextureResource(getResourceById(AssetsLoader.IMAGE_URL_MAIN).data as BitmapData)
                ,new BitmapTextureResource(getResourceById(AssetsLoader.IMAGE_URL_BUMP).data as BitmapData)
                ,new BitmapTextureResource(getResourceById(AssetsLoader.IMAGE_URL_SPECULAR).data as BitmapData)
                ,new BitmapTextureResource(getResourceById(AssetsLoader.IMAGE_URL_GLOSS).data as BitmapData)
            );
            
            // モデルパース
            var parser : ParserCollada = new ParserCollada();
            parser.parse(XML(getResourceById(AssetsLoader.DAE_MODEL_URL).data));

            for (var i : int = 0; i < parser.objects.length; i++)
            {
                if (parser.hierarchy.length > i)
                {
                    // メッシュオブジェクトの場合
                    if (parser.hierarchy[i] is Mesh)
                    {
                        var mesh : Mesh = parser.hierarchy[i] as Mesh;
                        // ## StandardMaterialを適用させる場合はTangent情報が必要。
                        mesh.geometry.calculateTangents(0);
                        mesh.setMaterialToAllSurfaces(material);
                        container.addChild(mesh);
                        // 確認用ワイヤーフレーム
                        // var wire : WireFrame = WireFrame.createEdges(mesh, 0x00f0c2, 1 );
                        // _container.addChild(wire);
                    }
                }
            }
            
            // --------------------------------------------------------------------------
            //
            // resource upload
            //
            // --------------------------------------------------------------------------
            
            for each (var resource:Resource in scene.getResources(true))
            {
                resource.upload(stage3D.context3D);
            }
        }

        private function render(evt : Event) : void
        {
            container.rotationZ -= 0.02;
            camera.z = Math.cos(getTimer() / 1024) * 64 + 150;
            TransformUtils.lookAt(centerObj, camera);
            camera.render(stage3D);
        }
    }
}


import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.Object3D;

import jp.progression.commands.lists.LoaderList;
import jp.progression.commands.net.LoadBitmapData;
import jp.progression.commands.net.LoadURL;

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
import flash.system.LoaderContext;
class TransformUtils {
    private static var radian:Number = Math.PI/180;

    public function TransformUtils() {
    }
    public static function lookAt(target:Object3D, camera:Camera3D):void {
        var dx:Number = target.x - camera.x;
        var dy:Number = target.y - camera.y;
        var dz:Number = target.z - camera.z;
        camera.rotationX = Math.atan2(dz, Math.sqrt(dx*dx + dy*dy)) - 90*radian;
        camera.rotationY = 0;
        camera.rotationZ = - Math.atan2(dx, dy);
    }
}

// --------------------------------------------------------------------------
//
// assets loader
//
// --------------------------------------------------------------------------
class AssetsLoader extends EventDispatcher
{
    private var _startFlag : Boolean;
    
    private static const DOMAIN_PATH : String = "http://www.romatica.com/dev/wonderfl/octcat/";
    
    public static const DAE_MODEL_URL : String = DOMAIN_PATH + "octcat_model.dae";
    
    public static const IMAGE_URL_MAIN : String = DOMAIN_PATH + "octcat.png";
    public static const IMAGE_URL_BUMP : String = DOMAIN_PATH + "octcat_bump.png";
    public static const IMAGE_URL_SPECULAR : String = DOMAIN_PATH + "octcat_specular.png";
    public static const IMAGE_URL_GLOSS : String = DOMAIN_PATH + "octcat_gloss.png";
    
    public static const GROUND_TEXTURE : String = DOMAIN_PATH + "ground/texture.png";
    public static const GROUND_REFRECTION : String = DOMAIN_PATH + "ground/refrec.png";
    public static const GROUND_BUMP : String = DOMAIN_PATH + "ground/bump.png";
    public static const GROUND_OPACITY : String = DOMAIN_PATH + "ground/alpha.png";
    
    public static const ENV_TOP : String = DOMAIN_PATH + "bg/top.jpg";
    public static const ENV_BOTTOM : String = DOMAIN_PATH + "bg/bottom.jpg";
    public static const ENV_LEFT : String = DOMAIN_PATH + "bg/left.jpg";
    public static const ENV_RIGHT : String = DOMAIN_PATH + "bg/right.jpg";
    public static const ENV_FRONT : String = DOMAIN_PATH + "bg/front.jpg";
    public static const ENV_BACK : String = DOMAIN_PATH + "bg/back.jpg";
    
    public static var instance : AssetsLoader;
    

    public static function getInstance() : AssetsLoader
    {
        if ( instance == null ) instance = new  AssetsLoader(new SingletonEnforcer());
        return instance;
    }

    public function AssetsLoader(pvt : SingletonEnforcer)
    {
        if (pvt == null) throw new Error("TextureLoader is a singleton class, use getInstance() instead");
    }

    public function loadStart() : void
    {
        if (_startFlag) return;
        _startFlag = true;

        var loaderList : LoaderList = new LoaderList({
                onProgress:function():void{
//                    trace(this.percent);
                }                
                ,onComplete:function():void 
                {
                    dispatchEvent(new Event(Event.COMPLETE));
                }
                ,catchError:function(target:Object, e:Error):void
                {
                    trace( target, e );
                    // _tf.text = "catchError :" + e.toString();
                    // target.interrupt();
                }
                ,onError:function():void
                {
                    // _tf.text = "onError :" 
                }
            });
        loaderList.addCommand(new LoadBitmapData(new URLRequest(IMAGE_URL_MAIN),{context: new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(IMAGE_URL_BUMP),{context: new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(IMAGE_URL_GLOSS),{context: new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(IMAGE_URL_SPECULAR),{context: new LoaderContext(true)}));

        loaderList.addCommand(new LoadBitmapData(new URLRequest(ENV_BACK),{context: new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(ENV_TOP),{context: new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(ENV_FRONT), {context:new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(ENV_LEFT), {context:new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(ENV_RIGHT), {context:new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(ENV_BOTTOM), {context:new LoaderContext(true)}));

        loaderList.addCommand(new LoadBitmapData(new URLRequest(GROUND_BUMP), {context:new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(GROUND_OPACITY), {context:new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(GROUND_REFRECTION), {context:new LoaderContext(true)}));
        loaderList.addCommand(new LoadBitmapData(new URLRequest(GROUND_TEXTURE), {context:new LoaderContext(true)}));

        loaderList.addCommand(new LoadURL(new URLRequest(DAE_MODEL_URL), {context:new LoaderContext(true)}));
       
        loaderList.execute();
    }
}
internal class SingletonEnforcer
{
}