Zh/Shader Authoring: Difference between revisions

From Valve Developer Community
< Zh
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 21: Line 21:
Perl 可在 [https://www.perl.com/download.csp#win32 download page] 安装,推荐安装的一个版本是 [https://strawberryperl.com/ Strawberry 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> 启动。
橙盒 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>
输入以下命令来安装包文件: <code>install String::CRC32</code>


{{note|If you have problems with cpan.bat, try to open PowerShell in <code><StrawberryPerlInstallDir>\perl\bin\</code> and run command: <code>cpan String::CRC32</code>}}
{{note|如果你的 cpan.bat 有问题, 试试在 PowerShell 里输入 <code><StrawberryPerlInstallDir>\perl\bin\</code> 然后再运行 <code>cpan String::CRC32</code>}}




====DirectX SDK====
====DirectX SDK====
To install the November 2008 DirectX SDK, go to [https://archive.org/details/dxsdk_nov08 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).
为安装 November 2008 DirectX SDK, 请访问 [https://archive.org/details/dxsdk_nov08 https://archive.org/details/dxsdk_nov08]。(因为微软早就不给这么老的 API 提供支持了,所以只能去 Internet Archive 下载)




{{note|Just having DirectX 9.x installed is '''not''' sufficient, the <code>DirectX SDK</code> must be installed!}}
{{note|只是安装了 DirectX 9 的运行时 '''并不''' 满足开发需求, 我们必须安装开发套件 <code>DirectX SDK</code>!这就好像只安装 VC 运行库并不满足 C++ 开发一样。}}
{{note|'''It is recommended that you use the November 2008 SDK as needed binaries (psa.exe, vsa.exe) have been removed from newer releases.'''}}
{{note|'''因为一些需要的二进制(psa.exe, vsa.exe)已从更新的 DirectX SDK 中移除,所以建议安装 November 2008 SDK。'''}}




====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 <code>C:\Perl</code>, and DirectX into <code>C:\DXSDK</code>, your "Path" variable should contain both <code>C:\Perl\bin</code> and <code>C:\DXSDK\Utilities\bin\x86</code> in a semicolon delimited list.   
Perl DirectX SDK 安装好之后, 确保你的 "Path" 环境变量已被正确配置。在 Windows 11 里, 你可以通过'''设置 -> properties, going to the Advanced tab, and clicking on "Environment Variables". ''' Assuming that Perl is installed into <code>C:\Perl</code>, and DirectX into <code>C:\DXSDK</code>, your "Path" variable should contain both <code>C:\Perl\bin</code> and <code>C:\DXSDK\Utilities\bin\x86</code> 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.
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.
Line 44: Line 44:




===Option 2 - SCell555's Shader Compiler===
===选择二:SCell555 的着色器编译工具===
To install SCell555's Shader Compile tools, you must download the zip file which they provide with every [https://github.com/SCell555/ShaderCompile/releases/latest release].
为安装 SCell555 的着色器编译工具, 访问 [https://github.com/SCell555/ShaderCompile/releases/latest release] 来下载。


Once you have downloaded the zip (or .7z) file, open it up and you should see the following folders:
当你下载好压缩文件后,打开压缩包应该会发现以下三个文件夹:
*bin
*bin
*headers
*headers
Line 53: Line 53:




Place the <code>bin</code> folder into the <code>devtools</code> folder.
<code>bin</code> 文件夹放入 <code>devtools</code> 文件夹。


Place the <code>stdshaders</code> folder into the <code>materialsytem</code> folder.  If it asks you to replace the files, say yes.
<code>stdshaders</code> 文件夹放入 <code>materialsytem</code> 文件夹。如果系统询问是否替换文件,选择是。


Navigate into the <code>headers</code> folder in the zip.  You should see a <code>cshader.h</code> file and a <code>VS2013</code> folder (which also contains another <code>cshader.h</code> file).  If you are using Visual Studio 2013 or above, place the <code>cshader.h</code> in the <code>VS2013</code> folder into <code>public\shaderlib</code> and replace it.  If you are using a Visual Studio version before 2013, use the {{path|cshader|h}} file that isn't in the <code>VS2013</code> folder instead.
Navigate into the <code>headers</code> folder in the zip.  You should see a <code>cshader.h</code> file and a <code>VS2013</code> folder (which also contains another <code>cshader.h</code> file).  If you are using Visual Studio 2013 or above, place the <code>cshader.h</code> in the <code>VS2013</code> folder into <code>public\shaderlib</code> and replace it.  If you are using a Visual Studio version before 2013, use the {{path|cshader|h}} file that isn't in the <code>VS2013</code> folder instead.

Revision as of 18:50, 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 启动。

输入以下命令来安装包文件: install String::CRC32

Note.png注意:如果你的 cpan.bat 有问题, 试试在 PowerShell 里输入 <StrawberryPerlInstallDir>\perl\bin\ 然后再运行 cpan String::CRC32


DirectX SDK

为安装 November 2008 DirectX SDK, 请访问 https://archive.org/details/dxsdk_nov08。(因为微软早就不给这么老的 API 提供支持了,所以只能去 Internet Archive 下载)


Note.png注意:只是安装了 DirectX 9 的运行时 并不 满足开发需求, 我们必须安装开发套件 DirectX SDK!这就好像只安装 VC 运行库并不满足 C++ 开发一样。
Note.png注意:因为一些需要的二进制(psa.exe, vsa.exe)已从更新的 DirectX SDK 中移除,所以建议安装 November 2008 SDK。


设置环境变量

当 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.

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


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

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