Properties block reference in ShaderLab
Reference for the Properties block in ShaderLab.
읽는 시간 6분최근 업데이트: 23일 전
This page contains information on using a block in your ShaderLab code to define material properties for a Shader object.
PropertiesRender pipeline compatibility
Feature name | Universal Render Pipeline (URP) | High Definition Render Pipeline (HDRP) | Custom SRP | Built-in Render Pipeline |
|---|---|---|---|---|
| ShaderLab: Properties block | Yes In your HLSL code, you must put per-material variables in the same | Yes In your HLSL code, you must put per-material variables in the same | Yes In your HLSL code, you must put per-material variables in the same | Yes |
For more information about using a block, refer to Write an unlit shader with color input in URP.
CBUFFERSyntax
Signature | Function |
|---|---|
| Saves the given properties as part of the material asset, and uses the values stored in the material asset during rendering. A |
Material property declarations
All material property declarations follow this basic format:
[optional: attribute] name("display text in Inspector", type name) = default value
The exact syntax varies by type.
This section contains information on:
Material property declaration syntax by type
The type name and the syntax for the default value depend on the type of the property.
In shader code, it is conventional to begin all property names with an underscore character. The examples on this page follow this convention.
Type | Example syntax | Comment |
|---|---|---|
| Integer | | This type is backed by a real integer (unlike the legacy |
| Int (legacy) | | Note: This legacy type is backed by a float, rather than an integer. It is supported for backwards compatibility reasons only. Use the |
| Float | | The maximum and minimum values for the range slider are inclusive. |
| Texture2D | | Put the following values in the default value string to use one of Unity's built-in textures: “white” (RGBA: 1,1,1,1), “black” (RGBA: 0,0,0,1), “gray” (RGBA: 0.5,0.5,0.5,1), “bump” (RGBA: 0.5,0.5,1,1) or “red” (RGBA: 1,0,0,1). If you leave the string empty or enter an invalid value, it defaults to "gray". Note: these default textures are not visible in the Inspector. |
| Texture2DArray | | For more information, see Texture arrays. |
| Texture3D | | The default value is a "gray" (RGBA: 0.5,0.5,0.5,1) texture. |
| Cubemap | | The default value is a "gray" (RGBA: 0.5,0.5,0.5,1) texture. |
| CubemapArray | | See Cubemap arrays. |
| Color | | This maps to a float4 in your shader code. The Material Inspector displays a color picker. If you would rather edit the values as four individual floats, use the Vector type. |
| Vector | | This maps to a float4 in your shader code. The Material Inspector displays four individual float fields. To display a different number of fields, add If you would rather edit the values using a color picker, use the Color type.> |
Reserved material property names
Unity has some reserved names for material properties. When you create a material property with one of these names, Unity performs predefined operations. Do not use these names unless you intend to use this functionality.
Name | Example syntax | Function |
|---|---|---|
| | Unity considers a color with this name as the main color for a material. Use the MainColor attribute to specify a different property as the main color. |
| | In the Universal Render Pipeline, Unity considers a texture with this name as the main texture for a material. Use the MainTexture attribute to specify a different property as the main texture. |
| | In the Built-In Render Pipeline, Unity considers a texture with this name as the main texture for a material. Use the MainTexture attribute to specify a different property as the main texture. |
| | In the Built-In Render Pipeline, enables custom RGB transparency during lightmapping. For more information, see Lightmapping and shaders. |
Material property attributes
Material property declarations can have an optional attribute that tells Unity how to handle them.
In addition to the attributes listed here, you can use the same syntax to add a MaterialPropertyDrawer to a material property. These let you control how material properties appear in the Inspector window.
Attribute | Function |
|---|---|
| Indicates that a float or vector property uses sRGB values, which means that it must be converted along with other sRGB values if the color space in your project requires this. For more information, see Properties in Shader Programs. |
| Indicates that a texture or color property uses high dynamic range (HDR) values. For texture properties, the Unity Editor displays a warning if an LDR texture is assigned. For color properties, the Unity Editor uses the HDR color picker to edit this value. |
| Tells the Unity Editor to hide this property in the Inspector. |
| Sets the main texture for a Material, which you can access using Material.mainTexture. By default, Unity considers a texture with the property name If you use this attribute more than once, Unity uses the first property and ignores subsequent ones. Note: When you set the main texture using this attribute, the texture isn't visible in the Game view when you use the texture mipmap streaming debugging view mode, or a custom debug tool. |
| Sets the main color for a Material, which you can access using Material.color. By default, Unity considers a color with the property name |
| Tells the Unity Editor to hide tiling and offset fields for this texture property. |
| Indicates that a texture property expects a normal map. The Unity Editor displays a warning if you assign an incompatible texture. |
| Indicates that a texture property will be coming from per-renderer data in the form of a MaterialPropertyBlock. The Material inspector shows these properties as read-only. |