Shader semantics
Proporcionar información del vértice a vertex programs

Accediendo a propiedades del sombreador en Cg

El Sombreador declara propiedades del Material en un bloque Properties. Si usted quiere tener acceso a algunas de esas propiedades en un shader program, usted necesitará declarar una variable Cg/HLSL con el mismo nombre y tipo de correspondencia. Un ejemplo es dado en Shader Tutorial: Vertex and Fragment Programs.

Por ejemplo estas propiedades del sombreador:

_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) = "" {} 

serían declaradas para acceso en código Cg/HLSL como:

fixed4 _MyColor; // low precision type is usually enough for colors
float4 _MyVector;
float _MyFloat; 
sampler2D _MyTexture;
samplerCUBE _MyCubemap;

Cg puede también aceptar la palabra clave uniform, pero no es necesario:

uniform float4 _MyColor;

Tipos de propiedades en ShaderLab mapean a tipos de variable de Cg/HLSL de esta forma:

  • Color and Vector properties map to float4, half4 or fixed4 variables.
  • Range and Float properties map to float, half or fixed variables.
  • Propiedades de Textura mapean a variables sampler2D para texturas (2D) regulares; Cubemaps mapean a samplerCUBE; y texturas 3D mapean a sampler3D.

How property values are provided to shaders

Shader property values are found and provided to shaders from these places:

  • Per-Renderer values set in MaterialPropertyBlock. This is typically “per-instance” data (e.g. customized tint color for a lot of objects that all share the same material).
  • Values set in the Material that’s used on the rendered object.
  • Global shader properties, set either by Unity rendering code itself (see built-in shader variables), or from your own scripts (e.g. Shader.SetGlobalTexture).

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.

Serialized and Runtime Material properties

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).

Special Texture properties

For each texture that is setup as a shader/material property, Unity also sets up some extra information in additional vector properties.

Texture tiling & offset

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 value
  • y contains Y tiling value
  • z contains X offset value
  • w contains Y offset value

For example, if a shader contains texture named _MainTex, the tiling information will be in a _MainTex_ST vector.

Texture size

{TextureName}_TexelSize - a float4 property contains texture size information:

  • x contains 1.0/width
  • y contains 1.0/height
  • z contains width
  • w contains height

Texture HDR parameters

{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.

Color spaces and color/vector shader data

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).

Véase también

Shader semantics
Proporcionar información del vértice a vertex programs