スポンサーサイト

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

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

[ActionScript 3.0] PaperVision3Dで流体シミュレーション

2007-11-19 | 21:16

後で最適化する予定
package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.filters.*;
    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.*;
    import org.papervision3d.materials.*;

    [SWF(width="600", height="400", backgroundColor="#0")]
        public class Fluid 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 linesX:Array= new Array();
            private var linesZ:Array= new Array();

            private var w:int;
            private var h:int;

            private var buffer:Array = new Array();
            private var renderBuffer:int = 0;

            private var k1:Number;
            private var k2:Number;
            private var k3:Number;

            public function Fluid():void
            {
                stage.frameRate = 20;
                stage.quality   = "MEDIUM";
                stage.scaleMode = "noScale";
                stage.align = StageAlign.TOP_LEFT;

                canvas = new Sprite();
                canvas.x = 300;
                canvas.y = 200;
                stage.addChild( canvas );

                scene = new Scene3D( canvas );

                rootNode = new DisplayObject3D();
                scene.addChild( rootNode );

                var range:int = 80;

                var x:int,y:int,z:int;
                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.x = 100;
                camera.y = 100;
                camera.z = -400;
                camera.focus = 100;
                camera.zoom = 50;

                //stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
                var tm:Timer = new Timer(200);
                tm.addEventListener(TimerEvent.TIMER, loop);
                tm.start();

                scene.renderCamera( camera );

                w = h = range;

                for(var x:int = -range/2 ; x < range/2; x++){
                    var vertexes:Array = new Array();
                    for (var z:int = -range/2 ; z < range/2 ; z++){
                        vertexes.push(new Vertex3D(x,0,z));
                    }
                    var line:Line3D = new Line3D(vertexes, 0xffffff, 0.3, 0.05);
                    linesZ.push(line);
                    scene.addChild(line);
                }
                for(z= -range/2 ; z < range/2; z++){
                    vertexes = new Array();
                    for (x= -range/2 ; x < range/2 ; x++){
                        vertexes.push(new Vertex3D(x,0,z));
                    }
                    line = new Line3D(vertexes, 0xffffff, 0.3, 0.05);
                    linesX.push(line);
                    scene.addChild(line);
                }

                // 変位が格納される3次元配列(2*w*h)
                for(var i:int = 0 ; i < 2; i++){
                    var a:Array = new Array();
                    for(var j:int = 0 ; j < h; j++){
                        var aa:Array = new Array();
                        for(var k:int = 0 ; k < w; k++)
                            aa.push(new Number3D(j,0,k));
                        a.push(aa);
                    }

                    buffer.push(a);
                }

                var d:Number = 1;
                var t:Number = 0.1;
                var c:Number = 2.1;
                var mu:Number = 0.001;

                var f1:Number = c*c*t*t/(d*d);
                var f2:Number = 1/(mu*t+2);

                k1 = (4 - 8*f1)*f2;
                k2 = (mu*t-2)*f2;
                k3 = 2*f1*f2;

                //for(i=0; i < 9 ; i++){
                buffer[renderBuffer][range/2][range/2].y = 1;
                buffer[renderBuffer][range/2][range/2+1].y = 1;
                buffer[renderBuffer][range/2+1][range/2].y = 1;
                buffer[renderBuffer][range/2+1][range/2+1].y = 1;
                //}
            }

            private function evaluate():void{
                for(var j:int = 1 ; j < h-1; j++){
                    var crnt:Array = buffer[renderBuffer][j];
                    var prev:Array = buffer[1-renderBuffer][j];
                    for(var i:int = 1 ; i < w-1; i++){
                        var currentN:Number3D = (Number3D)(buffer[renderBuffer][j + 1][i]);
                        var currentP:Number3D = (Number3D)(buffer[renderBuffer][j - 1][i]);
                        ((Number3D)(prev[i])).y = 
                            k1*((Number3D)(crnt[i])).y + k2*((Number3D)(prev[i])).y + 
                            k3*(((Number3D)(crnt[i+1])).y + ((Number3D)(crnt[i-1])).y
                                    + currentN.y + currentP.y);
                    }
                }

                renderBuffer = 1-renderBuffer;
            }

            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 loop( event:Event ):void
            {
                trace( buffer[renderBuffer][1][0]);
                theta += 0.05;

                for(var x:int = 0 ; x < linesZ.length ; x++){
                    for(var z:int = 0 ; z < h-1 ; z++){
                        linesZ[x].segments[z].vertex1.y = buffer[renderBuffer][x][z].y*3;
                        //if( buffer[renderBuffer][x][z] .y != 0)
                        //   trace( buffer[renderBuffer][x][z]);
                    }
                }
                for(z = 0 ; z < linesX.length ; z++){
                    for(x = 0 ; x < w-1 ; x++){
                        linesX[z].segments[x].vertex1.y = buffer[renderBuffer][x][z].y*3;
                    }
                }


                rotateCamera(0.001);

                evaluate();

                scene.renderCamera(camera);
            }
        }
}

スポンサーサイト

Comment

Post a comment

Secret

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