This page contains information on using a Properties
block in your ShaderLab code to define material properties for a Shader object.
In your ShaderLab code, you can define material properties. A material property is a property that Unity stores as part of the material asset. This allows artists to create, edit and share materials with different configurations.
If you use material properties:
If you do not use material properties: * You can still get or set the value of a variable in a Shader object by calling a function on a material. * There is no visual editor for these values. * Changes do not persist between sessions.
The only times that you would normally not create a material property is if you want to set shader property values entirely using scripts (for example, if you are making procedural content), if the properties cannot be made into material properties, or if you don’t want them to be edited in the Inspector.
Feature name | 빌트인 렌더 파이프라인 | 유니버설 렌더 파이프라인(URP) | 고해상도 렌더 파이프라인(HDRP) | Custom SRP |
---|---|---|---|---|
ShaderLab: Properties block | 지원 | Yes In your HLSL code, you must put per-material variables in the same CBUFFER for SRP Batcher compatibility. |
Yes In your HLSL code, you must put per-material variables in the same CBUFFER for SRP Batcher compatibility. |
Yes In your HLSL code, you must put per-material variables in the same CBUFFER for SRP Batcher compatibility. |
To assign material properties to a Shader object in ShaderLab, you place a Properties
block inside a Shader
block.
Signature | 기능 |
---|---|
Properties { <Material property declaration> <Material property declaration> }
|
Saves the given properties as part of the material asset, and uses the values stored in the material asset during rendering. A Properties block can contain any number of 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.
이 섹션에는 다음 정보가 있습니다.
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.
타입 | Example syntax | Comment |
---|---|---|
Int | _ExampleName ("Int display name", Int) = 1 |
Note: In spite of the name, this type is actually backed by a float. |
Float |
_ExampleName ("Float display name", Float) = 0.5 _ExampleName ("Float with range", Range(0.0, 1.0)) = 0.5
|
The maximum and minimum values for the range slider are inclusive. |
Texture2D |
_ExampleName ("Texture2D display name", 2D) = "" {} _ExampleName ("Texture2D display name", 2D) = "red" {}
|
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,0.5) 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 | _ExampleName ("Texture2DArray display name", 2DArray) = "" {} |
For more information, see Texture arrays. |
Texture3D | _ExampleName ("Texture3D", 3D) = "" {} |
The default value is a “gray” (RGBA: 0.5,0.5,0.5,1) texture. |
Cubemap | _ExampleName ("Cubemap", Cube) = "" {} |
The default value is a “gray” (RGBA: 0.5,0.5,0.5,1) texture. |
CubemapArray | _ExampleName ("CubemapArray", CubeArray) = "" {} |
See Cubemap arrays. |
Color | _ExampleName("Example color", Color) = (.25, .5, .5, 1) |
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 | _ExampleName ("Example vector", Vector) = (.25, .5, .5, 1) |
This maps to a float4 in your shader code. The Material Inspector displays four individual float fields. If you would rather edit the values using a color picker, use the Color type. |
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.
속성 | 기능 |
---|---|
[Gamma] |
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. |
[HDR] |
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. |
[HideInInspector] |
Tells the Unity Editor to hide this property in the Inspector. |
[MainTexture] |
Sets the main texture for a Material, which you can access using Material.mainTexture. By default, Unity considers a texture with the property name name _MainTex as the main texture. Use this attribute if your texture has a different property name, but you want Unity to consider it the main texture.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 is not visible in the Game view when you use the texture streaming debugging view mode, or a custom debug tool. |
[MainColor] |
Sets the main color for a Material, which you can access using Material.color. By default, Unity considers a color with the property name name _Color as the main color. Use this attribute if your color has a different property name, but you want Unity to consider it the main color. If you use this attribute more than once, Unity uses the first property and ignores subsequent ones. |
[NoScaleOffset] |
Tells the Unity Editor to hide tiling and offset fields for this texture property. |
[Normal] |
Indicates that a texture property expects a normal map. The Unity Editor displays a warning if you assign an incompatible texture. |
[PerRendererData] |
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. |
Material properties are represented in C# code by the MaterialProperty class.
To access variables defined in your HLSL code, you can call Material.GetFloat, Material.SetFloat. There are other, similar methods; see the Material API documentation for a full list. When you access HLSL variables using these APIs, it doesn’t matter whether the variable is a material property or not.
In the Unity Editor, you can control how material properties appear in the Inspector window. The easiest way to do this is using a MaterialPropertyDrawer. For more complex needs, you can use the MaterialEditor, MaterialProperty, and ShaderGUI classes. For more information on creating custom GUIs for shaders, see ShaderLab:assigning a custom editor.
To set the value of a variable in your ShaderLab code from a material property, put the material property name in square brackets in your ShaderLab code.
This example code demonstrates the syntax for using a material property to set the units
value of the ShaderLab Offset
command.
Shader "Examples/MaterialPropertyShaderLab"
{
Properties
{
// Change this value in the Material Inspector to affect the value of the Offset command
_OffsetUnitScale ("Offset unit scale", Integer) = 1
}
SubShader
{
// The code that defines the rest of the SubShader goes here
Pass
{
Offset 0, [_OffsetUnitScale]
// The code that defines the rest of the Pass goes here
}
}
}
To set the value of a variable in HLSL code using a material property, give the material property the same name as the shader property.
You can see this technique in the following articles, which include working code examples: