Game Development Community

dev|Pro Game Development Curriculum

Different visibleDistance on sky object for ghosting and render

by Fyodor -bank- Osokin · 06/20/2007 (8:41 am) · 2 comments

About 15 minutes ago our artist told me he will increase visible distance in certain missions because "it looks much better". I was telling him that it will affect the server and network performance... After a couple of minutes of hard talking I decided to have a look at code...
And here is a result: You can set different visible distances for "server ghosting" and for "actually visible distance to render world".

So, if you have visibleDistance less then client-side one, the ghosted objects will disappear earlier. If larger - objects will not be rendered, but still ghosted. And yeah, if values are equal - it will perform the same as without this mod.

The code:

engine/terrain/sky.h
after:
F32 mVisibleDistance;
add:
F32 mClientVisibleDistance; //bank: client-side mVisibleDistance

engine/terrain/sky.cc

in Sky::Sky()
after:
mVisibleDistance = 250;
add:
mClientVisibleDistance = 300; //bank: client-side mVisibleDistance

almost at the end of void Sky::setVisibility()
change from:
mSceneManager->setVisibleDistance(mVisibleDistance);
to:
//bank: client-side mVisibleDistance
      if (isServerObject())
         mSceneManager->setVisibleDistance(mVisibleDistance);
      else
         mSceneManager->setVisibleDistance(mClientVisibleDistance);
      //bank: client-side mVisibleDistance

if void Sky::initPersistFields()
after:
addField("visibleDistance",         TypeF32,       Offset(mVisibleDistance, Sky));
add:
addField("clientVisibleDistance",   TypeF32,       Offset(mClientVisibleDistance, Sky)); //bank: client-side mVisibleDistance

in void Sky::unpackUpdate(NetConnection *, BitStream *stream)
after:
stream->read(&mVisibleDistance);
add:
stream->read(&mClientVisibleDistance); //bank: client-side mVisibleDistance

in U32 Sky::packUpdate(NetConnection *, U32 mask, BitStream *stream)
replace the whole block
if(stream->writeFlag(mask & VisibilityMask))
   {
      ...
      ...
   }
with:
if(stream->writeFlag(mask & VisibilityMask))
   {
      //bank: client-side mVisibleDistance
      if (mClientVisibleDistance > MAXVISIBLEDISTANCE)
      {
         Con::errorf("Error: clientVisibleDistance is capped at %g.", MAXVISIBLEDISTANCE);
         Con::errorf("To increase this limitation requires C++ code changes.");
         Con::errorf("Please see comment at the top of sky.cc");
         mClientVisibleDistance = MAXVISIBLEDISTANCE;
      }
      if (mVisibleDistance > MAXVISIBLEDISTANCE)
      {
         Con::errorf("Error: visibleDistance is capped at %g.", MAXVISIBLEDISTANCE);
         Con::errorf("To increase this limitation requires C++ code changes.");
         Con::errorf("Please see comment at the top of sky.cc");
         mVisibleDistance = MAXVISIBLEDISTANCE;
      }
      //bank: client-side mVisibleDistance
      stream->write(mVisibleDistance);
      stream->write(mClientVisibleDistance); //bank: client-side mVisibleDistance
      stream->write(mFogDistance);
   }

in void Sky::calcAlphas_Heights( ... )
change:
visDis = setVis = mVisibleDistance +
      (mFogVolumes[mNumFogVolumes-1].visibleDistance - mVisibleDistance) *
         mFogVolumes[mNumFogVolumes-1].percentage;
to:
//bank: client-side mVisibleDistance
   visDis = setVis = [b]mClientVisibleDistance[/b] +
      (mFogVolumes[mNumFogVolumes-1].visibleDistance - [b]mClientVisibleDistance[/b]) *
         mFogVolumes[mNumFogVolumes-1].percentage;
and the code:
F32 distance = mVisibleDistance +
         (mFogVolumes[x].visibleDistance - mVisibleDistance) *
            mFogVolumes[x].percentage;
replace with:
//bank: client-side mVisibleDistance
      F32 distance = [b]mClientVisibleDistance[/b] +
         (mFogVolumes[x].visibleDistance - [b]mClientVisibleDistance[/b]) *
            mFogVolumes[x].percentage;

in void Sky::calcPoints()
change:
F32 visDisMod = mVisibleDistance;
to:
F32 visDisMod = mClientVisibleDistance; //bank: client-side mVisibleDistance

Save, compile. Start your new binary, load the mission and your visibility should be limited to 300 by default. Change it to the value you think is good enough.

If you find a bug or have an improvement - please post it back here, I'll update the resource.

#1
07/04/2007 (9:53 am)
I can't believe no one has commented on this. I see a lot of potential here. I have been looking for a way to give the player a good sense of "scale" as far as the terrain goes without bogging down the game. I haven't integrated your code yet, but I will at my first opportunity. I'll let you know how it goes!
#2
05/03/2008 (9:58 pm)
excellent, i got it to work on tge152+afx. Now i am able to make this value configurable and allow slower machines to show lesser players at a nearer distance.