Adding PBR to Your Mod

From Valve Developer Community
Revision as of 12:21, 24 February 2020 by GamerDude27 (talk | contribs) (Cleanup, better description of PBR)
Jump to navigation Jump to search

Introduction

The goal of this article is to implement Thexa4's PBR shader into your own Source SDK 2013 mod.


Physically Based Rendering or PBR is a shading model in computer graphics that seeks to render graphics in a way that more accurately models the flow of light in the real world.

Many PBR pipelines (though not all) have an accurate simulation of photorealism as their goal, often in real-time computing.


Implementing this shader will allow you to use the metalness/roughness PBR workflow in materials.

Requirements

  • Ability to compile shaders.
  • Ability to compile the solution.
Note.pngNote:If you do not know how to compile shaders yet, it is advised to follow this article first: Shader Authoring

Implementation

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


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

Then save the files to your Source mod's code directory under src/materialsystem/stdshaders/.


After you're done 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 "fxc"
	{
		$File	"pbr_ps30.fxc"
		$File	"pbr_vs30.fxc"
	}

	$Folder "Source Files"
	{
		$File	"pbr_dx9.cpp"
	}
}

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

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 it:

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

// There are no examples of such shaders in the SDK, but add yours here.

pbr_vs30b.fxc
pbr_ps30b.fxc

Adding a new text file 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.

Make sure you've followed Shader Authoring to the point where the newly compiled shaders will be properly placed on your mod's modfolder/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. You can check first if pbr_dx9.cpp exists in the shader project.

If not, add it to the .vpc file.


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

https://wiki.empiresmod.com/PBR lists the shader parameters you can use when creating materials.

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 hard-coding it in src/game/client/c_physicsprop.cpp