Adding PBR to Your Mod: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Move the intro to a separate page; add categories)
(Article update: PBR shader now supports Shader Model 2.0)
Line 25: Line 25:


* [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]
* [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_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_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]




Line 33: Line 36:
$Project "Shaders"
$Project "Shaders"
{
{
$Folder "fxc"
$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 "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"
}
}


Line 50: Line 73:
=== Shader Compilation ===
=== Shader Compilation ===


We need to compile the shaders that we have before we can use them. Create a file in '''src/materialsystem/stdshaders/''' named '''mymod_dx9_30.txt''' and add this inside of it:
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 ====
<pre>
<pre>
//
//
Line 60: Line 84:
//     _vs30.vcs
//     _vs30.vcs
//
//
// There are no examples of such shaders in the SDK, but add yours here.


pbr_vs30.fxc
pbr_vs30.fxc
Line 67: Line 89:
</pre>
</pre>


Adding a new text file for shader compilation will allow you to compile the PBR shaders without affecting the standard [[LightmappedGeneric]] shaders.
==== mymod_dx9_20b.txt ====
<pre>
//
// Standard shaders collection
//
//  These shaders are compiled as the following shader models:
//     _ps20.vcs
//     _ps20b.vcs
//     _vs20.vcs
//
 
pbr_vs20b.fxc
pbr_ps20b.fxc
</pre>
 
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.
This will also reduce compile times significantly.
Line 95: Line 132:
If the shaders compile without problems, go back to your source code directory '''/src/''' and run '''createallprojects.bat'''.
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'''.  
Run the solution and build shaders in '''Release'''.


{{note|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.}}
{{note|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.}}
=== VMT Setup ===
Here is an example VMT, something you would use on a model:
<pre>
PBR
{
        $basetexture  "models\props\pbr_asset_d"
        $bumpmap      "models\props\pbr_asset_n"
        $mraotexture  "models\props\pbr_asset_mrao"
        $emissive    "models\props\pbr_asset_e"
        $envmap      "env_cubemap"
        $model        "1"
}
</pre>
[[$basetexture]]
: Will be the Base Color texture.
[[$bumpmap]]
: Will be the Normal texture.
<code>$mraotexture</code>
: Will be the MRAO texture.
<code>$emissive</code>
: Will be for any glowing sections of the model.
{{note|The Emissive texture is a color texture, not a mask.}}
{{note|[[$envmap]] should just be left as <code>env_cubemap</code>.}}
{{warning|[[$model_(VMT)|$model 1]] '''''MUST'''''  be added for models to work!}}


== Fixes ==
== Fixes ==
Line 143: Line 145:
Here is how you can hardcode it:
Here is how you can hardcode it:


Above <code>#include "tier0/memdbgon.h"</code> add this:
Below <code>#include "tier0/memdbgon.h"</code> add this:
<source lang="cpp">
<source lang="cpp">
#define PBR_CHANGE
#define PBR_CHANGE
Line 157: Line 159:
</source>
</source>


== Conclusion ==
== Parameters ==
 
=== PBR Implemenation Specific ===
And that's it! You should now have PBR in your mod.
* '''$normaltexture''' - Sets the normal map (for backwards compatibility).
 
* '''$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 <0|1>''' - Makes it use the lowest mip level of the cubemap for ambient light instead of the ambient cubes. Can cause artifacts with moving props.


You can find a list of all the shader parameters you can use when creating materials here:
=== Default Source Engine ===
* [[$basetexture]] - The albedo texture.
* [[$bumpmap]] - Sets the normal map (new way of setting normalmap).
* [[$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.
* [[%keywords]] - A list of keywords separated by commas. Examples of keywords are: architectural,brown,gray,grime,hanger,industrial,metal,modern,shed,urban,wall,floor
* [[$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]] '''<0|1>''' - Enables clipping the pixel when the alpha goes below $alphatestreference.
* [[$alphatestreference]] '''<float>''' - 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.


https://wiki.empiresmod.com/PBR


== Parameters ==
{{note|$emissiontexture is a color texture, not a mask.}}
* $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.
* $envmap - Defaults to "env_cubemap", allows you to use a custom one.
* $surfaceprop - One of the following: https://developer.valvesoftware.com/wiki/$surfaceprop
* $model - Should have a value of "1" if the texture is used on a prop.
* $translucent - Setting this to "1" enables alpha blending.
* %keywords - A list of keywords separated by commas, should contain 'empires' as first element. Examples of keywords are: architectural,brown,gray,grime,hanger,industrial,metal,modern,shed,urban,wall,floor
* $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 <0|1> - Enables clipping the pixel when the alpha goes below $alphatestreference.
* $alphatestreference <float> -
* $emissiontexture - Allows setting an emission texture. Enabled self illumination.
* $useEnvAmbient <0|1> - Makes it use the lowest mip level of the cubemap for ambient light instead of the ambient cubes. Can cause artifacts with moving props.
* $speculartexture - Enables use of colored F0 (specular map), overrides metalness from MRAO texture


== Examples ==
== Examples ==
=== Brush (non-model surfaces) material example ===
=== Brush (non-model surfaces) material example ===
"PBR"
<pre>
{
PBR
  "$basetexture"   "mymod/newtexture_albedo"
{
  "$bumpmap"       "mymod/newtexture_normal"
        $basetexture     "pbr_asset/texture_albedo"
  "$mraotexture"   "mymod/newtexture_mrao"
        $bumpmap         "pbr_asset/texture_normal"
        $mraotexture     "pbr_asset/texture_mrao"
  "$surfaceprop"metal"
 
  "$model"        "0"
        $envmap          "env_cubemap"
}
        $surfaceprop     "metal"
        $model           "0"
}
</pre>


=== Model material example ===
=== Model material example ===
"PBR"
<pre>
{
PBR
  "$basetexture"   "mymod/newtexture_albedo"
{
  "$bumpmap"       "mymod/newtexture_normal"
        $basetexture     "models/pbr_asset/texture_albedo"
  "$mraotexture"   "mymod/newtexture_mrao"
        $bumpmap         "models/pbr_asset/texture_normal"
        $mraotexture     "models/pbr_asset/texture_mrao"
  "$surfaceprop"metal"
 
  "$model"        "1"
        $envmap          "env_cubemap"
}
        $surfaceprop     "metal"
        $model           "1"
}
</pre>


=== Model material example with specular map ===
=== Model material example with specular map ===
{{Note|Metalness channel is ignored and $basetexture is treated as diffuse.}}
{{Note|Metalness channel is ignored and [[$basetexture]] is treated as diffuse.}}
"PBR"
<pre>
{
PBR
  "$basetexture"       "mymod/newtexture_diffuse"
{
  "$bumpmap"           "mymod/newtexture_normal"
        $basetexture     "models/pbr_asset/texture_diffuse"
  "$mraotexture"       "mymod/newtexture_mrao"
        $bumpmap         "models/pbr_asset/texture_normal"
  "$speculartexture"   "mymod/newtexture_specular"
        $mraotexture     "models/pbr_asset/texture_mrao"
        $speculartexture "models/pbr_asset/texture_specular"
  "$surfaceprop"      "metal"
 
  "$model"            "1"
        $envmap          "env_cubemap"
}
        $surfaceprop     "metal"
        $model           "1"
}
</pre>


{{note|[[$envmap]] should just be left as <code>env_cubemap</code>.}}
{{warning|[[$model_(VMT)|$model 1]] '''''MUST'''''  be added for models to work!}}
== Conclusion ==
And that's it! You should now have PBR in your mod.


[[Category:Modding]]
[[Category:Modding]]
[[Category:Tutorials]]
[[Category:Tutorials]]

Revision as of 09:31, 23 May 2020

Left: Base Source Engine shaders.
Right: The PBR shader.

The goal of this article is to show you how to implement Thexa4's PBR shader into your own Source SDK 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.


Note.pngNote:If you do not know how to compile shaders, or haven't worked with shaders in 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 game_shader_dx9_*.vpc file appropriate for your situation (base/hl2mp/hl2/episodic), and add the PBR files within $Project "Shaders" like so:

$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

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

//
// 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 LightmappedGeneric shaders.

This will also reduce compile times significantly.


Now open the buildsdkshaders.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 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.

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 createallprojects.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 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

PBR Implemenation Specific

  • $normaltexture - Sets the normal map (for backwards compatibility).
  • $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 <0|1> - Makes it use the lowest mip level of the cubemap for ambient light instead of the ambient cubes. Can cause artifacts with moving props.

Default Source Engine

  • $basetexture - The albedo texture.
  • $bumpmap - Sets the normal map (new way of setting normalmap).
  • $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.
  • %keywords - A list of keywords separated by commas. Examples of keywords are: architectural,brown,gray,grime,hanger,industrial,metal,modern,shed,urban,wall,floor
  • $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 <0|1> - Enables clipping the pixel when the alpha goes below $alphatestreference.
  • $alphatestreference <float> - 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.


Note.pngNote:$emissiontexture is a color texture, not a mask.

Examples

Brush (non-model surfaces) material example

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

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 $basetexture is treated as diffuse.
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:$envmap should just be left as env_cubemap.
Warning.pngWarning:$model 1 MUST be added for models to work!

Conclusion

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