Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Ayanami0 · Oct 16, 2010 at 10:30 AM · shadersurfacedeferred

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"

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ayanami0 · Oct 28, 2010 at 12:59 AM 0
Share

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!

avatar image Ayanami0 · Oct 28, 2010 at 01:03 AM 0
Share

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

avatar image robert · Nov 05, 2010 at 12:37 PM 0
Share

I'm glad that helped :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges