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

forked from: UV と Vertex の関係

// forked from ksnmt's UV と Vertex の関係
package {

    import alternativ5.engine3d.controllers.*;
    import alternativ5.engine3d.core.*;
    import alternativ5.engine3d.display.*;
    import alternativ5.types.*;
    import alternativ5.utils.*;
    
    import flash.events.*;
    import flash.system.*;
    import flash.text.*;
    import flash.display.*;
    import flash.net.*;
    import flash.geom.*;
    
    import com.bit101.components.*;

    [SWF(backgroundColor="#000000", frameRate="120")]
    public class Main extends Sprite {
        
        private var scene:Scene3D;
        private var view:View;
        private var camera:Camera3D;
        private var cameraController:CameraController;
        
        private var image:BitmapData;
        private var loader:Loader;
        
        private var db:XML;
        private var mesh:Mesh;
        
        private var showingMeshKey:int = 0;
        
        private var aU:HUISlider;
        private var aV:HUISlider;

        private var bU:HUISlider;
        private var bV:HUISlider;

        private var cU:HUISlider;
        private var cV:HUISlider;
        
        private var aUV:Point = new Point(0, 0);
        private var bUV:Point = new Point(1, 0);
        private var cUV:Point = new Point(1, 1);
        
        private static const RANGE:Number = 10;
        
        
        public function Main() {
            this.addEventListener(Event.ADDED, init);
        }
        
        public function init(e:Event):void {
            this.removeEventListener(Event.ADDED, init);
            loader = new Loader();
            loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/3/35/35c2/35c2ea9f0b44ebde51b055163e3da4836e51d22c"), new LoaderContext(true));
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
        }
        
        
        private function loaded(e:Event):void{
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loaded);
            image = new BitmapData(loader.content.width, loader.content.height,true);
            image.draw(loader);
            
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            scene = new Scene3D();
            scene.root = new Object3D();
            
            db = 
            <mesh>
                <surface materialType="TextureMaterial">
                    <face>
                        <UVs>
                        // ABC
                            <aUV x="0" y="0" />
                            <bUV x="1" y="0" />
                            <cUV x="1" y="1" />
                        </UVs>
                        // ABCD
                        <vertex x="-100" y="-100" z="0" />
                        <vertex x="100" y="-100" z="0" />
                        <vertex x="100" y="100" z="0" />
                        <vertex x="-100" y="100" z="0" />
                    </face>
                </surface>
            </mesh>
            ;
            
            mesh = EZCreatMesh.create(db, (db.surface[0].@materialType == "TextureMaterial") ? image : null);
            scene.root.addChild(mesh);
            
            camera = new Camera3D();
            camera.z = 300;
            scene.root.addChild(camera);
            
            view = new View();
            this.addChild(view);
            view.camera = camera;
            
            cameraController = new CameraController(this);
            cameraController.camera = camera;
            cameraController.lookAt(new Point3D);
            cameraController.checkCollisions = false;
            
            FPS.init(stage);
            
            
            const MARGIN_LEFT:Number = 5.0;
            const MAXIMUM:Number = 10.0 * RANGE;
            const MINIMUM:Number = -RANGE;
            
            const L:String = "/ " + RANGE;
            
            aU = new HUISlider(stage, MARGIN_LEFT, 0, "aU", onChange);        new Label(stage, 180, 0, L);
            aV = new HUISlider(stage, MARGIN_LEFT, 20, "aV", onChange);        new Label(stage, 180, 20, L);
            
            bU = new HUISlider(stage, MARGIN_LEFT, 60, "bU", onChange);        new Label(stage, 180, 60, L);
            bV = new HUISlider(stage, MARGIN_LEFT, 80, "bV", onChange);        new Label(stage, 180, 80, L);
            
            cU = new HUISlider(stage, MARGIN_LEFT, 120, "cU", onChange);    new Label(stage, 180, 120, L);
            cV = new HUISlider(stage, MARGIN_LEFT, 140, "cV", onChange);    new Label(stage, 180, 140, L);
            
            aU.maximum = aV.maximum = bU.maximum = bV.maximum = cU.maximum = cV.maximum = MAXIMUM;
            aU.minimum = aV.minimum = bU.minimum = bV.minimum = cU.minimum = cV.minimum = MINIMUM; 
            
            bU.value = cU.value = cV.value = MAXIMUM / RANGE;
            
            
            stage.addEventListener(Event.RESIZE, onResize);
            stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
            onResize(null);
        }
        
        private function onChange(e:Event):void {
           var v:Number = e.currentTarget.value / RANGE;
            
            switch(e.currentTarget){
                case aU : db..aUV.@x = v; break;
                case aV : db..aUV.@y = v; break;
                case bU : db..bUV.@x = v; break;
                case bV : db..bUV.@y = v; break;
                case cU : db..cUV.@x = v; break;
                case cV : db..cUV.@y = v; break;
            }
            
            scene.root.removeChild(mesh);
            mesh = EZCreatMesh.create(db, (db.surface[0].@materialType == "TextureMaterial") ? image : null);
            scene.root.addChild(mesh);
        }

        
        private function onResize(e:Event):void {
            view.width = stage.stageWidth;
            view.height = stage.stageHeight;
        }
        
        private function onEnterFrame(e:Event):void {
            cameraController.processInput();
            scene.calculate();
        }
    }
}



import alternativ5.engine3d.core.*;
import alternativ5.engine3d.materials.*;
import alternativ5.types.*;
import alternativ5.utils.*;

import flash.geom.*;
import flash.display.*;

class EZCreatMesh {
    
    /**
     * static const
     */
    public static const  DEV:String = "DevMaterial";
    public static const  FILL:String = "FillMaterial";
    public static const  TEXTURE:String = "TextureMaterial";
    public static const WIRE:String = "WireMaterial";
    public static const MOVIECLIP:String = "MovieClipMaterial";
    
    /**
     * 簡単に より早くメッシュを作成する為に
     * 
     * Alternativa3Dを利用したマップ(Mesh)作成時に ASでは 頂点やフェイス、サーフェス全てに id(名前)をつける必要があり、これらの作業は非常に面倒です
     * XMLに情報を記述することによって 値 変更時にコンパイルが不要になり、誰でも簡単にマップ作成が可能です
     * 
     * @author br
     */
    
    /**
     * メッシュ作成
     * @param    xml メッシュの情報が記載されたXML
     * @return Mesh
     */
    public static function create(xml:XML, bmd:BitmapData = null):Mesh {
        
        var _mesh:Mesh = new Mesh();
        var surfaces:Array = new Array();
        
        for (var a:uint = 0; a < xml.surface.length(); a++)
        {
            var surfaceName:String = String("s" + a);
            var faces:Array = new Array();
            
            for (var b:uint = 0; b < xml.surface[a].face.length(); b++)
            {
                var faceName:String = String("f" + a + b);
                var vertices:Array = new Array();
                
                for (var c:uint = 0; c < xml.surface[a].face[b].vertex.length(); c++)
                {
                    var vertexName:String = String("v" + a + b + c);
                    
                    /**
                     * createVertex
                     */
                    _mesh.createVertex(
                        xml.surface[a].face[b].vertex[c].@x,
                        xml.surface[a].face[b].vertex[c].@y,
                        xml.surface[a].face[b].vertex[c].@z,
                        vertexName
                    );
                    vertices.push(vertexName);
                }
                
                /**
                 * createFace
                 */
                _mesh.createFace(vertices, faceName);
                
                /**
                 * setUVsToFace
                 */
                if ("UVs" in xml.surface[a].face[b]) {
                    _mesh.setUVsToFace(
                        new Point(
                            Number(xml.surface[a].face[b].UVs.aUV.@x),
                            Number(xml.surface[a].face[b].UVs.aUV.@y)
                        ),
                        new Point(
                            Number(xml.surface[a].face[b].UVs.bUV.@x),
                            Number(xml.surface[a].face[b].UVs.bUV.@y)
                        ),
                        new Point(
                            Number(xml.surface[a].face[b].UVs.cUV.@x),
                            Number(xml.surface[a].face[b].UVs.cUV.@y)
                        ),
                        faceName
                    );
                }
                
                /**
                 * push Faces
                 */
                faces.push(faceName);
            }
            
            /**
             * createSurface
             */
            _mesh.createSurface(faces, surfaceName);
            surfaces.push(surfaceName);
            
            _mesh.setMaterialToSurface(createMaterial(xml.surface[a]), surfaceName);
        }
        
        return _mesh;
        
        
        
        /**
         * マテリアルを作成
         * @param    material マテリアル情報が記載されたXML
         * @return SurfaceMaterial
         * @exampleText XMLの例を示します
         * <listing version="3.0">
         * <surface materialType="FillMaterial" color="0x99ab4e" wirethickness="1" wirecolor="0x7b8d42" />
         * <surface materialType="TextureMaterial" repeat="true" smooth="true" src="image" precision="BEST" />
         * </listing>
         */
        function createMaterial(material:XML):SurfaceMaterial {
            switch (true) {
                /**
                 * DEV
                 */
                case material.@materialType == DEV : 
                    return new DevMaterial(
                        uint(material.@parametertype) ? material.@parametertype : 0,
                        uint(material.@color) ? material.@color : 0xFFFFFF,
                        Number(material.@maxparametervalue) ? material.@maxparametervalue : 20,
                        Boolean(material.@shownormals) ? material.@shownormals : false,
                        uint(material.@normalscolor) ? material.@normalscolor : 0x00FFFF,
                        int(material.@minmobility) ? material.@minmobility : 0,
                        int(material.@maxmobility) ? material.@maxmobility : 255,
                        Number(material.@alpha) ? material.@alpha : 1,
                        String(material.@blendmode) ? material.@blendmode : "normal",
                        Number(material.@wirethickness) ? material.@wirethickness : -1,
                        Number(material.@wirecolor) ? material.@wirecolor : 0
                    );
                break;
                
                /**
                 * FILL
                 */
                case material.@materialType == FILL : 
                
                    return new FillMaterial(
                        uint(material.@color)? material.@color: 0xffffff * Math.random(),
                        Number(material.@alpha)? material.@alpha : 1,
                        String(material.@blendmode)? material.@blendmode : "normal",
                        Number(material.@wirethickness)? material.@wirethickness : -1,
                        Number(material.@wirecolor)? material.@wirecolor : 0
                    );
                break;
                
                /**
                 * TEXTURE
                 */
                case material.@materialType == TEXTURE :
                    return new TextureMaterial(
                    new Texture(bmd),
                    uint(material.@alpha)? material.@alpha : 1,
                    Boolean(material.@repeat == "true")? true : true,
                    Boolean(material.@smooth == "true")? true : false,
                    String(material.@blendmode.toString())? material.@blendmode : BlendMode.NORMAL,
                    Number(material.@wirethickness)? material.@wirethickness : -1,
                    uint(material.@wirecolor)? material.@wirecolor : 0,
                    String(material.@precision.toString())? createTextureMaterialPrecision(material.@precision.toString()) : 10
                    );
                break;
                
                /**
                 * WIRE
                 */
                case material.@materialType == WIRE : 
                    return new WireMaterial(
                        Number(material.@thickness) ? material.@thickness : 0,
                        uint(material.@color) ? material.@color : 0xffffff * Math.random(),
                        Number(material.@alpha) ? material.@alpha : 1,
                        String(material.@blendmode) ? material.@blendmode : "normal"
                    );
                break;
                
                default : 
                return null;
                break;
            }
        }
        
        /**
         * createTextureMaterialPrecision
         * @param    precision
         * @return
         */
        function createTextureMaterialPrecision(precision:String):Number {
            switch(true) {
                case precision == "BEST" : return TextureMaterialPrecision.BEST; break;
                case precision == "HIGH" : return TextureMaterialPrecision.HIGH; break;
                case precision == "LOW" : return TextureMaterialPrecision.LOW; break;
                case precision == "MEDIUM" : return TextureMaterialPrecision.MEDIUM; break;
                case precision == "NONE" : return TextureMaterialPrecision.NONE; break;
                case precision == "VERY_HIGH" : return TextureMaterialPrecision.VERY_HIGH; break;
                case precision == "VERY_LOW" : return TextureMaterialPrecision.VERY_LOW; break;
                default : return TextureMaterialPrecision.MEDIUM;
            }
        }
        
        
        /**
         * XMLを元にプロパティを変更したメッシュを返します
         * @param    mesh メッシュ
         * @param    properties プロパティが記載されたXML
         * @return mesh プロパティが変更されたメッシュを返します
         * @exampleText XMLの例を示します
         * <listing version="3.0">
         * <mesh z="-100" scaleX="5" scaleY="5" />
         * </listing>
         */
        function meshPropertiesSetup(mesh:Mesh, properties:XML):Mesh {
            if(properties.hasOwnProperty("@x")) mesh.x = Number(properties.@x);
            if(properties.hasOwnProperty("@y")) mesh.y = Number(properties.@y);
            if(properties.hasOwnProperty("@z")) mesh.z = Number(properties.@z);
            if(properties.hasOwnProperty("@scaleX")) mesh.scaleX = Number(properties.@scaleX);
            if(properties.hasOwnProperty("@scaleY")) mesh.scaleY = Number(properties.@scaleY);
            if(properties.hasOwnProperty("@scaleZ")) mesh.scaleZ = Number(properties.@scaleZ);
            if(properties.hasOwnProperty("@rotationX")) mesh.rotationX = MathUtils.toRadian(Number(properties.@rotationX));
            if(properties.hasOwnProperty("@rotationY")) mesh.rotationY = MathUtils.toRadian(Number(properties.@rotationY));
            if(properties.hasOwnProperty("@rotationZ")) mesh.rotationZ = MathUtils.toRadian(Number(properties.@rotationZ));
            return mesh;
        }
    }
}