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.
For example these shaderA program that runs on the GPU. More info
See in Glossary 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 ShaderLabUnity’s language for defining the structure of Shader objects. More info
See in Glossary 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 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.
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 HDRhigh dynamic range
See in Glossary (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).
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.