Shaders - Claudia Doppioslash - Unity With the Best

75
Shader, The Shader, The Unknown Unknown at UnityWTB - by @doppioslash - 26/03/2016 at UnityWTB - by @doppioslash - 26/03/2016

Transcript of Shaders - Claudia Doppioslash - Unity With the Best

Shader, TheShader, TheUnknownUnknown

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

Hi, I'mHi, I'm

Claudia DoppioslashClaudia Doppioslash

@doppioslash@doppioslash

FunctionalFunctionalProgrammerProgrammer

GameDevGameDev &&

Shader Programmer onShader Programmer onCyberCook (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 owncustom 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 specifictarget platformtarget platform

(Like VR)(Like VR)

Why make your own custom shader(s)?Why make your own custom shader(s)?

more workneeds thoughtneeds time from the artistsneeds to study those Graphics papers!

Drawbacks:Drawbacks:

So what is aSo what is ashader?shader?

So what is a shader?So what is a shader?

"A piece of code that runs on"A piece of code that runs onthe 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

ForwardDeferredForward+Object Space

Renderer TypesRenderer Types

ForwardForwardShading 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 ashader 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 mappinghelper functionsadd 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 ShadersAnatomyAnatomy

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 slotsdata from the various phases has to gothrough them

Unity Shaders BasicUnity Shaders Basic

Surf functionSurf function

Unity Shaders BasicUnity Shaders Basic

custom lighting functioncustom lighting function

use the naming conventionLighting<name>

Unity Shaders BasicUnity Shaders Basic

(optional) custom vertex shader(optional) custom vertex shader

Unity Shaders BasicUnity Shaders Basic

The Standard shaderThe Standard shaderimplementation can be implementation can be downloadeddownloaded

Unity Shaders BasicUnity Shaders Basic

The include files are important toThe include files are important tointeroperate 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...

diffusespecularnormaltexture BRDF lookupfresnel

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

Specular Specular

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

HDRHDRMany operators for Tone MappingMany operators for Tone Mapping

[Ref: ]filmicgames.com

Reinhardplain 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 PBRIn 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 NayarCook TorranceAshikhmin Shirleyand 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 exceptyour 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 withdifferent 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 SubsurfaceScattering)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 parameterspossiblepossible

Drawbacks to choosingDrawbacks to choosingunusual 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 couldwork okwork ok

Artist facing decisionsArtist facing decisions

Artist facing decisionsArtist facing decisions

more configurability meansmore configurability meansmore confusing for the artistmore confusing for the artist

need to figure out whichneed to figure out whichparameters 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 tacklemaking 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 :)