Shader Authoring

As a courtesy, please do not edit this while this message is displayed.
If this page has not been edited for at least several hours to a few days, please remove this template. This message is intended to help reduce edit conflicts; please remove it between editing sessions to allow others to edit the page.
The person who added this notice will be listed in its edit history should you wish to contact them.

You can help by finishing the translation.
If this page cannot be translated for some reason, or is left untranslated for an extended period of time after this notice is posted, the page should be requested to be deleted.
Also, please make sure the article complies with the alternate languages guide.
这篇文档会教你怎么写着色器,更多有关着色器的信息以及它们有什么用、应该怎么用,可参考 Shader。
开始吧
每个起源引擎的材质都有它们特定的着色器,着色器用于渲染材质的视觉效果。一般来说,着色器会定义材质表面的基本光传输逻辑,以便最终呈现高质量的渲染效果。
起源 SDK 拥有完整的 DirectX 着色语言 HLSL (High-Level Shading Language)以及着色器汇编的支持。我们建议一开始就用 HLSL 写着色器,最后涉及到精细的系统性能优化再使用汇编。
我们需要一些额外的工具来为起源 SDK 编译着色器,接下来的部分会详细介绍这些工具,并提供它们的链接和简要的安装说明。
用到的工具有:Perl,DirectX SDK 和 make。一般而言以下过程只需进行一次,因为只是环境配置。
创建着色器的选择

选择一:Peral + DirectX SDK
Perl
Perl 可在 download page 安装,推荐安装的一个版本是 Strawberry Perl。
橙盒 SDK 也可依赖于非标准的 Perl, String::CRC32。如果你用的是 Strawberry Perl, 你可以通过 CPAN Client 安装它并通过 <StrawberryPerlInstallDir>\perl\bin\cpan.bat
启动。
输入以下命令来安装包文件: install String::CRC32

<StrawberryPerlInstallDir>\perl\bin\
然后再运行 cpan String::CRC32
DirectX SDK
为安装 November 2008 DirectX SDK, 请访问 https://archive.org/details/dxsdk_nov08。(因为微软早就不给这么老的 API 提供支持了,所以只能去 Internet Archive 下载)

DirectX SDK
!这就好像只安装 VC 运行库并不满足 C++ 开发一样。
设置环境变量
当 Perl 和 DirectX SDK 安装好之后, 确保你的 "Path" 环境变量已被正确配置。在 Windows 11 里, 你可以通过设置 -> properties, going to the Advanced tab, and clicking on "Environment Variables". Assuming that Perl is installed into C:\Perl
, and DirectX into C:\DXSDK
, your "Path" variable should contain both C:\Perl\bin
and C:\DXSDK\Utilities\bin\x86
in a semicolon delimited list.
By setting up the Environmental Variable in this way, you can update the Perl and DirectX SDK as you please without having to copy files. If for some reason you do not wish to do this, you can simply copy the required files into a different directory and add that to your path instead.

选择二:SCell555 的着色器编译工具
为安装 SCell555 的着色器编译工具, 访问 release 来下载。
当你下载好压缩文件后,打开压缩包应该会发现以下三个文件夹:
- bin
- headers
- stdshaders
把 bin
文件夹放入 devtools
文件夹。
把 stdshaders
文件夹放入 materialsytem
文件夹。如果系统询问是否替换文件,选择是。
Navigate into the headers
folder in the zip. You should see a cshader.h
file and a VS2013
folder (which also contains another cshader.h
file). If you are using Visual Studio 2013 or above, place the cshader.h
in the VS2013
folder into public\shaderlib
and replace it. If you are using a Visual Studio version before 2013, use the cshader.h
file that isn't in the VS2013
folder instead.
Completing the Setup
Modify stdshader_dx9_20b.txt
Open up materialsystem/stdshaders/stdshader_dx9_20b.txt
and comment out all of the lines which compile shaders like so:
//
// Standard shaders collection
//
// These shaders are compiled as the following shader models:
// _ps20.vcs
// _ps20b.vcs
// _vs20.vcs
//
//example_model_ps20b.fxc
//example_model_vs20.fxc
//SDK_Bloom_ps2x.fxc
//SDK_screenspaceeffect_vs20.fxc
//SDK_bloomadd_ps2x.fxc
Modify buildepisodicshaders.bat
Open src\materialsystem\stdshaders\buildepisodicshaders.bat
and set the GAMEDIR AND SDKBINDIR as instructed.
Inside buildsdkshaders.bat
, you might also need to modify the call to vsvars32.bat on line 7 - If you are using Visual Studio 2013 it should point to %VS120COMNTOOLS% instead.
Note: GAMEDIR AND SDKBINDIR must be kept to the 8.3 directory name standard.
rem == Set the absolute path to your mod's game directory here ==
rem == Note that this path needs does not support long file/directory names ==
rem == So instead of a path such as "C:\Program Files\Steam\steamapps\mymod" ==
rem == you need to find the 8.3 abbreviation for the directory name using 'dir /x' ==
rem == and set the directory to something like C:\PROGRA~2\Steam\steamapps\sourcemods\mymod ==
set GAMEDIR= YOUR PATH HERE
rem == Set the relative path to SourceSDK\bin\orangebox\bin ==
rem == As above, this path does not support long directory names or spaces ==
rem == e.g. ..\..\..\..\..\PROGRA~2\Steam\steamapps\<USER NAME>\sourcesdk\bin\orangebox\bin ==
set SDKBINDIR= YOUR PATH HERE
Shader Compile
Navigate into the materialsystem\stdshaders
folder. Then type in the name of the bat file for compiling your shaders; it will probably be buildepisodicshaders.bat
. If you did everything correctly, it should compile with no issues.

D3DVERTEXTEXTURESAMPLER0
being undefined when using SCell555's compiler. The fix is to simply remove any references to this definition from register
functions in the Vertex Shader files. register( D3DVERTEXTEXTURESAMPLER0, s0 )
in them to make finding the references easier.
See also
- You can get more information about HLSL and shader assembly programming in the online MSDN docs.
- Category:Material System -- for more general information on the usage of shaders in materials.