This section presents a brief description of how to access the shader Properties
in Cg/HLSL programming, and the data types and common methods used in Cg/HLSL programming.
Shader can be declared with properties in a Properties
block. If you want to access some of those properties in a Cg/HLSL shader program, you need to declare a Cg/HLSL variable with the same name and a matching type.
Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { ...... CGPROGRAM sampler2D _MainTex; ...
Property types to Cg/HLSL variable types are as follows:
Color
andVector
properties map tofloat4
variables.Range
andFloat
properties map tofloat
variables.Texture
properties map tosampler2D
variables for regular (2D) textures.CUBE
andRECT
textures map tosamplerCUBE
andsamplerRECT
variables, respectively.
Cg/HLSL has six basic data types. Some of them are the same as in C, while others are especially added for GPU programming. These types are:
Date type |
Description |
---|---|
A 32-bit floating point number (high precision floating point. Generally 32 bits, just like float type in regular programming languages). | |
A 16-bit floating point number (medium-precision floating point. Generally 16 bits, with a range of -60000 to +60000 and 3.3 decimal digits of precision) | |
A 32-bit integer. | |
A 12-bit fixed point number (low-precision fixed point. Generally 11 bits, with a range of -2.0 to +2.0 and 1/256th precision). | |
A Boolean variable (FALSE = 0, TRUE = 1). | |
Represents a texture object (sampler1D, sampler2D, sampler3D, samplerCUBE, samplerRECT). |
Cg/HLSL also features vector
and matrix
data types that are based on the basic data types, such as float3
and float4x4
. Such data types are quite common when dealing with 3D graphics programming. Cg/HLSL also has struct
and array
data types, which work in a similar way to their C equivalents.
Method |
Description |
---|---|
Dot product of two vectors. | |
Cross product of vectors A and B; A and B must be three-component vectors. | |
Maximum of a and b. | |
Minimum of a and b. | |
Get largest integer not greater than x. | |
Get closest integer to x. | |
Get smallest integer not less than x. | |
Computes x raised to the power y. | |
Returns a vector of length 1 that points in the same direction as vector v. | |
Clamps x to the [0, 1] range. | |
2D texture lookup (sampled data at the location indicated by the texture coordinate set in the sampler object). |
The preceding methods are the common methods that you can use to create your shader with Cg/HLSL. There are a lot of methods that you can also use in Cg/HLSL.
For more details, you can refer to the following site:
http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_e.html.
Tip
Note that UnpackNormal( x )
is the method that is provided by Unity to unpack the normal or bump texture, which you can find in the UnityCG.cginc
file inside Unity {unity install path}/Data/CGIncludes/UnityCG.cginc
on Windows, and in /Applications/Unity/Unity.app/Contents/CGIncludes/UnityCG.cginc
on Mac.