The Source engine is a 3D game engine developed by Valve Corporation. Its unique features include a large degree of modularity and flexibility, an artist-driven, shader-based renderer, accurate lip sync and facial expression technology, and a powerful, efficient and completely network-enabled physics system.
This short tutorial contains simple information on building shaders within the Source Engine for Half Life 2 mods.
Posted by justinms66 on Nov 6th, 2007
Basic Client Side Coding.
I have been wanting to learn about shaders for source for a while now, but the content that was available to me (including the other shader tutorials here on moddb) was very confusing, so i decided to share what i found out. Also this tutorial is about teaching, not giving out code, so please don't ask for it.
For the shader compile to work, you need to have Perl installed, so get ActivePerl and install it from here. You will also need the DirectX SDK if you do not already, which you can get here. Add the include, bin, and library paths to your Visual Studio. If you do not know how to do that:
Go to Tools -> Options:
Then go to Projects and Solutions -> VC++ Directories
And add your directX directories. You can change from executables to includes or libraries by going here:
and double click on
Now since the shaders were updated and the code was not, we first have to fix some things. We need to change the names of alot of the shader names to put an SDK in front of it. The easiest way to do this is press "ctrl+F" (find) enter in this for the "Find What" box:
And set the rest of the params to look like this:
Now keep pressing "Find Next", and with everything it finds, before the shader name, add "SDK_" (no quotes) so for example: BEGIN_VS_SHADER( Refract_DX90, "Help for Refract" ) would become BEGIN_VS_SHADER( SDK_Refract_DX90, "Help for Refract" )
The very basics of what a shader is is almost always two parts:
Shaders can be programmed with several languages. The common ones are:
Since we are using DirectX for the source engine, we will be programming in HLSL. There are several types of shaders:
The basic shader structure is within 3 files:
TestShader.cpp is the only file that will be compiled with the stdshader solution that you should still have open. the .fxc files are separate and will take another technique to compile. However all of these files go in the same directory, the one you should still be in: ~MOD_SOURCE~\materialsystem\stdshaders
Some things to keep in mind while coding in HLSL. When using float2,float3...etc, that simply defines how many variables are held inside of it. For example:
Now if i had 3 values in float2:
It would return an error during compile, I would need to use:
When getting values FROM a variable, you would use something like this (say you wanted to get value_2 from a float3, there are multiple ways, all can work):
Or i could EVEN get two of the 3 parameters:
That now holds:
Now you can use tex2D or tex3D.... etc for sampling texture data. here are the major ones:
I am now editing "TestShader_vs20.fxc" a Vertex Shader. The main purpose of this file is sending the variables that you want to send, to the pixel shader. The 1st line that you want in your vertex shader is:
This adds the pre-designed shader functions and headers for ease of use. Now this part is easier to understand if you are familiar with structures of classes, no matter what language. If we were coding in C++ this part would be in the ".h" file (just to give you some reference). It is simply defining the variables, except with HLSL you don't define the functions as well. only the variables.
pos is going to be the position of the vertex
tex is going to be the texture coordinate.
Now we set the "Main()" function:
In the parenthesis of the function we set the variables we want. but we put a "in " before it.
then we define an output, set the variables of it, and return it (that goes to the pixel shader). I am now editing "TestShader_ps20.fxc" . The main purpose of this file is to actually modify the screen however you wish. The 1st line that you want in your pixel shader is:
That IS a different file than the one we used for the vertex shader!!! Now right away we need to declare the sampler:
You can use and manipulate more than 1 sampler, but for now i am just going to use 1. Since I am going to be using the variable "Color" later on, I am going to define it up here:
Again, for this next part we are just going to declare the variable names:
Now lets get to the fun part:
Inside the main() function parenthesis i again define the variables we are getting. put "in " before them again also. That is the basic structure, go on to the next part, which is making some effects!
Time to work on Shader Effects Examples and the pixel shader, and only adding to the middle of the main() function, where i have: // your modification code will go here.
What i have done here is added the coordinants of slightly off the current position to the current color and divided it by how many there are so that it averages out the brightness, creating a blur effect.
RIPPLE / UNDERWATER EFFECT