Zh/Shader Authoring: Difference between revisions
No edit summary |
No edit summary |
||
Line 6: | Line 6: | ||
==开始吧== | ==开始吧== | ||
每个起源引擎的材质都有它们特定的着色器,着色器用于渲染材质的视觉效果。一般来说,着色器会定义材质表面的基本光传输逻辑,以便最终呈现高质量的渲染效果。 | |||
起源 SDK 拥有完整的 DirectX 着色语言 [[HLSL]] (High-Level Shading Language)以及着色器汇编的支持。我们建议一开始就用 HLSL 写着色器,最后涉及到精细的系统性能优化再使用汇编。 | |||
我们需要一些额外的工具来为起源 SDK 编译着色器,接下来的部分会详细介绍这些工具,并提供它们的链接和简要的安装说明。 | |||
用到的工具有:Perl,DirectX SDK 和 make。一般而言以下过程只需进行一次,因为只是环境配置。 | |||
== | ==创建着色器的选择== | ||
{{important| | {{important| 更准确地说,在 Source SDK 2013 Multiplayer 更新 TF2 代码之后,在 2013MP SDK 更新了 TF2 的代码后,默认情况下包含了 SCell555 着色器编译器的修改版本。[https://github.com/misyltoad/ShaderCompile2 this] version.}} | ||
=== | ===选择一:Peral + DirectX SDK=== | ||
====Perl==== | ====Perl==== | ||
Perl 可在 [https://www.perl.com/download.csp#win32 download page] 安装,推荐安装的一个版本是 [https://strawberryperl.com/ Strawberry Perl]。 | |||
橙盒 SDK 也可依赖于非标准的 Perl, String::CRC32. 如果你用的是 Strawberry Perl, 你可以通过 CPAN Client 安装它并通过 <code><StrawberryPerlInstallDir>\perl\bin\cpan.bat</code> 启动。 | |||
Enter the following command to install the package: <code>install String::CRC32</code> | Enter the following command to install the package: <code>install String::CRC32</code> |
Revision as of 02:09, 14 August 2025

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
启动。
Enter the following command to install the package: install String::CRC32

<StrawberryPerlInstallDir>\perl\bin\
and run command: cpan String::CRC32
DirectX SDK
To install the November 2008 DirectX SDK, go to https://archive.org/details/dxsdk_nov08, download, and install the DirectX SDK (The old Microsoft link is dead, so we need to use the Wayback Machine).

DirectX SDK
must be installed!
Setting The Path
With Perl and the DirectX SDK installed, make sure your "Path" environmental variable has been updated appropriately. In Windows, you can check this by right clicking on My Computer -> 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.

Option 2 - SCell555's Shader Compiler
To install SCell555's Shader Compile tools, you must download the zip file which they provide with every release.
Once you have downloaded the zip (or .7z) file, open it up and you should see the following folders:
- bin
- headers
- stdshaders
Place the bin
folder into the devtools
folder.
Place the stdshaders
folder into the materialsytem
folder. If it asks you to replace the files, say yes.
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.