Env projectedtexture/fixes

From Valve Developer Community
< Env projectedtexture
Revision as of 18:23, 27 January 2010 by TomEdwards (talk | contribs) (moved from env_projectedtexture)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Enabling multiple shadow maps

Valve's games only allow one projected texture to cast shadows at a time - including the player's flashlight! To surmount this, shadow casting can be disabled on each entity with the enableshadows KV, or for a proper solution a programmer can perform this C++ fix:

In CClientShadowMgr::Init() (Clientshadowmgr.cpp around line 1293), replace:

bool bTools = CommandLine()->CheckParm( "-tools" ) != NULL;
m_nMaxDepthTextureShadows = bTools ? 4 : 1;	// Just one shadow depth texture in games, more in tools

With:

m_nMaxDepthTextureShadows = YOUR_CHOSEN_MAX; //with your number

Warning: A new render texture will be created for each shadow map you enable support for, regardless of whether it's used. Having too many RTs hurts performance, so keep your limit within reason (ideally something below 10).

Fixing targeting

Because the targeting code isn't finished in the SDK, A projected texture will flicker when bound to a target. To fix this, open c_env_projectedtexture.cpp and around line 174 there is an else block containing an assert and some commented code. Replace the whole block with this:

else
{
	Vector vecToTarget = m_hTargetEntity->GetAbsOrigin() - GetAbsOrigin();
	QAngle vecAngles;
	VectorAngles( vecToTarget, vecAngles );
	AngleVectors( vecAngles, &vForward, &vRight, &vUp );
}

The server needs code to update the entity with angles on a target as well. Open env_projectedtexture.cpp and around line 245 edit the InitialThink() function to recalculate angles towards the target:

void CEnvProjectedTexture::InitialThink( void )
{
	if ( m_hTargetEntity == NULL && m_target != NULL_STRING )
		m_hTargetEntity = gEntList.FindEntityByName( NULL, m_target );
	if ( m_hTargetEntity == NULL )
		return;
	 
	Vector vecToTarget = (m_hTargetEntity->GetAbsOrigin() - GetAbsOrigin());
	QAngle vecAngles;
	VectorAngles( vecToTarget, vecAngles );
	SetAbsAngles( vecAngles );
	 
	SetNextThink( gpGlobals->curtime + 0.1 );
}

Fixing cuts in projected texture

Warning: Having this set will reduce your performance, but only by a small fraction, though noticeable.

Projected texture being cut.

When using multiple env_projectedtexture, projected textures might be cut at certain viewing angles. To fix this, force r_flashlightscissor 0 for your mod or map.

You can do this by adding the following code block into the constructor of c_basehlplayer.cpp

ConVarRef scissor( "r_flashlightscissor" );
scissor.SetValue( "0" );

Fixing Parenting

By default, this entity's position only gets updated when it gets turned on. Thus, parenting does not work. To fix this, open c_env_projectedtexture.cpp and in line 225, comment out 3 lines like this:

	// if ( bForceUpdate == false )
	// {
		g_pClientShadowMgr->UpdateProjectedTexture( m_LightHandle, true );
	// }

In line 233 in the Simulate function, change the argument false to true like this:

    UpdateLight( true );

Enabling visibility tests

Projected textures are not tested for visibility. Being constantly enabled creates issues like shadows bleeding through walls. To fix them, head into c_env_projectedtexture.cpp, and make these changes:

Add a new function (remember to declare it in the header):

bool C_EnvProjectedTexture::ShouldDraw()
{
	//No need to check the flashlight for visibility.
	if( m_bCameraSpace )
		return true;

	//Trace forward to the nearest brush face to get a good leaf test vector.
	Vector vForward;
	GetVectors( &vForward, NULL, NULL );

	Vector vTraceStart = GetAbsOrigin();
	Vector vTraceEnd = GetAbsOrigin() + vForward * m_flFarZ;

	trace_t tr;
	CTraceFilterWorldOnly filter;

	UTIL_TraceLine( vTraceStart, vTraceEnd, CONTENTS_SOLID && CONTENTS_OPAQUE, &filter, &tr );

	//Check to see if our end vector is in a visible leaf.
	int LeafNum = enginetrace->GetLeafContainingPoint( tr.endpos );
	return render->AreAnyLeavesVisible( &LeafNum,1 );
}

In C_EnvProjectedTexture::UpdateLight, change the first line to read:

if ( m_bState == false || !ShouldDraw() )
Bug: This can cause the light to not draw under certain visleaf conditions. A better solution might be to test the entity's PVS?

Enabling shadow receiving on the view model

Shadow receiving is disabled on various types of renderables. If you want to have shadows cast upon the view model then head into baseviewmodel_shared.h, find the ShouldReceiveProjectedTextures function and make it return true.

For example:

virtual bool ShouldReceiveProjectedTextures( int flags )
{
	return true;
}

This change also works for other renderables like detail models. Just search the solution for ShouldReceiveProjectedTextures.

You could also simply comment out the entire ShouldReceiveProjectedTextures function, instead of changing it to return true.