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

Flex 3 SDKとで三次元を遊ぶ[19]

/**
 * Copyright Akiyah ( http://wonderfl.net/user/Akiyah )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/c2u5
 */

package{
    import Box2D.Collision.b2AABB;
    import Box2D.Collision.Shapes.b2CircleDef;
    import Box2D.Collision.Shapes.b2PolygonDef;
    import Box2D.Collision.Shapes.b2Shape;
    import Box2D.Common.Math.b2Vec2;
    import Box2D.Dynamics.b2Body;
    import Box2D.Dynamics.b2BodyDef;
    import Box2D.Dynamics.b2World;
    import Box2D.Dynamics.Joints.b2MouseJoint;
    import Box2D.Dynamics.Joints.b2MouseJointDef;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.system.Security;
    import org.papervision3d.core.data.UserData;
    import org.papervision3d.core.effects.view.ReflectionView;
    import org.papervision3d.core.geom.TriangleMesh3D;
    import org.papervision3d.core.proto.MaterialObject3D;
    import org.papervision3d.events.InteractiveScene3DEvent;
    import org.papervision3d.materials.BitmapFileMaterial;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.materials.special.CompositeMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.materials.WireframeMaterial;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.cameras.CameraType;
    import org.papervision3d.view.BasicView;

    public class Sample extends BasicView {
        public var m_world:b2World;
        public var sd:b2CircleDef;
        public var bd:b2BodyDef;
        public var cubes:Array = [];
        public var plane_for_drug:Plane;
        public var m_mouseJoint:b2MouseJoint;
        public var RADIUS:Number = 4;
        public var BAR_SIZE:Number = 2;
        
        Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");
        public var path:String = "http://assets.wonderfl.net/images/related_images";
        public var filename:String = path+"/3/35/3590/359061d357d721a573fae94cc4d8b2819a72122c";
        public var bar_filename:String = path+"/8/8e/8e07/8e07fdfd489d07d793678d1b8a690a68723578ec";
        
        public function Sample() {
            init_box2d();
            init_papervision();
            createBox();
            addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(MouseEvent.MOUSE_UP, released);
        }

        public function update(e:Event):void{
            m_world.Step(1.0 / 30, 10);
            
            for (var i:Number = 0; i < cubes.length; i++) {
                var cube:TriangleMesh3D = cubes[i];
                var b:b2Body = cube.userData.data;

                if (b.IsFrozen()) {
                    bd.position.Set(0, - 50);
                    b = m_world.CreateBody(bd);
                    b.CreateShape(sd);
                    b.SetMassFromShapes();
                    cube.userData.data = b;
                }
                
                var p:b2Vec2 = b.GetPosition();
                cube.x = - p.x * 10;
                cube.y = - p.y * 10;
                cube.rotationZ = b.GetAngle() * 180 / Math.PI
            }

            var camera_length:Number = 200;
            var c:Number = (mouseX / stage.width - 0.5) * (Math.PI / 2);
            var d:Number = (mouseY / stage.height) * (Math.PI / 2 / 2);
            camera.x = Math.sin(c) * Math.cos(d) * camera_length;
            camera.z = Math.cos(c) * Math.cos(d) * camera_length;
            camera.y = Math.sin(d) * camera_length;

            singleRender();
        }

        public function init_papervision():void{
            //var material_for_drug:MaterialObject3D = new MaterialObject3D();
            var material_for_drug:MaterialObject3D = new WireframeMaterial();
            material_for_drug.doubleSided = true;
            material_for_drug.interactive = true;
            plane_for_drug = new Plane(material_for_drug, 100, 100, 10, 10);
            plane_for_drug.visible = false;
            scene.addChild(plane_for_drug);
            plane_for_drug.addEventListener(InteractiveScene3DEvent.OBJECT_MOVE, moved);

            //var material:MaterialObject3D = new WireframeMaterial();
            var material:BitmapFileMaterial = new BitmapFileMaterial(bar_filename);
            var materialTop:ColorMaterial = new ColorMaterial(0x404040);
            var mlist:MaterialsList = new MaterialsList({all:material, top:materialTop});
            var u:Number = BAR_SIZE * 5 * 4;
            var ground:Cube = new Cube(mlist, u * 5, u * 5, u, 5, 2, 5);
            ground.rotationZ = -10;
            scene.addChild(ground);
            
            viewport.interactive = true;
        }

        public function init_box2d():void {
            var worldAABB:b2AABB = new b2AABB();
            //worldAABB.lowerBound.Set(-1000.0, -1000.0);
            //worldAABB.upperBound.Set(1000.0, 1000.0);
            worldAABB.lowerBound.Set(-100.0, -100.0);
            worldAABB.upperBound.Set(100.0, 100.0);
            m_world = new b2World(worldAABB, new b2Vec2(0.0, 10.0), true);

            var groundSd:b2PolygonDef = new b2PolygonDef();
            var groundBd:b2BodyDef = new b2BodyDef();
            var groundB:b2Body;
            //groundBd.position.Set(0, 20);
            groundBd.angle = -10 / 180 * Math.PI;
            groundSd.SetAsBox(BAR_SIZE * 5, BAR_SIZE);
            groundB = m_world.CreateBody(groundBd);
            groundB.CreateShape(groundSd);
            groundB.SetMassFromShapes();
            
            sd = new b2CircleDef();
            bd = new b2BodyDef();
            sd.density = 1.0;
            sd.radius = RADIUS;
        }
        
        public function createBox():void {
            var materialTop:BitmapFileMaterial = new BitmapFileMaterial(filename);
            var materialSide:ColorMaterial = new ColorMaterial(0x000000);
            materialTop.interactive = true
            materialSide.interactive = true

            for (var i:Number = 0; i < 10; i++){
                var cube:Cylinder = new Cylinder(materialSide, RADIUS * 10, RADIUS * 10 * 4, 8 * 2, 6, -1, true, true, materialTop, materialTop);
                cube.rotationX = 90;
                scene.addChild(cube);
                cubes.push(cube);

                cube.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, pressed);
                cube.addEventListener(InteractiveScene3DEvent.OBJECT_MOVE, moved);
                
                bd.position.Set(0, - 50);
                var b:b2Body = m_world.CreateBody(bd);
                b.CreateShape(sd);
                b.SetMassFromShapes();
                
                cube.userData = new UserData();
                cube.userData.data = b;
            }
        }

        public function pressed(event:InteractiveScene3DEvent):void {
            var x:Number = event.renderHitData.x;
            var y:Number = event.renderHitData.y;
            var z:Number = event.renderHitData.z;

            plane_for_drug.x = x;
            plane_for_drug.y = y;
            plane_for_drug.z = z;
            plane_for_drug.visible = true;
 
            if (!m_mouseJoint) {
                var cube:DisplayObject3D = event.displayObject3D;
                var body:b2Body = cube.userData.data;
                var md:b2MouseJointDef = new b2MouseJointDef();
                md.body1 = m_world.m_groundBody;
                md.body2 = body;
                md.target.Set( - x / 10, - y / 10);
                md.maxForce = 300.0 * body.m_mass;
                md.timeStep = 1.0 / 30;
                m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
                body.WakeUp();
            }
        }
        
        public function moved(event:InteractiveScene3DEvent):void {
            var x:Number = event.renderHitData.x;
            var y:Number = event.renderHitData.y;
            var z:Number = event.renderHitData.z;

            plane_for_drug.x = x;
            plane_for_drug.y = y;

            if (m_mouseJoint) {
                m_mouseJoint.SetTarget(new b2Vec2( - x / 10, - y / 10));
            }
        }
        
        public function released(evt:MouseEvent):void {
            plane_for_drug.visible = false;

            if (m_mouseJoint) {
                m_world.DestroyJoint(m_mouseJoint);
                m_mouseJoint = null;
            }
        }
    }
}



import org.papervision3d.Papervision3D;
import org.papervision3d.core.geom.*;
import org.papervision3d.core.geom.renderables.Triangle3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.core.math.NumberUV;
import org.papervision3d.core.proto.*;    

/**
* The Cylinder class lets you create and display Cylinders.
* <p/>
* The Cylinder is divided in vertical and horizontal segment, the smallest combination is two vertical and three horizontal segments.
*/
/*public*/ class Cylinder extends TriangleMesh3D
{
    /**
    * Number of segments horizontally. Defaults to 8.
    */
    public var segmentsW :Number;

    /**
    * Number of segments vertically. Defaults to 6.
    */
    public var segmentsH :Number;

    /**
    * Default radius of Cylinder if not defined.
    */
    static public const DEFAULT_RADIUS :Number = 100;

    /**
    * Default height if not defined.
    */
    static public const DEFAULT_HEIGHT :Number = 100;

    /**
    * Default scale of Cylinder texture if not defined.
    */
    static public const DEFAULT_SCALE :Number = 1;

    /**
    * Default value of gridX if not defined.
    */
    static public const DEFAULT_SEGMENTSW :Number = 8;

    /**
    * Default value of gridY if not defined.
    */
    static public const DEFAULT_SEGMENTSH :Number = 6;

    /**
    * Minimum value of gridX.
    */
    static public const MIN_SEGMENTSW :Number = 3;

    /**
    * Minimum value of gridY.
    */
    static public const MIN_SEGMENTSH :Number = 1;

    //public var topFace
    // ___________________________________________________________________________________________________
    //                                                                                               N E W
    // NN  NN EEEEEE WW    WW
    // NNN NN EE     WW WW WW
    // NNNNNN EEEE   WWWWWWWW
    // NN NNN EE     WWW  WWW
    // NN  NN EEEEEE WW    WW

    /**
    * Create a new Cylinder object.
    * <p/>
    * @param    material    A MaterialObject3D object that contains the material properties of the object.
    * <p/>
    * @param    radius        [optional] - Desired radius.
    * <p/>
    * @param    segmentsW    [optional] - Number of segments horizontally. Defaults to 8.
    * <p/>
    * @param    segmentsH    [optional] - Number of segments vertically. Defaults to 6.
    * <p/>
    * @param    topRadius    [optional] - An optional parameter for con- or diverging cylinders.
    * <p/>
    * @param    topFace        [optional] - An optional parameter specifying if the top face of the cylinder should be drawn.
    * <p/>
    * @param    bottomFace    [optional] - An optional parameter specifying if the bottom face of the cylinder should be drawn.
    * <p/>
    */
    public function Cylinder( material:MaterialObject3D=null, radius:Number=100, height:Number=100, segmentsW:int=8, segmentsH:int=6, topRadius:Number=-1, topFace:Boolean=true, bottomFace:Boolean=true,topMat:MaterialObject3D = null ,bottomMat:MaterialObject3D = null)
    {
        super( material, new Array(), new Array(), null );

        this.segmentsW = Math.max( MIN_SEGMENTSW, segmentsW || DEFAULT_SEGMENTSW); // Defaults to 8
        this.segmentsH = Math.max( MIN_SEGMENTSH, segmentsH || DEFAULT_SEGMENTSH); // Defaults to 6
        if (radius==0) radius = DEFAULT_RADIUS; // Defaults to 100
        if (height==0) height = DEFAULT_HEIGHT; // Defaults to 100
        if (topRadius==-1) topRadius = radius;

        var scale :Number = DEFAULT_SCALE;

        buildCylinder( radius, height, topRadius, topFace, bottomFace,topMat,bottomMat );
    }

    private function buildCylinder( fRadius:Number, fHeight:Number, fTopRadius:Number, fTopFace:Boolean, fBottomFace:Boolean ,topMat:MaterialObject3D,bottomMat:MaterialObject3D):void
    {
        var matInstance:MaterialObject3D = material;

        var i:Number, j:Number, k:Number;

        var iHor:Number = Math.max(MIN_SEGMENTSW, this.segmentsW);
        var iVer:Number = Math.max(MIN_SEGMENTSH, this.segmentsH);
        var aVertice:Array = this.geometry.vertices;
        var aFace:Array = this.geometry.faces;
        var aVtc:Array = new Array();
        for (j=0;j<(iVer+1);j++) { // vertical
            var fRad1:Number = Number(j/iVer);
            var fZ:Number = fHeight*(j/(iVer+0))-fHeight/2;//-fRadius*Math.cos(fRad1*Math.PI);
            var fRds:Number = fTopRadius+(fRadius-fTopRadius)*(1-j/(iVer));//*Math.sin(fRad1*Math.PI);
            var aRow:Array = new Array();
            var oVtx:Vertex3D;
            for (i=0;i<iHor;i++) { // horizontal
                var fRad2:Number = Number(2*i/iHor);
                var fX:Number = fRds*Math.sin(fRad2*Math.PI);
                var fY:Number = fRds*Math.cos(fRad2*Math.PI);
                //if (!((j==0||j==iVer)&&i>0)) { // top||bottom = 1 vertex
                oVtx = new Vertex3D(fY,fZ,fX);
                aVertice.push(oVtx);
                //}
                aRow.push(oVtx);
            }
            aVtc.push(aRow);
        }
        var iVerNum:int = aVtc.length;

        var aP4uv:NumberUV, aP1uv:NumberUV, aP2uv:NumberUV, aP3uv:NumberUV;
        var aP1:Vertex3D, aP2:Vertex3D, aP3:Vertex3D, aP4:Vertex3D;

        for (j=0;j<iVerNum;j++) {
            var iHorNum:int = aVtc[j].length;
            for (i=0;i<iHorNum;i++) {
                if (j>0&&i>=0) {
                    // select vertices
                    var bEnd:Boolean = i==(iHorNum-0);
                    aP1 = aVtc[j][bEnd?0:i];
                    aP2 = aVtc[j][(i==0?iHorNum:i)-1];
                    aP3 = aVtc[j-1][(i==0?iHorNum:i)-1];
                    aP4 = aVtc[j-1][bEnd?0:i];
                    // uv
                    var fJ0:Number = j        / iVerNum;
                    var fJ1:Number = (j-1)    / iVerNum;
                    var fI0:Number = (i+1)    / iHorNum;
                    var fI1:Number = i        / iHorNum;
                    aP4uv = new NumberUV(fI0,fJ1);
                    aP1uv = new NumberUV(fI0,fJ0);
                    aP2uv = new NumberUV(fI1,fJ0);
                    aP3uv = new NumberUV(fI1,fJ1);
                    // 2 faces
                    aFace.push( new Triangle3D(this, [aP1,aP2,aP3], matInstance, [aP1uv,aP2uv,aP3uv]) );
                    aFace.push( new Triangle3D(this, [aP1,aP3,aP4], matInstance, [aP1uv,aP3uv,aP4uv]) );
                }
            }
            if (j==0||j==(iVerNum-1)) {
                for (i=0;i<(iHorNum-2);i++) {
                    // uv
                    var iI:int = Math.floor(i/2);
                    aP1 = aVtc[j][iI];
                    aP2 = (i%2==0)? (aVtc[j][iHorNum-2-iI]) : (aVtc[j][iI+1]);
                    aP3 = (i%2==0)? (aVtc[j][iHorNum-1-iI]) : (aVtc[j][iHorNum-2-iI]);

                    var bTop:Boolean = j==0;
                    aP1uv = new NumberUV( (bTop?1:0)+(bTop?-1:1)*(aP1.x/fRadius/2+.5), aP1.z/fRadius/2+.5 );
                    aP2uv = new NumberUV( (bTop?1:0)+(bTop?-1:1)*(aP2.x/fRadius/2+.5), aP2.z/fRadius/2+.5 );
                    aP3uv = new NumberUV( (bTop?1:0)+(bTop?-1:1)*(aP3.x/fRadius/2+.5), aP3.z/fRadius/2+.5 );

                    // face
                    if (j == 0) {
                        if(bottomMat)
                        {
                            if (fBottomFace) aFace.push( new Triangle3D(this, [aP1, aP3, aP2], bottomMat, [aP1uv, aP3uv, aP2uv]) );
                        }
                        else
                        {
                            if (fBottomFace) aFace.push( new Triangle3D(this, [aP1, aP3, aP2], matInstance, [aP1uv, aP3uv, aP2uv]) );
                        }

                    }
                    else {

                        if(topMat)
                        {
                            if (fTopFace) aFace.push( new Triangle3D(this, [aP1, aP2, aP3], topMat, [aP1uv, aP2uv, aP3uv]));

                        }
                        else
                        {
                            if (fTopFace) aFace.push( new Triangle3D(this, [aP1, aP2, aP3], matInstance, [aP1uv, aP2uv, aP3uv]));
                        }


                    }
                }
            }
        }
        this.geometry.ready = true;

        if(Papervision3D.useRIGHTHANDED)
            this.geometry.flipFaces();
    }
}