スポンサーサイト

-------- | --:--

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

[ActionScript 3.0]PaperVision3D

2007-08-07 | 21:21

PaperVision3Dを使ったサンプル

package {
    import flash.display.*;
    import flash.events.*;
 
    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
 
    [SWF(width="400", height="400", backgroundColor="#ffffff")]
    public class CubeSample extends Sprite{

        private var canvas : Sprite;
        private var scene     : Scene3D;
        private var camera    : Camera3D;
        private var rootNode  : DisplayObject3D;
 
        private var ball  : DisplayObject3D;
        private var cube  : DisplayObject3D;
 
        private var vx:Number = 1.5;
        private var vy:Number = 0;
        private var vz:Number = 2;

        public static const GRAVITY:Number = 0.49;
        public static const RADIUS:Number = 50;

        public function CubeSample():void
        {
            stage.frameRate = 60;
            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;
 
            canvas = new Sprite();
            canvas.x = 200;
            canvas.y = 200;
            stage.addChild( canvas );
 
            scene = new Scene3D( canvas );
 
            rootNode = new DisplayObject3D();
            scene.addChild( rootNode );
 
            ball = new Sphere(new ColorMaterial(0xff0000),RADIUS,24,24);
            scene.addChild( ball );
            ball.x = 200;
            ball.y = 400;
            ball.z = 200;
 
            cube = new Cube(new ColorMaterial(0xff0000,0.3), 400,400,400, 12,12,12);
            scene.addChild( cube );
            cube.x = 200;
            cube.y = 200;
            cube.z = 200;

            camera = new Camera3D();
            camera.y = 600;
            camera.z = -800;
            camera.focus = 100;
            camera.zoom = 2;

            stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

            scene.renderCamera( camera );
        }

        private function rotateCamera(rad:Number):void{
            trace(rad);
            var x:Number = camera.x;
            var z:Number = camera.z;
            camera.x = Math.cos(rad)*x - Math.sin(rad)*z;
            camera.z = Math.sin(rad)*x + Math.cos(rad)*z;
        }

        private function onEnterFrame( event:Event ):void
        {
            rotateCamera(0.003);

            vy -= GRAVITY;
            ball.x += vx;
            ball.y += vy;
            ball.z += vz;

            if(ball.x < RADIUS || ball.x > 400 - RADIUS)
                vx *= -1;
            if(ball.y < RADIUS)
                vy *= -1;
            if(ball.z < RADIUS || ball.z > 400 - RADIUS)
                vz *= -1;

            if(ball.y < RADIUS)
                ball.y = RADIUS;
           
            scene.renderCamera( camera );

//            trace(ball.x + " , " + ball.y + " , " + ball.z);
//            trace("v:" + vx + " , " + vy + " , " + vz);
        }
    }
}
スポンサーサイト
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。