Adding PBR to Your Mod: Difference between revisions
m (PBR comparison update) |
(Formatting update) |
||
Line 9: | Line 9: | ||
{{Distinguish|Adapting PBR Textures to Source}} | {{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 | 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. | ||
== Introduction == | == Introduction == | ||
Line 21: | Line 21: | ||
{{note|If you do not know how to compile shaders, or haven't worked with shaders in | {{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 == | ||
Line 30: | Line 30: | ||
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...". | 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 | 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_dx9.cpp pbr_dx9.cpp] | ||
Line 40: | Line 40: | ||
After you've finished saving the files, open the | 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" | ||
$Project "Shaders" | |||
{ | { | ||
$Folder "Header Files" | $Folder "Header Files" | ||
Line 74: | Line 73: | ||
$File "pbr_dx9.cpp" | $File "pbr_dx9.cpp" | ||
} | } | ||
} | }</nowiki>}} | ||
</ | |||
We do this so that when we refresh the solution they will appear in the solution explorer. | We do this so that when we refresh the solution they will appear in the solution explorer. | ||
Line 81: | Line 79: | ||
=== Shader Compilation === | === Shader Compilation === | ||
We need to compile the shaders that we have before we can use them. Create two files in | 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 94: | Line 90: | ||
pbr_vs30.fxc | pbr_vs30.fxc | ||
pbr_ps30.fxc | pbr_ps30.fxc</nowiki>}} | ||
</ | |||
= | {{CodeBlock|lines=11|src=mymod_dx9_20b.txt|<nowiki>// | ||
< | |||
// | |||
// Standard shaders collection | // Standard shaders collection | ||
// | // | ||
Line 109: | Line 102: | ||
pbr_vs20b.fxc | pbr_vs20b.fxc | ||
pbr_ps20b.fxc | pbr_ps20b.fxc</nowiki>}} | ||
</ | |||
Adding new text files for shader compilation will allow you to compile the PBR shaders without affecting the standard | |||
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. | This will also reduce compile times significantly. | ||
Now open the | 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_20b -game %GAMEDIR% -source %SOURCEDIR% | %BUILD_SHADER% stdshader_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30}} | ||
%BUILD_SHADER% stdshader_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30 | |||
To: | To: | ||
{{CodeBlock|%BUILD_SHADER% mymod_dx9_20b -game %GAMEDIR% -source %SOURCEDIR% | |||
%BUILD_SHADER% mymod_dx9_20b | %BUILD_SHADER% mymod_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30}} | ||
%BUILD_SHADER% mymod_dx9_30 | |||
This will allow you to use the custom text file that we made earlier to compile our PBR shader. | 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 | 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 | 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.}} | {{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 | 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 | Run the solution and build shaders in {{Code|Release}}. | ||
{{note|If | {{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 == | == Fixes == | ||
Line 148: | Line 137: | ||
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. | 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 | 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: | Here is how you can hardcode it: | ||
Below | Below {{code|select=all|#include "tier0/memdbgon.h"}} add this: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#define PBR_CHANGE | #define PBR_CHANGE | ||
</source> | </source> | ||
Then find | Then find {{code|select=all|ConVar r_PhysPropStaticLighting("r_PhysPropStaticLighting", "1");}} and turn it into this: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#ifdef PBR_CHANGE | #ifdef PBR_CHANGE | ||
Line 168: | Line 157: | ||
== Parameters == | == Parameters == | ||
* [[$basetexture]] - The albedo texture. | * [[$basetexture]] - The albedo texture. | ||
* '''$normaltexture''' - Sets the normal map (for backwards compatibility). | * '''$normaltexture''' - Sets the normal map (for backwards compatibility). | ||
Line 196: | Line 186: | ||
== Examples == | == Examples == | ||
=== Brush (non-model surfaces) material example === | === Brush (non-model surfaces) material example === | ||
< | {{CodeBlock|lines=10|<nowiki>PBR | ||
{ | { | ||
$basetexture "pbr_asset/texture_albedo" | $basetexture "pbr_asset/texture_albedo" | ||
Line 206: | Line 195: | ||
$surfaceprop "metal" | $surfaceprop "metal" | ||
$model 0 | $model 0 | ||
} | }</nowiki>}} | ||
</ | |||
=== Model material example === | === Model material example === | ||
< | {{CodeBlock|lines=10|<nowiki>PBR | ||
{ | { | ||
$basetexture "models/pbr_asset/texture_albedo" | $basetexture "models/pbr_asset/texture_albedo" | ||
Line 220: | Line 207: | ||
$surfaceprop "metal" | $surfaceprop "metal" | ||
$model 1 | $model 1 | ||
} | }</nowiki>}} | ||
</ | |||
=== Model material example with specular map === | === Model material example with specular map === | ||
{{Note|Metalness channel is ignored and | {{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" | $basetexture "models/pbr_asset/texture_diffuse" | ||
Line 237: | Line 222: | ||
$model 1 | $model 1 | ||
} | } | ||
</ | </nowiki>}} | ||
{{note| | {{note|{{Shader_Name|$envmap|type=param}} should just be left as <code>env_cubemap</code>.}} | ||
{{warning|[[$model_(VMT)|$model 1]] '''MUST''' be added for models to work!}} | {{warning|{{Shader_Name|[[$model_(VMT)|$model 1]]|type=param}} '''MUST''' be added for models to work!}} | ||
== Conclusion == | == Conclusion == | ||
And that's it! You should now have PBR in your mod. | And that's it! You should now have PBR in your mod. |
Revision as of 06:11, 19 July 2023

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


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 src/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:
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:
Adding new text files for shader compilation will allow you to compile the PBR shaders without affecting the standard
LightmappedGeneric
shaders.
This will also reduce compile times significantly.
Now open the buildsdkshaders.bat
file using Notepad and edit this part below from:
To:
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 buildhl2mpshaders.bat
, buildhl2shaders.bat
or buildepisodicshaders.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.

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.


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 src/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


Examples
Brush (non-model surfaces) material example
Model material example
Model material example with specular map


$basetexture
is treated as diffuse.


$envmap
should just be left as env_cubemap
.

$model 1
MUST be added for models to work!Conclusion
And that's it! You should now have PBR in your mod.