Shader In Unity - IGDA Denmarkigda.dk/wp-content/uploads/2013/10/Shaders.pdf · Overview •...
Transcript of Shader In Unity - IGDA Denmarkigda.dk/wp-content/uploads/2013/10/Shaders.pdf · Overview •...
Shader In UnityTim Cooper@stramit
Who Am I?
• Developer at Unity
• Used to make games!
• Bioshock 1 / 2
• Responsible for:
• Editor features
• Graphics features
Topics• Overview
• How do shaders work?• Vertex / Fragment
• Shaders in Unity• Render pipeline
• Material Usability• Making them nice for artists
Overview• Everything is rendered with shaders!
• Need to know HOW things get onto the screen
• What happens to verts / pixels?
• How does unity wrap all of this for you?
• How can we expose this to artists?
What is a Vertex• Data containing
• Position
• Color
• Normal
• Texture information
• Other (usage specific)
A Basic Render Pipeline• Start with a mesh
• Made up of vertices
• Vertices live in ‘local space’
A Basic Render Pipeline• Vertex shader
• Translate vertices
• Output verts in projection space
• Do per vertex calculations
• Pass data to pixel shader
A Basic Render Pipeline• Pixel shader
• Shade each triangle
A Basic Render Pipeline• Vertex interpolation
• Anything output from the vertex shader will have it’s values interpolated between vertices.
• Each vertex outputs a different color
• Color is interpolated over the triangle
A Basic Render Pipeline• Basic Lighting
• Uses a light direction and surface normal
• No specular
• Hard coded light direction
• Lambert Model
A Basic Render Pipeline• Texturing
• Need extra vertex data
• Maps each vertex texel in a texture
• In the range 0,1 (normalized)
• Each UV is interpolated
A Basic Render Pipeline• Blending
• How do output pixels from the pixel shader get written to the screen?
• They can
• Overwrite what is there
• Blend with what is there
• No nothing to what is here
A Basic Render Pipeline• Blending
• Syntax:
• Source: the value coming out of the pixel shader
• Destination: the existing value in the buffer
• Blend function format:
• Blend (srcmode) (dstmode)
• Blend (srcColor * srcMode) + (dstColor * dstMode)
A Basic Render Pipeline• Blending
• Overwrite what is there:
• Blend One Zero
• Output = One * srcColor + Zero * dstColor
A Basic Render Pipeline• Blending
• Blend with what is there:
• Alpha blending
• Blend SrcAlpha OneMinusSrcAlpha
• Output = output.a * srcColor + (1 - output.a) * dstColor
• 2x Multiplicative
• Blend DstColor SrcColor
• Output = DstColor * SrcColor + SrcColor * DstColor
A Basic Render Pipeline• Blending
• Available blend flags
• One, Zero, SrcColor, SrcAlpha, DstColor, DstAlpha, OneMinusSrcColor, OneMinusSrcAlpha, OneMinusDstColor, OneMinusDstAlpha
• Some hardware also supports separable alpha blending (alpha is blended differently from RGB).
• Blend SrcFactor DstFactor, SrcFactorA DstFactorA
A Basic Render Pipeline• Multiple Passes
• Most effects require multiple passes
• Multiple light rendering
• Doing multiple things
• But how do passes relate?
The Unity Render Pipeline• So now we know a bit about how rendering works...
• How can Unity help?
The Unity Render Pipeline• Unity provides a framework
• Automatic calculation of lights
• Automatic calculation of camera data
• Built in multi pass rendering techniques
• ‘Surface Shader’ syntax (does heavy lifting)
• Gets data from the scene into the rendering pipeline
• Provides the glue to hook everything together
The Unity Render Pipeline• Automatic calculation
• Unity provides a lot of the glue code for your shaders
• Matrices for spacial conversion (object2world, projection)
• Matrices for texture scaling and offset
• Requested vertex data (normals, color, uv)
• Light locations (depending on renderer configuration)
• Camera location
• Lots of other stuff
The Unity Render Pipeline• Helper functions:
• UnityCG.cginc - lots of helper functions
• Spacial conversions
• Normal map helpers
• Shadowing code
• Image space calculation
The Unity Render Pipeline• Helper functions:
• Lighting.cginc - lighting helper functions
• Lambert lighting model
• Blinn-Phone lighting model
• Forward / Deferred configuration
The Unity Render Pipeline• Rendering Paths - Are different ways to render objects
• Why do we need more than one?
• Different content
• Different hardware
• Unity Supports
• Vertex Lit
• Forward Rendering
• Deferred Lighting (Light Prepass)
The Unity Render Pipeline• Vertex Lit Path
• Old school rendering path
• Common on DX7 / DX8 games
• Lighting is calculated per vertex
• Interpolated over the triangle
The Unity Render Pipeline• Vertex Lit Path - In Unity
• Should probably be called ‘the simple path’
• This is because you don't really have to do vertex lighting
• Default shaders that use this path are vertex lit
• You can write your own shaders here that do per-pixel lighting
• Supports lightmaps
• No shadows, light cookies, or SH
• Fast on the CPU
The Unity Render Pipeline• Vertex Lit Path - In Unity
• 4 Lights per pass
• sequential
• sorted from brightest to dimmest
• directional / point lights only
The Unity Render Pipeline• Vertex Lit Path - In Unity
• Separate passes to handle
• Realtime lit objects
• Tags { "LightMode" = "Vertex" }
• Lightmapped objects
• Tags { "LightMode" = "VertexLM" }
• Tags { "LightMode" = "VertexLMRGBM" }
• You will need to tag your passes to get the expected shader data
The Unity Render Pipeline• Vertex Lit Path - In Unity
• Good for:
• Fast lightmapping only shaders
• Custom lighting shaders, e.g. that handle 2 light only
• Mobile / barebones shaders
The Unity Render Pipeline• Forward Rendering Path
• Your traditional rendering path
• Rendering an object performs full shading on it right away
• More passes for every (per pixel) light hitting the object
The Unity Render Pipeline• Forward Rendering Path - The Good
• Arbitrary lighting models
• Unlit surfaces
The Unity Render Pipeline• Forward Rendering Path - The Bad
• Not suitable for many lights
• We try to LOD away the less important lights
• Additional limitations to make it run fast
• Shadows only for directional lights by default
The Unity Render Pipeline
• Less important lights calculated in an approximate manner
• per-vertex
• projected onto a SH probe
The Unity Render Pipeline• Forward Rendering Path
• Base pass { "LightMode" = "ForwardBase" }
• Are there lightmaps?
• Lightmaps, ambient (in the lightmap)
• No lightmaps?
• Directional light, ambient, 4 point vertex lights, SH
The Unity Render Pipeline• Forward Rendering Path
• Additive pass { "LightMode" = "ForwardAdd" }
• All the other lights, one pass per light
• directional, point or spot
• Simple, as it doesn’t need to care about lightmaps, vertex lights, SH, blending realtime shadows with lightmaps, etc.
• But more shader variants due to more light types
The Unity Render Pipeline• Forward Rendering Path
• Important lights are normally rendered with additive pass
• If there is no add pass they are passed to the (base) shader as vertex lights (point or spot) or in SH (directional)
• When the object is lightmapped those lights are ignored, this is a known issue
The Unity Render Pipeline• Deferred Rendering Path
• Modern approach for real-time lighting heavy scenes
• Defers light calculation to decouple lighting complexity from scene complexity
• Render objects into G-buffer first
• Calculate lighting looking just at the G-buffer
• Render objects a second time, this time with albedo and modulate with light from the light buffer
The Unity Render Pipeline• Deferred Rendering Path
• Pass 1, generate the G-buffer
• { "LightMode" = "PrePassBase" }
• G-buffer: normals and specular (RGBA) + depth
The Unity Render Pipeline• Deferred Rendering Path
• Lighting Passes: Calculate raw lighting using G-Buffer
• Done by projecting shapes into the G-Buffer with lighting information.
• Point Light
The Unity Render Pipeline• Deferred Rendering Path
• Lighting Passes: Calculate raw lighting using G-Buffer
• Done by projecting shapes into the G-Buffer with lighting information.
• Spot Light
The Unity Render Pipeline• Deferred Rendering Path
• Final Pass
• Rerender each object using the lighting buffer calculation for illumination values.
• { "LightMode" = "PrePassFinal" }
The Unity Render Pipeline• Deferred Rendering Path
• The Good
• Tons of dynamic lights!
• Cheap (screen space cost)
• Small lights
• Partially occluded lights
The Unity Render Pipeline• Deferred Rendering Path
• The Bad
• Have to render things twice (up front)
• Bandwidth (uses multiple frame buffers)
• Very limited lighting model flexibility
• Shadow casting still needs to be calculated
The Unity Render Pipeline• Deferred Rendering Path
• The Ugly
• Doesn’t handle transparent objects - need to render them afterwards, in the ‘forward’ modes, possibly in multiple passes
• Same when using shaders without ‘PrePassBase’ and ‘PrePassFinal’
• Unity handles that, of course
The Unity Render Pipeline• Putting this all together
• Unity exposes a few different rendering methods to you.
• You can use or not use them, depending on what you want to do
• Most games use a combination of techniques
• Unique look
• Performance
Surface Shaders• Lots of talk about different passes and tags
• There has to be a simpler way!
• Surface Shaders!
Surface Shaders• Describe a surface
• Instead of defining• Light interaction
• You define Surface Properties• Emission
• Albedo
• Alpha
Surface Shaders• Unity generates all the ‘boring’ passes and lighting
model code.
• Surface Shader Examples
Surface Shaders• Basic Example
• Properties
• Textures
• Normal Maps
• Emission
• Specular
Material Keywords• Compile variants of your shader
• To do different things based on keywords
• Each line declares a set of defines
• Only one per line can be active at a time
• Make sure you enable one, otherwise Unity will pick one for you
Material Keywords• #pragma multi_compile AAA BBB
• #pragma multi_compile CCC
• two variants • AAA CCC
• BBB CCC
Material Keywords• #pragma multi_compile AAA BBB
• #pragma multi_compile CCC DDD
• Four variants • AAA CCC
• BBB CCC
• AAA DDD
• BBB DDD
Material Keywords• Example!
Material Keywords• Applications
• Switch the shaders in your scene via a keyword • completely change the look of your game
• Per material overrides
• One shader for: diffuse, normal maps, etc.
Material Property Drawers• You can write custom editors for shaders...
• But sometimes it’s too much
• Instead write property drawers!• New in Unity 4.3!
Material Property Drawers• Example!
Questions?@stramit