Fading The Player In Thirdperson 2013

== 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 40. 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 557. You should see this:
		g_ThirdPersonManager.PositionCamera( C_BasePlayer::GetLocalPlayer(), desiredCamAngles );
Immediately below that line insert the following code:
		// fade the player character when they get too close to the camera
		C_BasePlayer* localPlayer = C_BasePlayer::GetLocalPlayer();
		if (localPlayer->GetRenderMode() != kRenderTransTexture)
			localPlayer->SetRenderMode(kRenderTransTexture);
		float fNextPlayerAlpha = ((g_ThirdPersonManager.GetCameraOffsetAngles()[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);
Your code should now resemble this:
		g_ThirdPersonManager.PositionCamera( C_BasePlayer::GetLocalPlayer(), desiredCamAngles );
		// fade the player character when they get too close to the camera
		C_BasePlayer* localPlayer = C_BasePlayer::GetLocalPlayer();
		if (localPlayer->GetRenderMode() != kRenderTransTexture)
			localPlayer->SetRenderMode(kRenderTransTexture);
		float fNextPlayerAlpha = ((g_ThirdPersonManager.GetCameraOffsetAngles()[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.
Category:Programming
Category:Tutorials