标题: [Unity 组件参考手册]着色器参考之着色器语法:Shader [打印本页] 作者: . 时间: 2013-2-24 09:55 标题: [Unity 组件参考手册]着色器参考之着色器语法:Shader Shader is the root command of a shader file. Each file must define one (and only one) Shader. It specifies how any objects whose material uses this shader are rendered.
Shader 是一个着色器文件的根命令。每个文件必须定义一个(唯一)Shader. 其中定义了材质如何使用这个着色器来渲染对象。
Syntax 语法
Shader "name" { [Properties] Subshaders [Fallback] } Defines a shader. It will appear in the material inspector listed under name. Shaders optionally can define a list of properties that show up as material settings. After this comes a list of SubShaders, and optionally a fallback.
Shader "name" { [Properties] Subshaders [Fallback] } 定义了一个着色器。这些内容在材质监视器上列于name下。着色器通过properties来可选的定义一个可通过材质设定界面来自定义的列表。后面紧跟SubShaders的列表,并可额外添加一个代码块用于应对fallback的情况。
Details 细节
Properties 属性
Shaders can have a list of properties. Any properties declared in a shader are shown in the material inspector inside Unity. Typical properties are the object color, textures, or just arbitrary values to be used by the shader.
着色器拥有一个properties的列表。任何定义在着色器中的属性在Unity中会显示在材质检视器中。典型的属性是颜色,纹理,或是任何被着色器使用的值。
SubShaders & Fallback 子着色器和降级
Each shader is comprised of a list of sub-shaders. You must have at least one. When loading a shader, Unity will go through the list of subshaders, and pick the first one that is supported by the end user's machine. If no subshaders are supported, Unity will try to use fallback shader.
着色器包含一个子着色器的列表。至少包含一个。当加载一个着色器时,Unity将遍历这个列表,获取第一个能被用户机器支持的着色器。如果没有子着色器被支持,Unity将尝试使用降级shader.
Different graphic cards have different capabilities. This raises an eternal issue for game developers; you want your game to look great on the latest hardware, but don't want it to be available only to those 3% of the population. This is where subshaders come in. Create one subshader that has all the fancy graphic effects you can dream of, then add more subshaders for older cards. These subshaders may implement the effect you want in a slower way, or they may choose not to implement some details.
不同的图形卡有不同的性能。这对游戏开发者来说是永恒的问题,你希望你的游戏在最新的硬件上能表现卓越,但并不希望只有百分之3的人能玩到你的游戏。对于这个问题,子着色器将发挥作用。为你所期望的梦幻效果编写一个子着色器,然后为更多古老的显卡编制更多着色器。这些子着色器能选择使用更低层次的方式来实现你的效果,或者选择放弃实现某些细节。
Examples 示例
Here is one of the simplest shaders possible:
这有一个可行的最简单的例子:
// colored vertex lighting 顶点灯光着色
Shader "Simple colored lighting" {
// a single color property
一个简单的颜色属性
Properties {
_Color ("Main Color", Color) = (1,.5,.5,1)
}
// define one subshader
定义一个子着色器
SubShader {
Pass {
Material {
Diffuse [_Color]
}
Lighting On
}
}
}
This shader defines a color property _Color (that shows up in material inspector as Main Color) with a default value of (1, 0.5, 0.5, 1). Then a single subshader is defined. The subshader consists of one Pass that turns on vertex lighting and sets up basic material for it.
这个着色器定义了一个颜色属性 _Color(在材质检视器中描述为主颜色) ,这个属性有一个缺省的值(1, 0.5, 0.5, 1). 然后定义了一个唯一的子着色器。这个子着色器包含了一个Pass 开启顶点光照并设定基本材质