Shader, TheShader, The
UnknownUnknown
at UnityWTB - by @doppioslash - 26/03/2016at UnityWTB - by @doppioslash - 26/03/2016
Hi, I'mHi, I'm
Claudia DoppioslashClaudia Doppioslash
@doppioslash@doppioslash
FunctionalFunctional
ProgrammerProgrammer
GameDevGameDev &&
Shader Programmer onShader Programmer on
CyberCook (iPad&VR)CyberCook (iPad&VR)
I writeI write
www.shadercat.comwww.shadercat.com
A site about Shaders, Rust,A site about Shaders, Rust,
and Graphics Programmingand Graphics Programming
@shadercat
Why make your ownWhy make your own
custom shader(s)?custom shader(s)?
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)?Why make your own custom shader(s)?
But!But!
Why make your own custom shader(s)?Why make your own custom shader(s)?
Journey's look was not achieved with a standard shader.
Why make your own custom shader(s)?Why make your own custom shader(s)?
Optimise for a specificOptimise for a specific
target platformtarget platform
(Like VR)(Like VR)
Why make your own custom shader(s)?Why make your own custom shader(s)?
more work
needs thought
needs time from the artists
needs to study those Graphics papers!
Drawbacks:Drawbacks:
So what is aSo what is a
shader?shader?
So what is a shader?So what is a shader?
"A piece of code that runs on"A piece of code that runs on
the GPU, takes vertices,the GPU, takes vertices,
textures, and other data,textures, and other data,
and returns pixel colours"and returns pixel colours"
So what is a shader?So what is a shader?
PipelinePipeline
scene -> objects with the same material ->scene -> objects with the same material ->
So what is a shader?So what is a shader?
PipelinePipeline
drawcall -> list of vertices & other info ->drawcall -> list of vertices & other info ->
So what is a shader?So what is a shader?
PipelinePipeline
vertex shader -> fragment + interpolated data ->vertex shader -> fragment + interpolated data ->
So what is a shader?So what is a shader?
PipelinePipeline
fragment shader (lighting) -> final pixel colorfragment shader (lighting) -> final pixel color
(An aside)(An aside)
Renderer TypesRenderer Types
Renderer TypesRenderer Types
-> (See )GDC 2016
Forward
Deferred
Forward+
Object Space
Renderer TypesRenderer Types
ForwardForward
Shading happens at each object's raster timeShading happens at each object's raster time
One pass for each lightOne pass for each light
Expensive when many lightsExpensive when many lights
But good support for complex lightingBut good support for complex lighting
Unity supports itUnity supports it
Renderer TypesRenderer Types
DeferredDeferred
Shading happens last, onceShading happens last, once
First rasterises the objects data into 2d buffersFirst rasterises the objects data into 2d buffers
Cheap when many lightsCheap when many lights
Limited support for complex lightingLimited support for complex lighting
Unity supports itUnity supports it
Unity Shaders BasicUnity Shaders Basic
Unity Shaders BasicUnity Shaders Basic
Each material has aEach material has a
shader assignedshader assigned
Unity Shaders BasicUnity Shaders Basic
The language is CgThe language is Cg
(NVIDIA) + ShaderLab(NVIDIA) + ShaderLab
Unity Shaders BasicUnity Shaders Basic
Pre-filled templatesPre-filled templates
Unity Shaders BasicUnity Shaders Basic
Surface Shaders give you:Surface Shaders give you:
easier normal mapping
helper functions
add passes for forward renderer
Occasionally it's in the way.Occasionally it's in the way.
But!But!
Unity Shaders BasicUnity Shaders Basic
Surface ShadersSurface Shaders
AnatomyAnatomy
Unity Shaders BasicUnity Shaders Basic
Properties:Properties:
We can also build custom inspector editors
Unity Shaders BasicUnity Shaders Basic
Properties Custom InspectorProperties Custom Inspector
Unity Shaders BasicUnity Shaders Basic
SubshaderSubshader
Unity Shaders BasicUnity Shaders Basic
StructsStructs
Unity Shaders BasicUnity Shaders Basic
StructsStructs
limited slots
data from the various phases has to go
through them
Unity Shaders BasicUnity Shaders Basic
Surf functionSurf function
Unity Shaders BasicUnity Shaders Basic
custom lighting functioncustom lighting function
use the naming convention
Lighting<name>
Unity Shaders BasicUnity Shaders Basic
(optional) custom vertex shader(optional) custom vertex shader
Unity Shaders BasicUnity Shaders Basic
The Standard shaderThe Standard shader
implementation can beimplementation can be downloadeddownloaded
Unity Shaders BasicUnity Shaders Basic
The include files are important toThe include files are important to
interoperate with Unity shadows/lightmapsinteroperate with Unity shadows/lightmaps
Pre-PBS principlesPre-PBS principles
Non-PBS principlesNon-PBS principles
In the olden days...In the olden days...
diffuse
specular
normal
texture BRDF lookup
fresnel
we lacked ALU power to do complex lightingwe lacked ALU power to do complex lighting
Parts of the shaderParts of the shader
Diffuse = dot(N,L)Diffuse = dot(N,L)
lacks a model of the surface behaviourslacks a model of the surface behaviours
it can give back more light than it receivesit can give back more light than it receives
dot product of the Normal and Light directionsdot product of the Normal and Light directions
Parts of the shaderParts of the shader
Diffuse = dot(N,L)Diffuse = dot(N,L)
Parts of the shaderParts of the shader
Normal MappingNormal Mapping
is still largely the sameis still largely the same
a way to add cheap detail with one texturea way to add cheap detail with one texture
Parts of the shaderParts of the shader
SpecularSpecular
depends just on direction and specular mapdepends just on direction and specular map
no concept of Roughnessno concept of Roughness
Specular Power :/Specular Power :/
PBS principlesPBS principles
PBS principlesPBS principles
LinearLinear
[ref: ]The importance of being linear
PBS principlesPBS principles
LinearLinear
[ref: ]Linear-Space Lighting (i.e. Gamma)
PBS principlesPBS principles
LinearLinear
PBS principlesPBS principles
LinearLinear
mobile toLinear
mobile toGamma
PBS principlesPBS principles
HDRHDR
PBS principlesPBS principles
HDRHDR
Many operators for Tone MappingMany operators for Tone Mapping
[Ref: ]filmicgames.com
Reinhard
plain linear &
exposure adjustment
PBS principlesPBS principles
The Rendering EquationThe Rendering Equation
[Ref: ]@levork
(using only 1000 most used English words)
PBS principlesPBS principles
The Rendering EquationThe Rendering Equation
[Ref: ]Raytracing Lecture
PBS principlesPBS principles
Microfacet theoryMicrofacet theory
Introduces a good explanation of RoughnessIntroduces a good explanation of Roughness
Surfaces have micro-facetsSurfaces have micro-facets
each reflect light according to its normaleach reflect light according to its normal
PBS principlesPBS principles
Microfacet theoryMicrofacet theory
[Ref: ]Moving Frostbite to PBR
In a picture
PBS principlesPBS principles
Energy conservationEnergy conservation
More intense highlight are smallerMore intense highlight are smaller
Less intense are largerLess intense are larger
Can't reflect more light than receivedCan't reflect more light than received
PBS principlesPBS principles
Area LightsArea Lights
Sadly no realtime support in UnitySadly no realtime support in Unity
More expensive but more realisticMore expensive but more realistic
PBS principlesPBS principles
More accurate BRDFsMore accurate BRDFs
Oren Nayar
Cook Torrance
Ashikhmin Shirley
and many more...
PBS principlesPBS principles
Indirect lightIndirect light
Prefiltered CubeMapsPrefiltered CubeMaps
Global IlluminationGlobal Illumination
Physically Based ProcessPhysically Based Process
Normal Process:Normal Process:
fail and no idea whyfail and no idea why
Physically Based ProcessPhysically Based Process
You've got nothing to compare with except
your own imagination.
And small errors can pile up unseen
PB Process:PB Process:
fail and know why/wherefail and know why/where
by comparing with refby comparing with ref
Physically Based ProcessPhysically Based Process
PB Process:PB Process:
same settings will work withsame settings will work with
different lighting conditionsdifferent lighting conditions
Physically Based ProcessPhysically Based Process
Different BRDFsDifferent BRDFs
for Different surfacesfor Different surfaces
LambertLambert
Different BRDFsDifferent BRDFs
Diffuse onlyDiffuse only
PlainestPlainest
Very cheapVery cheap
Ok for matte plastic surfacesOk for matte plastic surfaces
Oren NayarOren Nayar
Different BRDFsDifferent BRDFs
Adds a roughness modelAdds a roughness model
Diffuse onlyDiffuse only
Pretty decent for skin, clothPretty decent for skin, cloth
(even not using Subsurface(even not using Subsurface
Scattering)Scattering)
BlinnBlinn
Different BRDFsDifferent BRDFs
plasticplastic
with energy conservation it's a viable choicewith energy conservation it's a viable choice
Cook TorranceCook Torrance
Different BRDFsDifferent BRDFs
flexible, good all aroundflexible, good all around
there are better BRDFs for metalthere are better BRDFs for metal
A great specularA great specular
Ashikhmin ShirleyAshikhmin Shirley
Different BRDFsDifferent BRDFs
Excellent for metalExcellent for metal
AnisotropicAnisotropic
As an extra layer for wet surfacesAs an extra layer for wet surfaces
DisneyDisney
Different BRDFsDifferent BRDFs
onesize fits allonesize fits all
maybe easier for artists but not as flexiblemaybe easier for artists but not as flexible
they chose to keep the fewest parametersthey chose to keep the fewest parameters
possiblepossible
Drawbacks to choosingDrawbacks to choosing
unusual BRDFs:unusual BRDFs:
BRDFs window shoppingBRDFs window shopping
Indirect lighting calculation can be trickyIndirect lighting calculation can be tricky
But using one for a more common BRDF couldBut using one for a more common BRDF could
work okwork ok
Artist facing decisionsArtist facing decisions
Artist facing decisionsArtist facing decisions
more configurability meansmore configurability means
more confusing for the artistmore confusing for the artist
need to figure out whichneed to figure out which
parameters are less relevantparameters are less relevant
Unity Frame DebuggerUnity Frame Debugger
BRDF DemonstrationBRDF Demonstration
Let's shade some heads in Unity...Let's shade some heads in Unity...
ConclusionConclusion
Now you have the basics to tackleNow you have the basics to tackle
making your own custom lightingmaking your own custom lighting
system.system.
ReferencesReferences
Find them at:
www.shadercat.com/unity-wtb-talk-shader-the-unknown/
That's all folks!That's all folks!
@doppioslash @shadercat
See you on twitter :)

Shaders - Claudia Doppioslash - Unity With the Best