Writing vertex and fragment shaders 写顶点与片断着色器 - Unity Shader Reference 系列6

Writing vertex and fragment shaders 写顶点与片断着色器

本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.2版本
参考链接:https://docs.unity3d.com/Manual/SL-ShaderPrograms.html

ShaderLab shaders encompass more than just “hardware shaders”. They do many things. They describe properties that are displayed in the Material Inspector, contain multiple shader implementations for different graphics hardware, configure fixed function hardware state and so on. The actual programmable shaders - like vertex and fragment programs - are just a part of the whole ShaderLab’s “shader” concept. Take a look at shader tutorial for a basic introduction. Here we’ll call the low-level hardware shaders shader programs.
ShaderLab 着色器不仅仅包含“硬件着色器”。他们做很多事情。它们描述了在材质检查器中显示的属性,包含不同图形硬件的多个着色器实现,配置固定渲染管线硬件状态等等。实际的可编程着色器——比如顶点和片段程序——只是整个着色实验室的“着色器”概念的一部分。看一看shader 教程的基本介绍。这里我们将调用低级硬件着色器着色程序。
If you want to write shaders that interact with lighting, take a look at Surface Shaders documentation. For some examples, take a look at Vertex and Fragment Shader Examples. The rest of this page assumes shaders do not interact with Unity lights (for example special effects, post-processed effects etc.)
如果您想要编写与光照交互的着色器,请查看表面着色器文档。对于一些例子,我们来看看顶点和片段着色器示例。本页面的其余部分假设着色器不与Unity光照交互(例如特殊效果、后期处理效果等)。
Shader programs are written in HLSL language, by embedding “snippets” in the shader text, somewhere inside the Pass command. They usually look like this:
着色程序是用HLSL语言编写的,通过在着色器文本中嵌入“片段”,在Pass命令中。它们通常是这样的:

Pass {
      // ... the usual pass state setup ...
      
      CGPROGRAM
      // compilation directives for this snippet, e.g.:
      #pragma vertex vert
      #pragma fragment frag
      
      // the Cg/HLSL code itself
      
      ENDCG
      // ... the rest of pass setup ...
  }

HLSL snippets
HLSL program snippets are written between CGPROGRAM and ENDCG keywords, or alternatively between HLSLPROGRAM and ENDHLSL. The latter form does not automatically include HLSLSupport and UnityShaderVariables built-in header files.
HLSL程序片段是在CGPROGRAM和ENDCG关键字之间编写的,或者在HLSLPROGRAM和ENDHLSL之间进行。后一种形式不会自动包含HLSLSupport和UnityShaderVariables内置头文件。
At the start of the snippet compilation directives can be given as #pragma statements. Directives indicating which shader functions to compile:
在代码片段编译的开始时,可以将其作为#pragma 语句给出。指令指示要编译哪个着色器函数:

  • #pragma vertex name - compile function name as the vertex shader. 编译函数名作为顶点着色器
  • #pragma fragment name - compile function name as the fragment shader. 编译函数名作为片断着色器
  • #pragma geometry name - compile function name as DX10 geometry shader. Having this option automatically turns on #pragma target 4.0, described below. 编译函数名为DX10几何(geometry )着色器。有了这个选项,就会自动打开“#pragma target 4.0”,如下所述。
  • #pragma hull name - compile function name as DX11 hull shader. Having this option automatically turns on #pragma target 5.0, described below. 编译函数名为DX11 hull 着色器。有了这个选项,就会自动打开#pragma target 5.0,如下所述。
  • #pragma domain name - compile function name as DX11 domain shader. Having this option automatically turns on #pragma target 5.0, described below. 编译函数名为DX11域(domain )着色器。有了这个选项,就会自动打开#pragma target 5.0,如下所述。

Other compilation directives:

  • #pragma target name - which shader target to compile to. See Shader Compilation Targets page for details. 编译的着色目标是哪个。
  • #pragma require feature … - fine grained control on which GPU features a shader needs, see Shader Compilation Targets page for details. 在GPU的特性上,着色器需要的细粒度控制,请参阅着色器编译目标页面的详细信息。
  • #pragma only_renderers space separated names - compile shader only for given renderers. By default shaders are compiled for all renderers. See Renderers below for details. 只对给定的渲染器编译着色器。默认的着色器是为所有渲染器编译的。请参阅下面的渲染器以获得详细信息。
  • #pragma exclude_renderers space separated names - do not compile shader for given renderers. By default shaders are compiled for all renderers. See Renderers below for details.
  • #pragma multi_compile … - for working with multiple shader variants. 用于处理多个着色器变体。
  • #pragma enable_d3d11_debug_symbols - generate debug information for shaders compiled for DirectX 11, this will allow you to debug shaders via Visual Studio 2012 (or higher) Graphics debugger. 为DirectX 11编译的着色器生成调试信息,这将允许您通过Visual Studio 2012(或更高)的图形调试器调试着色器。
  • #pragma hardware_tier_variants renderer name - generate multiple shader hardware variants of each compiled shader, for each hardware tier that could run the selected renderer. See Renderers below for details.为每个可运行选定的渲染器的硬件层生成多个着色器硬件变体。请参阅下面的渲染器以获得详细信息。

Each snippet must contain at least a vertex program and a fragment program. Thus #pragma vertex and #pragma fragment directives are required.
每个片段必须包含至少一个顶点程序和一个片段程序。因此,需要有一个顶点和一个代码片段指令。
Compilation directives that don’t do anything starting with Unity 5.0 and can be safely removed: #pragma glsl, #pragma glsl_no_auto_normalization, #pragma profileoption, #pragma fragmentoption.
以下编译指令不做任何事情,从Unity 5.0开始,可以安全删除:#pragma glsl, #pragma glsl_no_auto_normalization, #pragma profileoption, #pragma fragmentoption.

Rendering platforms

Unity supports several rendering APIs (e.g. Direct3D 11 and OpenGL), and by default all shader programs are compiled into all supported renderers. You can indicate which renderers to compile to using #pragma only_renderers or #pragma exclude_renderers directives. This is mostly useful in cases where you are explicitly using some shader language features that you know aren’t possible on some platforms. Supported renderer names are:
Unity支持几个渲染APIs (例如Direct3D 11和OpenGL),默认情况下所有的着色程序都被编译成所有支持的渲染器。您可以指示要编译哪些渲染器来使用#pragma only_renderers或#pragma exclude_renderers指令。在某些情况下,这是非常有用的,因为您可以显式地使用一些在某些平台上不可能出现的着色语言特性。支持的渲染器名称是:

  • d3d11 - Direct3D 11/12
  • glcore - OpenGL 3.x/4.x
  • gles - OpenGL ES 2.0
  • gles3 - OpenGL ES 3.x
  • metal - iOS/Mac Metal
  • vulkan - Vulkan
  • d3d11_9x - Direct3D 11 9.x feature level, as commonly used on WSA (Window Store App)platforms
  • xboxone - Xbox One
  • ps4 - PlayStation 4
  • psp2 - PlayStation Vita
  • n3ds - Nintendo 3DS
  • wiiu - Nintendo Wii U
    For example, this line would only compile shader into D3D11 mode:
    例如,这一行只会将着色器编译成D3D11模式:
#pragma only_renderers d3d11
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值