An Introduction to
Shader Programming
in Unity
A MEF Training Presentation - 2016
Graphics Hardware
Central
Processor
CPU
Memory
Graphics
Processor
GPU
Memory
Frame Buffer
Its Programmable Hardware!
OpenGL
API
GLSL
Programs
Cg
Programs (Nvidia)
Khronos Group
EGL
API
Vendor Specific
Fixed Functions
*Microsoft equivalent
is DirectX and HLSL (Similar to Cg)
Well some parts are
programmable & others can be
configured with fixed function calls
OpenGL
API
OpenGL Pipeline
Vertex Buffer
Objects
Vertex
Shader
Primitive
Assembly
Rasterization
Texture
Memory
Fragment
Shader
Fragment
Operations
Frame Buffer
EGL
API
Vertex Buffer Objects
You store Vertex properties in these:
Positions,
Texture Coordinates,
and so on…
Op
en
GL
Pip
elin
e
Vertex Shader
You write a piece of code that
can change those Vertex properties
Primitive Assembly
Converts the Vertices into primitives and
clips shapes that are out of
the camera view
Triangles
Lines
Points
etc…
Rasterization
Converts the primitives
into Fragments
(Unborn Pixels)
Texture Memory
You upload Textures into the GPU
memory. These are called Samplers.
Fragment Shader
You write a piece of code that
colors the fragments.
Fragment Operations
Recap…
OpenGL
API
OpenGL Pipeline
Vertex Buffer
Objects
Vertex
Shader
Primitive
Assembly
Rasterization
Texture
Memory
Fragment
Shader
Fragment
Operations
Frame Buffer
EGL
API
Before we dive into
code…
We must gain a basic understanding
of the
Model View Projection Matrix!
Models are created in
‘Model Space’ coordinates
But their coordinates transform
when you put them in
‘World Space’
And coordinates transform once
again when viewed through
‘Camera Space’
Finally the coordinates transform
once more based on the ‘Projection’
Therefore EVERY vertex has
to go through…
Model Coordinates
World Coordinates
Camera Coordinates
Projection Coordinates
[Model Matrix]
[View Matrix]
[Projection Matrix]
For model vertices to become
screen vertices,
we must transform them via
complex matrix multiplications…
The bad news is that for OpenGL
we have to
calculate the MVP matrix manually…
But Unity makes it very very easy for us
with a built in matrix variable we can use…
UNITY_MATRIX_MVP
UNITY_MATRIX_MVP
X
Model Vertex Position
=
Screen Ready Position!
Unity makes programming
graphics and shaders
VERY easy…
In order to appreciate that, it is
important to look at what it takes
to render a cube if there was no Unity…
Fire up Xcode on your MacBooks!
So Unity takes care of all this complexity
plus gives us ShaderLab: Any easy
interface to write shaders with…
Op
en
GL
Pip
elin
e
Vertex
Shader
Fragment
Shader
ShaderLab
Surface
Shader
ShaderLab is a meta language that
wraps around a Cg program
Vertex
Shader
Fragment
Shader
ShaderLab
Cg Code
ShaderLab
Code
Surface
Shader
A ‘Shader Program’ requires both a
Vertex Shader + Fragment Shader
Vertex
Shader
Fragment
Shader+ =
Shader
Program
Shader Core Inputs & Outputs
Vertex
Shader
Runs for each Vertex
Fragment
Shader
Runs for each Fragment
Vertices Fragments
Colored FragmentsUniforms
Let’s get going with
writing Shaders…
Fire up Unity!
The rest of this presentation is a hands-on shader writing workshop…

Shader Programming With Unity