Jump to content
Sign in to follow this  
tpw

Simple Dynamic Viewdistance Script

Recommended Posts

Gday all.

Here's a simple script I've been working on which does a few useful things:

What it does:

1 - It dynamically and smoothly adjusts viewdistance according to your framerate.

2 - It prevents the framerate from dropping and staying too low in areas of scenic complexity or lots of ai.

3 - It stabilises framerate so that there are less noticeable fps changes.

4 - It increases the viewdistance when using scopes/binoculars.

How it works:

The script launches a background process to monitor the average fps over a user specified time period (eg 10 seconds). This is then compared to the average from the previous 10 seconds, and if the fps has decreased then the viewdistance is dropped accordingly. Conversely, if the fps increases, the viewdistance is increased. It's set up so that it won't cause large oscillations in viewdistance/framerate.

The script also checks if you are using a scope or binocs and increases the viewdistance to simulate being able to see further and with more detail than with the naked eye.

These kind of scripts work on the assumption that in areas of high scenic complexity such as the middle of Zargabad or in a dense forest, you won't really notice that the view distance has been dropped. Similarly, if there are a lot of enemy ai trying to smoke you, you're probably more interested in getting a smooth bead on them than in admiring distant scenery. Another assumption is that increasing viewdistance when looking down a scoped weapon doesn't actually negatively affect fps most of the time.

You'll notice the viewdistance changing if you look for it, just like you'll notice lod switching or clutter popping in if you look for them too. After a while you simply stop noticing.

How to use it

Save the script below as whatever you like, wherever you like, and call it thusly from your init.sqf:

nul = [this,400,1800,25,40,10] execvm "nameofscript.sqf"; 

These are the values I use on my middle range system (Dual coreE6750, GTX460)

400 = minimum view distance (m)

1800 = maximum view distance (m)

25 = minimum framerate

40 = preferred (realistic!) framerate

10 = time to average framerate over

Obviously you'll change these values to suit your system. If you can't deal with fps below 30 then set the minimum to 30, and/or lower the minimum viewdistance even further. If your system can comfortably pull 75 fps at 5000m viewdistance then adjust accordingly (alternatively your computer is so powerful it probably doesn't need this script!).

Comment out the hintsilent line near the bottom of the script to remove debugging messages.

Script below:

// Script to smoothly scale the viewdistance according to fps and whether scope is used.
// Should help stabilise FPS too.
// TPW 20110122

// Variables
tpw_vmin = _this select 1; // minimum draw distance
tpw_vmax = _this select 2; // maximum draw distance
tpw_fmin = _this select 3; // max framerate
tpw_fmax = _this select 4; // min framerate
tpw_avtime = _this select 5; // how often to adjust viewdistance (sec) ~10
_zoomed = "n"; // start off not zoomed in
_zf = 1.5; // zoom factor to increase viewdistance by when scope used 

// Adjust view distance per map
if (worldname in ["zargabad","chernarus"]) then {tpw_vmin = (0.75 * tpw_vmin)}; // allow for worse performance on these maps
if (worldname in ["torabora","isoladicapraia","isladuala","utes","mb_manaisland","shapur_baf","desert_e","provinggrounds_pmc"]) then {tpw_vmin = (1.5 * tpw_vmin);tpw_vmax = (1.5 * tpw_vmax)}; // allow for better performance on these maps
tpw_steps = (tpw_vmax-tpw_vmin)/(tpw_fmax-tpw_fmin);

// Background process to determine average fps over tpw_avtime seconds and adjust view distance accordingly
tpw_lastfps = tpw_fmin;
[] spawn {while {alive player} do {
_sum = 0; for "_c" from 1 to tpw_avtime do {_sum = (_sum + diag_fps);sleep 1}; tpw_avfps = floor (_sum/tpw_avtime); // Average fps over tpw_avtime sec
_buffps = (tpw_avfps + tpw_lastfps)/2; // should stop huge oscillations in viewdistance
_mfactor = (_buffps - tpw_fmin); if (_mfactor < 0) then {_mfactor = 0};
tpw_vdist = floor (tpw_vmin + (tpw_steps*_mfactor)); // will linearly scale viewdistance from tpw_fmin = tpw_vmin to tpw_fmax = tpw_vmax 
tpw_lastfps = tpw_avfps;
};
};

// Initial viewdistance 
_oldvdist = tpw_vmin;
setviewdistance tpw_vmin;
sleep (tpw_avtime * 1.5 ); // free up CPU for other init stuff and wait for 1st run through background process

// Adjust viewdistance when zooming/unzooming scopes
while {alive player } do {
_vdist = tpw_vdist;
if ((cameraView == "GUNNER") and (_zoomed == "y")) then {_vdist = (_vdist * _zf)};
if ((cameraView == "GUNNER") and (_zoomed == "n")) then {_vdist = (_vdist * _zf); _zoomed="y"};
if ((cameraView == "INTERNAL") and (_zoomed == "y")) then {_zoomed="n"};

// Smoothly change viewdistance - if it has changed since last time
if (_vdist != _oldvdist) then 
{_inc = ((_vdist - _oldvdist)/10); // setviewdistance increments 1/10
_vdist = _oldvdist;
for "_i" from 1 to 10 do {_vdist = (_vdist + _inc); setviewdistance _vdist};
};
_oldvdist = _vdist;

//Debugging output
hintsilent format ["av fps:%1, vd:%2",tpw_avfps,_vdist];

sleep 1;
};

If you can see a way to improve this script or have any suggestions then I'd love to hear them.

Lastly, if you want a more comprehensive system to adjust framerate then look at VictorFarbau's VF FPS Saver (VFFPSS)

http://forums.bistudio.com/showthread.php?t=73964&highlight=dynamic+viewdistance

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks guys, looking forward to your feedback.

Share this post


Link to post
Share on other sites

Inside your mission's folder.

Though, I think it would be pretty awesome to have this as a client-side addon, rather than a mission-dependent script. Good work nonetheless, tpw. :D

Share this post


Link to post
Share on other sites

Well, other than the fact that I have zero idea how to turn it into and addon, I'd be happy to help. When I was talking about a simple script, I meant simple for me :)

Before I go to the bother, can a few people at least tell me if it works properly for someone other than me...

Share this post


Link to post
Share on other sites
I'll help out making a pbo version for you if you require - shoot me a PM :)

Thanks mate, will do.

Share this post


Link to post
Share on other sites

Hey mate,

Works beautifully. I have tested it on an ACE2 setup with Tanks/Choppers. The smoke popping from tanks is the big thing that helps test the framerate reduction.

so far I have only noticed one thing that is a big of an issue:

When using binoculars/scope and looking in to the air, the viewdistance jumps up to 4000-5000, which causes a fair bit of an fps lag spike when you zoom out.

I don't script, unfortunately, so I don't know what i'd need to adjust to limit that to like 3000 or something, but it doesnt seem to be affected by any of the preset variables.

Apart from that, it works very well. Without the script my framerate drops to 15-19fps. With it enabled it generally stays around the 30+ mark

setup:

C2D QX9650 @ 3.4Ghz

4G 1066Mhz DDR2 RAM

Radeon 1G 5890 or something.

Standard 7,200rpm HDD

Share this post


Link to post
Share on other sites

Thanks so much for the feedback. I'm very pleased that it works for you.

You can easily edit the amount of zoom when using scopes/binocs. _zf is currently 1.5, but can be set to 1 or even less.

I find that there's a bit of lag when zooming/unzooming regardless of whether I use the script or not.

Share this post


Link to post
Share on other sites

Nice work, I use the other FPS helper from some time back but like the other options this offers with bino zoom etc.

I wont mention the other one, but could there be a chance to have a config with it so you can set you own max/min option and fps minimum maximum?

Also as been noted and looks like it will happen a PBO version of this is borderline illegal not to have :)

Share this post


Link to post
Share on other sites

I could provide an addon version with an on/off button similar to my FPS counter if interested (no CBA requirement then) :)

Share this post


Link to post
Share on other sites

The legendary Das Attorney has put together an proper pbo addon for me. I'll give it a quick test run and then you're all good to go! Thanks again Das.

Share this post


Link to post
Share on other sites

Little bit of bad news. I've inadvertently wrote in a dependency for CBA on the version I've sent you. It's my bad, some sloppy coding :(

Give me a little bit of time and I should get it sorted for you.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×