Version: 2017.1
ShaderLab 语法
ShaderLab:SubShader

ShaderLab:Properties

Shaders can define a list of parameters to be set by artists in Unity’s material inspector. The Properties block in the shader file defines them.

语法

属性

Properties { Property [Property ...]}

定义属性代码块。在大括号内,多个属性定义如下。

数字和滑动条

name ("display name", Range (min, max)) = number
name ("display name", Float) = number
name ("display name", Int) = number

These all defines a number (scalar) property with a default value. The Range form makes it be displayed as a slider between min and max ranges.

颜色和矢量

name ("display name", Color) = (number,number,number,number)
name ("display name", Vector) = (number,number,number,number)

Defines a color property with default value of given RGBA components, or a 4D vector property with a default value. Color properties have a color picker shown for them, and are adjusted as needed depending on the color space (see Properties in Shader Programs). Vector properties are displayed as four number fields.

纹理

name ("display name", 2D) = "defaulttexture" {}
name ("display name", Cube) = "defaulttexture" {}
name ("display name", 3D) = "defaulttexture" {}

分别定义 2D 纹理立方体贴图3D(体积)属性。

详细信息

着色器中的每个属性均通过 name 引用(在 Unity 中,着色器属性名称通常以下划线开头)。属性在材质检视面板中将显示为 display name。每个属性都在等号后给出默认值:

  • 对于 RangeFloat 属性,默认值仅仅是单个数字,例如“13.37”。
  • 对于 ColorVector 属性,默认值是括在圆括号中的四个数字,例如“(1,0.5,0.2,1)”。
  • 对于 2D 纹理,默认值为空字符串或内置默认纹理之一:“white”(RGBA:1,1,1,1)、“black”(RGBA:0,0,0,0)、“gray”(RGBA:0.5,0.5,0.5,0.5)、“bump”(RGBA:0.5,0.5,1,0.5)或“red”(RGBA:1,0,0,0)。
  • 对于非 2D 纹理(立方体、3D 或 2D 数组),默认值为空字符串。如果材质未指定立方体贴图/3D/数组纹理,则使用灰色(RGBA:0.5,0.5,0.5,0.5)。

Later on in the shader’s fixed function parts, property values can be accessed using property name in square brackets: [name]. For example, you could make blending mode be driven by a material property by declaring two integer properties (say “SrcBlend“ and ”DstBlend”), and later on make Blend Command use them: Blend [_SrcBlend] [_DstBlend].

Shader parameters that are in the Properties block are serialized as Material data. Shader programs can actually have more parameters (like matrices, vectors and floats) that are set on the material from code at runtime, but if they are not part of the Properties block then their values will not be saved. This is mostly useful for values that are completely script code-driven (using Material.SetFloat and similar functions).

属性特性和绘制器

In front of any property, optional attributes in square brackets can be specified. These are either attributes recognized by Unity, or they can indicate your own MaterialPropertyDrawer classes to control how they should be rendered in the material inspector. Attributes recognized by Unity:

  • [HideInInspector] - 不在材质检视面板中显示属性值。
  • [NoScaleOffset] - 对于具有此特性的纹理属性,材质检视面板不会显示纹理平铺/偏移字段。
  • [Normal] - 表示纹理属性需要法线贴图。
  • [HDR] - 表示纹理属性需要高动态范围 (HDR) 纹理。
  • [Gamma] - 表示在 UI 中将浮点/矢量属性指定为 sRGB 值(就像颜色一样),并且可能需要根据使用的颜色空间进行转换。请参阅着色器程序中的属性
  • [PerRendererData] - indicates that a texture property will be coming from per-renderer data in the form of a MaterialPropertyBlock. Material inspector changes the texture slot UI for these properties.

示例

// 水着色器的属性
Properties
{
    _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // 滑动条
    _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.5
    _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.4
    _RefrColor ("Refraction color", Color) = (.34, .85, .92, 1) // 颜色
    _ReflectionTex ("Environment Reflection", 2D) = "" {} // 纹理
    _RefractionTex ("Environment Refraction", 2D) = "" {}
    _Fresnel ("Fresnel (A) ", 2D) = "" {}
    _BumpMap ("Bumpmap (RGB) ", 2D) = "" {}
}

纹理属性选项(5.0 中已删除)

Before Unity 5, texture properties could have options inside the curly brace block, e.g. TexGen CubeReflect. These were controlling fixed function texture coordinate generation. This functionality was removed in 5.0; if you need texgen you should write a vertex shader instead. See Implementing Fixed Function TexGen page page for examples.

另请参阅

ShaderLab 语法
ShaderLab:SubShader