Adding PBR to Your Mod: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Cleanup)
Line 1: Line 1:
== 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.
 
The goal of this article is to implement [https://github.com/thexa4/source-pbr Thexa4's PBR shader] into our own Source SDK 2013 mod.  
 
PBR is a physically based shading model that is used in newer games. Implementing this shader will enable us to use metalness and roughness workflows in our materials.
 
== 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 yet, it is advised to follow this article first: [[Shader Authoring]]}}
 
== 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.
 
 
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/'''.
 
* [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]
 
 
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 <code>$Project "Shaders"</code> like so:
<pre>
<pre>
$Project "Shaders"
{
$Folder "fxc"
$Folder "fxc"
{
{
Line 21: Line 36:
}
}


        $Folder "Source Files"
$Folder "Source Files"
{
{
$File "pbr_dx9.cpp"
$File "pbr_dx9.cpp"
}
}
}
</pre>
</pre>
We do this so that when we refresh the solution they will appear in our solution.


=== 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  
 
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:
<pre>
<pre>
//
//
Line 44: Line 63:
pbr_ps30b.fxc
pbr_ps30b.fxc
</pre>
</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
Adding a new text file for shader compilation will enable us to compile the PBR shaders without affecting the standard [[LightmappedGeneric]] shaders.  
from
 
This will also reduce compile times significantly.
 
 
Now open the '''buildsdkshaders.bat''' file using Notepad and edit this part below from:
<pre>
<pre>
%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  
</pre>
</pre>
to
 
To:
<pre>
<pre>
%BUILD_SHADER% mymod_dx9_20b         -game %GAMEDIR% -source %SOURCEDIR%
%BUILD_SHADER% mymod_dx9_20b         -game %GAMEDIR% -source %SOURCEDIR%
%BUILD_SHADER% mymod_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30  
%BUILD_SHADER% mymod_dx9_30 -game %GAMEDIR% -source %SOURCEDIR% -dx9_30 -force30  
</pre>
</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.
This will enable us 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|Depending on your PC and the complexity of the shader, it might take a while.}}


Thats it! you should have PBR in your mod. https://wiki.empiresmod.com/PBR lists the shader parameters you can use to create materials.
 
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 ===
=== Fixes ===
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
 
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>r_PhysPropStaticLighting 1</code> on the console or by hard-coding it in '''src/game/client/c_physicsprop.cpp'''

Revision as of 12:21, 23 February 2020

Introduction

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

PBR is a physically based shading model that is used in newer games. Implementing this shader will enable us to use metalness and roughness workflows in our 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 enable us 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 enable us 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 1 on the console or by hard-coding it in src/game/client/c_physicsprop.cpp