Unity Shaders

@CGCV @Unity @shader @Zucconi

Source: Alan Zucconi intro to shaders

A shader is a small program specifically written to be executed on the GPU rather than CPU.

Components

  1. 3D model has a ...
  2. Material, which uses a
  3. Shader

So 3D models need materials, which are in turn "wrappers which contain a shader and the values for its properties."

Insight: materials can thus share a shader program.

3D model:

  • Model points (vertex positions)
  • Normals (vertex directions)
  • Vertex colours
  • UV data for texture mapping
    • UV is a 2D vector indicates a point of the texture
    • Think UV just stands for the UV in UVW (a convention, like xyz or abc).

Material:

  • Textures
  • Shader property values

Shader:

  • CG / HLSL code
  • CG / HLSL code (alternative version)

Shader anatomy

  • SubShader sections contains actual GPU instructions
  • You can have multiple subshaders.
  • Subshaders are executed in order
  • You have to assign a shader to a material in the Unity editor

Code from Zucconi blog:

Shader "MyShader"
{
 Properties
 {
 // The properties of your shaders
 // - textures
 // - colours
 // - parameters
 // ...
 }

 SubShader
 {
 // The code of your shaders
 // - surface shader
 //    OR
 // - vertex and fragment shader
 //    OR
 // - fixed function shader
 } 
}

Shader properties

  • Like C# public fields, they show up in the editor
    • But notice that whatever you define as a Property in the Shader needs to be declared in the SubShader code!
    • The variable name has to match exactly
    • But the type is only declared in the Subshader code, even though you can already assign default values in the Properties section
  • Because properties affect materials, changes made to properties are always persistent
    • This is because materials are assets saved on disk.

Basic property types. Again, source is Zucconi blog:

Properties
{
 _MyTexture ("My texture", 2D) = "white" {}
 _MyNormalMap ("My normal map", 2D) = "bump" {} // Grey

 _MyInt ("My integer", Int) = 2
 _MyFloat ("My float", Float) = 1.5
 _MyRange ("My range", Range(0.0, 1.0)) = 0.5

 _MyColor ("My colour", Color) = (1, 0, 0, 1) // (R, G, B, A)
 _MyVector ("My Vector4", Vector) = (0, 0, 0, 0) // (x, y, z, w)
}

Initialization colors for textures:

  • white
  • black
  • gray
  • bump (texture is used as a normal map)

SubShader body and rendering order

  • Cg / HLSL code is executed for each pixel (more or less?)
    • So performance is critical!
  • Start CG/HLSL code met CGPROGRAM / ENDCG directives
  • Tags give instructions about the rendering order (Queue) and how to render (Opaque)
    • Basically indicates a priority number in the rendering process

Basic rendering order is by starting with the object furthest from the camera. The idea behind this is of course to make closer objects cover objects behind them.

However Zucconi points out that this tends to fail with transparent objects. He doesn't explain why and I can guess but do not really understand the problem. Perhaps the issue is that if you first draw the background and then a transparent item in front, the color is not "filtered" correctly and you get artefacts?

Example from Zucconi blog:

SubShader
{
 Tags
 {
 "Queue" = "Geometry"
 "RenderType" = "Opaque"
 }
 CGPROGRAM
 // Cg / HLSL code of the shader
 // ...
 ENDCG
}

Queue accepts mnemonic labels for default values:

  • Background : 1000
    • and skyboxes
  • Geometry : 2000
    • solid objects
  • Transparent : 3000
    • glass, fire, particles, water
  • Overlay : 4000
    • lens flares, GUI elements, texts

Lower is drawn first.

Ztesting

Q: Why do a Transparent object not necessarily appear above an object from Geometry?

A: "The ZTest culls the pixels which are hidden by other objects, regardless the order in which they are drawn onto the screen."

So the queue indicates drawing order, but not that ultimately the pixel will be displayed.

Surface shader + example code

When to use a surface shader?

  • If object needs to be affected by lights realistically, you can often use a built-in concept of light rendering
  • Then you can abstract away from the underlying (and complicated) lighting model by only specifying more high-level properties you need in the function called surf (short for ... surface).
  • You do have to specify which lighting model to use though.
  • Shaders only using the albedo property are usually called diffuse
  • Note that even when you do not use date from the input 3D model, you have to define the struct Input anyways (notice the capital).

N.B. albedo is the whiteness/reflection of an object (think of albino's in the sun).

Boilerplate:

CGPROGRAM
// Uses the Labertian lighting model
#pragma surface surf Lambert

sampler2D _MainTex; // The input texture

struct Input {
 float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
 o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG

N.B. uv_MainTex reads out the UV data (normalized Cartesian coordinates) of the texture for the current pixel

Surface output

  • fixed3 Albedo : base object colour / texture
  • fixed3 Normal : face direction (determines reflection angle)
  • fixed3 Emission : light generated by object itself
  • half Specular : how reflective is the material (0 to 1)
  • fixed Gloss : how diffuse is the (specular) reflection?
  • fixed Alpha : how transparent is the material?

Types:

  • half is 15 bits
  • fixed is 10 bits (values from -2 to +2)

See GCGV lecture notes.

Texture mapping, UV model

Mapping texture onto a model using its UV model:

source is part 2 of Zucconi blog:

Shader "Example/Diffuse Texture" {
Properties {
  _MainTex ("Texture", 2D) = "white" {}
}

SubShader {
  Tags { "RenderType" = "Opaque" }
  CGPROGRAM
  #pragma surface surf Lambert
  struct Input {
      float2 uv_MainTex;
  };
  sampler2D _MainTex;
  void surf (Input IN, inout SurfaceOutput o) {
      o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
  }
  ENDCG
} 
Fallback "Diffuse"
}

It is important to remember that the UV coordinate are stored only in the vertices. When the shader evaluates a pixel which is not a vertex, the function tex2D interpolates the UV coordinates of the three closest vertices.

Surface Input

Depending on what you want to adjust, you can load various states by declaring pre-defined variables.

  • float2 uv_MainTex : see above, UV coordinates in texture space
  • float3 worldPos : initializes world position at the point currently passing through surf
  • float3 viewDir : view direction
  • float4 name : COLOR : variable name will be assigned the color of the vertex.
  • float4 screenPos : pixel location on the screen

If you declare worldPos, you can use IN.worldPos in the surf function where Input IN is passed.

Vertex modifying function (change vertices before surf)

Surf manipulates colours in RGBA space.

But you could also adjust the 3D model before reaching the surf function. This also means you have to do operations on the 3D "triangles" in 3D.

E.g. to make a model chubbier, you can extend triangles in the direction of the normal. This is called normal extrusion.

Use the vert function for this (also used in vertex and fragment shaders)

  • appdata_full gives all vertex data
  • WorldNormalVector : map normals to world coordinates (not used here, but handy)

Example from Zucconi blog part 2:

 Shader "Example/Normal Extrusion" {
    Properties {
      _MainTex ("Texture", 2D) = "white" {}
      _Amount ("Extrusion Amount", Range(-0.0001,0.0001)) = 0
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert vertex:vert
      struct Input {
          float2 uv_MainTex;
      };
      float _Amount;
      void vert (inout appdata_full v) {
          v.vertex.xyz += v.normal * _Amount;
      }
      sampler2D _MainTex;
      void surf (Input IN, inout SurfaceOutput o) {
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
      }
      ENDCG
    } 
    Fallback "Diffuse"
  }

Vertex and fragment shader

  • No built-in concept of light rendering
  • 2D effects
  • postprocessing
  • 3D too complex for surface shaders
  • Whole CGPROGRAM must be encloded in a Pass section

    • this is optional for surface shaders
    • Why?
  • vert: alter vertices

  • frag : assign final RGB color per pixel

Q: in underlying code, it seems as if vert and frag are merely the chosen names with type vertex and fragment. So is this just a good convention, or do we have to write vert and frag? I assume the latter, I'm just not sure. They are probably predefined directives.

Boilerplate:

Pass {
CGPROGRAM

#pragma vertex vert             
#pragma fragment frag

struct vertInput {
 float4 pos : POSITION;
};  

struct vertOutput {
 float4 pos : SV_POSITION;
};

vertOutput vert(vertInput input) {
 vertOutput o;
 o.pos = mul(UNITY_MATRIX_MVP, input.pos);
 return o;
}

half4 frag(vertOutput output) : COLOR {
 return half4(1.0, 0.0, 0.0, 1.0); 
}
ENDCG
}

Efficiency

GPU is optimized for sequential code, so try to avoid branching with if-else statements.

Use pre-defined functions like "saturate" and "step" to avoid them.

Compute shaders

  • General purpose GPU program
    • i.e. not necessarily part in the overall graphics pipeline, like CUDA
  • Must include a compute kernel
  • Compute kernel is executed multiple times in parallel
  • Threads are organized in groups; group number specified at dispatch time
  • Group has three dimensions [numthreads({x}, {y}, {z})]
    • How many threads per group
  • Specify which function is a compute kernel (vs a helper function etc.) with the #pragma kernel {function name} directive.
  • As always: declare variables before using them

Boilerplate from Unity:

// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain

// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;

[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
    // TODO: insert actual code here!

    Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);
}
  • [numthreads(8,8,1)] -> 8 threads for x and y-dimension, 1 for z.
  • Result stores a readable/writable 2D float4 render texture
    • "Render texture" is a texture that can be rendered to.
    • I.e. it will be filled with the output of the compute shader

Further links