スポンサーサイト

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

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

[ActionScript 3.0] PaperVision3Dで曲面表示

2007-12-03 | 20:56

1. 楕円体の方程式を媒介変数表示
x = a * cos(u)* sin(v)
y = b * sin(u) * sin(v)
z = c * cos(v)

2. u,vに対応する点を頂点とする線を引く

という方法で曲面を表示しています

package {
        import flash.display.*;
        import flash.events.*;
        import flash.geom.*;
        import flash.utils.*;
        
        import org.papervision3d.core.*;
        import org.papervision3d.core.geom.*;
        import org.papervision3d.scenes.*;
        import org.papervision3d.objects.*;
        import org.papervision3d.cameras.*;
        
        [SWF(width="600", height="600", backgroundColor="#0")]
        public class Parametric3D extends Sprite{
                
                private var canvas : Sprite;
                private var scene     : MovieScene3D;
                private var camera    : Camera3D;
                
                private var linesU:Array= new Array();
                private var linesV:Array= new Array();
                
                private var range:int = 40;
                
                private var WIDTH:int = 600;
                private var HEIGHT:int = 600;
                
                public function Parametric3D():void
                {
                        stage.frameRate = 20;
                        stage.quality   = "MEDIUM";
                        stage.scaleMode = "noScale";
                        stage.align = StageAlign.TOP_LEFT;
                        
                        canvas = new Sprite();
                        canvas.x = WIDTH/2;
                        canvas.y = HEIGHT/2;
                        stage.addChild( canvas );
                        
                        scene = new MovieScene3D( canvas );
                        
                        var x:int,y:int,z:int;
                        
                        scene.addChild( new Line3D([ new Vertex3D(-range*5,0,0), new Vertex3D(range*5,0,0)],
                                                                                              0xff0000, 0.5, 0.2));
                        scene.addChild( new Line3D([ new Vertex3D(0,-range*5,0), new Vertex3D(0,range*5,0)],
                                                                                              0xff00, 0.5, 0.2));
                        scene.addChild( new Line3D([ new Vertex3D(0,0,-range*5), new Vertex3D(0,0,range*5)],
                                                                                              0xff, 0.5, 0.2));
                        
                        
                        camera = new Camera3D();
                        camera.x = 200;
                        camera.y = 200;
                        camera.z = -800;
                        camera.focus = 100;
                        camera.zoom = 20;
                        
                        scene.renderCamera( camera );
                        
                        
                        render();
                        
                        
                        stage.addEventListener(MouseEvent.CLICK, function(e:*):void{
                        });
                        stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:*):void{
                                var rad:Number = ( WIDTH/2- stage.mouseX)/WIDTH*(Math.PI)*2;
                                camera.x = Math.cos(rad)*100 - Math.sin(rad)*(-HEIGHT);
                                camera.z = Math.sin(rad)*100 + Math.cos (rad)*(-HEIGHT);
                                
                                camera.y = (stage.mouseY - HEIGHT/2);
                                
                                scene.renderCamera( camera );
                        });
                }
                
                
                private function rotateCamera(rad:Number):void{
                        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 render():void
                {
                        var m:Model = new Model(range);
                        m.calc();
                        for(var i:int = 0 ; i < linesU.length ; i++)
                        scene.removeChild(linesU[i]);
                        for(i = 0 ; i < linesV.length ; i++)
                        scene.removeChild(linesV[i]);
                        
                        linesU = linesV = [];
                        
                        for(var u:int = 0 ; u < range ; u++){
                                var vertexes:Array = [];
                                for(var v:int = 0 ; v < range ; v++){
                                        vertexes.push(new Vertex3D(m.buffer[u][v].x, m.buffer[u][v].y, m.buffer[u][v].z));
                                }
                                var line:Line3D = new Line3D(vertexes, 0xffffff, 0.3, 0.05);
                                linesV.push(line);
                                scene.addChild(line);
                        }
                        for(v = 0 ; v < range ; v++){
                                vertexes= [];
                                for(u = 0 ; u < range ; u++){
                                        vertexes.push(new Vertex3D(m.buffer[u][v].x, m.buffer[u][v].y, m.buffer[u][v].z));
                                }
                                line = new Line3D(vertexes, 0xffffff, 0.3, 0.05);
                                linesU.push(line);
                                scene.addChild(line);
                        }
                        
                        scene.renderCamera (camera);
                }
                
        }
}

internal class Model{
        public var buffer:Array = new Array();
        
        private var range:int ;
        private var splitCnt:int = 0;
        private var base:Number = 10;
        
        public function Model(range:int){
                this.range = range;
                clear();
        }
        
        public function clear():void{
                buffer = [];
                for(var j:int = 0 ; j < range; j++){
                        var aa:Array = new Array();
                        for(var k:int = 0 ; k < range; k++)
                        aa.push(new Object());
                        buffer.push(aa);
                }
        }
        
        public function calc():void{
                for (var u:int = 0 ; u < range ; u++){
                        for (var v:int = 0 ; v < range ; v++){
                                
                                buffer[u][v] = {x:40* Math.cos(Math.PI*2*u/range)*Math.sin(Math.PI*2*v/range),
                                                                                                        y:40*Math.sin(Math.PI*2*u/range)*Math.sin(Math.PI*2*v/range),
                                                                                                                                                  z:90*Math.cos(Math.PI*2*v/range)};
                        }
                }
        }
}

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