背景: #EDF0F5 #FAFBE6 #FFF2E2 #FDE6E0 #F3FFE1 #DAFAF3 #EAEAEF 默认  
阅读新闻

用编程方式实现3D效果

[日期:2004-10-03] 来源:  作者: [字体: ]
点击浏览该文件
点击开新窗口欣赏该FLASH动画![全屏欣赏]

这是源文件
点击浏览该文件
//照相机和屏幕的距离
d = 350;
//原点坐标 X
center_x = 0;
//原点坐标 Y
center_y = 0;
pmx = 0;
pmy = 0;
jl = 0;
//得到景深
function GetRatio(z) {
return d/(d+z);
}
function ChangeJl(newvalue) {
jl = newvalue;
}
//改变坐标轴原点
function ChangeOx(newx, newy) {
var ox = center_x;
var oy = center_y;
var opmx = pmx;
var opmy = pmy;
center_x = newx;
center_y = newy;
ChangeMd((ox+opmx)-newx, newy-(oy-opmy));
}
function ChangeMd(newx, newy) {
pmx = newx;
ey._x = center_x+newx;
pmy = newy;
ex._y = center_y-newy;
}
function Change(v, z) {
return GetRatio(z)*v;
}
//变换坐标3D-2D
function ChangeX(x, z) {
z += jl;
return center_x+(d*x+z*pmx)/(z+d);
}
//function ChangeX(x, z) {
// return center_x+GetRatio(z)*x;
//}
//function ChangeY(y, z) {
// return center_y-GetRatio(z)*y+pmy;
//}
function ChangeY(y, z) {
z += jl;
return center_y-(d*y+z*pmy)/(z+d);
}
//取得弧度值
function GetFuduFxy(x, y) {
return Math.atan2(y, x);
}
//取得弧度值
function GetFudu(a) {
return a*Math.PI/180;
}
//取得角度值
function GetJiaodu(fd) {
return fu*180/Math.PI;
}
//定义2维点 对象
function point2d(x, y) {
this.x = x;
this.y = y;
}
//定义3维点 对象
function point3d(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
//按X轴旋转
function RotateX(p3d, a) {
var x1 = p3d.x;
var y1 = p3d.y*Math.cos(GetFudu(a))-p3d.z*Math.sin(GetFudu(a));
var z1 = p3d.z*Math.cos(GetFudu(a))+p3d.y*Math.sin(GetFudu(a));
//return new point2d(ChangeX(x1, z1), ChangeY(y1, z1));
return new point3d(x1, y1, z1);
}
//按Y轴旋转
function RotateY(p3d, a) {
var x1 = p3d.x*Math.cos(GetFudu(a))-p3d.z*Math.sin(GetFudu(a));
//var x1=Math.sin(GetFudu(a))*p3d.z;
//var z1=Math.cos(GetFudu(a))*p3d.z;
var y1 = p3d.y;
var z1 = p3d.z*Math.cos(GetFudu(a))+p3d.x*Math.sin(GetFudu(a));
//return new point2d(ChaneX(x1, z1), ChangeY(y1, z1));
return new point3d(x1, y1, z1);
}
//按Z轴旋转
function RotateZ(p3d, a) {
var x1 = p3d.x*Math.cos(GetFudu(a))-p3d.y*Math.sin(GetFudu(a));
var y1 = p3d.y*Math.cos(GetFudu(a))+p3d.x*Math.sin(GetFudu(a));
var z1 = p3d.z;
// return new point2d(ChaneX(x1, z1), ChangeY(y1, z1));
return new point3d(x1, y1, z1);
}
function mMoveTo(p) {
moveTo(ChangeX(p.x, p.z), ChangeY(p.y, p.z));
}
//3维中画线
function Line(p1, p2) {
moveTo(ChangeX(p1.x, p1.z), ChangeY(p1.y, p1.z));
lineTo(ChangeX(p2.x, p2.z), ChangeY(p2.y, p2.z));
}
function aLineTo(p1) {
lineTo(ChangeX(p1.x, p1.z), ChangeY(p1.y, p1.z));
}
//3维中画矩形
function DrawBox(p1, p2, p3, p4) {
Line(p1, p2);
Line(p2, p3);
Line(p3, p4);
Line(p4, p1);
}
function DrawLFT(pos_x, pos_y, pos_z) {
clear();
lineStyle(1, 0xff0000);
mMoveTo(new point3d(pos_x[0], pos_y[0], pos_z[0]));
aLineTo(new point3d(pos_x[1], pos_y[1], pos_z[1]));
aLineTo(new point3d(pos_x[2], pos_y[2], pos_z[2]));
aLineTo(new point3d(pos_x[3], pos_y[3], pos_z[3]));
aLineTo(new point3d(pos_x[0], pos_y[0], pos_z[0]));
mMoveTo(new point3d(pos_x[4], pos_y[4], pos_z[4]));
aLineTo(new point3d(pos_x[5], pos_y[5], pos_z[5]));
aLineTo(new point3d(pos_x[6], pos_y[6], pos_z[6]));
aLineTo(new point3d(pos_x[7], pos_y[7], pos_z[7]));
aLineTo(new point3d(pos_x[4], pos_y[4], pos_z[4]));
Line(new point3d(pos_x[0], pos_y[0], pos_z[0]), new point3d(pos_x[4], pos_y[4], pos_z[4]));
Line(new point3d(pos_x[1], pos_y[1], pos_z[1]), new point3d(pos_x[5], pos_y[5], pos_z[5]));
Line(new point3d(pos_x[2], pos_y[2], pos_z[2]), new point3d(pos_x[6], pos_y[6], pos_z[6]));
Line(new point3d(pos_x[3], pos_y[3], pos_z[3]), new point3d(pos_x[7], pos_y[7], pos_z[7]));
}
function DrawLFT2(Points) {
clear();
lineStyle(1, 0xff0000);
//
mMoveTo(Points[0]);
aLineTo(Points[1]);
aLineTo(Points[2]);
aLineTo(Points[3]);
aLineTo(Points[0]);
//
mMoveTo(Points[4]);
aLineTo(Points[5]);
aLineTo(Points[6]);
aLineTo(Points[7]);
aLineTo(Points[8]);
//
Line(Points[0], Points[4]);
Line(Points[1], Points[5]);
Line(Points[2], Points[6]);
Line(Points[3], Points[7]);
}
//**************************************************************
//**************************************************************
fx = 1;
onLoad = function () {
ChangeOx(150, 300);
// ChangeMd(150, 150);
ChangeMd(0, 0);
pos_x = new Array(-50, 50, 50, -50, -50, 50, 50, -50);
pos_y = new Array(-50, -50, 50, 50, -50, -50, 50, 50);
pos_z = new Array(-50, -50, -50, -50, 50, 50, 50, 50);
pos_xo = new Array(-50, 50, 50, -50, -50, 50, 50, -50);
pos_yo = new Array(-50, -50, 50, 50, -50, -50, 50, 50);
pos_zo = new Array(-50, -50, -50, -50, 50, 50, 50, 50);
jd = 0;
b = 0.001*180/Math.PI;
drawe();
};
onEnterFrame = function () {
if (jd<-360) {
jd = 0;
}
for (var i = 0; i<8; i++) {
var p=new point3d(pos_x[i], pos_y[i], pos_z[i]);
if (xzc.getValue()) {
p = RotateX(new point3d(pos_x[i], pos_y[i], pos_z[i]), 15);
}
if (yzc.getValue()) {
p = RotateY(p, 15);
}
if (zzc.getValue()) {
p = RotateZ(p, 15);
}
pos_x[i] = p.x;
pos_y[i] = p.y;
pos_z[i] = p.z;
}
drawe();
jd -= 5;
};
function drawe() {
clear();
lineStyle(1, 0xff0000);
mMoveTo(new point3d(pos_x[0], pos_y[0], pos_z[0]));
aLineTo(new point3d(pos_x[1], pos_y[1], pos_z[1]));
aLineTo(new point3d(pos_x[2], pos_y[2], pos_z[2]));
aLineTo(new point3d(pos_x[3], pos_y[3], pos_z[3]));
aLineTo(new point3d(pos_x[0], pos_y[0], pos_z[0]));
mMoveTo(new point3d(pos_x[4], pos_y[4], pos_z[4]));
aLineTo(new point3d(pos_x[5], pos_y[5], pos_z[5]));
aLineTo(new point3d(pos_x[6], pos_y[6], pos_z[6]));
aLineTo(new point3d(pos_x[7], pos_y[7], pos_z[7]));
aLineTo(new point3d(pos_x[4], pos_y[4], pos_z[4]));
Line(new point3d(pos_x[0], pos_y[0], pos_z[0]), new point3d(pos_x[4], pos_y[4], pos_z[4]));
Line(new point3d(pos_x[1], pos_y[1], pos_z[1]), new point3d(pos_x[5], pos_y[5], pos_z[5]));
Line(new point3d(pos_x[2], pos_y[2], pos_z[2]), new point3d(pos_x[6], pos_y[6], pos_z[6]));
Line(new point3d(pos_x[3], pos_y[3], pos_z[3]), new point3d(pos_x[7], pos_y[7], pos_z[7]));
}
阅读:
录入:admin

推荐 】 【 打印
上一篇:键盘的响应效果
下一篇:sound对象在FLASH中的应用
相关新闻      
本文评论       全部评论
发表评论


点评: 字数
姓名:

  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款