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
0
Question by Brantkings · Mar 19, 2021 at 10:50 PM · shadergraphicsocclusiondepth-buffer

How to make clip() work with Ambient Occlusion?

Hey so I've made a shader that dithers the geometry behind the player so they're always possible to be seen! But I think even the clipped geometry is writing in the depth buffer, causing a bug on this Ambient Occlusion effect. Is there any fix to this? Am I thinking this in the wrong way?

Here's an image with the buggy outcome:alt text

Here's the shader code:

 SubShader
     {
         Tags { "RenderType"="Opaque" "Queue" = "Geometry-2" }
         //Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
         
 
         LOD 200
         //Blend SrcAlpha OneMinusSrcAlpha
         Cull Back
         ZWrite On
         ZTest LEqual
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         void surf (Input IN, inout SurfaceOutputStandard o)
         {
             // Albedo comes from a texture tinted by color
             fixed4 ground = tex2D (_GroundTex, IN.worldPos.xz) * _Color;
             fixed4 wallA = tex2D (_WallTex, IN.worldPos.xy) * _Color;
             fixed4 wallB = tex2D (_WallTex, IN.worldPos.zy) * _Color;
 
             fixed4 c = ground * abs(IN.worldNormal.y) + wallA * abs(IN.worldNormal.z) + wallB * abs(IN.worldNormal.x);
             o.Albedo = c.rgb;
 
             //float ditherValue = GetDitherValue(IN.screenPos.xy / IN.screenPos.w, _DitheringTex, _DitheringTex_TexelSize);
             float ditherValue = GetDitherValue(IN.screenPos.xy / IN.screenPos.w, _DitheringTex, _DitheringTex_TexelSize);
             
             ClipParts(IN, ditherValue);
 
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = c.a;
         }
         ENDCG
         
         LOD 200
         //Blend SrcAlpha OneMinusSrcAlpha
         Cull Front
         ZWrite On
         ZTest LEqual
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         void surf (Input IN, inout SurfaceOutputStandard o)
         {
             //float ditherValue = GetDitherValue(IN.screenPos.xy / IN.screenPos.w, _DitheringTex, _DitheringTex_TexelSize);
             float ditherValue = GetDitherValue(IN.screenPos.xy / IN.screenPos.w, _DitheringTex, _DitheringTex_TexelSize);
 
             o.Albedo = fixed3(0.,0.,0.);
             ClipParts(IN, ditherValue);
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = 1.;
 
         }
         ENDCG
     }

I've removed some functions I call for the sake of making the question smaller.

Tell me if you need more info.

screenshotbugsmall.png (129.8 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Mar 21, 2021 at 02:57 AM

The depth texture is tied into the shadowcaster pass of a shader - in this case you are updating the visibility of the main shader without affecting the shadowcaster pass, so the depth texture will think the whole object is still being rendered. With surface shaders this is a fairly simple fix - just add the 'addshadow' parameter to the surface declaration;

 #pragma surface surf Standard fullforwardshadows addshadow

From what I can see this shader can also be done in a single pass rather than two;

 struct Input
 {
     ...
     float3 viewDir;
     ...
 };
 
 ...
 
 Cull Off
 
 CGPROGRAM
 #pragma surface surf Standard fullforwardshadows addshadow
  
 #pragma target 3.0
 
 void surf (Input IN, inout SurfaceOutputStandard o)
 {
     // Albedo comes from a texture tinted by color
     fixed4 ground = tex2D (_GroundTex, IN.worldPos.xz) * _Color;
     fixed4 wallA = tex2D (_WallTex, IN.worldPos.xy) * _Color;
     fixed4 wallB = tex2D (_WallTex, IN.worldPos.zy) * _Color;
  
     fixed4 c = ground * abs(IN.worldNormal.y) + wallA * abs(IN.worldNormal.z) + wallB * abs(IN.worldNormal.x);
     o.Albedo = c.rgb * (dot (IN.viewDir, IN.worldNormal) < 0);
  
     float ditherValue = GetDitherValue(IN.screenPos.xy / IN.screenPos.w, _DitheringTex, _DitheringTex_TexelSize);
              
     ClipParts(IN, ditherValue);
  
     o.Metallic = _Metallic;
     o.Smoothness = _Glossiness;
     o.Alpha = c.a;
 }
 ENDCG

The basic idea is to check if we are drawing a backface and just manipulate the parts that need to change on each side of the mesh (dot (viewDir, worldNormal) will be positive if we are drawing a backface).

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

184 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 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

Use camera depth texture to find distance from camera 0 Answers

UI masking in a jet fighter HUD 0 Answers

Gradient mapping in shader graph 1 Answer

Intersection Highlight Shader -1 Answers

Accessing rendertexture depthbuffer using shader 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