查看: 1973|回复: 0
打印 上一主题 下一主题

[Unity 组件参考手册]iOS:着色器参考之编写顶点和片段着色器

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-2-24 09:57:34 |只看该作者 |倒序浏览
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着色器包含的不只是"硬件着色器"。它们能做很多的事情。它们描述的属性在材质检视面板内可以查看到。为了不同的图形硬件包含实现了多个着色器,配置了固定程序硬件状态等等。这就像点和片段程序,实际上可编程着色器只是整个ShaderLab着色器概念的一部分。参看一下着色器教程这些基础介绍。在这里我们将调用低层次的硬件着色器着色编程。
If you want to write shaders that interact with lighting, take a look at Surface Shaders documentation. The rest of this page will assume shaders that do not interact with Unity lights (e.g. special effects, Image Effects etc.)
如果你想编写一个与光照相互影响的着色器,请参看 表面着色器 文档。其余部分本页将负责介绍不与Unity中的光照相互影响的着色器。例如:特效, 图片效果等。
Shader programs are written in Cg / HLSL language, by embedding "snippets" in the shader text, somewhere inside the Pass command. They usually look like this:
着色器编程是通过Cg/HLSL语言编写,把它嵌入在着色器文档中的通道(Pass)命令内部"小片段"中,就像下面这样:
  Pass {

      // ... the usual pass state setup ...
      CGPROGRAM

      // compilation directives for this snippet, e.g.:

      #pragma vertex vert

      #pragma fragment frag
      // the Cg code itself
      ENDCG

      // ... the rest of pass setup ...

  }


【Cg snippets Cg小片段】
Cg program snippets are written between CGPROGRAM and ENDCG.
Cg程序小片段是编写在CGPROGRAM和ENDCG之间的。
At the start of the snippet compilation directives can be given as #pragma statements. Directives recognized by Unity are:
在小片段(snippet)开始可以通过提供的 #pragma命令声明编译指令。Unity认可的指令有以下这些:
    #pragma vertex name - indicates that function name is a vertex program.

    #pragma vertex name - 表明这是个以name为名字的函数的顶点程序。

    #pragma fragment name - indicates that function name is a fragment program.

    #pragma fragment name- 表明这是个以name为名字的函数的片段程序。

    #pragma fragmentoption option - adds option to the compiled OpenGL fragment program. See the ARB fragment program specification for a list of allowed options. This directive has no effect on vertex programs or programs that are compiled to non-OpenGL targets.

    #pragma fragmentoption option - 添加选项到编译的OpenGL 片段程序。 通过 ARB 片段程序 可以查询到所允许的规范的选项列表。 这个指令对顶点程序或者不是以OpenGL为编译目标的程序无效。

    #pragma target name - which shader target to compile to. See shader targets for details.

    #pragma target name - 着色器目标编译。详情请参考 着色器目标。

    #pragma only_renderers space separated names - compile shader only for given renderers. By default shaders are compiled for all renderers. See renderers for details.

    #pragma only_renderers space separated names - 仅用给定的渲染器编译着色器。默认情况下用所有的渲染器都编译着色器。详情请参考渲染。

    #pragma exclude_renderers space separated names - do not compile shader for given renderers. By default shaders are compiled for all renderers. See renderers for details.

    #pragma exclude_renderers space separated names - 不用给定的渲染器编译着色器。 默认情况下用所有的渲染器都编译着色器。详情请参考渲染。

    #pragma glsl - when compiling shaders for desktop OpenGL platforms, convert Cg/HLSL into GLSL (instead of default setting which is ARB vertex/fragment programs)

    #pragma glsl - 用桌面OpenGL平台编译着色器时,转换成GLSL里面的Cg/HLSL(而不是默认设置的ARB顶点/片段程序)。
Each snippet must contain a vertex program, a fragment program, or both. Thus a #pragma vertex or #pragma fragment directive is required, or both.
每个小片段必须包含一个顶点程序或者片段程序(或者两者兼有。因此需要一个#pragma vertex命令或者#pragma fragment命令或者两者兼有。
[Shader targets 着色器目标]
By default, Unity compiles vertex programs into a shader model 1.1 equivalent, and fragment programs into a shader model 2.0 equivalent. Using #pragma target allows shaders to be compiled into other capability levels. Currently these targets are supported:
默认情况下,Unity编译顶点程序与着色器模式1.1相同,片段程序与着色器模式2.0相同。使用#pragma target使着色器编译成其他等级的能力。目前支持下面这些目标:
    #pragma target default - compile to default target:

    #pragma target default - 编译默认目标:

        Vertex shader 1.1 and pixel shader 2.0 on Direct3D 9.

        在Direct3D 9环境中:顶点着色器1.1和像索着色器2.0

        ARB_vertex_program with 128 ins***ction limit and ARB_fragment_program with 96 ins***ction limit (32 texture + 64 arithmetic), 16 temporary registers and 4 texture indirections.

        ARB顶点程序 有128位指令限制,ARB片段程序 有96位指令限制(32位纹理 + 64位算术运算),16个临时寄存器和4个间接纹理。

    #pragma target 3.0 - compile to shader model 3.0:

    #pragma target 3.0 - 编译着色器模式3.0:

        Vertex shader 3.0 and pixel shader 3.0 on Direct3D 9.

        在Direct3D 9环境中:顶点着色器3.0和像索着色器 3.0

        ARB_vertex_program with no ins***ction limit and ARB_fragment_program with 1024 ins***ction limit (512 texture + 512 arithmetic), 32 temporary registers and 4 texture indirections. It is possible to override these limits using #pragma profileoption directive. E.g. #pragma profileoption MaxTexIndirections=256 raises texture indirections limit to 256. Note that some shader model 3.0 features, like derivative ins***ctions, aren't supported by ARB_vertex_program/ARB_fragment_program. You can use #pragma glsl to translate to GLSL instead which has fewer restrictions.

        ARB顶点程序 没有指令限制,ARB片段程序 有1024位指令限制(512位纹理 + 512位算术运算),32个临时寄存器和4个间接纹理。可以通过使用 #pragma profileoption 命令扩展限制值。例如: #pragma profileoption MaxTexIndirections=256增加间接纹理上限到256个。需要注意的是某些着色器模式3.0中的特性不支持ARB顶点程序和ARB片段程序,比如派生指令这样的。你可以使用 #pragma glsl命令转换到GLSL中,这样子限制较少。

    When compiling to 3.0 target, both vertex and fragment programs need to be present.

    编译3.0目标时,顶点和片断程序需要同时存在。
[Renderers 渲染器]
Unity supports several rendering APIs (e.g. Direct3D 9 and OpenGL), and by default all shader programs are compiled into for supported renderers. You can indicate which renderers to compile to using #pragma only_renderers or #pragma exclude_renderers directives. This is useful if you know you will only target Mac OS X (where there's no Direct3D), or only Windows (where Unity defaults to D3D), or if some particular shader is only possible in one renderer and not others. Currently supported renderer names are:
Unity支持多种渲染API(比如:Direct3D 9 和 OpenGL),默认情况下被支持的渲染器会编译所有的着色器程序。你可以使用#pragma only_renderers 或 #pragma exclude_renderers指令来指定渲染器编译着色器。如果你只想针对Mac OS X系统(Mac 系统不支持Direct3D),或者只针对Windows系统(在Windows系统下Unity默认使用D3D),或者一些特定的着色器只能在一个渲染器中被编译,那么这两个命令是非常有用的。目前支持的渲染器有以下这些:
    d3d9 - Direct3D 9.

    opengl - OpenGL.

    gles - OpenGL ES 2.0.

    xbox360 - Xbox 360.

    ps3 - PlayStation 3.

    flash - Flash.
For example, this line would only compile shader into D3D9 mode:
例如:只在D3D9模式下编译:
  #pragma only_renderers d3d9
【Subsections 子章节】
    Accessing shader properties in Cg 在Cg中访问着色器属性

    Providing vertex data to vertex programs 传递顶点数据到顶点程序

    Built-in state variables in shader programs 在着色器程序中的内置状态变量

    GLSL Shader Programs GLSL着色器编程

【来源:互联网】

更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-27 19:43 , Processed in 0.090646 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部