javascript - How can I squeeze an additional 10 FPS out of my [primitive] voxel algorithm? -


so, made simple 4dof voxel algorithm in javascript, , feel quite proud of myself, don't have near enough time debug , i've lost several days worth of sleep, notice it's running relatively slowly.

now, there obvious %-scaling , setinterval loops slowing down, getting less 20 fps on 100 voxels, of aren't on screen.

please excuse following code, adapted primitive little game engine, won't run on own page, can test @ 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 hardly adequate landscape or anything, , want @ least 10 fps boost.

this looks 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 believe it, still runs @ same speed! 4 fps increase @ most!

it make day if had way of running lot faster.

one obvious thing have precalculated values radiant values, meaning you'll have 2 arrays sinus , cosinus in increments of 0.02, 2 x pi (which complete circle).

those calculations heavy on engine.

also, using setinterval animation, in 33 ms intervals, leaves maximum of 30 fps. timing on setinterval isn't exact measurement either, because can delayed other factors (other timers etc), maybe better of try run them possible , adjust animation fps instead?

and use settimeout low value free engine other task in between frames., not exhaust cpu.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -