スポンサーサイト

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

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

[ActionScript 3.0] PaperVision3Dで色々な線を書いてみる

2007-11-14 | 20:34


package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.filters.*;
    import flash.geom.*;

    import org.papervision3d.core.geom.*;
    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;

    [SWF(width="500", height="500", backgroundColor="#ffffff")]
        public class PV3DLine extends Sprite{

            private var canvas : Sprite;
            private var scene     : Scene3D;
            private var camera    : Camera3D;
            private var rootNode  : DisplayObject3D;

            private var theta:Number = 0;
            private var spheres:Array = new Array();
            private var line:Line3D;
            private var line2:Line3D;
            private var line3:Line3D;

            public function PV3DLine():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 );

                var range:int = 500;

                var x:int,y:int,z:int;
                var step:int = 200;
                var alpha:Number = 0.1;
                for(x = -range ; x <= range ; x+=step){ 
                    scene.addChild( new Line3D([ new Vertex3D(x,0,-range), new Vertex3D(x,0,range)],
                                0xff0000, alpha, w));
                }
                for(z = -range ; z <= range ; z+=step){ 
                    scene.addChild( new Line3D([ new Vertex3D(-range,0,z), new Vertex3D(range,0,z)],
                                0xff0000, alpha, w));
                }

                scene.addChild( new Line3D([ new Vertex3D(-range,0,0), new Vertex3D(range,0,0)],
                            0xff0000, 0.5, w));
                scene.addChild( new Line3D([ new Vertex3D(0,-range,0), new Vertex3D(0,range,0)],
                            0xff00, 0.5, w));
                scene.addChild( new Line3D([ new Vertex3D(0,0,-range), new Vertex3D(0,0,range)],
                            0xff, 0.5, w));

                camera = new Camera3D();
                camera.y = 200;
                camera.z = -500;
                camera.focus = 100;
                camera.zoom = 4;

                stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

                scene.renderCamera( camera );

                var vertexes:Array = new Array();
                var w:int = 1;
                for (var i:int = -20 ; i < 20 ; i++){
                    vertexes.push(new Vertex3D(i*30,0,0));
                }
                line = new Line3D(vertexes, 0xf50000, 0.5, w);
                scene.addChild(line);

                vertexes = new Array();
                for (var y:int = -20 ; y < 20 ; y++){
                    vertexes.push(new Vertex3D(0,y*30,0));
                }
                line2 = new Line3D(vertexes, 0xf500, 0.5, w);
                scene.addChild(line2);

                vertexes = new Array();
                for (var z:int = -20 ; z < 20 ; z++){
                    vertexes.push(new Vertex3D(0,0,z*30));
                }
                line3 = new Line3D(vertexes, 0xf5, 0.5, w);
                scene.addChild(line3);

            }

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

            private function onEnterFrame( event:Event ):void
            {
                theta += 0.05;
                var i:int;
                var phase:Number = 0.4;
                for(i= 0 ; i < line.segments.length ; i++){
                    line.segments[i].vertex1.y = Math.sin(theta + phase*i )*100;
                }
                line.segments[line.segments.length - 1].vertex2.y = Math.sin(theta + phase*i )*100;

                for(i = 0 ; i < line2.segments.length ; i++){
                    line2.segments[i].vertex1.x = Math.cos(theta + phase*i )*1000/(i+1);
                    line2.segments[i].vertex1.z = Math.sin(theta + phase*i )*1000/(i+1);
                }
                line2.segments[line2.segments.length - 1].vertex2.x = Math.cos(theta + phase*i )*1000/(i+1);
                line2.segments[line2.segments.length - 1].vertex2.z = Math.sin(theta + phase*i )*1000/(i+1);

                for(i = 0 ; i < line3.segments.length ; i++){
                    line3.segments[i].vertex1.x = Math.sin(theta + phase*i )*100;
                }

                rotateCamera(0.003);

                scene.renderCamera( camera );
            }
        }
}

Line関係のソースは http://blog.zupko.info/?p=19から取得した。
スポンサーサイト

Comment

Post a comment

Secret

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