Adding PBR to Your Mod

The goal of this article is to show you how to implement Thexa4's PBR shader into your own 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 .
* pbr_dx9.cpp
* pbr_vs30.fxc
* pbr_ps30.fxc
* pbr_vs20b.fxc
* pbr_ps20b.fxc
* pbr_common_ps2_3_x.h
After you've finished saving the files, open the file appropriate for your situation (base/hl2mp/hl2/episodic), and add the PBR files within 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 named and and add this inside:
Adding new text files for shader compilation will allow you to compile the PBR shaders without affecting the standard shaders.
This will also reduce compile times significantly.
Now open the 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 , or 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 folder.
If the shaders compile without problems, go back to your source code directory and run .
Run the solution and build shaders in .
== 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 on the console or by hardcoding it in .
Here is how you can hardcode it:
Below add this:
#define PBR_CHANGE
Then find 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 ===
== Conclusion ==
And that's it! You should now have PBR in your mod.
==See also==
*PBR (shader)
Category:Modding
Category:Tutorials
Category:PBR