Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 callebo_FK · Dec 18, 2017 at 10:42 AM · shaderlightingcustomssaoao

SSAO showing through objects with custom shader

Hello,

My problem is that the SSAO from objects is rendered in front of objects using my custom shader. I can set the Render Queue of these custom objects to Transparent to stop the SSAO being rendered on top off them from other objects, but this also stops the SSAO rendering from the object itself.

alt text ^ Current setup. Object behind is rendering its SSAO on top of front object.

alt text ^ If I set front objects Render Queue to Transparent, but no SSAO on front object either. Turning off SSAO gives similar results.


Basically, I want SSAO to be rendered on the object but not from objects behind it, so just turning off SSAO is not the way to go. Here's my shader:

 Shader "Custom/FoliageDoublesided" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _NormalMap("Normalmap", 2D) = "bump" {}
         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
     }
     SubShader {
         Tags { 
             "RenderType"="TransparentCutout" 
             "Queue"="AlphaTest"
             "IgnoreProjector"="True"        
             }
         LOD 200
         Cull Off
 
         CGPROGRAM
         #pragma surface surf TwoSided fullforwardshadows alphatest:_Cutoff
         #pragma target 3.0
 
         sampler2D _MainTex;
         sampler2D _NormalMap;
 
         struct Input {
             float2 uv_MainTex;
         };
 
         fixed4 _Color;
 
          #pragma instancing_options assumeuniformscaling
         UNITY_INSTANCING_CBUFFER_START(Props)
         UNITY_INSTANCING_CBUFFER_END
 
         half4 LightingTwoSided(SurfaceOutput s, half3 lightDir, half atten) {
              half NdotL = dot (s.Normal, lightDir);
              half INdotL = dot (-s.Normal, lightDir);
              half diff = (NdotL < 0) ? INdotL : NdotL;
 
              half4 c;
              c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten) ;
              c.a = s.Alpha;
 
              return c;
          }
 
         void surf (Input IN, inout SurfaceOutput o) {
             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
             o.Albedo = c.rgb;
             o.Normal = UnpackNormal (tex2D (_NormalMap, IN.uv_MainTex));
             o.Specular = 1;
 
             o.Alpha = c.a;
         }
         ENDCG
     }
     FallBack "StandardDoubleSided"
 }

Any help is appreciated

ao.png (130.3 kB)
ao-nop.png (121.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by callebo_FK · Dec 19, 2017 at 02:48 PM

It seems that as soon as you use any lighting other than Standard in your shader, ambient occlusion (from the Post Processing Stack) is rendered on top of your mesh. Even if you invoke the standard lighting function, it still messes up the AO.

What magic inside "#pragma surface surf Standard" am I missing? How can I use my own lighting functions and have the AO work properly?

Comment
Add comment · Show 2 · 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 FrenchToastFella · Nov 07, 2018 at 11:55 PM 0
Share

Did you ever figure this out? I'm having some troubles myself with this kind of stuff...

avatar image chadfranklin47 FrenchToastFella · Dec 05, 2018 at 11:56 PM 0
Share

Same here. Any update?

avatar image
0

Answer by SunnyChow · Dec 06, 2018 at 07:57 AM

i see it's a cutoff shader. i think adding "FallBack "Standard"" should work. if not, you can also try adding a shadowcaster pass:

          Pass {
         Name "Caster"
         Tags { "LightMode" = "ShadowCaster" }
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma target 2.0
             #pragma multi_compile_shadowcaster
             #pragma multi_compile_instancing // allow instanced shadow pass for most of the shaders
             #include "UnityCG.cginc"
 
             struct v2f {
                 V2F_SHADOW_CASTER;
                 float2  uv : TEXCOORD1;
                 UNITY_VERTEX_OUTPUT_STEREO
             };
 
             uniform float4 _MainTex_ST;
 
             v2f vert( appdata_base v )
             {
                 v2f o;
                 UNITY_SETUP_INSTANCE_ID(v);
                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                 return o;
             }
 
             uniform sampler2D _MainTex;
             uniform fixed _Cutoff;
             uniform fixed4 _Color;
 
             float4 frag( v2f i ) : SV_Target
             {
                 fixed4 texcol = tex2D( _MainTex, i.uv );
                 clip( texcol.a*_Color.a - _Cutoff );
 
                 SHADOW_CASTER_FRAGMENT(i)
             }
             ENDCG
 
                 
 
         }

PS: i haven't fully tested so not sure it will. but i hope it gives you some direction

Comment
Add comment · 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

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

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Recieving Shadow on Custom Shader 1 Answer

Passing arrays of variable length to shader. 0 Answers

Shader light - Problem 0 Answers

Unity 5 Light problem 2 Answers

Point Light Behave different from editor and IOS device 0 Answers


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