Env projectedtexture/fixes: Difference between revisions
(Fixed "Fixing targeting") |
(Add fix for projected textures fading out at distance) |
||
(41 intermediate revisions by 16 users not shown) | |||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:env_projectedtexture/fixes}} | |||
{{note|All files (unless explicitly specified as '''server''') in this article are available in the '''client''' project}} | {{note|All files (unless explicitly specified as '''server''') in this article are available in the '''client''' project}} | ||
== Enabling multiple shadow maps == | == Enabling multiple shadow maps. == | ||
Valve's games only allow one projected texture to cast shadows at a time - including the player's flashlight | 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 <code>enableshadows</code> KV, or for a proper solution a programmer can perform this simple C++ fix: | ||
In <code>CClientShadowMgr::Init()</code> (Clientshadowmgr.cpp around line 1293), replace: | In <code>CClientShadowMgr::Init()</code> (located in : src\game\client\'''Clientshadowmgr.cpp''', and around '''line 1293'''), replace the following: | ||
<source lang=cpp>bool bTools = CommandLine()->CheckParm( "-tools" ) != NULL; | <source lang=cpp>bool bTools = CommandLine()->CheckParm( "-tools" ) != NULL; | ||
m_nMaxDepthTextureShadows = bTools ? 4 : 1; // Just one shadow depth texture in games, more in tools</source> | m_nMaxDepthTextureShadows = bTools ? 4 : 1; // Just one shadow depth texture in games, more in tools</source> | ||
With: | With this: | ||
<source lang=cpp>m_nMaxDepthTextureShadows = | <source lang=cpp>m_nMaxDepthTextureShadows = 10; //10 - shadow maps in the game. It doesn't matter if the -tools are enabled..</source> | ||
{{warning|A new [[render texture]] will be created for each shadow map you enable support for, regardless of whether it | {{warning|A new [[render texture]] will be created for each shadow map you enable support for (including flashlight), regardless of whether it is used. This can use up a lot of video memory, and if D3D9Ex is not in use, a lot of RAM.}} | ||
{{tip|A better solution for performance is to only render shadows for the closest projected texture. This can be done by modifying <code>CClientShadowMgr::BuildActiveShadowDepthList</code>.}} | |||
== Fixing targeting == | == 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 <code>else</code> block containing an assert and some commented code. Replace the whole block with this: | 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''' (Located in : src\game\client...) and around ('''line 174''') there is an <code>else</code> block containing an assert and some commented code. Replace the whole block with this: | ||
<source lang=cpp>else | <source lang=cpp> else | ||
{ | { | ||
// VXP: Fixing targeting | |||
Vector vecToTarget; | |||
Vector vecToTarget | |||
QAngle vecAngles; | QAngle vecAngles; | ||
VectorAngles( vecToTarget, vecAngles ); | if ( m_hTargetEntity == NULL ) | ||
{ | |||
vecAngles = GetAbsAngles(); | |||
} | |||
else | |||
{ | |||
vecToTarget = m_hTargetEntity->GetAbsOrigin() - GetAbsOrigin(); | |||
VectorAngles( vecToTarget, vecAngles ); | |||
} | |||
AngleVectors( vecAngles, &vForward, &vRight, &vUp ); | AngleVectors( vecAngles, &vForward, &vRight, &vUp ); | ||
}</source> | |||
}</source> | |||
The server needs code to update the entity with angles on a target as well. Open ''env_projectedtexture.cpp'' and around line | The server needs code to update the entity with angles on a target as well. Open '''env_projectedtexture.cpp''' (Located in src\game\server) and around ('''line 240''') edit the <code>InitialThink()</code> function to recalculate angles towards the target: | ||
<source lang=cpp>void CEnvProjectedTexture::InitialThink( void ) | <source lang=cpp>void CEnvProjectedTexture::InitialThink( void ) | ||
Line 53: | Line 56: | ||
}</source> | }</source> | ||
== Fixing cuts in projected texture == | == Fixing cuts in the projected texture. == | ||
{{warning|Having this enabled causes shadows to no longer be clipped at the projectedtextures frustum. This will cause added shadow rendering, and can vary in cost based on the complexity of the scene. However due to the nature of odd shadow clipping from general projected light sources, the impact on overall rendering cost is small enough to not worry about.}} | {{warning|Having this enabled causes shadows to no longer be clipped at the projectedtextures frustum. This will cause added shadow rendering, and can vary in cost based on the complexity of the scene. However due to the nature of odd shadow clipping from general projected light sources, the impact on overall rendering cost is small enough to not worry about.}} | ||
[[ | [[File:Cut.jpg|thumb|Projected texture being cut.]] | ||
When using multiple env_projectedtexture, projected textures might be cut at certain viewing angles. | 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. | 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 | You can do this by adding the following code block into the ''constructor'' of '''c_basehlplayer.cpp''' (located in : src\game\client\hl2...) | ||
<source lang=cpp>ConVarRef scissor( "r_flashlightscissor" ); | <source lang=cpp>static ConVarRef scissor( "r_flashlightscissor" ); | ||
scissor.SetValue( "0" );</source> | scissor.SetValue( "0" );</source> | ||
== Fixing Parenting == | == 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''' (Located in : src\game\client...) and around ('''line 222'''), comment out the 3 lines like this: | |||
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 | |||
<source lang=cpp highlight=1-2,4> // if ( bForceUpdate == false ) | <source lang=cpp highlight=1-2,4> // if ( bForceUpdate == false ) | ||
Line 76: | Line 78: | ||
// }</source> | // }</source> | ||
and around ('''line 248''') in the Simulate function, change the ''UpdateLight'' call to: | |||
<source lang=cpp> UpdateLight( GetMoveParent() != NULL );</source> | <source lang=cpp> UpdateLight( GetMoveParent() != NULL );</source> | ||
Line 82: | Line 84: | ||
== Enabling visibility tests == | == Enabling visibility tests == | ||
Projected textures are not tested for visibility. To enable AABB-frustum visibility culling, open up '''clientshadowmgr.cpp''', and find the function '''CClientShadowMgr::BuildFlashlight'''. Add the following highlighted code to the beginning of the function: | Projected textures are not tested for visibility. To enable AABB-frustum visibility culling, open up '''clientshadowmgr.cpp''' (located in : src\game\client\...), and find the function '''CClientShadowMgr::BuildFlashlight'''. Add the following highlighted code to the beginning of the function: | ||
<source lang=cpp highlight=15-20>void CClientShadowMgr::BuildFlashlight( ClientShadowHandle_t handle ) | <source lang=cpp highlight=15-20>void CClientShadowMgr::BuildFlashlight( ClientShadowHandle_t handle ) | ||
Line 106: | Line 108: | ||
{{note|It is generally advised to manage the states of your env_projectedtexture entities by turning them on and off with triggers. It is also possible to use distances programmed via code much like model fade distances.}} | {{note|It is generally advised to manage the states of your env_projectedtexture entities by turning them on and off with triggers. It is also possible to use distances programmed via code much like model fade distances.}} | ||
== Removing incorrect culling == | |||
The built-in env_projectedtexture culling is broken. If an env_projectedtexture is placed pointing in one direction, and the player turns to face the opposite direction, the light can be seen in the direction that the player is pointing under certain circumstances. YouTube video to demonstrate the issue[http://www.youtube.com/watch?v=WPbt2sGtLlY]. | |||
The below code in the function '''CClientShadowMgr::BuildActiveShadowDepthList()''' in '''clientshadowmgr.cpp''' (located in : src\game\client\...), can be commented out to fix this behavior, around ('''Line 3900'''): | |||
<source lang=cpp highlight=7-20> | |||
const FlashlightState_t& flashlightState = shadowmgr->GetFlashlightState( shadow.m_ShadowHandle ); | |||
// Bail if this flashlight doesn't want shadows | |||
if ( !flashlightState.m_bEnableShadows ) | |||
continue; | |||
// Calculate an AABB around the shadow frustum | |||
/*Vector vecAbsMins, vecAbsMaxs; | |||
CalculateAABBFromProjectionMatrix( shadow.m_WorldToShadow, &vecAbsMins, &vecAbsMaxs ); | |||
Frustum_t viewFrustum; | |||
GeneratePerspectiveFrustum( viewSetup.origin, viewSetup.angles, viewSetup.zNear, viewSetup.zFar, viewSetup.fov, viewSetup.m_flAspectRatio, viewFrustum ); | |||
// FIXME: Could do other sorts of culling here, such as frustum-frustum test, distance etc. | |||
// If it's not in the view frustum, move on | |||
if ( R_CullBox( vecAbsMins, vecAbsMaxs, viewFrustum ) ) | |||
{ | |||
shadowmgr->SetFlashlightDepthTexture( shadow.m_ShadowHandle, NULL, 0 ); | |||
continue; | |||
}*/ | |||
if ( nActiveDepthShadowCount >= nMaxDepthShadows ) | |||
{ | |||
static bool s_bOverflowWarning = false;</source> | |||
{{note|This can (and probably should) also be used in conjunction with the '''"Enabling visibility tests"''' code fix.}} | |||
== Enabling shadow receiving on the view model == | == 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 <code>ShouldReceiveProjectedTextures</code> function and make it return <code>true</code>. | 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''' (located in : src\game\shared...), find the <code>ShouldReceiveProjectedTextures</code> function and make it return <code>true</code>. | ||
For example: | For example: | ||
Line 120: | Line 156: | ||
This change also works for other renderables like detail models. Just search the solution for <code>ShouldReceiveProjectedTextures</code>. | This change also works for other renderables like detail models. Just search the solution for <code>ShouldReceiveProjectedTextures</code>. | ||
You could also simply comment out the entire <code> | {{note|'''You could also simply comment out the entire''' ''ShouldReceiveProjectedTextures'' '''function, instead of having to change it to''' <code>return true;</code>, for example : | ||
<source lang=cpp>/* virtual bool ShouldReceiveProjectedTextures( int flags ) | |||
{ | |||
return false; | |||
}*/</source>}} | |||
== Decreasing shadow bleeding and "ghosting" == | == Decreasing shadow bleeding and "ghosting" == | ||
Line 127: | Line 166: | ||
Shadows will sometimes separate from their caster, self-shadows can also "bleed" on their caster. These two values will help with that. | 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 4. You can change these values by changing the ConVar constructor at the beginning of ''c_env_projectedtexture.cpp''. | Set the default value of ''mat_depthbias_shadowmap'' to 0.00001 and ''mat_slopescaledepthbias_shadowmap'' to 4. You can change these values by changing the ConVar constructor at the beginning of ''c_env_projectedtexture.cpp'' (Located in : src\game\client...). | ||
{{warning|Reducing the depth bias ConVars results in small, grain like | {{warning|Reducing the depth bias ConVars results in small, grain-like artifacts on the flashlight when using larger depth resolutions (i.e. 2048). A good compromise for ''mat_slopescaledepthbias_shadowmap'' is 16.}} | ||
== Lowering the amount of "grain" on shadows == | == Lowering the amount of "grain" on shadows == | ||
[[ | [[File:Grain_high.jpg|thumb| Default (3.0f)]] | ||
[[ | [[File:Grain_low.jpg|thumb| Modified (1.0f) ]] | ||
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. | 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. The most annoying thing about this "blur" is that the noise pattern doesn't move with the player's camera, so it seems like there's some kind of radiation grain attached to the camera or something behind the shadow. 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. | ||
Open '''imaterialsystem.h''', find the constructor for <code>FlashlightState_t</code> and lower the default value of <code>m_flShadowFilterSize</code> from it's current value (3.0f) down to something like | Open '''imaterialsystem.h''' (located in : src\public\materialsystem...), find the constructor for <code>FlashlightState_t</code> and lower the default value of <code>m_flShadowFilterSize</code> from it's current value ('''3.0f''') down to something like ('''0.5f'''). At increasing depth resolutions, a lower filter size can be used to still effectively soften shadow edges, but with a lower detail loss. See the table below for recommended values: | ||
{| class=standard-table | {| class=standard-table | ||
Line 143: | Line 182: | ||
|- | |- | ||
| 512 | | 512 | ||
| | | 2.0f | ||
|- | |- | ||
| 1024 | | 1024 | ||
| | | 0.8f | ||
|- | |- | ||
| 2048 | | 2048 | ||
| 0. | | 0.3f | ||
|- | |- | ||
| 4096 | | 4096 | ||
| 0. | | 0.1f | ||
|} | |} | ||
Line 160: | Line 199: | ||
As rendertargets created with <code>RT_SIZE_OFFSCREEN</code> 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 <code>RT_SIZE_NO_CHANGE</code>. However, another problem arises when <code>IVRenderView::Push3DView</code> does not properly inform the material system of our new depth texture and render target. Therefore, after the <code>IVRenderView::Push3DView</code> call, we need to manually push our new render target and depth texture to the material system. | As rendertargets created with <code>RT_SIZE_OFFSCREEN</code> 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 <code>RT_SIZE_NO_CHANGE</code>. However, another problem arises when <code>IVRenderView::Push3DView</code> does not properly inform the material system of our new depth texture and render target. Therefore, after the <code>IVRenderView::Push3DView</code> call, we need to manually push our new render target and depth texture to the material system. | ||
To fix these issues, start by opening '''clientshadowmgr.cpp''' and finding <code>void CClientShadowMgr::InitDepthTextureShadows()</code>. Add the code highlighted below | To fix these issues, start by opening '''clientshadowmgr.cpp''' (located in : src\game\client\...), and finding <code>void CClientShadowMgr::InitDepthTextureShadows()</code>. Add the code highlighted below : | ||
<source lang=cpp highlight=8-13,31-32,52-53,56-57,66-67> | <source lang=cpp highlight=8-13,31-32,52-53,56-57,66-67> | ||
Line 214: | Line 253: | ||
depthTex.InitRenderTargetSurface( 1, 1, dstFormat, false ); | depthTex.InitRenderTargetSurface( 1, 1, dstFormat, false ); | ||
#else | #else | ||
// SAUL: we want to create a *DEPTH TEXTURE* of specific size, so use RT_SIZE_NO_CHANGE | // SAUL: we want to create a *DEPTH TEXTURE* of specific size, so use RT_SIZE_NO_CHANGE | ||
depthTex.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_NO_CHANGE, dstFormat, | depthTex.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_NO_CHANGE, dstFormat, MATERIAL_RT_DEPTH_NONE, false, strRTName ); | ||
#endif | #endif | ||
Line 232: | Line 271: | ||
}</source> | }</source> | ||
Now open '''viewrender.cpp''' and find <code>void CShadowDepthView::Draw()</code>. '''Immediately before''' <code>SetupCurrentView(...)</code> (line 4654), add: | Now open '''viewrender.cpp''' (located in : src\game\client\...), and find <code>void CShadowDepthView::Draw()</code>. '''Immediately before''' <code>SetupCurrentView(...)</code> ('''line 4654'''), add: | ||
<source lang=cpp>pRenderContext.GetFrom(materials); | <source lang=cpp>pRenderContext.GetFrom(materials); | ||
Line 238: | Line 277: | ||
pRenderContext.SafeRelease();</source> | pRenderContext.SafeRelease();</source> | ||
Move to the end of the function, and '''immediately before''' <code>render->PopView( GetFrustum() );</code> (line 4702), add: | Move to the end of the function, and '''immediately before''' <code>render->PopView( GetFrustum() );</code> ('''line 4702'''), add: | ||
<source lang=cpp>pRenderContext->PopRenderTargetAndViewport();</source> | <source lang=cpp>pRenderContext->PopRenderTargetAndViewport();</source> | ||
== Fix configurable texture value in Hammer == | == Fix configurable texture value in Hammer == | ||
Open '''env_projectedtexture.cpp''' in | Open '''env_projectedtexture.cpp''' (Located in : src\game\server...), find the line <code>DEFINE_AUTO_ARRAY_KEYFIELD( m_SpotlightTextureName, FIELD_CHARACTER, "texturename" ),</code> and comment it out. And immediately after this line, add: | ||
<source lang=cpp>DEFINE_AUTO_ARRAY( m_SpotlightTextureName, FIELD_CHARACTER ),</source> | <source lang=cpp>DEFINE_AUTO_ARRAY( m_SpotlightTextureName, FIELD_CHARACTER ),</source> | ||
Find the function <code>bool CEnvProjectedTexture::KeyValue( const char *szKeyName, const char *szValue )</code> (line 157) further down the file, and replace the entire function with: | Find the function <code>bool CEnvProjectedTexture::KeyValue( const char *szKeyName, const char *szValue )</code> ('''line 157''') further down the file, and replace the entire function with: | ||
<source lang=cpp> if ( FStrEq( szKeyName, "lightcolor" ) ) | <source lang=cpp> if ( FStrEq( szKeyName, "lightcolor" ) ) | ||
{ | { | ||
Line 267: | Line 306: | ||
{{note|Paths of textures in entity properties are relative to '''<your mod>/materials''' and should have no extension. For example: <code>effects/flashlight001</code>}} | {{note|Paths of textures in entity properties are relative to '''<your mod>/materials''' and should have no extension. For example: <code>effects/flashlight001</code>}} | ||
== Alternative "proper" Hammer fix == | |||
For those who would like to enter a texture name with Smart Edit on, which provides the material browser, you may elect to do the following: | |||
1)- In sourcesdk/bin/whateveryourmoduses/bin you can copy base.fgd to your mod's main folder. Now Hammer will read from this file, instead of the one in the SDK. (you may even want to change the name to something like mymod_base.fgd. If you do this though, make sure subsequent files in your mod's .fgd chain 'include' this altered file name) | |||
2)- In your new base .fgd file, find the env_projectedtexture entry and add a line of code within it's brackets: | |||
<source lang=cpp> texturename(material) : "Texture" : : "path/texture to be projected. Relative to main/materials/" </source> | |||
{{important|Keep in mind that this KV takes a VTF, not a VMT, so options shown in the texture browser may be inaccurate. This is unavoidable without modifying Hammer.}} | |||
== Fixing back projection == | |||
Projected textures will cast the light backwards from the direction they are facing. This is because the flashlight shader doesn't cull pixels behind its origin. | |||
Add this line into <code>flashlight_ps2x.fxc</code> and recompile the shader. Code fix sourced from [[Alien Swarm]] SDK. | |||
<source lang=cpp highlight=5> | |||
#else | |||
spotColor = tex2D( SpotSampler, vProjCoords ); | |||
#endif | |||
spotColor *= i.spotTexCoord.www > float3(0,0,0); // Catch back projection (PC-only at the moment) | |||
float4 baseColor = 0.0f; | |||
</source> | |||
== Fixing engine crash related to ragdoll rendering == | |||
The engine may rarely crash when rendering a [[ragdoll]] whose model instance was snatched from another entity. This is because the engine forgets to update an internal list for model-to-shadow mappings. | |||
<source lang=cpp highlight=7-8> | |||
bool C_BaseEntity::SnatchModelInstance( C_BaseEntity *pToEntity ) | |||
{ | |||
ModelInstanceHandle_t handle = GetModelInstance(); | |||
if ( !modelrender->ChangeInstance( handle, pToEntity ) ) | |||
return false; // engine could move modle handle | |||
// remove stale shadow data | |||
shadowmgr->RemoveAllShadowsFromModel( handle ); | |||
</source> | |||
== Fixing projected textures fading out at a distance == | |||
Projected textures fade out the further you move away from them. This is because the flashlight shader uses the wrong position to calculate light falloff - it uses your eye position rather than flashlight position. | |||
Add these lines in <code>CBaseVSShader::DrawFlashlight_dx90</code>: | |||
<source lang=cpp highlight=3-8> | |||
s_pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, atten, 1 ); | |||
float pos[4]; | |||
pos[0] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin | |||
pos[1] = flashlightState.m_vecLightOrigin[1]; | |||
pos[2] = flashlightState.m_vecLightOrigin[2]; | |||
pos[3] = 1.0f; | |||
pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_POSITION_RIM_BOOST, pos, 1 ); | |||
SetFlashlightVertexShaderConstants( vars.m_bBump, vars.m_nBumpTransform, bDetail, vars.m_nDetailScale, bSeamless ? false : true ); | |||
</source> | |||
Modify these lines in <code>flashlight_ps2x.fxc</code>: | |||
<source lang=cpp highlight=2> | |||
const float4 g_FlashlightAttenuation : register( PSREG_FLASHLIGHT_ATTENUATION ); | |||
const float4 g_FlashlightPos : register( PSREG_FLASHLIGHT_POSITION_RIM_BOOST ); | |||
const float4 g_DetailConstants : register( c0 ); | |||
</source> | |||
<source lang=cpp highlight=2> | |||
// Compute per-pixel distance attenuation | |||
float3 delta = g_FlashlightPos.xyz - i.worldPos_worldTransition.xyz; | |||
float distSquared = dot( delta, delta ); | |||
</source> | |||
[[Category:Bug fixes]] | [[Category:Bug fixes]] | ||
[[Category:Snippets]] | [[Category:Snippets]] | ||
Latest revision as of 09:03, 19 August 2025

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 simple C++ fix:
In CClientShadowMgr::Init()
(located in : src\game\client\Clientshadowmgr.cpp, and around line 1293), replace the following:
bool bTools = CommandLine()->CheckParm( "-tools" ) != NULL;
m_nMaxDepthTextureShadows = bTools ? 4 : 1; // Just one shadow depth texture in games, more in tools
With this:
m_nMaxDepthTextureShadows = 10; //10 - shadow maps in the game. It doesn't matter if the -tools are enabled..


CClientShadowMgr::BuildActiveShadowDepthList
.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 (Located in : src\game\client...) and around (line 174) there is an else
block containing an assert and some commented code. Replace the whole block with this:
else
{
// VXP: Fixing targeting
Vector vecToTarget;
QAngle vecAngles;
if ( m_hTargetEntity == NULL )
{
vecAngles = GetAbsAngles();
}
else
{
vecToTarget = m_hTargetEntity->GetAbsOrigin() - GetAbsOrigin();
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 (Located in src\game\server) and around (line 240) 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 the 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 (located in : src\game\client\hl2...)
static 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 (Located in : src\game\client...) and around (line 222), comment out the 3 lines like this:
// if ( bForceUpdate == false )
// {
g_pClientShadowMgr->UpdateProjectedTexture( m_LightHandle, true );
// }
and around (line 248) in the Simulate function, change the UpdateLight call to:
UpdateLight( GetMoveParent() != NULL );
Enabling visibility tests
Projected textures are not tested for visibility. To enable AABB-frustum visibility culling, open up clientshadowmgr.cpp (located in : src\game\client\...), and find the function CClientShadowMgr::BuildFlashlight. Add the following highlighted code to the beginning of the function:
void CClientShadowMgr::BuildFlashlight( ClientShadowHandle_t handle )
{
// For the 360, we just draw flashlights with the main geometry
// and bypass the entire shadow casting system.
ClientShadow_t &shadow = m_Shadows[handle];
if ( IsX360() || r_flashlight_version2.GetInt() )
{
// This will update the matrices, but not do work to add the flashlight to surfaces
shadowmgr->ProjectFlashlight( shadow.m_ShadowHandle, shadow.m_WorldToShadow, 0, NULL );
return;
}
VPROF_BUDGET( "CClientShadowMgr::BuildFlashlight", VPROF_BUDGETGROUP_SHADOW_DEPTH_TEXTURING );
// Don't project the flashlight if the frustum AABB is not in our view
Vector mins, maxs;
CalculateAABBFromProjectionMatrix(shadow.m_WorldToShadow, &mins, &maxs);
if(engine->CullBox(mins, maxs))
return;

Removing incorrect culling
The built-in env_projectedtexture culling is broken. If an env_projectedtexture is placed pointing in one direction, and the player turns to face the opposite direction, the light can be seen in the direction that the player is pointing under certain circumstances. YouTube video to demonstrate the issue[1].
The below code in the function CClientShadowMgr::BuildActiveShadowDepthList() in clientshadowmgr.cpp (located in : src\game\client\...), can be commented out to fix this behavior, around (Line 3900):
const FlashlightState_t& flashlightState = shadowmgr->GetFlashlightState( shadow.m_ShadowHandle );
// Bail if this flashlight doesn't want shadows
if ( !flashlightState.m_bEnableShadows )
continue;
// Calculate an AABB around the shadow frustum
/*Vector vecAbsMins, vecAbsMaxs;
CalculateAABBFromProjectionMatrix( shadow.m_WorldToShadow, &vecAbsMins, &vecAbsMaxs );
Frustum_t viewFrustum;
GeneratePerspectiveFrustum( viewSetup.origin, viewSetup.angles, viewSetup.zNear, viewSetup.zFar, viewSetup.fov, viewSetup.m_flAspectRatio, viewFrustum );
// FIXME: Could do other sorts of culling here, such as frustum-frustum test, distance etc.
// If it's not in the view frustum, move on
if ( R_CullBox( vecAbsMins, vecAbsMaxs, viewFrustum ) )
{
shadowmgr->SetFlashlightDepthTexture( shadow.m_ShadowHandle, NULL, 0 );
continue;
}*/
if ( nActiveDepthShadowCount >= nMaxDepthShadows )
{
static bool s_bOverflowWarning = false;

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 (located in : src\game\shared...), 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
.

return true;
, for example :
/* virtual bool ShouldReceiveProjectedTextures( int flags )
{
return false;
}*/
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 4. You can change these values by changing the ConVar constructor at the beginning of c_env_projectedtexture.cpp (Located in : src\game\client...).

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. The most annoying thing about this "blur" is that the noise pattern doesn't move with the player's camera, so it seems like there's some kind of radiation grain attached to the camera or something behind the shadow. 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.
Open imaterialsystem.h (located in : src\public\materialsystem...), find the constructor for FlashlightState_t
and lower the default value of m_flShadowFilterSize
from it's current value (3.0f) down to something like (0.5f). At increasing depth resolutions, a lower filter size can be used to still effectively soften shadow edges, but with a lower detail loss. See the table below for recommended values:
Depth resolution | Recommended filter size |
---|---|
512 | 2.0f |
1024 | 0.8f |
2048 | 0.3f |
4096 | 0.1f |
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
. 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, start by opening clientshadowmgr.cpp (located in : src\game\client\...), and finding void CClientShadowMgr::InitDepthTextureShadows()
. Add the code highlighted below :
//-----------------------------------------------------------------------------
// 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
depthTex.InitRenderTarget( m_nDepthTextureResolution, m_nDepthTextureResolution, RT_SIZE_NO_CHANGE, dstFormat, MATERIAL_RT_DEPTH_NONE, 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 (located in : src\game\client\...), 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();
Fix configurable texture value in Hammer
Open env_projectedtexture.cpp (Located in : src\game\server...), find the line DEFINE_AUTO_ARRAY_KEYFIELD( m_SpotlightTextureName, FIELD_CHARACTER, "texturename" ),
and comment it out. And immediately after this line, add:
DEFINE_AUTO_ARRAY( m_SpotlightTextureName, FIELD_CHARACTER ),
Find the function bool CEnvProjectedTexture::KeyValue( const char *szKeyName, const char *szValue )
(line 157) further down the file, and replace the entire function with:
if ( FStrEq( szKeyName, "lightcolor" ) )
{
Vector tmp;
UTIL_ColorStringToLinearFloatColor( tmp, szValue );
m_LinearFloatLightColor = tmp;
}
else if ( FStrEq(szKeyName, "texturename" ) )
{
Q_strcpy( m_SpotlightTextureName.GetForModify(), szValue );
}
else
{
return BaseClass::KeyValue( szKeyName, szValue );
}
return true;
After recompiling these code fixes, you will now be able to set a texture value in Hammer for your env_projectedtextures. However, as the texturename KeyValue is not present in the base FGD, you will need to manually set the texturename KeyValue in each entity you wish to have a non-default projected texture. To do this, de-toggle SmartEdit and then click on Add. Set "texturename" as the name, and the path to your texture as the "value". Now your projected texture will have a specific texture at spawn without having to be set via entity IO post-spawn.

effects/flashlight001
Alternative "proper" Hammer fix
For those who would like to enter a texture name with Smart Edit on, which provides the material browser, you may elect to do the following:
1)- In sourcesdk/bin/whateveryourmoduses/bin you can copy base.fgd to your mod's main folder. Now Hammer will read from this file, instead of the one in the SDK. (you may even want to change the name to something like mymod_base.fgd. If you do this though, make sure subsequent files in your mod's .fgd chain 'include' this altered file name)
2)- In your new base .fgd file, find the env_projectedtexture entry and add a line of code within it's brackets:
texturename(material) : "Texture" : : "path/texture to be projected. Relative to main/materials/"

Fixing back projection
Projected textures will cast the light backwards from the direction they are facing. This is because the flashlight shader doesn't cull pixels behind its origin.
Add this line into flashlight_ps2x.fxc
and recompile the shader. Code fix sourced from Alien Swarm SDK.
#else
spotColor = tex2D( SpotSampler, vProjCoords );
#endif
spotColor *= i.spotTexCoord.www > float3(0,0,0); // Catch back projection (PC-only at the moment)
float4 baseColor = 0.0f;
The engine may rarely crash when rendering a ragdoll whose model instance was snatched from another entity. This is because the engine forgets to update an internal list for model-to-shadow mappings.
bool C_BaseEntity::SnatchModelInstance( C_BaseEntity *pToEntity )
{
ModelInstanceHandle_t handle = GetModelInstance();
if ( !modelrender->ChangeInstance( handle, pToEntity ) )
return false; // engine could move modle handle
// remove stale shadow data
shadowmgr->RemoveAllShadowsFromModel( handle );
Fixing projected textures fading out at a distance
Projected textures fade out the further you move away from them. This is because the flashlight shader uses the wrong position to calculate light falloff - it uses your eye position rather than flashlight position.
Add these lines in CBaseVSShader::DrawFlashlight_dx90
:
s_pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, atten, 1 );
float pos[4];
pos[0] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin
pos[1] = flashlightState.m_vecLightOrigin[1];
pos[2] = flashlightState.m_vecLightOrigin[2];
pos[3] = 1.0f;
pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_POSITION_RIM_BOOST, pos, 1 );
SetFlashlightVertexShaderConstants( vars.m_bBump, vars.m_nBumpTransform, bDetail, vars.m_nDetailScale, bSeamless ? false : true );
Modify these lines in flashlight_ps2x.fxc
:
const float4 g_FlashlightAttenuation : register( PSREG_FLASHLIGHT_ATTENUATION );
const float4 g_FlashlightPos : register( PSREG_FLASHLIGHT_POSITION_RIM_BOOST );
const float4 g_DetailConstants : register( c0 );
// Compute per-pixel distance attenuation
float3 delta = g_FlashlightPos.xyz - i.worldPos_worldTransition.xyz;
float distSquared = dot( delta, delta );