Fading The Player In Thirdperson: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
Line 9: Line 9:


Scroll down this file to around line 52. You should see something like this:
Scroll down this file to around line 52. You should see something like this:
<pre>
<source lang="cpp">
static ConVar c_maxdistance( "c_maxdistance",  "200", FCVAR_ARCHIVE );
static ConVar c_maxdistance( "c_maxdistance",  "200", FCVAR_ARCHIVE );
static ConVar c_mindistance( "c_mindistance",  "30", FCVAR_ARCHIVE );
static ConVar c_mindistance( "c_mindistance",  "30", FCVAR_ARCHIVE );
static ConVar c_orthowidth( "c_orthowidth",  "100", FCVAR_ARCHIVE );
static ConVar c_orthowidth( "c_orthowidth",  "100", FCVAR_ARCHIVE );
static ConVar c_orthoheight( "c_orthoheight",  "100", FCVAR_ARCHIVE );
static ConVar c_orthoheight( "c_orthoheight",  "100", FCVAR_ARCHIVE );
</pre>
</source>


Below these lines of code insert the following two lines of code:
Below these lines of code insert the following two lines of code:
<pre>
<source lang="cpp">
static ConVar cam_fadestartdist( "cam_fadestartdist", "100");
static ConVar cam_fadestartdist( "cam_fadestartdist", "100");
static ConVar cam_fadeenddist( "cam_fadeenddist", "50");
static ConVar cam_fadeenddist( "cam_fadeenddist", "50");
</pre>
</source>
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.
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:
After inserting those lines you should have something similar to the following:
<pre>
<source lang="cpp">
static ConVar c_maxdistance( "c_maxdistance",  "200", FCVAR_ARCHIVE );
static ConVar c_maxdistance( "c_maxdistance",  "200", FCVAR_ARCHIVE );
static ConVar c_mindistance( "c_mindistance",  "30", FCVAR_ARCHIVE );
static ConVar c_mindistance( "c_mindistance",  "30", FCVAR_ARCHIVE );
Line 31: Line 31:
static ConVar cam_fadestartdist( "cam_fadestartdist", "100");
static ConVar cam_fadestartdist( "cam_fadestartdist", "100");
static ConVar cam_fadeenddist( "cam_fadeenddist", "50");
static ConVar cam_fadeenddist( "cam_fadeenddist", "50");
</pre>
</source>


Now scroll down to about line 582. You should see this:
Now scroll down to about line 582. You should see this:
<pre>
<source lang="cpp">
// For now, I'd rather see the insade of a player model than punch the camera through a wall
// 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
// might try the fade out trick at some point
//if( camOffset[ DIST ] < CAM_MIN_DIST )
//if( camOffset[ DIST ] < CAM_MIN_DIST )
//    camOffset[ DIST ] = CAM_MIN_DIST; // clamp up to minimum
//    camOffset[ DIST ] = CAM_MIN_DIST; // clamp up to minimum
</pre>
</source>


Immediately below those comments insert the following code:
Immediately below those comments insert the following code:
<pre>
<source lang="cpp">
if (localPlayer->GetRenderMode() != kRenderTransTexture)
if (localPlayer->GetRenderMode() != kRenderTransTexture)
localPlayer->SetRenderMode( kRenderTransTexture );
localPlayer->SetRenderMode( kRenderTransTexture );
Line 64: Line 64:
localPlayer->SetRenderColorA( fNextPlayerAlpha );
localPlayer->SetRenderColorA( fNextPlayerAlpha );
</pre>
</source>


So now your code should resemble this:
So now your code should resemble this:
<pre>
<source lang="cpp">
// For now, I'd rather see the insade of a player model than punch the camera through a wall
// 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
// might try the fade out trick at some point
Line 96: Line 96:
localPlayer->SetRenderColorA( fNextPlayerAlpha );
localPlayer->SetRenderColorA( fNextPlayerAlpha );


</pre>
</source>


Then you can save and compile your game.
Then you can save and compile your game.

Revision as of 04:44, 17 May 2009

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.