Post on 19-Feb-2017
An Introduction to Shader Programming
in Unity
A MEF Training Presentation - 2016
Graphics Hardware
Central Processor
CPUMemory
Graphics Processor
GPUMemory
Frame Buffer
Its Programmable Hardware!
OpenGLAPI
GLSLPrograms
CgPrograms (Nvidia)
Khronos Group
EGLAPI
Vendor Specific
Fixed Functions
*Microsoft equivalentis DirectX and HLSL (Similar to Cg)
Well some parts areprogrammable & others can be
configured with fixed function calls
OpenGLAPI
OpenGL Pipeline
Vertex Buffer Objects
VertexShader
PrimitiveAssembly Rasterization
TextureMemory
FragmentShader
FragmentOperations Frame Buffer
EGLAPI
Vertex Buffer Objects
You store Vertex properties in these:
Positions,Texture Coordinates,
and so on…
OpenGL Pipeline
Vertex Shader
You write a piece of code thatcan change those Vertex properties
Primitive Assembly
Converts the Vertices into primitives and clips shapes that are out of
the camera view
TrianglesLinesPointsetc…
Rasterization
Converts the primitivesinto Fragments(Unborn Pixels)
Texture Memory
You upload Textures into the GPUmemory. These are called Samplers.
Fragment Shader
You write a piece of code thatcolors the fragments.
Fragment Operations
Recap…
OpenGLAPI
OpenGL Pipeline
Vertex Buffer Objects
VertexShader
PrimitiveAssembly Rasterization
TextureMemory
FragmentShader
FragmentOperations Frame Buffer
EGLAPI
Before we dive intocode…
We must gain a basic understandingof the
Model View Projection Matrix!
Models are created in ‘Model Space’ coordinates
But their coordinates transformwhen you put them in
‘World Space’
And coordinates transform onceagain when viewed through
‘Camera Space’
Finally the coordinates transformonce more based on the
‘Projection’
Therefore EVERY vertex hasto 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 viacomplex matrix multiplications…
The bad news is that for OpenGLwe 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_MVPX
Model Vertex Position=
Screen Ready Position!
Unity makes programminggraphics and shaders
VERY easy…
In order to appreciate that, it isimportant to look at what it takesto 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 easyinterface to write shaders with…
OpenGL Pipeline
VertexShader
FragmentShader
ShaderLab
SurfaceShader
ShaderLab is a meta language that
wraps around a Cg program
VertexShader
FragmentShader
ShaderLab
Cg Code
ShaderLabCode
SurfaceShader
A ‘Shader Program’ requires both a
Vertex Shader + Fragment Shader
VertexShader
FragmentShader+ = Shader
Program
Shader Core Inputs & Outputs
VertexShader
Runs for each Vertex
FragmentShader
Runs for each FragmentVertices Fragments
Colored FragmentsUniforms
Let’s get going withwriting Shaders…
Fire up Unity!The rest of this presentation is a hands-on shader writing workshop…