Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Shaders - Claudia Doppioslash - Unity With the Best

770 views

Published on

Shader programming is one of the things that most influences how good your game will look, yet it's perceived as a black art, hidden away and feared.

In this talk, Claudia described:

1. How shader programming works
2. How Unity lets you take almost full control of the shader subsystem
3. What you can achieve with that control
4. How to implement a custom Physically Based Lighting system and the logic behind every choice

Published in: Technology
  • Be the first to comment

Shaders - Claudia Doppioslash - Unity With the Best

  1. 1. Shader, TheShader, The UnknownUnknown at UnityWTB - by @doppioslash - 26/03/2016at UnityWTB - by @doppioslash - 26/03/2016
  2. 2. Hi, I'mHi, I'm Claudia DoppioslashClaudia Doppioslash @doppioslash@doppioslash FunctionalFunctional ProgrammerProgrammer GameDevGameDev &&
  3. 3. Shader Programmer onShader Programmer on CyberCook (iPad&VR)CyberCook (iPad&VR)
  4. 4. I writeI write www.shadercat.comwww.shadercat.com A site about Shaders, Rust,A site about Shaders, Rust, and Graphics Programmingand Graphics Programming @shadercat
  5. 5. Why make your ownWhy make your own custom shader(s)?custom shader(s)?
  6. 6. Why make your own custom shader(s)?Why make your own custom shader(s)? Unity 5 has got a pretty decent default shader system
  7. 7. Why make your own custom shader(s)?Why make your own custom shader(s)? But!But!
  8. 8. Why make your own custom shader(s)?Why make your own custom shader(s)? Journey's look was not achieved with a standard shader.
  9. 9. 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)
  10. 10. 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:
  11. 11. So what is aSo what is a shader?shader?
  12. 12. 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"
  13. 13. So what is a shader?So what is a shader? PipelinePipeline scene -> objects with the same material ->scene -> objects with the same material ->
  14. 14. So what is a shader?So what is a shader? PipelinePipeline drawcall -> list of vertices & other info ->drawcall -> list of vertices & other info ->
  15. 15. So what is a shader?So what is a shader? PipelinePipeline vertex shader -> fragment + interpolated data ->vertex shader -> fragment + interpolated data ->
  16. 16. So what is a shader?So what is a shader? PipelinePipeline fragment shader (lighting) -> final pixel colorfragment shader (lighting) -> final pixel color
  17. 17. (An aside)(An aside) Renderer TypesRenderer Types
  18. 18. Renderer TypesRenderer Types -> (See )GDC 2016 Forward Deferred Forward+ Object Space
  19. 19. 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
  20. 20. 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
  21. 21. Unity Shaders BasicUnity Shaders Basic
  22. 22. Unity Shaders BasicUnity Shaders Basic Each material has aEach material has a shader assignedshader assigned
  23. 23. Unity Shaders BasicUnity Shaders Basic The language is CgThe language is Cg (NVIDIA) + ShaderLab(NVIDIA) + ShaderLab
  24. 24. Unity Shaders BasicUnity Shaders Basic Pre-filled templatesPre-filled templates
  25. 25. 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!
  26. 26. Unity Shaders BasicUnity Shaders Basic Surface ShadersSurface Shaders AnatomyAnatomy
  27. 27. Unity Shaders BasicUnity Shaders Basic Properties:Properties: We can also build custom inspector editors
  28. 28. Unity Shaders BasicUnity Shaders Basic Properties Custom InspectorProperties Custom Inspector
  29. 29. Unity Shaders BasicUnity Shaders Basic SubshaderSubshader
  30. 30. Unity Shaders BasicUnity Shaders Basic StructsStructs
  31. 31. Unity Shaders BasicUnity Shaders Basic StructsStructs limited slots data from the various phases has to go through them
  32. 32. Unity Shaders BasicUnity Shaders Basic Surf functionSurf function
  33. 33. Unity Shaders BasicUnity Shaders Basic custom lighting functioncustom lighting function use the naming convention Lighting<name>
  34. 34. Unity Shaders BasicUnity Shaders Basic (optional) custom vertex shader(optional) custom vertex shader
  35. 35. Unity Shaders BasicUnity Shaders Basic The Standard shaderThe Standard shader implementation can beimplementation can be downloadeddownloaded
  36. 36. 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
  37. 37. Pre-PBS principlesPre-PBS principles
  38. 38. 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
  39. 39. 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
  40. 40. Parts of the shaderParts of the shader Diffuse = dot(N,L)Diffuse = dot(N,L)
  41. 41. 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
  42. 42. 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 :/
  43. 43. PBS principlesPBS principles
  44. 44. PBS principlesPBS principles LinearLinear [ref: ]The importance of being linear
  45. 45. PBS principlesPBS principles LinearLinear [ref: ]Linear-Space Lighting (i.e. Gamma)
  46. 46. PBS principlesPBS principles LinearLinear
  47. 47. PBS principlesPBS principles LinearLinear mobile toLinear mobile toGamma
  48. 48. PBS principlesPBS principles HDRHDR
  49. 49. PBS principlesPBS principles HDRHDR Many operators for Tone MappingMany operators for Tone Mapping [Ref: ]filmicgames.com Reinhard plain linear & exposure adjustment
  50. 50. PBS principlesPBS principles The Rendering EquationThe Rendering Equation [Ref: ]@levork (using only 1000 most used English words)
  51. 51. PBS principlesPBS principles The Rendering EquationThe Rendering Equation [Ref: ]Raytracing Lecture
  52. 52. 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
  53. 53. PBS principlesPBS principles Microfacet theoryMicrofacet theory [Ref: ]Moving Frostbite to PBR In a picture
  54. 54. 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
  55. 55. 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
  56. 56. PBS principlesPBS principles More accurate BRDFsMore accurate BRDFs Oren Nayar Cook Torrance Ashikhmin Shirley and many more...
  57. 57. PBS principlesPBS principles Indirect lightIndirect light Prefiltered CubeMapsPrefiltered CubeMaps Global IlluminationGlobal Illumination
  58. 58. Physically Based ProcessPhysically Based Process
  59. 59. 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
  60. 60. 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
  61. 61. PB Process:PB Process: same settings will work withsame settings will work with different lighting conditionsdifferent lighting conditions Physically Based ProcessPhysically Based Process
  62. 62. Different BRDFsDifferent BRDFs for Different surfacesfor Different surfaces
  63. 63. LambertLambert Different BRDFsDifferent BRDFs Diffuse onlyDiffuse only PlainestPlainest Very cheapVery cheap Ok for matte plastic surfacesOk for matte plastic surfaces
  64. 64. 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)
  65. 65. BlinnBlinn Different BRDFsDifferent BRDFs plasticplastic with energy conservation it's a viable choicewith energy conservation it's a viable choice
  66. 66. 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
  67. 67. 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
  68. 68. 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
  69. 69. 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
  70. 70. Artist facing decisionsArtist facing decisions
  71. 71. 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
  72. 72. Unity Frame DebuggerUnity Frame Debugger
  73. 73. BRDF DemonstrationBRDF Demonstration Let's shade some heads in Unity...Let's shade some heads in Unity...
  74. 74. 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.
  75. 75. 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 :)

×