Fading The Player In Thirdperson

From Valve Developer Community
Jump to: navigation, search
Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Dead End - Icon.png
This article has no links to other VDC articles. Please help improve this article by adding links that are relevant to the context within the existing text.
January 2024


Note.pngNote:Does not work with engine branches newer than 2007. Source 2013 in_camera.cpp has many structural changes

Introduction

This is pretty much a copy and paste tutorial so not much expertise is required to implement this.

What this code does is fade the player when the camera gets too close to the player in third-person view. This way when the camera is forced really close to the player in enclosed spaces the player fades out so that the player model does not block the player's view.

in_camera.cpp (client dll)

Okay, first you want to go into the client dll and open up 'in_camera.cpp'

Scroll down this file to around line 52. You should see something like this:

static ConVar c_maxdistance( "c_maxdistance",   "200", FCVAR_ARCHIVE );
static ConVar c_mindistance( "c_mindistance",   "30", FCVAR_ARCHIVE );
static ConVar c_orthowidth( "c_orthowidth",   "100", FCVAR_ARCHIVE );
static ConVar c_orthoheight( "c_orthoheight",   "100", FCVAR_ARCHIVE );

Below these lines of code insert the following two lines of code:

static ConVar cam_fadestartdist( "cam_fadestartdist", "100");
static ConVar cam_fadeenddist( "cam_fadeenddist", "50");

These define at which distance betwen the camera and the player model the player will start fading (cam_fadestartdist) and at what distance the player will be completely invisible (cam_fadeenddist). These will be convars that can be edited via the console. Their default values are currently 100 and 50 respectively.

After inserting those lines you should have something similar to the following:

static ConVar c_maxdistance( "c_maxdistance",   "200", FCVAR_ARCHIVE );
static ConVar c_mindistance( "c_mindistance",   "30", FCVAR_ARCHIVE );
static ConVar c_orthowidth( "c_orthowidth",   "100", FCVAR_ARCHIVE );
static ConVar c_orthoheight( "c_orthoheight",   "100", FCVAR_ARCHIVE );
static ConVar cam_fadestartdist( "cam_fadestartdist", "100");
static ConVar cam_fadeenddist( "cam_fadeenddist", "50");

Now scroll down to about line 582. You should see this:

			// For now, I'd rather see the insade of a player model than punch the camera through a wall
			// might try the fade out trick at some point
			//if( camOffset[ DIST ] < CAM_MIN_DIST )
			//    camOffset[ DIST ] = CAM_MIN_DIST; // clamp up to minimum

Immediately below those comments insert the following code:

		if (localPlayer->GetRenderMode() != kRenderTransTexture)
				localPlayer->SetRenderMode( kRenderTransTexture );

			float fNextPlayerAlpha = ( ( m_vecCameraOffset[ DIST ] - cam_fadeenddist.GetFloat() ) * ( 100 / ( cam_fadestartdist.GetFloat()  -  cam_fadeenddist.GetFloat() ) )  );
			// alpha = 0-100
			//opacity as percentage (unclamped) = (dist-fadeenddist) * (100 / (fadestartdist - fadeenddist))
			//opacity 0-255 = 2.55 * opacity as percentage
			
			
			fNextPlayerAlpha = fNextPlayerAlpha * 2.55; // convert 0-100 into 0-255

			if (fNextPlayerAlpha > 255) // clamp 100%
				fNextPlayerAlpha = 255;
			
			if (fNextPlayerAlpha < 0)	// clamp 0%
				fNextPlayerAlpha = 0;
			

			
			localPlayer->SetRenderColorA( fNextPlayerAlpha );

So now your code should resemble this:

			// For now, I'd rather see the insade of a player model than punch the camera through a wall
			// might try the fade out trick at some point
			//if( camOffset[ DIST ] < CAM_MIN_DIST )
			//    camOffset[ DIST ] = CAM_MIN_DIST; // clamp up to minimum

			if (localPlayer->GetRenderMode() != kRenderTransTexture)
				localPlayer->SetRenderMode( kRenderTransTexture );

			float fNextPlayerAlpha = ( ( m_vecCameraOffset[ DIST ] - cam_fadeenddist.GetFloat() ) * ( 100 / ( cam_fadestartdist.GetFloat()  -  cam_fadeenddist.GetFloat() ) )  );
			// alpha = 0-100
			//opacity as percentage (unclamped) = (dist-fadeenddist) * (100 / (fadestartdist - fadeenddist))
			//opacity 0-255 = 2.55 * opacity as percentage
			
			
			fNextPlayerAlpha = fNextPlayerAlpha * 2.55; // convert 0-100 into 0-255

			if (fNextPlayerAlpha > 255) // clamp 100%
				fNextPlayerAlpha = 255;
			
			if (fNextPlayerAlpha < 0)	// clamp 0%
				fNextPlayerAlpha = 0;
			

			
			localPlayer->SetRenderColorA( fNextPlayerAlpha );

Then you can save and compile your game.

Making the player mouth fade out too

The above code works fine but the player's mouth will not fade out. That is because it uses a shader that doesn't like being faded. To fix this we just have to override the shader it uses.

First go to your mod directory. Now go into the following folder (Create it if it does not exist): 'materials\Models\Humans\Male'.

Once in the folder create a file called 'mouth.vmt'.

Put this inside the file using notepad:

"VertexLitGeneric"
//"Teeth"
{
	"$basetexture" "models/Humans/Male/mouth"
//	"$model" 1
//	"$clientshader" "MouthShader"
	"$halflambert" 1
}

This will only affect the mouths on male models. To apply this change to female models you need to do the following.

Go back to your mod directory and then go into the following folder (Create it if it does not exist): 'materials\Models\Humans\Female'.

Now create a file called 'fmouth.vmt'.

Put this inside the file using notepad:

"VertexLitGeneric"
//"Teeth"
{
	"$basetexture" "models/Humans/Female/fmouth"
//	"$model" 1
//	"$clientshader" "MouthShader"
	"$halflambert" 1
}

And that concludes this tutorial.

You can modify the default values of the two console variables defined earlier in this tutorial to alter how close the camera has to be to the player for the player to fade out.

Hint: Use 'cam_showangles 1' to help you work out the distances.