ShaderLab: Properties
Shaders可以定義一個參數列表,讓設計師可以在Unity的material inspector中設置。Shader文件的Properties代碼塊是用來定義它們的。
Syntax(語法)
Properties
Properties { Property [Property ...] }
定義property代碼塊。 括號內的屬性定義如下。
Numbers and Slider(數字與滑塊)
name ("display name", Range (min, max)) = number
name ("display name", Float) = number
name ("display name", Int) = number
它們都定義了一個數值(標量) property 作爲默認值。Range
格式讓其顯示爲一個滑塊,可在min與max值之間滑動。
Colors and Vectors(顏色和向量)
name ("display name", Color) = (number,number,number,number)
name ("display name", Vector) = (number,number,number,number)
用所給的RGBA組件定義一個有默認值的顏色屬性,或者定義一個有默認值的4D向量屬性。顏色 properties 會有顏色拾取器, 並根據色彩空間的需要進行調整 (see Properties in Shader Programs)。 向量屬性展示爲4個數字變量。
Textures(紋理)
name ("display name", 2D) = "defaulttexture" {}
name ("display name", Cube) = "defaulttexture" {}
name ("display name", 3D) = "defaulttexture" {}
分別定義了一個 2D texture, cubemap 或 3D (volume) 屬性。
Details
在shader中的每個property,都是靠 name來引用的(在Unity中,用下劃線開頭的shader property名稱是很常見的。)。屬性會以display name顯示在材質的inspector面板中。每個屬性都要給予一個默認值在等號後:
- 對於 Range 和 Float properties 只是一個數字, 比如 “13.37”。
- 對於 Color 和 Vector properties 是四個數字在一個圓括號內, 例如 “(1,0.5,0.2,1)”。
- 對於 2D Textures, 默認值可以是一個空的字符串,或者內建的默認紋理:“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) or “red” (RGBA: 1,0,0,0)。
- 對於 non–2D Textures (Cube, 3D, 2DArray) 默認值爲空字符串。 當材質沒有被分配一個 Cubemap/3D/Array 紋理時,就會使用一個灰色(RGBA: 0.5,0.5,0.5,0.5)紋理。
然後在固定管線部分,property值可以直接通過方括號中的name來訪問: [name]. 例如,你可以使混合模式通過一個材質屬性驅動,通過定義兩個 integer properties(名爲 “SrcBlend“ 和 ”DstBlend”), 然後讓 Blend Command 使用它們: Blend [_SrcBlend] [_DstBlend]
.
Properties
塊中的着色器參數被序列化爲Material數據。 Shader programs 其實可以擁有更多參數(比如 matrices, vectors and floats )它們被代碼在運行中設置到材質裏,但是如果它們不是Properties塊的一部分,那麼它們的值將不會被保存下來。這對於那些完全由腳本代碼驅動的值非常有用 (使用 Material.SetFloat 和其他類似函數).
Property attributes and drawers
Property前的方括號內的屬性是可以指定的。 這些屬性被Unity識別或由您自定義的MaterialPropertyDrawer classes識別來控制property會如何渲染到material inspector。被Unity定義的屬性:
[HideInInspector]
- 不在material inspector中顯示property的值。[NoScaleOffset]
- material inspector將不會顯示被該屬性標記的紋理的tiling/offset變量。[Normal]
- 標示該紋理應該爲法向貼圖。[HDR]
- 標示紋理屬性應該爲高動態範圍(HDR)紋理。[Gamma]
- 標示一個float/vector property在UI中是被指定爲sRGB值的(就和color值一樣,通過color picker賦值?), 並可能需要根據使用的色彩空間轉換. See Properties in Shader Programs.[PerRendererData]
- 標示紋理將來自格式爲 MaterialPropertyBlock的per-renderer data. Material inspector changes the texture slot UI for these properties.
Example
// properties for a water shader
Properties
{
_WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // sliders
_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) // color
_ReflectionTex ("Environment Reflection", 2D) = "" {} // textures
_RefractionTex ("Environment Refraction", 2D) = "" {}
_Fresnel ("Fresnel (A) ", 2D) = "" {}
_BumpMap ("Bumpmap (RGB) ", 2D) = "" {}
}
Texture property options (removed in 5.0)
在Unity 5之前,紋理屬性可以在卷括號中選擇, 例如 TexGen CubeReflect。 它們都是用了控制固定管線紋理座標生成的。 這些函數在 5.0 中被移除了; 如果你需要 texgen 你應該寫一個 vertex shader 代替。 到 Implementing Fixed Function TexGen page 頁面查看例子。