Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by pantaryl · Sep 16, 2015 at 03:20 AM · shadershadowshaderlabstencil

Shadowcaster Fragment Shader Discard

Hello all –

I'm currently working through some bugs with one of my shaders. This shader draws meshes through windows in space. If the window is located behind the object, I don't want the mesh to render.

My first render in my entire renderpass renders the Z values of all of the portals into a depth buffer. I think sample that depth buffer to determine if the fragment being rendered is located in front or behind the portal depth.

Unfortunately, this seems to cull the entire shadow instead of just the area covered by the portal. I know this same logic works in a non-shadow shader, so I am sure that I am just not knowledgeable in this subject.

Here is the shader. Thank you for your help! // Pass to render object as a shadow caster Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" }

             Fog {Mode Off}
             ZWrite On ZTest LEqual Cull Off
             Offset 0, 0
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_shadowcaster
             #pragma fragmentoption ARB_precision_hint_fastest
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
             
             sampler2D _TempDepthTex;
 
             struct v2f { 
                 V2F_SHADOW_CASTER;
                 float4 screenPos : TEXCOORD0;
                 float4 posWorld  : TEXCOORD2;
                 float2 depth     : TEXCOORD1;
             };
 
             v2f vert( appdata_base v )
             {
                 v2f o;
                 TRANSFER_SHADOW_CASTER(o);
                 o.posWorld  = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.depth     = o.posWorld.zw;
                 o.screenPos = ComputeScreenPos(o.posWorld);
                 
                 return o;
             }
 
             float4 frag( v2f i ) : COLOR
             {
                 i.screenPos.xy /= i.screenPos.w;
                 float4 portalDepthXy  = tex2D(_TempDepthTex, i.screenPos.xy);
                 float  portalDepth    = portalDepthXy.x;
                 float  newDepth       = i.depth.x / i.depth.y;
                 if (newDepth > portalDepth)
                 {
                     discard;
                 }
                 
                 SHADOW_CASTER_FRAGMENT(i)
             }
             ENDCG
         } //Pass
Comment
Add comment · Show 2
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 Trejkaz · Jan 03, 2019 at 10:04 PM 0
Share

Did you ever solve this? I think I'm hitting a similar issue.

avatar image FabioZocklabs · Jul 11, 2019 at 10:05 PM 0
Share

I think, I got a similar problem; did you already find a solution?

0 Replies

· Add your reply
  • Sort: 

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

30 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

Related Questions

Stencil shader blocking shadows being drawn 'underneath' 2 Answers

What is the best approach to mask players vision? 1 Answer

Half Lambert / Wrap Diffuse Shader 0 Answers

Shaderlab copy a texture value 0 Answers

Trying to write a 2d mask shader with stencil 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