着色器可以定义 Unity 材质检视面板中由美术师设置的参数列表。着色器文件中的 Properties 代码块将定义这些属性。
Properties { Property [Property ...]}
定义属性代码块。在大括号内,多个属性定义如下。
name ("display name", Range (min, max)) = number
name ("display name", Float) = number
name ("display name", Int) = number
这些全都定义一个具有默认值的数字(标量)属性。Range
格式使该属性显示为一个滑动条,范围在 min 到 max 之间。
name ("display name", Color) = (number,number,number,number)
name ("display name", Vector) = (number,number,number,number)
使用给定 RGBA 分量的默认值定义颜色属性,或使用默认值定义 4D 矢量属性。颜色属性会显示拾色器,并根据颜色空间按需进行调整(请参阅着色器程序中的属性)。矢量属性显示为四个数字字段。
name ("display name", 2D) = "defaulttexture" {}
name ("display name", Cube) = "defaulttexture" {}
name ("display name", 3D) = "defaulttexture" {}
着色器中的每个属性均通过 name 引用(在 Unity 中,着色器属性名称通常以下划线开头)。属性在材质检视面板中将显示为 display name。每个属性都在等号后给出默认值:
稍后在着色器的固定函数部分中,可使用括在方括号中的属性名称来访问属性值:[name]。例如,可通过声明两个整数属性(例如“SrcBlend“和”DstBlend”)来使混合模式由材质属性驱动,然后让 Blend 命令使用它们:Blend [_SrcBlend] [_DstBlend]
。
Properties
代码块中的着色器参数被序列化为材质数据。着色器程序实际上可以有更多参数(如矩阵、矢量和浮点数),这些参数在运行时从代码中在材质上设置,但如果它们不是 Properties 代码块的一部分,则不会保存它们的值。这对于完全由脚本代码驱动的值最有用(使用 Material.SetFloat 和类似函数)。
在属性前面,可指定可选的特性(用方括号括起)。这些是 Unity 可以识别的特性,或者它们可以指示您自己的 MaterialPropertyDrawer 类 来控制它们在材质检视面板中的呈现方式。Unity 可以识别的特性包括:
[HideInInspector]
- 不在材质检视面板中显示属性值。[NoScaleOffset]
- 对于具有此特性的纹理属性,材质检视面板不会显示纹理平铺/偏移字段。[Normal]
- 表示纹理属性需要法线贴图。[HDR]
- 表示纹理属性需要高动态范围 (HDR) 纹理。[Gamma]
- 表示在 UI 中将浮点/矢量属性指定为 sRGB 值(就像颜色一样),并且可能需要根据使用的颜色空间进行转换。请参阅着色器程序中的属性。[PerRendererData]
- 表示纹理属性将以 MaterialPropertyBlock 的形式来自每渲染器数据。材质检视面板会更改这些属性的纹理字段 UI。// 水着色器的属性
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) = "" {}
}
在 Unity 5 之前,纹理属性可以将选项括在花括号代码块中,例如 TexGen CubeReflect
。这些纹理属性用于控制固定函数纹理坐标的生成。5.0 中删除了此功能;如果现在需要 texgen,应编写一个顶点着色器来代替。请参阅实现固定函数 TexGen页面以查看示例。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.