Adding PBR to Your Mod: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Use "YouTube" template for link)
 
(50 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{LanguageBar}}
{{warning
|text =  ''This shader does not work with lightmapped brushes when using stock VBSP compilers, however it technically is compatible with brushes.''
{{code fix|text = In {{Path|src/utils/common/utilmatlib|cpp|icon=file}} find {{code|InitMaterialSystem}} method and at the end add  {{code|g_pMaterialSystem->ModInit();}} and try to recompile tools.}}
{{tip|Fixed in {{strata}}, as well as the {{mapbase}}'s and {{slammin}}'s [[VBSP]]s.}}
}}
[[File:PBR comparison.png|420px|thumb|right|<center>'''Comparison of PBR'''</center><br> Left: Base Source Engine shaders.<br> Right: The PBR shader.]]
{{Distinguish|Adapting PBR Textures to Source}}
The goal of this article is to show you how to implement [https://github.com/thexa4/source-pbr Thexa4's PBR shader] into your own {{src13|4}} mod. Implementing this shader will allow you to use the metalness/roughness PBR workflow in materials.
For a video-based version of this tutorial, check out {{youtube|nThOxf-N6D8|page=watch|TheMaster974's YouTube video guide}}.
== Introduction ==
== Introduction ==
In this article we would look at the PBR shader and implement it to our SDK2013 Mod. PBR is a physically based shading model that is used on newer games. Implementing this shader to our mod would enable us to use metalness, roughness workflows to our materials.
 
{{Main|Physically Based Rendering}}
 
 
== Requirements ==
== Requirements ==
*1. Ability to compile shaders. if you do not know how to compile shaders yet, it is advised to follow this article first: https://developer.valvesoftware.com/wiki/Shader_Authoring
 
*2. Ability to compile the solution.
* Ability to compile shaders.
== Files we need ==
* Ability to compile the solution.
Before doing anything, we will clone this repository https://github.com/thexa4/source-pbr  so that we have all the files we will need. the PBR implementation is made by thexa4.
 
 
{{note|If you do not know how to compile shaders, or haven't worked with shaders in {{src|4}} yet, it is advised that you follow and understand these articles first: [[Shader Authoring]] & [[Source_SDK_2013:_Your_First_Shader|Your First Shader]]}}
 
 
== Implementation ==  
== Implementation ==  
The PBR shader consists of 3 files.
*1. pbr_dx9.cpp
*2. pbr_vs30.fxc
*3. pbr_ps30.fxc


After cloning the git repository above, go to mp/src/materialsystem/stdshaders/ and find for those 3 files. Copy them to src/materialsystem/stdshaders/ of your mod.  
Before doing anything, we need to download the files we need.
after you are done copying, open game_shader_dx9.vpc and add the PBR files so that when we refresh the solution they would appear.
 
<pre>
 
$Folder "fxc"
You can do this by going to the links below, clicking the "Raw" button and once the page has loaded right-click and select "Save As...".
 
Then all you have to do is save each one of the files to your Source mod's code directory under {{Path|src/materialsystem/stdshaders/}}.
 
* [https://github.com/thexa4/source-pbr/blob/feature/pbr-base/mp/src/materialsystem/stdshaders/pbr_dx9.cpp pbr_dx9.cpp]
* [https://github.com/thexa4/source-pbr/blob/feature/pbr-base/mp/src/materialsystem/stdshaders/pbr_vs30.fxc pbr_vs30.fxc]
* [https://github.com/thexa4/source-pbr/blob/feature/pbr-base/mp/src/materialsystem/stdshaders/pbr_ps30.fxc pbr_ps30.fxc]
* [https://github.com/thexa4/source-pbr/blob/feature/pbr-base/mp/src/materialsystem/stdshaders/pbr_vs20b.fxc pbr_vs20b.fxc]
* [https://github.com/thexa4/source-pbr/blob/feature/pbr-base/mp/src/materialsystem/stdshaders/pbr_ps20b.fxc pbr_ps20b.fxc]
* [https://github.com/thexa4/source-pbr/blob/feature/pbr-base/mp/src/materialsystem/stdshaders/pbr_common_ps2_3_x.h pbr_common_ps2_3_x.h]
 
 
After you've finished saving the files, open the {{Path|src/materialsystem/stdshaders/game_shader_dx9_*|vpc|icon=file}} file appropriate for your situation (base/hl2mp/hl2/episodic), and add the PBR files within {{Code|select=all|$Project "Shaders"}} like so:
{{CodeBlock|lines=33|<nowiki>$Project "Shaders"
{
$Folder "Header Files"
{
{
$File "pbr_ps30.fxc"
$File "common_vertexlitgeneric_dx9.h"
$File "pbr_vs30.fxc"
$File "common_lightmappedgeneric_fxc.h"
$File "common_flashlight_fxc.h"
}
}


        $Folder "Source Files"
$Folder "Shader Source"
{
$Folder "fxc"
{
$File "pbr_ps30.fxc"
$File "pbr_vs30.fxc"
$File "pbr_ps20b.fxc"
$File "pbr_vs20b.fxc"
}
 
$Folder "Headers"
{
$File "pbr_common_ps2_3_x.h"
}
 
$File "mymod_dx9_30.txt"
$File "mymod_dx9_20b.txt"
}
 
$Folder "Source Files"
{
{
$File "pbr_dx9.cpp"
$File "pbr_dx9.cpp"
}
}
</pre>
}</nowiki>}}
 
We do this so that when we refresh the solution they will appear in the solution explorer.
 


=== Shader Compilation ===
=== Shader Compilation ===
We need to compile the shaders that we have before we can use them. Create a file named mymod_dx9_30.txt and add  
 
<pre>
We need to compile the shaders that we have before we can use them. Create two files in {{Path|src/materialsystem/stdshaders/}} named {{Code|select=all|mymod_dx9_30.txt}} and {{Code|select=all|mymod_dx9_20b.txt}} and add this inside:
//
{{CodeBlock|lines=11|src=mymod_dx9_30.txt|<nowiki>//
// vs 3.0  ps 3.0  shaders collection
// vs 3.0  ps 3.0  shaders collection
//
//
Line 39: Line 97:
//
//


// There are no examples of such shaders in the SDK, but add yours here.
pbr_vs30.fxc
pbr_ps30.fxc</nowiki>}}
 
{{CodeBlock|lines=11|src=mymod_dx9_20b.txt|<nowiki>//
// Standard shaders collection
//
//  These shaders are compiled as the following shader models:
//     _ps20.vcs
//     _ps20b.vcs
//     _vs20.vcs
//
 
pbr_vs20b.fxc
pbr_ps20b.fxc</nowiki>}}
 
 
Adding new text files for shader compilation will allow you to compile the PBR shaders without affecting the standard {{Shader_Name|LightmappedGeneric}} shaders.
 
This will also reduce compile times significantly.
 
 
Now open the {{Path|buildsdkshaders|bat|icon=file}} file using Notepad and edit this part below from:
{{CodeBlock|%BUILD_SHADER% stdshader_dx9_20b -game %GAMEDIR% -source %SOURCEDIR%
%BUILD_SHADER% stdshader_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30}}
 
To:
{{CodeBlock|%BUILD_SHADER% mymod_dx9_20b -game %GAMEDIR% -source %SOURCEDIR%
%BUILD_SHADER% mymod_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30}}
 
This will allow you to use the custom text file that we made earlier to compile our PBR shader.
 
 
After that, we can now start the shader compilation by running {{Path|buildhl2mpshaders|bat|icon=file}}, {{Path|buildhl2shaders|bat|icon=file}} or {{Path|buildepisodicshaders|bat|icon=file}} depending on your mod's base.
 
Make sure you've followed the [[Shader Authoring]] article to the point where the newly compiled shaders will be properly placed on your mod's {{Path|shaders/fxc/}} folder.
{{note|Depending on your PC and the complexity of the shader, it might take a while.}}
 
 
If the shaders compile without problems, go back to your source code directory {{Path|src/}} and run {{Path|createallprojects|bat|icon=file}}.
 
Run the solution and build shaders in {{Code|Release}}.
 
{{note|If {{Path|pbr_dx9|cpp}} does not exist in the {{Code|Shader}} project. Make sure that you've properly included it in the {{Code|.vpc}} file you edited earlier.}}
 
 
== Fixes ==
 
Physics props turn black when used with the PBR shader after 2-3 seconds due to Prop Sleeping. After a set time, props "bake" their lighting for optimization.
 
You can bypass this by typing {{code|select=all|r_PhysPropStaticLighting 0}} on the console or by hardcoding it in {{Path|src/game/client/c_physicsprop|cpp|icon=file}}.
 
 
Here is how you can hardcode it:
 
Below {{code|select=all|<nowiki>#include "tier0/memdbgon.h"</nowiki>}} add this:
<source lang="cpp">
#define PBR_CHANGE
</source>
 
Then find {{code|select=all|ConVar r_PhysPropStaticLighting("r_PhysPropStaticLighting", "1");}} and turn it into this:
<source lang="cpp">
#ifdef PBR_CHANGE
ConVar r_PhysPropStaticLighting( "r_PhysPropStaticLighting", "0" );
#else
ConVar r_PhysPropStaticLighting( "r_PhysPropStaticLighting", "1" );
#endif
</source>
 
 
== Parameters ==
 
* [[$basetexture]] - The albedo texture.
* '''$normaltexture''' - Sets the normal map (for backwards compatibility).
* [[$bumpmap]] - Sets the normal map (new way of setting normalmap).
* [[$mraotexture]] - A texture with the metalness on the red channel, roughness on the green channel and ambient occlusion on the blue channel.
* '''$speculartexture''' - Enables use of colored F0 (specular map), overrides metalness from MRAO texture.
* '''$emissiontexture''' - Allows setting an emission texture. Enabled self illumination.
* '''$useenvambient''' <[[bool]]> - Makes it use the lowest mip level of the cubemap for ambient light instead of the ambient cubes. Can cause artifacts with moving props.
* [[$envmap]] - Defaults to "env_cubemap", allows you to use a custom one.
* [[$surfaceprop]] - Links the surface of either a [[material]] or [[model]] to a set of physical properties.
* [[$model]] - Should have a value of "1" if the texture is used on a prop.
* [[$translucent]] - Setting this to "1" enables alpha blending.
* [[$basetexturetransform]] - Looks like this: "center 0.5 0.5 scale 0.1025 0.1025 rotate 0 translate 0 0" for a 2m by 2m texture.
* [[$alphatest]] - Enables clipping the pixel when the alpha goes below $alphatestreference.
* [[$alphatestreference]] - Specifies the minimum color value of the alpha channel in which the effect is rounded to 255. A value of ".3" will create a thicker shape while a value of ".7" will create a thinner shape.
* [[%keywords]] - A list of keywords separated by commas. Examples of keywords are: architectural,brown,gray,grime,hanger,industrial,metal,modern,shed,urban,wall,floor
 
 
* '''$useparallax''' <[[bool]]> - Use Parallax Occlusion Mapping.
* '''$parallaxdepth''' <[[float]]> - Depth of the Parallax Map
* '''$parallaxcenter''' <[[float]]> - Center depth of the Parallax Map
 
 
{{note|$emissiontexture is a color texture, not a mask.}}
 
{{bug|hidetested=1|By default all shadows appear to point towards the user as if the user is holding a light, instead of using exterior light sources.}}
 
== Examples ==
=== Brush (non-model surfaces) material example ===
{{CodeBlock|lines=10|<nowiki>PBR
{
        $basetexture      "pbr_asset/texture_albedo"
        $bumpmap          "pbr_asset/texture_normal"
        $mraotexture      "pbr_asset/texture_mrao"
 
        $envmap          "env_cubemap"
        $surfaceprop      "metal"
        $model            0
}</nowiki>}}
 
=== Model material example ===
{{CodeBlock|lines=10|<nowiki>PBR
{
        $basetexture      "models/pbr_asset/texture_albedo"
        $bumpmap          "models/pbr_asset/texture_normal"
        $mraotexture      "models/pbr_asset/texture_mrao"
 
        $envmap          "env_cubemap"
        $surfaceprop      "metal"
        $model            1
}</nowiki>}}
 
=== Model material example with specular map ===
{{Note|Metalness channel is ignored and {{Shader_Name|$basetexture|type=param}} is treated as diffuse.}}
{{CodeBlock|lines=11|<nowiki>PBR
{
        $basetexture      "models/pbr_asset/texture_diffuse"
        $bumpmap          "models/pbr_asset/texture_normal"
        $mraotexture      "models/pbr_asset/texture_mrao"
        $speculartexture  "models/pbr_asset/texture_specular"
 
        $envmap          "env_cubemap"
        $surfaceprop      "metal"
        $model            1
}
</nowiki>}}
 
 
{{note|{{Shader_Name|$envmap|type=param}} should just be left as <code>env_cubemap</code>.}}
 
{{warning|{{Shader_Name|[[$model_(VMT)|$model 1]]|type=param}} '''MUST'''  be added for models to work!}}


pbr_vs30b.fxc
pbr_ps30b.fxc
</pre>
adding a new text file for shader compilation would enable us to compile the PBR shaders without affecting the standard LightmappedGeneric shaders. This would reduce compile times significantly.
open buildsdkshaders.bat using notepad and edit this part below
from
<pre>
%BUILD_SHADER% stdshader_dx9_20b -game %GAMEDIR% -source %SOURCEDIR%
%BUILD_SHADER% stdshader_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30
</pre>
to
<pre>
%BUILD_SHADER% mymod_dx9_20b         -game %GAMEDIR% -source %SOURCEDIR%
%BUILD_SHADER% mymod_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30
</pre>
that would enable us to use the custom text file that we made earlier to compile PBR.
After that, we can now run buildhl2mpshaders.bat/buildhl2shaders.bat/buildepisodicshaders.bat depending on your mod. make sure you followed Shader authoring so that the shaders will be properly placed on your modfolder/shaders/fxc. depending on your PC and the complexity of the shader, it will take a while.


If the shaders compile without problems, go back to your source code directory /src/ and run createallprojects.bat. run the solution and build shaders in release. you can check first if pbr_dx9.cpp exists in the shader project. if not, add it to the vpc file.
== Conclusion ==


Thats it! you should have PBR in your mod. https://wiki.empiresmod.com/PBR lists the shader parameters you can use to create materials.
And that's it! You should now have PBR in your mod.


=== Fixes ===
==See also==
Physics props turn black when used with the PBR shader after 2-3 seconds. this is due to Prop Sleeping. after a set time, props "bake" their lighting for optimization. you can bypass this by typing r_PhysPropStaticLighting 0 on console or hard-coding it on c_physicsprop.cpp
*[[PBR (shader)]]
[[Category:Modding]]
[[Category:Tutorials]]
[[Category:PBR]]

Latest revision as of 06:38, 22 May 2025

English (en)Русский (ru)Translate (Translate)
Warning.pngWarning:This shader does not work with lightmapped brushes when using stock VBSP compilers, however it technically is compatible with brushes.
Cpp.pngCode Fix:In Filesrc/utils/common/utilmatlib.cpp find InitMaterialSystem method and at the end add g_pMaterialSystem->ModInit(); and try to recompile tools.
Tip.pngTip:Fixed in Strata Source, as well as the Mapbase's and Slammin' Source Map Tools's VBSPs.
Comparison of PBR

Left: Base Source Engine shaders.
Right: The PBR shader.
Not to be confused with Adapting PBR Textures to Source.

The goal of this article is to show you how to implement Thexa4's PBR shader into your own Source 2013 Source 2013 mod. Implementing this shader will allow you to use the metalness/roughness PBR workflow in materials.

For a video-based version of this tutorial, check out YouTube logo TheMaster974's YouTube video guide.


Introduction


Requirements

  • Ability to compile shaders.
  • Ability to compile the solution.


Note.pngNote:If you do not know how to compile shaders, or haven't worked with shaders in Source Source yet, it is advised that you follow and understand these articles first: Shader Authoring & Your First Shader


Implementation

Before doing anything, we need to download the files we need.


You can do this by going to the links below, clicking the "Raw" button and once the page has loaded right-click and select "Save As...".

Then all you have to do is save each one of the files to your Source mod's code directory under 🖿src/materialsystem/stdshaders/.


After you've finished saving the files, open the Filesrc/materialsystem/stdshaders/game_shader_dx9_*.vpc file appropriate for your situation (base/hl2mp/hl2/episodic), and add the PBR files within $Project "Shaders" like so:

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
$Project "Shaders" { $Folder "Header Files" { $File "common_vertexlitgeneric_dx9.h" $File "common_lightmappedgeneric_fxc.h" $File "common_flashlight_fxc.h" } $Folder "Shader Source" { $Folder "fxc" { $File "pbr_ps30.fxc" $File "pbr_vs30.fxc" $File "pbr_ps20b.fxc" $File "pbr_vs20b.fxc" } $Folder "Headers" { $File "pbr_common_ps2_3_x.h" } $File "mymod_dx9_30.txt" $File "mymod_dx9_20b.txt" } $Folder "Source Files" { $File "pbr_dx9.cpp" } }

We do this so that when we refresh the solution they will appear in the solution explorer.


Shader Compilation

We need to compile the shaders that we have before we can use them. Create two files in 🖿src/materialsystem/stdshaders/ named mymod_dx9_30.txt and mymod_dx9_20b.txt and add this inside:

mymod_dx9_30.txt
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
// // vs 3.0 ps 3.0 shaders collection // // These shaders are forced to compile as shader model 3.0 // using the new compiler. // _ps30.vcs // _vs30.vcs // pbr_vs30.fxc pbr_ps30.fxc
mymod_dx9_20b.txt
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
// // Standard shaders collection // // These shaders are compiled as the following shader models: // _ps20.vcs // _ps20b.vcs // _vs20.vcs // pbr_vs20b.fxc pbr_ps20b.fxc


Adding new text files for shader compilation will allow you to compile the PBR shaders without affecting the standard Shader-ball.png LightmappedGeneric shaders.

This will also reduce compile times significantly.


Now open the Filebuildsdkshaders.bat file using Notepad and edit this part below from:

%BUILD_SHADER% stdshader_dx9_20b -game %GAMEDIR% -source %SOURCEDIR% %BUILD_SHADER% stdshader_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30

To:

%BUILD_SHADER% mymod_dx9_20b -game %GAMEDIR% -source %SOURCEDIR% %BUILD_SHADER% mymod_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30

This will allow you to use the custom text file that we made earlier to compile our PBR shader.


After that, we can now start the shader compilation by running Filebuildhl2mpshaders.bat, Filebuildhl2shaders.bat or Filebuildepisodicshaders.bat depending on your mod's base.

Make sure you've followed the Shader Authoring article to the point where the newly compiled shaders will be properly placed on your mod's 🖿shaders/fxc/ folder.

Note.pngNote:Depending on your PC and the complexity of the shader, it might take a while.


If the shaders compile without problems, go back to your source code directory 🖿src/ and run Filecreateallprojects.bat.

Run the solution and build shaders in Release.

Note.pngNote:If 🖿pbr_dx9.cpp does not exist in the Shader project. Make sure that you've properly included it in the .vpc file you edited earlier.


Fixes

Physics props turn black when used with the PBR shader after 2-3 seconds due to Prop Sleeping. After a set time, props "bake" their lighting for optimization.

You can bypass this by typing r_PhysPropStaticLighting 0 on the console or by hardcoding it in Filesrc/game/client/c_physicsprop.cpp.


Here is how you can hardcode it:

Below #include "tier0/memdbgon.h" add this:

#define PBR_CHANGE

Then find ConVar r_PhysPropStaticLighting("r_PhysPropStaticLighting", "1"); and turn it into this:

#ifdef PBR_CHANGE
	ConVar r_PhysPropStaticLighting( "r_PhysPropStaticLighting", "0" );
#else
	ConVar r_PhysPropStaticLighting( "r_PhysPropStaticLighting", "1" );
#endif


Parameters

  • $basetexture - The albedo texture.
  • $normaltexture - Sets the normal map (for backwards compatibility).
  • $bumpmap - Sets the normal map (new way of setting normalmap).
  • $mraotexture - A texture with the metalness on the red channel, roughness on the green channel and ambient occlusion on the blue channel.
  • $speculartexture - Enables use of colored F0 (specular map), overrides metalness from MRAO texture.
  • $emissiontexture - Allows setting an emission texture. Enabled self illumination.
  • $useenvambient <bool> - Makes it use the lowest mip level of the cubemap for ambient light instead of the ambient cubes. Can cause artifacts with moving props.
  • $envmap - Defaults to "env_cubemap", allows you to use a custom one.
  • $surfaceprop - Links the surface of either a material or model to a set of physical properties.
  • $model - Should have a value of "1" if the texture is used on a prop.
  • $translucent - Setting this to "1" enables alpha blending.
  • $basetexturetransform - Looks like this: "center 0.5 0.5 scale 0.1025 0.1025 rotate 0 translate 0 0" for a 2m by 2m texture.
  • $alphatest - Enables clipping the pixel when the alpha goes below $alphatestreference.
  • $alphatestreference - Specifies the minimum color value of the alpha channel in which the effect is rounded to 255. A value of ".3" will create a thicker shape while a value of ".7" will create a thinner shape.
  • %keywords - A list of keywords separated by commas. Examples of keywords are: architectural,brown,gray,grime,hanger,industrial,metal,modern,shed,urban,wall,floor


  • $useparallax <bool> - Use Parallax Occlusion Mapping.
  • $parallaxdepth <float> - Depth of the Parallax Map
  • $parallaxcenter <float> - Center depth of the Parallax Map


Note.pngNote:$emissiontexture is a color texture, not a mask.
Icon-Bug.pngBug:By default all shadows appear to point towards the user as if the user is holding a light, instead of using exterior light sources.

Examples

Brush (non-model surfaces) material example

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
PBR { $basetexture "pbr_asset/texture_albedo" $bumpmap "pbr_asset/texture_normal" $mraotexture "pbr_asset/texture_mrao" $envmap "env_cubemap" $surfaceprop "metal" $model 0 }

Model material example

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
PBR { $basetexture "models/pbr_asset/texture_albedo" $bumpmap "models/pbr_asset/texture_normal" $mraotexture "models/pbr_asset/texture_mrao" $envmap "env_cubemap" $surfaceprop "metal" $model 1 }

Model material example with specular map

Note.pngNote:Metalness channel is ignored and Shader-ball-settings.png $basetexture is treated as diffuse.
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
PBR { $basetexture "models/pbr_asset/texture_diffuse" $bumpmap "models/pbr_asset/texture_normal" $mraotexture "models/pbr_asset/texture_mrao" $speculartexture "models/pbr_asset/texture_specular" $envmap "env_cubemap" $surfaceprop "metal" $model 1 }


Note.pngNote:Shader-ball-settings.png $envmap should just be left as env_cubemap.
Warning.pngWarning:Shader-ball-settings.png $model 1 MUST be added for models to work!


Conclusion

And that's it! You should now have PBR in your mod.

See also