- Home /
How to Tell which part of surface shader belongs to which pass in deferred shading?
I want to understand how parts of surface shader corresponds to different parts of light pre-pass shader.
Light Pre-Pass (Unity3's Deferred Shading) consists of 3 parts,
1. G-Buffer Pass,
2. Lighting Pre-Pass,
3. Shading Pass.
So things like parallax, which is done per-object to UV should be done in pass #1 (G-Buffer) for correct normal, and pass #3 for correct Specular/Albedo Color. Part #2 (Lit Pre-Pass) is done per light, which means it needs to be located in surface shader so I can apply different lighting model, And Part #3 (Shading) needs to be located in a surface shader so I can apply processes to final color for one-time, per-pixel effect, such as damage blinking, and emmisive glow color.
I know that per-light pre-pass is done with custom lighting models, but So would somebody please help me to locate/modify in this surface shader where per-object G-Buffer pass is accumulated, and where per-pixel shading pass is located in a Unity 3 surface shader?
I am also confused by why the surf() outputs a normal, if it is the last part of rendering pipeline..
Thanks for any help!
Shader "test" { Properties { _Color("_Color", Color) = (1,0,0,1) }
SubShader
{
Tags
{
"Queue"="Geometry+0"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Back
ZWrite On
ZTest LEqual
CGPROGRAM
#pragma surface surf BlinnPhongEditor vertex:vert
#pragma target 2.0
struct EditorSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
};
inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
viewDir = normalize(viewDir);
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot (s.Normal, lightDir));
float nh = max (0, dot (s.Normal, h));
float3 spec = pow (nh, s.Specular*128.0) * s.Gloss;
half4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten * 2);
c.a = s.Alpha + _LightColor0.a * Luminance(spec) * atten;
return c;
}
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
{
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha + Luminance(spec);
return c;
}
struct Input {
float3 viewDir;
};
void vert (inout appdata_full v, out Input o) {
}
float4 _Color;
float _SpecPower;
float4 _RimColor;
float _RimPower;
void surf (Input IN, inout EditorSurfaceOutput o) {
o.Albedo = 0.0;
o.Normal = float3(0.0,0.0,1.0);
o.Emission = 0.0;
o.Gloss = 0.0;
o.Specular = 0.0;
o.Alpha = 1.0;
o.Albedo = _Color;
o.Normal = float3( 0.0, 0.0, 1.0);
o.Specular = _SpecPower.xxxx;
o.Gloss = Multiply0;
o.Alpha = 1.0;
}
ENDCG
}
Fallback "Diffuse"
}
Answer by robert · Oct 26, 2010 at 10:09 AM
So the story with pre-pass is that whatever you write in surf() will be used both to output to the g-buffer and will also be used to calculate final color in the final pass (a.k.a. forward pass, a.k.a. shading pass).
So if you write something into o.Normal and o.Specular, these values will actually be used to render the g-buffer.
Other values will be consumed by your lighting function (custom or not), which happens after the basic lighting was calculated and is available in the _LightBuffer.
If you're interested in digging into this more, you can add "#pragma debug" to your shader, then select the shader in the Project window and click "Open compiled shader" in the Inpsector. This will allow you the see the Cg code that gets generated from the surface shader code. For comparison you can also do it for the built-in shaders.
thanks! Yeah, I kind of starting to figure out last few days, but you made it so much clearer! And thanks for the cool tip! I'll try it out when I get to the office!
it was the order in which the functions were written in that through me off... stupid me..
Your answer
Follow this Question
Related Questions
How can I do a wrap diffuse in unity 3's defered lighting 0 Answers
Unity 3, Why custom Surface Shader (Transparent Reflective) doesn't render against background? 4 Answers
asign surface shader to material 2 Answers
Shader not working properly in Deferred render mode 0 Answers
Colour tinting layered textures with transparencies. 1 Answer