3D (Kamera & Szene / OOP)

Matthias Kannengiesser

Erfahrenes Mitglied
Hi Folks,

Hier was für die 3D - OOP Freaks. Einfach ins erste Frame.

PHP:
cubeData = [{colour:0xff9900, // Unten
            Sides:[
                      {x:-50, y:50, z:50}, // UL
                      {x:-50, y:50, z:-50}, // OL
                      {x:50, y:50, z:-50}, // OR
                      {x:50, y:50, z:50} // UR
                      ]},
            {colour:0xff9900, // Oben
            Sides:[
                      {x:-50, y:-50, z:-50}, // UL
                      {x:-50, y:-50, z:50}, // OL
                      {x:50, y:-50, z:50}, // OR
                      {x:50, y:-50, z:-50} // UR
                      ]},
            {colour:0xff8800, // Rechts
            Sides:[
                      {x:50, y:-50, z:50}, // OR
                      {x:50, y:50, z:50}, // UR
                      {x:50, y:50, z:-50}, // UL
                      {x:50, y:-50, z:-50} // OL
                      ]},
            {colour:0xff8800, // Links
            Sides:[
                      {x:-50, y:-50, z:-50}, // OR
                      {x:-50, y:50, z:-50}, // UR
                      {x:-50, y:50, z:50}, // UL
                      {x:-50, y:-50, z:50} // OL
                      ]},
            {colour:0xffaa00, // Vorne
            Sides:[
                      {x:-50, y:50, z:-50}, // UL
                      {x:-50, y:-50, z:-50}, // OL
                      {x:50, y:-50, z:-50}, // OR
                      {x:50, y:50, z:-50} // UR
                      ]},
            {colour:0xffaa00, // Hinten 
            Sides:[
                      {x:50, y:50, z:50}, // UL
                      {x:50, y:-50, z:50}, // OL
                      {x:-50, y:-50, z:50}, // OR
                      {x:-50, y:50, z:50} // UR
                      ]}
            ];
Objekt3d = function (data3d) {
    this.data3d = data3d;
    this.move3d = function(x, y, z) {
        var k = 0;
        while (k<this.data3d.length) {
            var i = 0;
            while (i<this.data3d[k].Sides.length) {
                this.data3d[k].Sides[i].x = this.data3d[k].Sides[i].x+x;
                this.data3d[k].Sides[i].y = this.data3d[k].Sides[i].y+y;
                this.data3d[k].Sides[i].z = this.data3d[k].Sides[i].z+z;
                i++;
            }
            k++;
        }
    };
    this.scale3d = function(x, y, z) {
        var k = 0;
        while (k<this.data3d.length) {
            var i = 0;
            while (i<this.data3d[k].Sides.length) {
                this.data3d[k].Sides[i].x = this.data3d[k].Sides[i].x*x;
                this.data3d[k].Sides[i].y = this.data3d[k].Sides[i].y*y;
                this.data3d[k].Sides[i].z = this.data3d[k].Sides[i].z*z;
                i++;
            }
            k++;
        }
    };
    this.rotate3d = function(xa, ya) {
        var rad = Math.PI/180;
        var px;
        var py;
        var pz;
        var sin_xa;
        var sin_ya;
        var cos_xa;
        var cos_ya;
        var tempz;
        var k = 0;
        while (k<this.data3d.length) {
            var i = 0;
            while (i<this.data3d[k].Sides.length) {
                px = this.data3d[k].Sides[i].x;
                py = this.data3d[k].Sides[i].y;
                pz = this.data3d[k].Sides[i].z;
                cos_ya = Math.cos(ya*rad);
                sin_ya = Math.sin(ya*rad);
                cos_xa = Math.cos(xa*rad);
                sin_xa = Math.sin(xa*rad);
                tempz = (pz*cos_ya)-(px*sin_ya);
                this.data3d[k].Sides[i].x = (pz*sin_ya)+(px*cos_ya);
                this.data3d[k].Sides[i].y = (py*cos_xa)-(tempz*sin_xa);
                this.data3d[k].Sides[i].z = (py*sin_xa)+(tempz*cos_xa);
                i++;
            }
            k++;
        }
    };
};
Kamera3d = function (Objekt3d, distance, angle, loc) {
    this.Objekt3d = Objekt3d;
    this.distance = distance;
    this.angle = angle;
    this.loc = loc;
    this.mc = this.create();
};
Kamera3d.prototype.create = function() {
    return (this.loc.createEmptyMovieClip("camMC"+Stage.camNum, Stage.camNum++));
};
Kamera3d.prototype.render = function() {
    this.mc.clear();
    var z;
    var pers;
    var sx;
    var sy;
    var tx;
    var ty;
    var polygon;
    var k = 0;
    while (k<this.Objekt3d.data3d.length) {
        polygon = [];
        var i = 0;
        while (i<this.Objekt3d.data3d[k].Sides.length) {
            z = this.Objekt3d.data3d[k].Sides[i].z+this.distance;
            pers = this.angle/z;
            sx = this.Objekt3d.data3d[k].Sides[i].x*pers;
            sy = this.Objekt3d.data3d[k].Sides[i].y*pers;
            polygon.push({sx:sx, sy:sy});
            i++;
        }
        z = ((polygon[1].sx-polygon[0].sx)*(polygon[2].sy-polygon[0].sy))-((polygon[1].sy-polygon[0].sy)*(polygon[2].sx-polygon[0].sx));
        if (z>0) {
            this.mc.beginFill(this.Objekt3d.data3d[k].colour, this.Objekt3d.data3d[k].colour);
            this.mc.moveTo(polygon[0].sx, polygon[0].sy);
            this.mc.lineTo(polygon[1].sx, polygon[1].sy);
            this.mc.lineTo(polygon[2].sx, polygon[2].sy);
            this.mc.lineTo(polygon[3].sx, polygon[3].sy);
            this.mc.lineTo(polygon[0].sx, polygon[0].sy);
        }
        k++;
    }
};
Cube3d = new Objekt3d(cubeData);
Szene3D = new Kamera3d(Cube3d, 200, 400, _root);
Szene3D.render();
Szene3D.mc._x = 275;
Szene3D.mc._y = 175;
_root.onEnterFrame = function() {
    Cube3d.rotate3d((this._xmouse+100)/100, (this._ymouse+200)/100);
    Szene3D.render();
};

Beispiel:
hier zur 3D Engine (Angina)

Be inspired

mfg
Matze K.
 
Das ist ja der Hammer !

Vielen Dank, Matze. ich freu mich wirklich auf Dein Buch. Da scheint 3D plötzlich wieder greifbar.

ganz, ganz toll !!!

thx, stephan
 
hmm, heisst das ich bin der einzige bei dems nicht funzt? bei mir beschwert sich flash immer wegen irgendeiner klammersetzung.. hab null schimmer was falsch ist, wenn überhaupt was falsch ist :-/

*sancho*
 
Sancho kannst es auch von flashstar.de ziehen. Da ist die FLA!

Skyla büchlein ist bereits erschienen und ich bin sicher da ist einiges für dich enthalten.

mfg
Matze K.
 
büchlein ist bereits erschienen und ich bin sicher da ist einiges für dich enthalten

Cool - Titel + ISBN bitte




CU Andreas
-------------------------------------------------------------
"There are only 10 types of people in the world, those that understand binary, and those that don't."
 
Zurück