Shader Programming With Unity

29
An Introduction to Shader Programming in Unity A MEF Training Presentation - 2016

Transcript of Shader Programming With Unity

Page 1: Shader Programming With Unity

An Introduction to Shader Programming

in Unity

A MEF Training Presentation - 2016

Page 2: Shader Programming With Unity

Graphics Hardware

Central Processor

CPUMemory

Graphics Processor

GPUMemory

Frame Buffer

Page 3: Shader Programming With Unity

Its Programmable Hardware!

OpenGLAPI

GLSLPrograms

CgPrograms (Nvidia)

Khronos Group

EGLAPI

Vendor Specific

Fixed Functions

*Microsoft equivalentis DirectX and HLSL (Similar to Cg)

Page 4: Shader Programming With Unity

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

Page 5: Shader Programming With Unity

Vertex Buffer Objects

You store Vertex properties in these:

Positions,Texture Coordinates,

and so on…

OpenGL Pipeline

Page 6: Shader Programming With Unity

Vertex Shader

You write a piece of code thatcan change those Vertex properties

Page 7: Shader Programming With Unity

Primitive Assembly

Converts the Vertices into primitives and clips shapes that are out of

the camera view

TrianglesLinesPointsetc…

Page 8: Shader Programming With Unity

Rasterization

Converts the primitivesinto Fragments(Unborn Pixels)

Page 9: Shader Programming With Unity

Texture Memory

You upload Textures into the GPUmemory. These are called Samplers.

Page 10: Shader Programming With Unity

Fragment Shader

You write a piece of code thatcolors the fragments.

Page 11: Shader Programming With Unity

Fragment Operations

Page 12: Shader Programming With Unity

Recap…

OpenGLAPI

OpenGL Pipeline

Vertex Buffer Objects

VertexShader

PrimitiveAssembly Rasterization

TextureMemory

FragmentShader

FragmentOperations Frame Buffer

EGLAPI

Page 13: Shader Programming With Unity

Before we dive intocode…

Page 14: Shader Programming With Unity

We must gain a basic understandingof the

Model View Projection Matrix!

Page 15: Shader Programming With Unity

Models are created in ‘Model Space’ coordinates

Page 16: Shader Programming With Unity

But their coordinates transformwhen you put them in

‘World Space’

Page 17: Shader Programming With Unity

And coordinates transform onceagain when viewed through

‘Camera Space’

Page 18: Shader Programming With Unity

Finally the coordinates transformonce more based on the

‘Projection’

Page 19: Shader Programming With Unity

Therefore EVERY vertex hasto go through…

Model Coordinates

World Coordinates

Camera Coordinates

Projection Coordinates

[Model Matrix]

[View Matrix]

[Projection Matrix]

Page 20: Shader Programming With Unity

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…

Page 21: Shader Programming With Unity

But Unity makes it very very easy for us

with a built in matrix variable we can use…

UNITY_MATRIX_MVP

Page 22: Shader Programming With Unity

UNITY_MATRIX_MVPX

Model Vertex Position=

Screen Ready Position!

Page 23: Shader Programming With Unity

Unity makes programminggraphics and shaders

VERY easy…

Page 24: Shader Programming With Unity

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!

Page 25: Shader Programming With Unity

So Unity takes care of all this complexity

plus gives us ShaderLab: Any easyinterface to write shaders with…

OpenGL Pipeline

VertexShader

FragmentShader

ShaderLab

SurfaceShader

Page 26: Shader Programming With Unity

ShaderLab is a meta language that

wraps around a Cg program

VertexShader

FragmentShader

ShaderLab

Cg Code

ShaderLabCode

SurfaceShader

Page 27: Shader Programming With Unity

A ‘Shader Program’ requires both a

Vertex Shader + Fragment Shader

VertexShader

FragmentShader+ = Shader

Program

Page 28: Shader Programming With Unity

Shader Core Inputs & Outputs

VertexShader

Runs for each Vertex

FragmentShader

Runs for each FragmentVertices Fragments

Colored FragmentsUniforms

Page 29: Shader Programming With Unity

Let’s get going withwriting Shaders…

Fire up Unity!The rest of this presentation is a hands-on shader writing workshop…