| 用Flash Mx动态画线工具创立实时3D物体 |
| 责任编辑:admin 更新日期:2005-8-6 |
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"] ++;
}
参数的意义如下表:
参数 意义
objtype 字符串;直线,曲线或平面中的其中一个
pointarray 多维数列,见下。
lineweight 数值;直线厚度,以像素计
linecolor 字符串;直线十六进制颜色值
linealpha 数值;直线可见性百分比
fillcolor 字符串;填充的十六进制颜色值
fillalpha 数值;填充的可见性百分比
pointarray(点数列)参数是一个多维数列,用来定义画3D直线,曲线和平面所需的点以及控点的坐标信息。可以在函数内部建立(如上例),也可以把它当成一个全局变量来建立,如下例所示。直线的pointarray参数包含两个数列。第一个数列对应于直线的起始点坐标,第二个对应于终点坐标。下面的actionscript将建立一个直线数列,此数列可被传递给make3Dobj()函数:
Point1 = new Array(0,0,0);
Point2 = new Array(100,0,0);
Pointarray = new Array(Point1, Point2);
曲线的pointarray参数包含三个数列,第一及第二个对应于曲线的起点和终点坐标,第三个则对应于控制点坐标。下面的范例actionscript可创建一个曲线并把它传递给
make3Dobj()函数:
Point1 = new Array(0,0,0);
Point2 = new Array(0,100,0);
Point3 = new Array(50, 50, 0);
Pointarray = new Array(Point1, Point2, Point3);
平面的pointarray参数则包含任意顺序或者数字的直线和曲线数列,范例actionscript将创建一个平面并将它传递给make3Dobj()函数:
Point1 = new Array(0,0,0);
Point2 = new Array(100,0,0);
Lineā1 = new Array(Point1, Point2);
Point3 = new Array(0,0,0);
Point4 = new Array(0,100,0);
Point5 = new Array(50, 50, 0);
Curve1 = new Array(Point3, Point4, Point5);
Pointarray = new Array(Line1, Curve1);
设置变换矩阵
接下来,我们需要一个方法来更新我们的变换矩阵以旋转3D物体。函数settransFormMatrix()可以通过先建立一个临时的变换矩阵然后调用MatrixMatrixmultiply()函数(作用是让矩阵相乘并返回结果)来完成这一任务。如果你读了矩阵计算那一部分,这些方程应该会有点熟悉:
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);
}
}
function MatrixMatrixMultiply(A, B) {
C = new & Array(new Array(), new Array(), new Array());
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;
}
输入的参数x,y和z定义了我们的全局变换矩阵旋转时所围绕的轴线。如果赋予x一个非零值,而y和z都为0的话,这条轴线就是x轴。对其它的轴也一样。
场景转化
最终,我们要建立一个函数来展现创建的3D物体。renderScene()通过在原物体和坐标系旋转后重新画出的物体之间循环来完成这一任务。旋转由完成矩阵乘法的MatrixMatrixMultiply()函数来理。要建立透视感,每个x和y都乘以变量F然后除以z。
以下是演示3d物体的代码:
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];
ysuām += 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));
上一页 [1] [2] [3] 下一页 |
|
| 上一篇文章: 一步一步学ActionScript 2.0 |
| 下一篇文章: as2.0的loading练习 |
|
|
|
|