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: Polygon Mesh from XML

Polygon Mesh Drawing from XM File (subset) by XELF 2009
XM : (XELF's Original 3D Model XML Format)
Get Adobe Flash player
by XELF 02 Mar 2009
    Embed
// forked from XELF's Polygon Mesh from XML
// Polygon Mesh Drawing from XM File (subset) by XELF 2009
// XM : (XELF's Original 3D Model XML Format)

package {
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.filters.DisplacementMapFilter;
    import flash.geom.Point;
    import flash.text.*;
    
    [SWF(width="465", height="465", backgroundColor="0x8080FF", frameRate="30")]
    public class Polygon extends Sprite {
        private var perlinBmp:BitmapData;
 
        private var mesh:Mesh;
        private var model:Model;
        private var text:TextField;

        public function Polygon() {
            addEventListener(Event.ENTER_FRAME, draw);
            
            text = new TextField();
            text.autoSize = TextFieldAutoSize.LEFT;
            addChild(text);
            
            //text.text = model.Faces[0].Vertices[1].Index.toString();
            try {
                model = Model.CreateFromDocument(TestDocument.Document.Meshes.Mesh[0]);
                mesh = Mesh.CreateFromModel(model);
            } catch(e:Error) {
                text.text = e.toString();
            }

            perlinBmp = new BitmapData(256, 256);
            perlinBmp.perlinNoise(256, 256, 10,
                Math.round(Math.random() * 100000),
                false, true, 1, false);

            var displacementMap:DisplacementMapFilter
                = new DisplacementMapFilter(perlinBmp,
                new Point(0, 0), 1, 1, 100, 100, "color", 1);
            
        }
        private function draw(e:Event) : void {
            mesh.Draw(graphics, perlinBmp);
        }
    }
}

import flash.display.*;

class Vector2 {
    public var X:Number;
    public var Y:Number;

    public static function Parse(value:String):Vector2 {
        var elements:Array = value.split(',');
        var result:Vector2 = new Vector2();
        result.X = parseFloat(elements[0]);
        result.Y = parseFloat(elements[1]);
        return result;
    }
}

class Vector3 {
    public var X:Number;
    public var Y:Number;
    public var Z:Number;

    public static function Parse(value:String):Vector3 {
        var elements:Array = value.split(',');
        var result:Vector3 = new Vector3();
        result.X = parseFloat(elements[0]);
        result.Y = parseFloat(elements[1]);
        result.Z = parseFloat(elements[2]);
        return result;
    }
}

class FaceVertex {
    public var Index:int;
    public var TexCoord:Vector2;

    public static function Parse(xml:XML):FaceVertex {
        var result:FaceVertex = new FaceVertex();
        result.Index = parseInt(xml.@Vertex);
        result.TexCoord = Vector2.Parse(xml.@TexCoord);
        return result;
    }
}

class Face {
    public var Vertices:Vector.<FaceVertex>;
    
    public static function Parse(xml:XML):Face {
        var result:Face = new Face();
        result.Vertices = new Vector.<FaceVertex>(xml.FaceVertex.length());
        for (var i:int = 0; i < result.Vertices.length; i++) {
            result.Vertices[i] = FaceVertex.Parse(xml.FaceVertex[i]);
        }
        return result;
    }
}

class Model {
    public var Vertices:Vector.<Vector3>;
    public var Faces:Vector.<Face>;
    
    public static function CreateFromDocument(document:XML):Model {
        var result:Model = new Model();
        
        result.Vertices = new Vector.<Vector3>(document.Vertices.Vertex.length());
        for (var i:int = 0; i < result.Vertices.length; i++) {
            result.Vertices[i] = Vector3.Parse(document.Vertices.Vertex[i].@Position);
        }
        
        result.Faces = new Vector.<Face>(document.Faces.Face.length());
        for (i = 0; i < result.Faces.length; i++) {
            result.Faces[i] = Face.Parse(document.Faces.Face[i]);
        }
        return result;
    }
}

class Vertex {
    public var Position:Vector3;
    public var TexCoord:Vector2;
}

class Mesh {
    public var Vertices:Vector.<Vertex>;
    public var Indices:Vector.<int>;
    
    protected var v:Vector.<Number>;
    protected var t:Vector.<Number>;

    public static function CreateFromModel(model:Model):Mesh {
        var result:Mesh = new Mesh();
        
        result.Indices = new Vector.<int>();
        result.Vertices = new Vector.<Vertex>();
        
        var vn:int = 0;
        for each (var face:Face in model.Faces) {
            var n:int = face.Vertices.length;
            if (n >= 3) {
                var a:int = face.Vertices[0].Index;
                for (var i:int = 2; i < n; i++) {
                    var b:int = face.Vertices[i - 1].Index;
                    var c:int = face.Vertices[i    ].Index;
                    result.Indices.push(vn, vn + 1, vn + 2);
                    
                    var va:Vertex = new Vertex();
                    var vb:Vertex = new Vertex();
                    var vc:Vertex = new Vertex();
                    va.Position = model.Vertices[a];
                    vb.Position = model.Vertices[b];
                    vc.Position = model.Vertices[c];
                    va.TexCoord = face.Vertices[a].TexCoord;
                    vb.TexCoord = face.Vertices[b].TexCoord;
                    vc.TexCoord = face.Vertices[c].TexCoord;
                    result.Vertices.push(va, vb, vc);
                    vn += 3;
                }
            }
        }
        
        result.v = new Vector.<Number>(result.Vertices.length * 2);
        result.t = new Vector.<Number>(result.Vertices.length * 3);

        return result;
    }
    
    public function Draw(graphics:Graphics, texture:BitmapData):void {
        v.length = 0;
        t.length = 0;
        
        for (var i:int = 0; i < Vertices.length; i++) {
            v.push(Vertices[i].Position.X, Vertices[i].Position.Y);
            t.push(Vertices[i].TexCoord.X, Vertices[i].TexCoord.Y, Vertices[i].Position.Z);
        }
                
        graphics.clear();
        graphics.beginBitmapFill(texture);
        graphics.drawTriangles(v, Indices, t);
        graphics.endFill();
    }
}
 
class TestDocument {
    public static var Document:XML =
      <Model>
        <Materials><Material/></Materials>
        <Meshes>
        <Mesh Name="">
        <Vertices>
          <Vertex Position="100,200,300"/>
          <Vertex Position="300,400,200"/>
          <Vertex Position="200,100,250"/>
          <Vertex Position="350,250,150"/>
        </Vertices>
        <Faces>
          <Face Material="0">
            <FaceVertex Vertex="0" TexCoord="0,0"/>
            <FaceVertex Vertex="1" TexCoord="1,0"/>
            <FaceVertex Vertex="3" TexCoord="0,1"/>
            <FaceVertex Vertex="2" TexCoord="1,1"/>
          </Face>
        </Faces>
        </Mesh>
        </Meshes>
      </Model>;
}