Env projectedtexture/fixes: Difference between revisions
(Added reasoning behind the the shadow map depth texture resolution changes) |
(Fixed grain images overlapping the section below) |
||
Line 133: | Line 133: | ||
Located in "imaterialsystem.h" lower the value for ''m_flShadowFilterSize'' from it's current value down to something like 1.0f | Located in "imaterialsystem.h" lower the value for ''m_flShadowFilterSize'' from it's current value down to something like 1.0f | ||
<br style="clear:both" /> | |||
== Create a shadow map depth texture greater than the framebuffer size == | == Create a shadow map depth texture greater than the framebuffer size == |
Revision as of 07:31, 28 October 2010
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

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

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( GetMoveParent() != NULL );
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() )

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.
Decreasing shadow bleeding and "ghosting"
Shadows will sometimes separate from their caster, self-shadows can also "bleed" on their caster. These two values will help with that.
Set the default value of mat_depthbias_shadowmap to 0.00001 and mat_slopescaledepthbias_shadowmap to 3 or 4. You can change these values by changing the ConVar constructor at the beginning of c_env_projectedtexture.cpp.
Lowering the amount of "grain" on shadows
Currently the edges of shadows are quite grainy, this can also cause the shadows to blur into nothing depending on the distance between the caster and the source. To counter this, we can lower the amount of "grain". Careful when lowering this value, it's there to cover shadow map aliasing and produce soft shadows, you might want to try with different values before deciding a final.
Located in "imaterialsystem.h" lower the value for m_flShadowFilterSize from it's current value down to something like 1.0f
Create a shadow map depth texture greater than the framebuffer size
As rendertargets created with RT_SIZE_OFFSCREEN
are resized to the nearest power-of-2 size that will fit into the framebuffer, we need to bypass this restriction by creating our rendertargets with RT_SIZE_NO_CHANGE
instead. However, another problem arises when IVRenderView::Push3DView
does not properly inform the material system of our new depth texture and render target. Therefore, after the IVRenderView::Push3DView
call, we need to manually push our new render target and depth texture to the material system.
To fix these issues, open clientshadowmgr.cpp and find void CClientShadowMgr::InitDepthTextureShadows()
. Replace the entire function with the following code block; my code changes are commented with // SAUL:
on the preceding line.
//-----------------------------------------------------------------------------
// Initialize, shutdown depth-texture shadows
//-----------------------------------------------------------------------------
void CClientShadowMgr::InitDepthTextureShadows()
{
VPROF_BUDGET( "CClientShadowMgr::InitDepthTextureShadows", VPROF_BUDGETGROUP_SHADOW_DEPTH_TEXTURING );
// SAUL: start benchmark timer
CFastTimer timer;
timer.Start();
// SAUL: set m_nDepthTextureResolution to the depth resolution we want
m_nDepthTextureResolution = r_flashlightdepthres.GetInt();
if( !m_bDepthTextureActive )
{
m_bDepthTextureActive = true;
ImageFormat dstFormat = materials->GetShadowDepthTextureFormat(); // Vendor-dependent depth texture format
#if !defined( _X360 )
ImageFormat nullFormat = materials->GetNullTextureFormat(); // Vendor-dependent null texture format (takes as little memory as possible)
#endif
materials->BeginRenderTargetAllocation();
#if defined( _X360 )
// For the 360, we'll be rendering depth directly into the dummy depth and Resolve()ing to the depth texture.
// only need the dummy surface, don't care about color results
m_DummyColorTexture.InitRenderTargetTexture( r_flashlightdepthres.GetInt(), r_flashlightdepthres.GetInt(), RT_SIZE_OFFSCREEN, IMAGE_FORMAT_BGR565, MATERIAL_RT_DEPTH_SHARED, false, "_rt_ShadowDummy" );
m_DummyColorTexture.InitRenderTargetSurface( r_flashlightdepthres.GetInt(), r_flashlightdepthres.GetInt(), IMAGE_FORMAT_BGR565, true );
#else
// SAUL: we want to create a render target of specific size, so use RT_SIZE_NO_CHANGE
m_DummyColorTexture.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_NO_CHANGE, nullFormat, MATERIAL_RT_DEPTH_NONE, false, "_rt_ShadowDummy" );
#endif
// Create some number of depth-stencil textures
m_DepthTextureCache.Purge();
m_DepthTextureCacheLocks.Purge();
for( int i=0; i < m_nMaxDepthTextureShadows; i++ )
{
CTextureReference depthTex; // Depth-stencil surface
bool bFalse = false;
char strRTName[64];
sprintf( strRTName, "_rt_ShadowDepthTexture_%d", i );
#if defined( _X360 )
// create a render target to use as a resolve target to get the shared depth buffer
// surface is effectively never used
depthTex.InitRenderTargetTexture( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_OFFSCREEN, dstFormat, MATERIAL_RT_DEPTH_NONE, false, strRTName );
depthTex.InitRenderTargetSurface( 1, 1, dstFormat, false );
#else
// SAUL: we want to create a *DEPTH TEXTURE* of specific size, so use RT_SIZE_NO_CHANGE and MATERIAL_RT_DEPTH_ONLY
depthTex.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_NO_CHANGE, dstFormat, MATERIAL_RT_DEPTH_ONLY, false, strRTName );
#endif
// SAUL: ensure the depth texture size wasn't changed
Assert(depthTex->GetActualWidth() == m_nDepthTextureResolution);
m_DepthTextureCache.AddToTail( depthTex );
m_DepthTextureCacheLocks.AddToTail( bFalse );
}
materials->EndRenderTargetAllocation();
}
timer.End();
DevMsg("InitDepthTextureShadows took %.2f msec\n", timer.GetDuration().GetMillisecondsF());
}
Now open viewrender.cpp and find void CShadowDepthView::Draw()
. Immediately before SetupCurrentView(...)
(line 4654), add:
pRenderContext.GetFrom(materials);
pRenderContext->PushRenderTargetAndViewport(m_pRenderTarget, m_pDepthTexture, 0, 0, m_pDepthTexture->GetMappingWidth(), m_pDepthTexture->GetMappingWidth());
pRenderContext.SafeRelease();
Move to the end of the function, and immediately before render->PopView( GetFrustum() );
(line 4702), add:
pRenderContext->PopRenderTargetAndViewport();