5D艺术网首页
商城
|
资讯
|
作品
|
博客
|
教程
|
论坛
登录
注册
加为好友
发短消息
来自:好人村
性别:先生
最后登录:2012-08-07
http://jh7086.5d.cn/
大坏人
首页
|
新闻
|
话题
|
博客
|
相册
|
艺术作品
|
社交关系
|
留言板
|
社交圈
2006/04/23 | Flash-3D-多面体-文字-jh
类别(Flash习作)
|
评论
(1)
|
阅读(358)
|
发表于 20:35
在一个国外网站上(http://lab.andre-michelle.com/)发现好多有趣的东西,于是全部下载收藏,偶然找到一段绘制3D多面体的函数(见下面的render函数),作了些修改后加以利用。
var pts = [];
var planes = [];
this.createEmptyMovieClip("scene", 1);
scene._x = 275;
scene._y = 200;
//红,黄,蓝,白,紫,绿
rgbs = [0xff6600, 0xffff66, 0x0099ff, 0xffffff, 0xff66cc, 0x99ff66, 0x663333, 0x003366, 0x336600];
//=======================显示帧速===================
function get_fps() {
var a = this.createEmptyMovieClip("fps_show", 100000);
a.createTextField("fps_text", 0, 0, 0, 16, 18);
a.fps_text.background = true;
a.k = 0;
//a._fps = 0;
a.oldtim = getTimer();
a.onEnterFrame = function() {
this.k++;
var time = getTimer()-this.oldtim;
if (time>=300) {
//this._fps = (this._fps+1000/(getTimer()-this.oldtim))/2;
this.fps_text.text = Math.round(this.k/time*1000);
this.oldtim = getTimer();
this.k = 0;
}
};
}
get_fps();
//=======================加载数据文件===========================
var file_path = _url.slice(0, _url.lastIndexOf("/")+1);
var file_name = _url.slice(_url.lastIndexOf("/")+1, -3)+"jh3d";
var _file = file_path+file_name;
//var _file="jh.jh3d"
var myLoad = new LoadVars();
myLoad.load(_file);
myLoad.onData = function(myData) {
if (myData != undefined) {
var p, rgb, temp;
myData = myData.split("\r\n\r\n");
//点集
pts = myData[0].split("\r\n");
for (p in pts) {
temp = pts[p].split(",");
pts[p] = {};
pts[p].x = -(Number(temp[1]));
pts[p].y = -(Number(temp[2]));
pts[p].z = -(Number(temp[0]));
}
//四边形
planes = myData[1].split("\r\n");
for (p in planes) {
if (p%5 == 4) {
rgb = rgbs[Math.floor(p/5)%6];
}
temp = planes[p].split(",");
planes[p] = [];
planes[p][0] = Number(temp[3]);
planes[p][1] = Number(temp[2]);
planes[p][2] = Number(temp[1]);
planes[p][3] = Number(temp[0]);
planes[p][4] = rgb;
}
trace(pts.length+" "+planes.length);
var id:Number = setInterval(run, 10);
} else {
trace("数据加载失败");
}
};
//==================3D多面体绘制函数===============
render = function (mc:MovieClip, ax:Number, ay:Number, pts:Array, planes:Array) {
mc.clear();
var sin = Math.sin;
var cos = Math.cos;
var sinax = sin(ax);
var cosax = cos(ax);
var sinay = sin(ay);
var cosay = cos(ay);
var tx;
var ty;
var tz;
var p;
for (p in pts) {
var pt = pts[p];
var x = pt.x;
var y = pt.y;
var z = pt.z;
ty = (y*cosax)-(z*sinax);
tz = (y*sinax)+(z*cosax);
y = ty;
z = tz;
tx = (x*cosay)+(z*sinay);
tz = (z*cosay)-(x*sinay);
x = tx;
z = tz;
pt.rx = x;
pt.ry = y;
pt.rz = z;
var pers = (500/(z+300));
pt.sx = x*pers;
pt.sy = y*pers;
}
var zSort = [];
for (p in planes) {
var plane = planes[p];
var mz = ((((((pts[plane[0]].rz+pts[plane[1]].rz)+pts[plane[2]].rz)-1000)+pts[plane[3]].rz)-1000)/4);
zSort.push({id:p, d:1000000-(mz*mz)});
}
zSort.sortOn("d");
for (p in planes) {
var plane = planes[zSort[p].id];
var p0 = pts[plane[0]];
var p1 = pts[plane[1]];
var p2 = pts[plane[2]];
var p3 = pts[plane[3]];
var p0x = p0.sx;
var p0y = p0.sy;
var p1x = p1.sx;
var p1y = p1.sy;
var p2x = p2.sx;
var p2y = p2.sy;
var p3x = p3.sx;
var p3y = p3.sy;
var z = (p1x-p0x)*(p2y-p0y)-(p1y-p0y)*(p2x-p0x);
if (z>0) {
var d01y = p0.ry-p1.ry;
var d12z = p1.rz-p2.rz;
var d01z = p0.rz-p1.rz;
var d12y = p1.ry-p2.ry;
var d12x = p1.rx-p2.rx;
var d01x = p0.rx-p1.rx;
var nx = d01y*d12z-d01z*d12y;
var ny = d01z*d12x-d01x*d12z;
var nz = d01x*d12y-d01y*d12x;
var nLen = Math.sqrt(nx*nx+ny*ny+nz*nz);
var colFac = Math.acos(-nz/nLen)/Math.PI;
var rgb = isNaN(plane[4]) ? 0xffffff : plane[4];
var r = rgb >> 16;
var g = (rgb & 0x00ff00) >> 8;
var b = (rgb & 0x0000ff);
with (mc) {
var col = (colFac*r << 16) | (colFac*g << 8) | (colFac*b);
beginFill(col);
lineStyle(2, 0xffffff, 10);
moveTo(p0x, p0y);
lineTo(p1x, p1y);
lineTo(p2x, p2y);
lineTo(p3x, p3y);
lineTo(p0x, p0y);
endFill();
}
}
}
};
//render(scene, 0, 0, pts, planes);
a = 0;
b = 0;
function run():Void {
a += scene._ymouse/2000;
if(a>Math.PI/4){
a=Math.PI/4
}else if(a<-Math.PI/4){
a=-Math.PI/4
}
b += scene._xmouse/2000;
if(b>Math.PI/4){
b=Math.PI/4
}else if(b<-Math.PI/4){
b=-Math.PI/4
}
render(scene, a, b, pts, planes);
updateAfterEvent();
}
0
评论
Comments
日志分类
首页
[163]
Flash习作
[85]
Flash进修
[33]
Flash推荐
[16]
Flash涂鸦
[4]
听~音乐
[9]
我滴地盘er
[16]