Shader declares Material properties in a Properties block. If you want to access some of those properties in a shader program, you need to declare a Cg/HLSL variable with the same name and a matching type. An example is provided in Shader Tutorial: Vertex and Fragment Programs.
For example these shader properties:
_MyColor ("Some Color", Color) = (1,1,1,1)
_MyVector ("Some Vector", Vector) = (0,0,0,0)
_MyFloat ("My float", Float) = 0.5
_MyTexture ("Texture", 2D) = "white" {}
_MyCubemap ("Cubemap", CUBE) = "" {}
would be declared for access in Cg/HLSL code as:
fixed4 _MyColor; // low precision type is usually enough for colors
float4 _MyVector;
float _MyFloat;
sampler2D _MyTexture;
samplerCUBE _MyCubemap;
Cg/HLSL can also accept uniform keyword, but it is not necessary:
uniform float4 _MyColor;
Property types in ShaderLab map to Cg/HLSL variable types this way:
Shader property values are found and provided to shaders from these places:
The order of precedence is like above: per-instance data overrides everything; then Material data is used; and finally if shader property does not exist in these two places then global property value is used. Finally, if there’s no shader property value defined anywhere, then “default” (zero for floats, black for colors, empty white texture for textures) value will be provided.
Materials can contain both serialized and runtime-set property values.
Serialized data is all the properties defined in shader’s Properties block. Typically these are values that need to be stored in the material, and are tweakable by the user in Material Inspector.
A material can also have some properties that are used by the shader, but not declared in shader’s Properties block. Typically this is for properties that are set from script code at runtime, e.g. via Material.SetColor. Note that matrices and arrays can only exist as non-serialized runtime properties (since there’s no way to define them in Properties block).
For each texture that is setup as a shader/material property, Unity also sets up some extra information in additional vector properties.
Materials often have Tiling and Offset fields for their texture properties. This information is passed into shaders in a float4 {TextureName}_ST
property:
x
contains X tiling valuey
contains Y tiling valuez
contains X offset valuew
contains Y offset valueFor example, if a shader contains texture named _MainTex
, the tiling information will be in a _MainTex_ST
vector.
{TextureName}_TexelSize
- a float4 property contains texture size information:
x
contains 1.0/widthy
contains 1.0/heightz
contains widthw
contains height{TextureName}_HDR
- a float4 property with information on how to decode a potentially HDR (e.g. RGBM-encoded) texture depending on the color space used. See DecodeHDR
function in UnityCG.cginc shader include file.
When using Linear color space, all material color properties are supplied as sRGB colors, but are converted into linear values when passed into shaders.
For example, if your Properties shader block contains a Color
property called “MyColor“, then the corresponding ”MyColor” HLSL variable will get the linear color value.
For properties that are marked as Float
or Vector
type, no color space conversions are done by default; it is assumed that they contain non-color data. It is possible to add [Gamma]
attribute for float/vector properties to indicate that they are specified in sRGB space, just like colors (see Properties).
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.