Zh/Shader Authoring: Difference between revisions

From Valve Developer Community
< Zh
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:
==开始吧==
==开始吧==


Every material that is used in Source specifies which shader it uses to render itself. The shader contains the logic and equations to take the source artwork and lighting, and produce the final rendered color for every pixel that the shader is used on.
每个起源引擎的材质都有它们特定的着色器,着色器用于渲染材质的视觉效果。一般来说,着色器会定义材质表面的基本光传输逻辑,以便最终呈现高质量的渲染效果。


The Source SDK fully supports Microsoft DirectX [[HLSL]] (High-Level Shading Language) and shader assembly languages for writing shaders. It is recommended to use HLSL whenever possible and to only write shader assembly as a last resort.
起源 SDK 拥有完整的 DirectX 着色语言 [[HLSL]] (High-Level Shading Language)以及着色器汇编的支持。我们建议一开始就用 HLSL 写着色器,最后涉及到精细的系统性能优化再使用汇编。


There are a number of external tools required to compile shaders for use in the Source SDK, the following sections detail the tools and provide links for download along with brief instructions on installation and usage.
我们需要一些额外的工具来为起源 SDK 编译着色器,接下来的部分会详细介绍这些工具,并提供它们的链接和简要的安装说明。


The tools used are Perl, the DirectX SDK and make, the following processes, in general, only have to be performed once before the process of compiling shaders can begin.
用到的工具有:Perl,DirectX SDK 和 make。一般而言以下过程只需进行一次,因为只是环境配置。


==Options for Building Shaders==
==创建着色器的选择==
{{important| After the 2013MP SDK got updated with TF2's code, a modified version of SCell555's shader compiler is included by default, more precisely [https://github.com/misyltoad/ShaderCompile2 this] version.}}
{{important| 更准确地说,在 Source SDK 2013 Multiplayer 更新 TF2 代码之后,在 2013MP SDK 更新了 TF2 的代码后,默认情况下包含了 SCell555 着色器编译器的修改版本。[https://github.com/misyltoad/ShaderCompile2 this] version.}}
===Option 1 - Perl & DirectX SDK===
===选择一:Peral + DirectX SDK===


====Perl====
====Perl====
To install Perl, go to the [https://www.perl.com/download.csp#win32 download page] on https://www.perl.com/, download, and install Perl - a recommended version of Perl for Windows is [https://strawberryperl.com/ Strawberry Perl].
Perl 可在 [https://www.perl.com/download.csp#win32 download page] 安装,推荐安装的一个版本是 [https://strawberryperl.com/ Strawberry Perl]


The Orange Box SDK also relies on the non-standard Perl package, String::CRC32. If you are using Strawberry Perl, you can install it through the CPAN Client which can be launched from <code><StrawberryPerlInstallDir>\perl\bin\cpan.bat</code>
橙盒 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

Under construction.png
This page is actively undergoing a major edit.
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.

Info content.png
This page is being translated.
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.(en)
English (en)Русский (ru)中文 (zh)Translate (Translate)

这篇文档会教你怎么写着色器,更多有关着色器的信息以及它们有什么用、应该怎么用,可参考 Shader

开始吧

每个起源引擎的材质都有它们特定的着色器,着色器用于渲染材质的视觉效果。一般来说,着色器会定义材质表面的基本光传输逻辑,以便最终呈现高质量的渲染效果。

起源 SDK 拥有完整的 DirectX 着色语言 HLSL (High-Level Shading Language)以及着色器汇编的支持。我们建议一开始就用 HLSL 写着色器,最后涉及到精细的系统性能优化再使用汇编。

我们需要一些额外的工具来为起源 SDK 编译着色器,接下来的部分会详细介绍这些工具,并提供它们的链接和简要的安装说明。

用到的工具有:Perl,DirectX SDK 和 make。一般而言以下过程只需进行一次,因为只是环境配置。

创建着色器的选择

Icon-Important.png重要: 更准确地说,在 Source SDK 2013 Multiplayer 更新 TF2 代码之后,在 2013MP SDK 更新了 TF2 的代码后,默认情况下包含了 SCell555 着色器编译器的修改版本。this version.

选择一: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

Note.png注意:If you have problems with cpan.bat, try to open PowerShell in <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).


Note.png注意:Just having DirectX 9.x installed is not sufficient, the DirectX SDK must be installed!
Note.png注意:It is recommended that you use the November 2008 SDK as needed binaries (psa.exe, vsa.exe) have been removed from newer releases.


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.

Note.png注意:You may need to restart your computer for these changes to the environment to take full effect.


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

Example of a successful 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.

Warning.png警告:You will encounter errors about 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.
待完善: List which files that have register( D3DVERTEXTEXTURESAMPLER0, s0 ) in them to make finding the references easier.
Warning.png警告:SCell's shader compiler doesn't produce any Pixel Shader files below 2.0b, and no Vertex Shader files below 2.0. The stock SDK shaders rely on fallbacks to lower versions of these files and, thus will need to be modified to account for this limitation.

See also