Adding Radial Fog To Source SDK 2013
This tutorial will guide you to adding radial fog to your source sdk 2013 mod. Completely overriding the default linear plane fog.
Requirements
- A mod running on Source SDK 2013
- Knowledge of C++
- Knowledge on compiling shaders. If you don't know how to then I suggest you watch this video by TheMaster974
Getting Started
To get started. You must open the everything.sln solution (or any other solution with either the Game Shaders (hl2) or Game Shaders (episodic) project in it).
Once the project is opened up. You will need to open up the Game Shaders (hl2) or Game Shaders (episodic) project.
You are now going to need to go to common_fxc.h in the shaders Header Files folder.
Once in here, at the very bottom of the file and just above:
#endif //#ifndef COMMON_FXC_H_
You need to add:
// Returns 1.0f for no fog, 0.0f for fully fogged
float CalcRadialFog_FixedFunction(float3 vWorldPos, float3 vEyePos, float flFogMaxDensity, float flFogEndOverRange, float flFogOORange)
{
float flDistance = distance(vEyePos.xyz, vWorldPos.xyz);
return max(flFogMaxDensity, (-flDistance * flFogOORange) + flFogEndOverRange);
}
// Returns 0.0f for no fog, 1.0f for fully fogged which is opposite of what fixed function fog expects so that we don't have to do a "1-x" in the pixel shader.
float CalcRadialFog_NonFixedFunction(float3 vWorldPos, float3 vEyePos, float flFogMaxDensity, float flFogEndOverRange, float flFogOORange)
{
float flDistance = distance(vEyePos.xyz, vWorldPos.xyz);
return min(flFogMaxDensity, saturate((flDistance * flFogOORange) - flFogEndOverRange));
}
These functions are the main functions used to calculate the radial fog.
You now want to head to common_ps_fxc.h and find:
float CalcPixelFogFactor( int iPIXELFOGTYPE, const float4 fogParams, const float flEyePosZ, const float flWorldPosZ, const float flProjPosZ )
(around line 240) and replace the entire function with:
float CalcPixelFogFactor(int iPIXELFOGTYPE, const float4 fogParams, const float3 vEyePos, const float3 vWorldPos, const float flProjPosZ)
{
float retVal = 0.0f;
if (iPIXELFOGTYPE == PIXEL_FOG_TYPE_NONE)
{
retVal = 0.0f;
}
else if (iPIXELFOGTYPE == PIXEL_FOG_TYPE_RANGE) //range fog, or no fog depending on fog parameters
{
float flFogMaxDensity = fogParams.z;
float flFogEndOverRange = fogParams.x;
float flFogOORange = fogParams.w;
retVal = CalcRadialFog_NonFixedFunction(vWorldPos, vEyePos, flFogMaxDensity, flFogEndOverRange, flFogOORange);
}
else if (iPIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT) //height fog
{
retVal = CalcWaterFogAlpha(fogParams.y, vEyePos.z, vWorldPos.z, flProjPosZ, fogParams.w);
}
return retVal;
}
// Legacy support overload, without range fog support.
float CalcPixelFogFactor(int iPIXELFOGTYPE, const float4 fogParams, const float flEyePosZ, const float flWorldPosZ, const float flProjPosZ)
{
// Old HLSL hack support... Can't just set here.
return CalcPixelFogFactor(iPIXELFOGTYPE, fogParams, float3(0, 0, flEyePosZ), float3(0, 0, flWorldPosZ), flProjPosZ);
}
Now head over to common_vs_fxc.h and inside it find the function:
float CalcFog( const float3 worldPos, const float3 projPos, const int fogType )
(around line 552) find:
return RangeFog( projPos );
and replace that with:
return CalcRadialFog_FixedFunction(worldPos, cEyePos, cFogMaxDensity, cFogEndOverFogRange, cOOFogRange);
Inside the same file find the function:
float CalcFog( const float3 worldPos, const float3 projPos, const bool bWaterFog )
(should be just below the other function mentioned above) and replace:
flFog = RangeFog( projPos );
with
flFog = CalcRadialFog_FixedFunction(worldPos, cEyePos, cFogMaxDensity, cFogEndOverFogRange, cOOFogRange);
Updating LightmappedGeneric
Inside the Header Files folder of your visual studio Game Shaders project. Add the existing file: 'lightmappedgeneric_ps2_3_x.h' to the folder.
Then open the file in a text editor OR in visual studio.
Inside the file you need to find:
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );
(around the bottom of the file) and replace it with:
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.xyz, i.worldPos_projPosZ.xyz, i.worldPos_projPosZ.w );
Now inside the Shader Source->fxc folder of the project. add these existing files:
lightmappedgeneric_ps2x.fxc
lightmappedgeneric_vs20.fxc
Updating VertexLitGeneric
Inside the Shader Source->fxc folder of your visual studio Game Shaders project. Add these existing files:
vertexlit_and_unlit_generic_bump_ps2x.fxc
vertexlit_and_unlit_generic_bump_vs20.fxc
vertexlit_and_unlit_generic_ps2x.fxc
vertexlit_and_unlit_generic_vs20.fxc
Now open up the 'vertexlit_and_unlit_generic_bump_ps2x.fxc' file. Inside this file, you would want to replace:
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );(around line 330) with:
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.xyz, i.worldPos_projPosZ.xyz, i.worldPos_projPosZ.w );
Now just under that (around line 332) replace:
float fogFactor = CalcPixelFogFactorConst( g_fPixelFogType, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );with:
float fogFactor = CalcPixelFogFactor( g_fPixelFogType, g_FogParams, g_EyePos.xyz, i.worldPos_projPosZ.xyz, i.worldPos_projPosZ.w );
Now open up the 'vertexlit_and_unlit_generic_ps2x.fxc' file. Inside this file, you would want to replace:
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.projPos.z );at line 471 with:
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.xyz, i.worldPos_projPosZ.xyz, i.projPos.z );
Now at around line 477, replace:
float fogFactor = CalcPixelFogFactorConst( g_fPixelFogType, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.projPos.z );with:
float fogFactor = CalcPixelFogFactor( g_fPixelFogType, g_FogParams, g_EyePos.xyz, i.worldPos_projPosZ.xyz, i.projPos.z );
Updating The Water Shader
You now must modify some of the water shader code for the radial fog to work on it. If you don't then water will use normal fog whilst (almost) everything else will use radial fog, which doesn't look good.
To get started, you must head to your mod's 'src/materialsystem/stdshaders' folder. Inside this folder, create a copy of these files:
water.cpp
water_ps2x.fxc
water_ps2x_helper.h
Water_vs20.fxc
WaterCheap_ps2x.fxc
WaterCheap_vs20.fxcand rename them to be
RadialFog_water.cpp
RadialFog_water_ps2x.fxc
RadialFog_water_ps2x_helper.h
RadialFog_Water_vs20.fxc
RadialFog_WaterCheap_ps2x.fxc
RadialFog_WaterCheap_vs20.fxc
Now in the visual studio side of things. In your Game Shaders project, add the newly created files into these project folders:
Source Files:
RadialFog_water.cpp
Header Files:
RadialFog_water_ps2x_helper.h
Shader Source -> fxc:
RadialFog_water_ps2x.fxc
RadialFog_Water_vs20.fxc
RadialFog_WaterCheap_ps2x.fxc
RadialFog_WaterCheap_vs20.fxc
Now open up the 'RadialFog_water.cpp' file. Inside this file. you would want to replace:
#include "WaterCheap_vs20.inc"
#include "WaterCheap_ps20.inc"
#include "WaterCheap_ps20b.inc"
#include "Water_vs20.inc"
#include "Water_ps20.inc"
#include "water_ps20b.inc"
with
#include "RadialFog_WaterCheap_vs20.inc"
#include "RadialFog_WaterCheap_ps20.inc"
#include "RadialFog_WaterCheap_ps20b.inc"
#include "RadialFog_Water_vs20.inc"
#include "RadialFog_Water_ps20.inc"
#include "RadialFog_water_ps20b.inc"
Now look for
DEFINE_FALLBACK_SHADER( Water, Water_DX9_HDR )
(at around line 24) and replace it with with
DEFINE_FALLBACK_SHADER( RadialFog_Water, RadialFog_Water_DX9_HDR )
Then just below it. Change:
BEGIN_VS_SHADER( Water_DX90,
with
BEGIN_VS_SHADER( RadialFog_Water_DX90,
Then at the very bottom of the file. Replace:
//-----------------------------------------------------------------------------
// This allows us to use a block labelled 'Water_DX9_HDR' in the water materials
//-----------------------------------------------------------------------------
BEGIN_INHERITED_SHADER( Water_DX9_HDR, Water_DX90,
"Help for Water_DX9_HDR" )
SHADER_FALLBACK
{
if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
{
return "WATER_DX90";
}
return 0;
}
END_INHERITED_SHADER
with
//-----------------------------------------------------------------------------
// This allows us to use a block labelled 'Water_DX9_HDR' in the water materials
//-----------------------------------------------------------------------------
BEGIN_INHERITED_SHADER( RadialFog_Water_DX9_HDR, RadialFog_Water_DX90,
"Help for Water_DX9_HDR" )
SHADER_FALLBACK
{
if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
{
return "RADIALFOG_WATER_DX90";
}
return 0;
}
END_INHERITED_SHADER
Now, in the same file, look for
pShaderAPI->SetPixelShaderFogParams( 8 );
(should be line 369). Once found, add:
float vEyePos[4];
pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
vEyePos[3] = 0.0f;
pShaderAPI->SetPixelShaderConstant( 9, vEyePos );
just under it.
Now look for
pShaderAPI->SetPixelShaderConstant( 1, cheapWaterParams );
(should be line 501) and under it add
float vEyePos[4];
pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
vEyePos[3] = 0.0f;
pShaderAPI->SetPixelShaderConstant( 4, vEyePos );
just under it.
Now, through out your file, You want to manually replace every instance of:
water_vs20 -> radialfog_water_vs20
water_ps20b -> radialfog_water_ps20b
water_ps20 -> radialfog_water_ps20
watercheap_vs20 -> radialfog_watercheap_vs20
watercheap_ps20b -> radialfog_watercheap_ps20b
watercheap_ps20 -> radialfog_watercheap_ps20for inside the
DECLARE_STATIC_VERTEX_SHADER(
SET_STATIC_VERTEX_SHADER(
DECLARE_DYNAMIC_VERTEX_SHADER(
SET_DYNAMIC_PIXEL_SHADER(macros.
Now open up 'RadialFog_water_ps2x_helper.h'. Inside the file, look for
float fWaterFogStart;
float fWaterFogEndMinusStart;
(around line 25) and add these 2 variables under it
float3 vEyePos;
float3 vWorldPos;
Now at the very bottom of the file. Replace:
#if (PIXELFOGTYPE == PIXEL_FOG_TYPE_RANGE)
fogFactor = CalcRangeFog( i.vProjPos.z, i.pixelFogParams.x, i.pixelFogParams.z, i.pixelFogParams.w );
#else
fogFactor = 0;
#endif
with:
fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, i.pixelFogParams, i.vEyePos.xyz, i.vWorldPos.xyz, i.vProjPos.z );
now open up 'RadialFog_Water_vs20.fxc'. and at around line 30. Replace
float4 vReflectXY_vRefractYX : TEXCOORD2;
float W : TEXCOORD3;with
float4 vReflectXY_vRefractYX : TEXCOORD2;
float4 vWorldPos_projPosW : TEXCOORD3;
now at line 71 replace
o.W = vProjPos.w;with
o.vWorldPos_projPosW.w = vProjPos.w;
now at line 81 and just under
#if !defined( _X360 )
o.vFog = o.fogFactorW;
#endifadd
o.vWorldPos_projPosW.xyz = vWorldPos;
now head to 'RadialFog_water_ps2x.fxc'. Inside this file and just under
const float4 g_PixelFogParams : register( c8 );(at line 44) add:
const float3 g_EyePos : register( c9 );
now at line 56. Replace
float W : TEXCOORD3;with
float4 vWorldPos_projPosW : TEXCOORD3;
now look for
params.w = i.W;(around line 82) and replace it with
params.w = i.vWorldPos_projPosW.w;
now look for
params.fWaterFogEndMinusStart = g_WaterFogEndMinusStart;(around line 96) and under it add
params.vEyePos = g_EyePos;
params.vWorldPos = i.vWorldPos_projPosW.xyz;
now look for
#include "water_ps2x_helper.h"(around line 25) and replace it with
#include "RadialFog_water_ps2x_helper.h"
now go to 'RadialFog_WaterCheap_vs20.fxc'. In this file under
float4 vExtraBumpTexCoord : TEXCOORD6;(at around line 23) you want to add:
float3 vWorldPos : TEXCOORD7;
now under
#if !defined( _X360 )
o.fog = o.fogFactorW;
#endif(around line 81) you want to add:
o.vWorldPos = worldPos;
now go to 'RadialFog_WaterCheap_ps2x.fxc' and look for
const float4 g_PixelFogParams : register( c3 );(around line 19). Under it add:
const float3 g_EyePos : register( c4 );
now look for
#if MULTITEXTURE
float4 vExtraBumpTexCoord : TEXCOORD6;
#endif(around line 43) and under it add:
float3 vWorldPos : TEXCOORD7;
now at line 147 find:
#if (PIXELFOGTYPE == PIXEL_FOG_TYPE_RANGE)
float fogFactor = CalcRangeFog( i.vRefract_W_ProjZ.w, g_PixelFogParams.x, g_PixelFogParams.z, g_PixelFogParams.w );
#else
float fogFactor = 0;
#endifand replace it with
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_PixelFogParams, g_EyePos.xyz, i.vWorldPos.xyz, i.vRefract_W_ProjZ.w );
Now. You will need to update every water material in the game to use the RadialFog_Water shader. To save you the hassle of doing that. I created a google drive link with all the hl2 + ep1 + ep2 water materials with the radial fog shaders added:
Miscellaneous Shader Fixes
Inside the Shader Source->fxc folder of your visual studio Game Shaders project. Add these existing files to your project:
cable_ps2x.fxc
cable_vs20.fxc
refract_ps2x.fxc
Refract_vs20.fxc
sprite_ps2x.fxc
sprite_vs20.fxc
skin_ps20b.fxc
skin_vs20.fxc
Now open up each one of the pixel shader (_ps2) files. Inside all these files, Your going to want to find a line that looks something like:
float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos_SpecExponent.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );Inside this function call. Change:
g_EyePos_SpecExponent.z -> g_EyePos_SpecExponent.xyz
i.worldPos_projPosZ.z -> i.worldPos_projPosZ.xyz
Finishing Up
Now, Head to the 'stdshader_dx9_20b.txt' file. Inside this, add this to the bottom of the file
//radial fog
lightmappedgeneric_ps11.fxc
lightmappedgeneric_ps2x.fxc
RadialFog_Water_vs20.fxc
RadialFog_water_ps2x.fxc
RadialFog_WaterCheap_vs20.fxc
RadialFog_WaterCheap_ps2x.fxc
vertexlit_and_unlit_generic_bump_ps2x.fxc
vertexlit_and_unlit_generic_bump_vs20.fxc
vertexlit_and_unlit_generic_ps2x.fxc
vertexlit_and_unlit_generic_vs20.fxc
skin_ps20b.fxc
skin_vs20.fxc
sprite_ps2x.fxc
sprite_vs20.fxc
cable_ps2x.fxc
cable_vs20.fxc
refract_ps2x.fxc
Refract_vs20.fxc
Finally, Head to the 'stdshader_dx9_30.txt' file. Inside this, add this to the bottom of the file
//radial fog
vertexlit_and_unlit_generic_bump_ps2x.fxc
vertexlit_and_unlit_generic_bump_vs20.fxc
vertexlit_and_unlit_generic_ps2x.fxc
vertexlit_and_unlit_generic_vs20.fxc
Now compile the shaders, build the shaders project in visual studio, and you should now have radial fog.