Shaders
When you write shaders for Unity, you use the following languages:
- HLSL: One of the official shading languages.
- ShaderLab: Unity-specific language which acts as a container for your shader programs.
General Ressources
DirectX HLSL Reference (Microsoft)
The Book of Shaders: Glossary A handy-dandy collection of common shader functions
Cg Standard Library Functions (NVidia Cg)
ShaderLab
ShaderLab features several blocks:
- A Shader object is a wrapper for shader programs and other information.
- A Properties block defines material properties inside the Shader object. Basically how unity materials deliver data to the shader.
- The SubShader block defines a part of the shader used for a shading step.
- In the Tags block you can assign tags to a subshader, which define, how this subshader will be used by Unity.
- It defines the LoD value
- Additional GPU commands can be issued like ZWrite, ZTest, Blend,...
- In a Pass block is where you actually use HSLS code to define what the shader does.
- A pass can also contain a Tags block (mostly for the LightMode).
- The code blocks can be a HLSLPROGRAM (which is newer) or a CGPROGRAM (Includes several of Unity’s built-in shader include files by default)
- GPU render state commands change the render state on the GPU such as
Blend,CullandZWrite. - Stencil command configures settings relating to the stencil buffer on the GPU.
Properties
| Type | Example syntax | Comment |
|---|---|---|
| Integer | _ExampleName ("Integer display name", Integer) = 1 |
This type is backed by a real integer (unlike the legacy Int type described below, which is backed by a float). Use this instead of Int when you want to use an integer. |
| Int (legacy) | _ExampleName ("Int display name", Int) = 1 |
Note: This legacy type is backed by a float, rather than an integer. It is supported for backwards compatibility reasons only. Use the Integer type instead. |
| Float | _ExampleName ("Float display name", Float) = 0.5 _ExampleName ("Float with range", Range(0.0, 1.0)) = 0.5 |
The maximum and minimum values for the range slider are inclusive. |
| Texture2D | _ExampleName ("Texture2D display name", 2D) = "" {} _ExampleName ("Texture2D display name", 2D) = "red" {} |
Put the following values in the default value string to use one of Unity’s built-in textures: “white” (RGBA: 1,1,1,1), “black” (RGBA: 0,0,0,1), “gray” (RGBA: 0.5,0.5,0.5,1), “bump” (RGBA: 0.5,0.5,1,1) or “red” (RGBA: 1,0,0,1). If you leave the string empty or enter an invalid value, it defaults to “gray”. Note: these default textures are not visible in the Inspector. |
| Texture2DArray | _ExampleName ("Texture2DArray display name", 2DArray) = "" {} |
For more information, see Texture arrays. |
| Texture3D | _ExampleName ("Texture3D", 3D) = "" {} |
The default value is a “gray” (RGBA: 0.5,0.5,0.5,1) texture. |
| ***Cubemap* ** | _ExampleName ("Cubemap", Cube) = "" {} |
The default value is a “gray” (RGBA: 0.5,0.5,0.5,1) texture. |
| CubemapArray | _ExampleName ("CubemapArray", CubeArray) = "" {} |
See Cubemap arrays. |
| Color | _ExampleName("Example color", Color) = (.25, .5, .5, 1) |
This maps to a float4 in your shader code. The Material Inspector displays a color picker. If you would rather edit the values as four individual floats, use the Vector type. |
| Vector | _ExampleName ("Example vector", Vector) = (.25, .5, .5, 1) |
This maps to a float4 in your shader code. The Material Inspector displays four individual float fields. If you would rather edit the values using a color picker, use the Color type. |
HLSL
Data Types
| Use this intrinsic type | To define this shader variable |
|---|---|
| Scalar | One-component scalar |
| Vector, Matrix | Multiple-component vector or matrix |
| Sampler, Texture or Buffer | Sampler, texture, or buffer object |
| Struct, User Defined | Custom structure or typedef |
| Array | Literal scalar expressions declared containing most other types |
| State Object | HLSL representations of state objects |
Intrinsic Functions
- Standard Math functions: abs ceil clamp floor fmod max min modf pow round sqrt trunc
-
Trigonometry functions: acos asin atan atan2 cos cosh sin sincos sinh tan
-
Vector math functions: cross dot distance dst length normalize reflect
- Texturing 1D: tex1D(s, t) tex1D(s, t, ddx, ddy) tex1Dbias tex1Dgrad tex1Dlod tex1Dproj
- Texturing 2D: tex2D(s, t) tex2D(s, t, ddx, ddy) tex2Dbias tex2Dgrad tex2Dlod tex2Dproj
Example
Shader "Vertegens/SemanticBaseColors"
{
Properties
{
_Color ("Color", Color) = (1,0,1,1)
}
SubShader
{
Tags {
"RenderType"="Opaque"
// "Queue"="Transparent"
}
LOD 100
ZWrite Off
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Name "Forward"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = _Color;
return col;
}
ENDCG
}
}
}