Version: 2022.2
Language : English
ShaderLab: defining a Shader object
ShaderLab: assigning a fallback

ShaderLab: defining material properties

This page contains information on using a Properties block in your ShaderLabUnity’s language for defining the structure of Shader objects. More info
See in Glossary
code to define material properties for a ShaderA program that runs on the GPU. More info
See in Glossary
object.

Overview

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:

  • You can get or set the value of a variable in a Shader objectAn instance of the Shader class, a Shader object is container for shader programs and GPU instructions, and information that tells Unity how to use them. Use them with materials to determine the appearance of your scene. More info
    See in Glossary
    by calling functions on the material (such as Material.SetFloat).
  • You can view and edit the values using the material InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    .
  • Unity saves the changes that you make as part of the material asset, so they persist between sessions.

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 scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary
(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.

Render pipeline compatibility

Feature name Built-in Render PipelineA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary
Universal Render Pipeline (URP) High Definition Render Pipeline (HDRP) Custom SRP
ShaderLab: Properties block Yes 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.

Using the Properties block

To assign material properties to a Shader object in ShaderLab, you place a Properties block inside a Shader block.

Signature Function
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.

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 _ExampleName ("Integer display name", Integer) = 1 This type is backed by a real integer (unlike the legacy Int type described below, which is backed by a float). Use this instead of Int when you want to use an integer.
Int (legacy) _ExampleName ("Int display name", Int) = 1 Note: This legacy type is backed by a float, rather than an integer. It is supported for backwards compatibility reasons only. Use the Integer type instead.
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.
CubemapA collection of six square textures that can represent the reflections in an environment or the skybox drawn behind your geometry. The six squares form the faces of an imaginary cube that surrounds an object; each face represents the view along the directions of the world axes (up, down, left, right, forward and back). More info
See in Glossary
_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.

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
_TransparencyLM _TransparencyLM ("Transmissive Texture", 2D) = "white" {} 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
[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.

Using material properties with C# code

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.

Using material properties to set variables in ShaderLab code

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
        }
    }
}

Using material properties to set variables in HLSL code

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:

ShaderLab: defining a Shader object
ShaderLab: assigning a fallback