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.
  • GPU render state commands change the render state on the GPU such as Blend, Cull and ZWrite.
  • 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

Intrinsic Functions Reference

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
        }
    }
}