Shader, The Unknown

at UnityWTB - by @doppioslash - 26/03/2016

Hi, I'm

Claudia Doppioslash

@doppioslash

Functional Programmer

GameDev

&

Shader Programmer on CyberCook (iPad&VR)

A site about Shaders, Rust,

and Graphics Programming

@shadercat

Why make your own custom shader(s)?

Why make your own custom shader(s)?

Unity 5 has got a pretty decent default shader system

Why make your own custom shader(s)?

But!

Why make your own custom shader(s)?

Journey's look was not achieved with a standard shader.

Why make your own custom shader(s)?

Optimise for a specific target platform

(Like VR)

Why make your own custom shader(s)?

  • more work
  • needs thought
  • needs time from the artists
  • needs to study those Graphics papers!

Drawbacks:

So what is a shader?

So what is a shader?

"A piece of code that runs on the GPU, takes vertices, textures, and other data, and returns pixel colours"

So what is a shader?

Pipeline

scene -> objects with the same material ->

So what is a shader?

Pipeline

drawcall -> list of vertices & other info -> 

So what is a shader?

Pipeline

vertex shader -> fragment + interpolated data ->

So what is a shader?

Pipeline

fragment shader (lighting) -> final pixel color

(An aside)

Renderer Types

Renderer Types

-> (See GDC 2016)

  • Forward
  • Deferred
  • Forward+
  • Object Space

Renderer Types

Forward

Shading happens at each object's raster time

One pass for each light

Expensive when many lights

But good support for complex lighting

Unity supports it

Renderer Types

Deferred

Shading happens last, once

First rasterises the objects data into 2d buffers

Cheap when many lights

Limited support for complex lighting

Unity supports it

Unity Shaders Basic

Unity Shaders Basic

Each material has a shader assigned

Unity Shaders Basic

The language is Cg (NVIDIA) + ShaderLab

Unity Shaders Basic

Pre-filled templates

Unity Shaders Basic

Surface Shaders give you:

  • easier normal mapping
  • helper functions
  • add passes for forward renderer

Occasionally it's in the way.

But!

Unity Shaders Basic

Surface Shaders Anatomy

Unity Shaders Basic

Properties:

We can also build custom inspector editors

Unity Shaders Basic

Properties Custom Inspector

Unity Shaders Basic

Subshader

Unity Shaders Basic

Structs

Unity Shaders Basic

Structs

  • limited slots
  • data from the various phases has to go through them

Unity Shaders Basic

Surf function

Unity Shaders Basic

custom lighting function

use the naming convention Lighting<name>

Unity Shaders Basic

(optional) custom vertex shader

Unity Shaders Basic

The Standard shader implementation can be downloaded

Unity Shaders Basic

The include files are important to interoperate with Unity shadows/lightmaps

Pre-PBS principles

Non-PBS principles

In the olden days...

  • diffuse
  • specular
  • normal
  • texture BRDF lookup
  • fresnel

we lacked ALU power to do complex lighting

Parts of the shader

Diffuse = dot(N,L)

lacks a model of the surface behaviours

it can give back more light than it receives

dot product of the Normal and Light directions

Parts of the shader

Diffuse = dot(N,L)

Parts of the shader

Normal Mapping

is still largely the same

a way to add cheap detail with one texture

Parts of the shader

Specular 

depends just on direction and specular map

no concept of Roughness

Specular Power :/

PBS principles

PBS principles

Linear

PBS principles

Linear

PBS principles

Linear

PBS principles

Linear

mobile toLinear

mobile toGamma

PBS principles

HDR

PBS principles

HDR

Many operators for Tone Mapping

[Ref: filmicgames.com ]

Reinhard

plain linear &

exposure adjustment

PBS principles

The Rendering Equation

[Ref: @levork ]

(using only 1000 most used English words)

PBS principles

The Rendering Equation

PBS principles

Microfacet theory

Introduces a good explanation of Roughness

Surfaces have  micro-facets

each reflect light according to its normal

PBS principles

Microfacet theory

In a picture

PBS principles

Energy conservation

More intense highlight are smaller

Less intense are larger

Can't reflect more light than received

PBS principles

Area Lights

Sadly no realtime support in Unity

More expensive but more realistic

PBS principles

More accurate BRDFs

  • Oren Nayar
  • Cook Torrance
  • Ashikhmin Shirley
  • and many more...

PBS principles

Indirect light

Prefiltered CubeMaps

Global Illumination

Physically Based Process

Normal Process:

fail and no idea why

Physically Based Process

You've got nothing to compare with except your own imagination.

And small errors can pile up unseen

PB Process:

fail and know why/where

by comparing with ref

Physically Based Process

PB Process:

same settings will work with different lighting conditions

Physically Based Process

Different BRDFs

for Different surfaces

Lambert

Different BRDFs

Diffuse only

Plainest

Very cheap

Ok for matte plastic surfaces

Oren Nayar

Different BRDFs

Adds a roughness model

Diffuse only

Pretty decent for skin, cloth

(even not using Subsurface Scattering)

Blinn

Different BRDFs

plastic

with energy conservation it's a viable choice

Cook Torrance

Different BRDFs

flexible, good all around

there are better BRDFs for metal

A great specular

Ashikhmin Shirley

Different BRDFs

Excellent for metal

Anisotropic

As an extra layer for wet surfaces

Disney

Different BRDFs

onesize fits all

maybe easier for artists but not as flexible

they chose to keep the fewest parameters possible

Drawbacks to choosing unusual BRDFs:

BRDFs window shopping

Indirect lighting calculation can be tricky

But using one for a more common BRDF could work ok

Artist facing decisions

Artist facing decisions

more configurability means more confusing for the artist

need to figure out which parameters are less relevant

Unity Frame Debugger

BRDF Demonstration

Let's shade some heads in Unity...

Conclusion

Now you have the basics to tackle making your own custom lighting system.

References

That's all folks!

@doppioslash

@shadercat

See you on twitter :)