如何从我的[原始]体素算法中挤出额外的 10 FPS?

发布于 2024-11-25 12:17:01 字数 2085 浏览 5 评论 0原文

所以,我用 JavaScript 制作了这个简单的 4dof 体素算法,我为自己感到非常自豪,但是我没有足够的时间来调试,并且我已经失去了几天的睡眠,但是我注意到它运行得相对缓慢。

现在,明显的 %-scaling 和 setInterval 循环减慢了速度,但我仅在 100 个体素上获得了低于 20 FPS 的速度,其中大多数体素甚至不在屏幕上。

请原谅下面的代码,它是针对我的原始小游戏引擎改编的,所以它不会在自己的页面上运行,但你可以在这个URL(http://nextgengame.webs.com/fun/sandbox.js)上测试它。嗯)。

world=new Array();

for(i=0;i<100;i++){
world[i]=[Math.random()*50-25,-3,Math.random()*50-25,['#FFFFFF','#FF0000','#00FF00','#0000FF','#FFFF33'][Math.round(Math.random()*4)]];
}

r=0;
camx=0;
camy=0;
camz=0;

fps=0;
frames=0;

window.setInterval(function(){
fps=frames;
frames=0;
},1000);

window.setInterval(function(){;
r+=0.02;
if(r>Math.PI*2)r=0;

points=[];

for(i=0;i<world.length;i++){;
u=world[i];
x1=u[0]-camx;
z1=u[2]-camz;
z2=x1*Math.sin(r)+z1*Math.cos(r);
z3=Math.round(z2);
if(!points[z3])points[z3]=[];
points[z3][points[z3].length]=[x1*Math.cos(r)-z1*Math.sin(r),u[1]-camy,z2,u[3]];
};

for(z=points.length-1;z>=0;z--){;
t=points[z];
if(t){;
for(i=0;i<t.length;i++){;
u=t[i];
z1=u[2];
z2=0-z1;
s=69/z1;
h=s*-0.5+50;
drawBox(u[0]/z1*50+h,u[1]/z2*50+h,s,s,u[3]);
};
};
};
frames+=1;
centertext(50,0,String(fps),'#FFFFFF');
sync();
},33);

这对于拍摄风景或其他任何东西来说几乎是不够的,我希望至少获得 10 FPS 的提升。

这就是没有旋转时的样子:

world=[];
points=[];
for(i=0;i<100;i++){
world[i]=[Math.random()*50-25,-3,Math.random()*50-25,['#FFFFFF','#FF0000','#00FF00','#0000FF','#FFFF33'][Math.round(Math.random()*4)]];
}

r=0;
camx=0;
camy=0;
camz=0;

fps=0;
frames=0;

window.setInterval(function(){
fps=frames;
frames=0;
},1000);


for(i=0;i<world.length;i++){;
u=world[i];
z3=Math.round(u[2]);
if(!points[z3])points[z3]=[];
points[z3][points[z3].length]=u;
};

window.setInterval(function(){;

for(z=points.length-1;z>=0;z--){;
t=points[z];
if(t){;
for(i=0;i<t.length;i++){;
u=t[i];
z1=u[2];
z2=0-z1;
s=69/z1;
h=s*-0.5+50;
drawBox(u[0]/z1*50+h,u[1]/z2*50+h,s,s,u[3]);
};
};
};
frames+=1;
centertext(50,0,String(fps),'#FFFFFF');
sync();
},33);

你相信吗,它仍然以大约相同的速度运行!最多 4 FPS 提升!

如果有人能找到一种运行速度更快的方法,那我真的会很高兴。

So, I made this simple 4dof voxel algorithm in JavaScript, and I feel quite proud of myself, but I don't have near enough time to debug and I've lost several days worth of sleep, however I notice that it's running relatively slowly.

Now, there are the obvious %-scaling and setInterval loops slowing it down, but I am getting less than 20 FPS on only 100 voxels, most of which aren't even on screen.

Please excuse the following code, it is adapted for my primitive little game engine, so it won't run on its own page, but you can test it at this URL (http://nextgengame.webs.com/fun/sandbox.htm).

world=new Array();

for(i=0;i<100;i++){
world[i]=[Math.random()*50-25,-3,Math.random()*50-25,['#FFFFFF','#FF0000','#00FF00','#0000FF','#FFFF33'][Math.round(Math.random()*4)]];
}

r=0;
camx=0;
camy=0;
camz=0;

fps=0;
frames=0;

window.setInterval(function(){
fps=frames;
frames=0;
},1000);

window.setInterval(function(){;
r+=0.02;
if(r>Math.PI*2)r=0;

points=[];

for(i=0;i<world.length;i++){;
u=world[i];
x1=u[0]-camx;
z1=u[2]-camz;
z2=x1*Math.sin(r)+z1*Math.cos(r);
z3=Math.round(z2);
if(!points[z3])points[z3]=[];
points[z3][points[z3].length]=[x1*Math.cos(r)-z1*Math.sin(r),u[1]-camy,z2,u[3]];
};

for(z=points.length-1;z>=0;z--){;
t=points[z];
if(t){;
for(i=0;i<t.length;i++){;
u=t[i];
z1=u[2];
z2=0-z1;
s=69/z1;
h=s*-0.5+50;
drawBox(u[0]/z1*50+h,u[1]/z2*50+h,s,s,u[3]);
};
};
};
frames+=1;
centertext(50,0,String(fps),'#FFFFFF');
sync();
},33);

This is hardly adequate to do a landscape or anything, and I want to get at least a 10 FPS boost.

This is what it looks like without rotations:

world=[];
points=[];
for(i=0;i<100;i++){
world[i]=[Math.random()*50-25,-3,Math.random()*50-25,['#FFFFFF','#FF0000','#00FF00','#0000FF','#FFFF33'][Math.round(Math.random()*4)]];
}

r=0;
camx=0;
camy=0;
camz=0;

fps=0;
frames=0;

window.setInterval(function(){
fps=frames;
frames=0;
},1000);


for(i=0;i<world.length;i++){;
u=world[i];
z3=Math.round(u[2]);
if(!points[z3])points[z3]=[];
points[z3][points[z3].length]=u;
};

window.setInterval(function(){;

for(z=points.length-1;z>=0;z--){;
t=points[z];
if(t){;
for(i=0;i<t.length;i++){;
u=t[i];
z1=u[2];
z2=0-z1;
s=69/z1;
h=s*-0.5+50;
drawBox(u[0]/z1*50+h,u[1]/z2*50+h,s,s,u[3]);
};
};
};
frames+=1;
centertext(50,0,String(fps),'#FFFFFF');
sync();
},33);

And would you believe it, it still runs at about the same speed! a 4 FPS increase at most!

It would really make my day if somebody had a way of running this a lot faster.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2024-12-02 12:17:01

一件明显的事情是为您的辐射值预先计算值,这意味着您将拥有两个具有正弦和余弦的数组,增量为 0.02,最大为 2 x pi(这是一个完整的圆)。

这些计算对引擎来说确实很繁重。

此外,您还对动画使用 setInterval,但间隔时间为 33 毫秒,因此最大帧率为 30 fps。 setInterval 上的计时也不是精确的测量,因为它可能会被其他因素(其他计时器等)延迟,所以也许您最好尝试尽快运行它们并将动画调整为 fps反而?

并使用较低值的 setTimeout 只是为了在帧之间释放引擎以执行其他任务,而不是耗尽 CPU。

One obvious thing would be to have precalculated values for your radiant values, meaning that you'll have two arrays with sinus and cosinus in increments of 0.02, up to 2 x pi (which is a complete circle).

Those calculations are really heavy on the engine.

Also, you are using setInterval for the animation, however in 33 ms intervals, that leaves you with a maximum of 30 fps. the timing on setInterval isn't an exact measurement either, because it can be delayed by other factors (other timers etc), so maybe you would be better of to just try to run them as soon as possible and adjust the animation to the fps instead?

And use a setTimeout with a low value just to free up the engine for other task in between the frames., to not exhaust the CPU.

掐死时间 2024-12-02 12:17:01

正如 ThiefMaster 上面所写,使用局部变量(参见 http://www.webreference.com/programming/javascript /jkm3)。你的循环中有很多计算。其中一些可以预先计算吗?您真的需要所有这些变量,还是可以通过编写稍微复杂的表达式来省略临时变量?

As ThiefMaster wrote above, use local variables (see http://www.webreference.com/programming/javascript/jkm3). There are lots of calculations inside your loops. Could some of them be pre-calculated? Do you really need all those variables, or could the temporary ones be omitted by writing slightly more complex expressions?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文