Modus ponens
модератор форума
Регистрация: Jul 2006
Адрес: #1=(list #1#)
Сообщений: 8,049
|
у Гурского был пример с рисованием кубика в перспективе... кажется...
Код:
/* */
InitMovie();
InitScene();
/* InitMovie() initializes the global variables in the movie. Takes no parameters. */
function InitMovie() {
TransformMatrix = [[1,0,0], [0,1,0], [0,0,1], [0,0,0]];
f = 200;
moviewidth = 600;
movieheight = 400;
lines = 0;
curves = 0;
surfaces = 0;
}
/* InitScene() initializes the 3D scene, creating movie clips for each 3D object. Takes no parameters. */
function InitScene() {
createEmptyMovieClip("Scene", 1);
Scene._x = moviewidth/2;
Scene._y = movieheight/2;
make3Dobj("surface", [[[100,100,50], [100,-100,50], [0,0,50]], [[100,-100,50], [-100,-100,50]], [[-100,-100,50], [-100,100,50], [0,0,50]], [[-100,100,50], [100,100,50]]], 1, "0x000000", 0, "0xff0000", 100);
make3Dobj("surface", [[[100,100,30], [100,-100,30], [0,0,30]], [[100,-100,30], [-100,-100,30]], [[-100,-100,30], [-100,100,30], [0,0,30]], [[-100,100,30], [100,100,30]]], 1, "0x000000", 0, "0xff9900", 100);
make3Dobj("surface", [[[100,100,10], [100,-100,10], [0,0,10]], [[100,-100,10], [-100,-100,10]], [[-100,-100,10], [-100,100,10], [0,0,10]], [[-100,100,10], [100,100,10]]], 1, "0x000000", 0, "0xffff00", 100);
make3Dobj("surface", [[[100,100,-10], [100,-100,-10], [0,0,-10]], [[100,-100,-10], [-100,-100,-10]], [[-100,-100,-10], [-100,100,-10], [0,0,-10]], [[-100,100,-10], [100,100,-10]]], 1, "0x000000", 0, "0x00ff00", 100);
make3Dobj("surface", [[[100,100,-30], [100,-100,-30], [0,0,-30]], [[100,-100,-30], [-100,-100,-30]], [[-100,-100,-30], [-100,100,-30], [0,0,-30]], [[-100,100,-30], [100,100,-30]]], 1, "0x000000", 0, "0x0000ff", 100);
make3Dobj("surface", [[[100,100,-50], [100,-100,-50], [0,0,-50]], [[100,-100,-50], [-100,-100,-50]], [[-100,-100,-50], [-100,100,-50], [0,0,-50]], [[-100,100,-50], [100,100,-50]]], 1, "0x000000", 0, "0x660066", 100);
}
/* Create a 3D object using the parameters specified in function call. */
function make3Dobj(objtype, pointarray, lineweight, linecolour, linealpha, fillcolour, fillalpha) {
obj = Scene.createEmptyMovieClip(objtype + "_" + this[objtype + "s"], lines+curves+surfaces);
obj.pointarray = pointarray;
obj.lineweight = lineweight;
obj.linecolour = linecolour;
obj.linealpha = linealpha;
obj.fillcolour = fillcolour;
obj.fillalpha = fillalpha;
this[objtype + "s"] ++;
}
/* Render the Scene, loop through all of the 3D objects and draw them using points calculated from transformation matrix. Takes no parameters. */
function RenderScene() {
i = 0;
while (i <= lines-1) {
obj = Scene["line_"+i];
obj.clear();
obj.lineStyle(obj.lineweight, obj.linecolour, obj.linealpha);
point1 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[0]);
point2 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[1]);
obj.moveTo(point1[0]/(1-(point1[2]/f)), point1[1]/(1-(point1[2]/f)));
obj.lineTo(point2[0]/(1-(point2[2]/f)), point2[1]/(1-(point2[2]/f)));
camdist = Math.sqrt(Math.pow((point1[0]+point2[0])/2,2)+Math.pow((point1[1]+point2[1])/2,2)+Math.pow(f-((point1[2]+point2[2])/2),2));
obj.swapDepths(Math.pow(f,3)-(Math.floor(camdist*100)));
i ++;
}
i = 0;
while (i <= curves-1) {
obj = Scene["curve_"+i];
obj.clear();
obj.lineStyle(obj.lineweight, obj.linecolour, obj.linealpha);
point1 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[0]);
point2 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[1]);
point3 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[2]);
obj.moveTo(point1[0]/(1-(point1[2]/f)), point1[1]/(1-(point1[2]/f)));
obj.curveTo(point3[0]/(1-(point3[2]/f)), point3[1]/(1-(point3[2]/f)), point2[0]/(1-(point2[2]/f)), point2[1]/(1-(point2[2]/f)));
camdist = Math.sqrt(Math.pow((point1[0]+point2[0]+point3[0])/3,2)+Math.pow((point1[1]+point2[1]+point3[1])/3,2)+Math.pow(f-((point1[2]+point2[2]+point3[2])/3),2));
obj.swapDepths(Math.pow(f,3)-(Math.floor(camdist*100)));
i ++;
}
i = 0;
while (i <= surfaces-1) {
obj = Scene["surface_"+i];
obj.clear();
obj.lineStyle(obj.lineweight, obj.linecolour, obj.linealpha);
obj.beginFill(obj.fillcolour, obj.fillalpha);
j = 0;
xsum = ysum = zsum = 0;
while (j <= obj.pointarray.length-1) {
if (j == 0) {
point1 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[j][0]);
obj.moveTo(point1[0]/(1-(point1[2]/f)), point1[1]/(1-(point1[2]/f)));
xsum += point1[0];
ysum += point1[1];
zsum += point1[2];
}
if (obj.pointarray[j].length == 2) {
point2 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[j][1]);
obj.lineTo(point2[0]/(1-(point2[2]/f)), point2[1]/(1-(point2[2]/f)));
xsum += point2[0];
ysum += point2[1];
zsum += point2[2];
} else {
point1 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[j][0]);
point2 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[j][1]);
point3 = MatrixVectorMultiply(TransformMatrix, obj.pointarray[j][2]);
obj.curveTo(point3[0]/(1-(point3[2]/f)), point3[1]/(1-(point3[2]/f)), point2[0]/(1-(point2[2]/f)), point2[1]/(1-(point2[2]/f)));
xsum += point1[0]+point2[0]+point3[0];
ysum += point1[1]+point2[1]+point3[1];
zsum += point1[2]+point2[2]+point3[2];
}
camdist = Math.sqrt(Math.pow((xsum)/(j+1),2)+Math.pow((ysum)/(j+1),2)+Math.pow(f-((zsum)/(j+1)),2));
obj.swapDepths(Math.pow(f,3)-(Math.floor(camdist*100)));
j ++;
}
obj.endFill();
i ++;
}
}
/* Sets the global transformation matrix using matrix multiplication routine. Temporary transformation matrix generated from x, y, z coordinates passed as parameters. */
function SetTransformMatrix(x, y, z, M) {
vectorLength = Math.sqrt(x*x+y*y+z*z);
if (vectorLength>.0001) {
x /= vectorLength;
y /= vectorLength;
z /= vectorLength;
Theta = vectorLength/500;
cosT = Math.cos(Theta);
sinT = Math.sin(Theta);
tanT = 1-cosT;
T =[[], [], []];
T[0][0] = tanT*x*x+cosT;
T[0][1] = tanT*x*y-sinT*z;
T[0][2] = tanT*x*z+sinT*y;
T[1][0] = tanT*x*y+sinT*z;
T[1][1] = tanT*y*y+cosT;
T[1][2] = tanT*y*z-sinT*x;
T[2][0] = tanT*x*z-sinT*y;
T[2][1] = tanT*y*z+sinT*x;
T[2][2] = tanT*z*z+cosT;
TransformMatrix = MatrixMatrixMultiply(T, M);
}
}
/* Performs a matrix-matrix multiplication with matrix A and matrix B parameters. Returns matrix C as result. */
function MatrixMatrixMultiply(A, B) {
C = [[], [], []];
C[0][0] = A[0][0]*B[0][0]+A[0][1]*B[1][0]+A[0][2]*B[2][0];
C[0][1] = A[0][0]*B[0][1]+A[0][1]*B[1][1]+A[0][2]*B[2][1];
C[0][2] = A[0][0]*B[0][2]+A[0][1]*B[1][2]+A[0][2]*B[2][2];
C[1][0] = A[1][0]*B[0][0]+A[1][1]*B[1][0]+A[1][2]*B[2][0];
C[1][1] = A[1][0]*B[0][1]+A[1][1]*B[1][1]+A[1][2]*B[2][1];
C[1][2] = A[1][0]*B[0][2]+A[1][1]*B[1][2]+A[1][2]*B[2][2];
C[2][0] = A[2][0]*B[0][0]+A[2][1]*B[1][0]+A[2][2]*B[2][0];
C[2][1] = A[2][0]*B[0][1]+A[2][1]*B[1][1]+A[2][2]*B[2][1];
C[2][2] = A[2][0]*B[0][2]+A[2][1]*B[1][2]+A[2][2]*B[2][2];
return C;
}
/* Performs a matrix-vector multiplication with matrix A and vector B parameters. Returns vector C as result. */
function MatrixVectorMultiply(A, B) {
C = [];
C[0] = A[0][0]*B[0]+A[0][1]*B[1]+A[0][2]*B[2];
C[1] = A[1][0]*B[0]+A[1][1]*B[1]+A[1][2]*B[2];
C[2] = A[2][0]*B[0]+A[2][1]*B[1]+A[2][2]*B[2];
return C;
}
Этому примеру сто лет в обед... но чет ничего другого не нашел...
__________________
Hell is the possibility of sanity
|