Legacy Documentation: Version 4.5
Previous
Vertex and Fragment Shader Examples
Next
Providing vertex data to vertex programs

Accessing shader properties in Cg

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 enough for colors
float4 _MyVector;
float _MyFloat; 
sampler2D _MyTexture;
samplerCUBE _MyCubemap;

Cg 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:

  • Color and Vector properties map to float4, half4 or fixed4 variables.
  • Range and Float properties map to float, half or fixed variables.
  • Texture properties map to sampler2D variables for regular (2D) textures; Cubemaps map to samplerCUBE; and 3D textures map to sampler3D.
Previous
Vertex and Fragment Shader Examples
Next
Providing vertex data to vertex programs