x


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"
}
more ▼

asked Oct 16 '10 at 10:30 AM

Ayanami0 gravatar image

Ayanami0
13 3 3 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Oct 26 '10 at 10:09 AM

robert gravatar image

robert ♦
1.1k 3 7 27

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!

Oct 28 '10 at 12:59 AM Ayanami0

it was the order in which the functions were written in that through me off... stupid me..

Oct 28 '10 at 01:03 AM Ayanami0

I'm glad that helped :)

Nov 05 '10 at 12:37 PM robert ♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1660
x181
x59
x33
x1

asked: Oct 16 '10 at 10:30 AM

Seen: 2030 times

Last Updated: Oct 16 '10 at 05:19 PM