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 daterre · May 27, 2014 at 09:15 AM · shadersshadowrenderqueue

Shadows have a mind of their own

I'm trying to make a shader that receives shadows and selectively outputs transparency based on the texture.

Even though my shader works, when I output transparent fragments Unity is still applying a translucent shadow in 3d space, and is using the geometry to occlude any background shadows based on the current viewing angle. This can be seen in the attached screenshot. Note the following:

  • Even though the female figure is transparent (the floor and cube can be seen behind it), there is a shadow being applied to the model.

  • The male character's shadow and the cube's shadow are occluded by her hip and hand, respectively.

alt text

Here's the subshader header. The contents of the shader don't matter because even if I change the shader to simply output fixed4(0,0,0,0) I still get this behavior. If I change the queue to Transparent then the behavior stops but causes SHADOW_ATTENUATION to always return 1, making my shader useless.

 SubShader {
    Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
       Pass {
          ZWrite Off
          Blend SrcAlpha OneMinusSrcAlpha 


Now, I can live with the shadow occlusion (even though I would like to understand why it happens) but the extra automatic pass to apply shadows to my model is something I would like to remove, as it feels utterly pointless. Can I indicate somehow to Unity that I am handling shadows myself and can you please leave my poor mesh alone?

Thanks in advance for any advice.

screen shot 2014-05-27 at 11.53.58 am.png (52.6 kB)
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 daterre · May 27, 2014 at 09:28 AM 0
Share

Another important observation is that the ghost shadow is only rendered against $$anonymous$$eshRenderer's marked as "receive shadow". In this screenshot the sand-colored plane is marked as 'receive shadows' and the water-colored plane is not. alt text

screen shot 2014-05-27 at 12.25.33 pm.png (19.0 kB)
avatar image daterre · May 27, 2014 at 07:59 PM 0
Share

So to clarify my last comment, a 2d surface marked with "receive shadows" causes a 3d shadow overlay to be rendered on top of it, regardless of the output of the shader.

1 Reply

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

Answer by MakeCodeNow · May 27, 2014 at 10:47 PM

This answer is partial, but I hope it puts you on the right path.

The basic idea of how realtime shadows work is that each shadow casting light renders a depth buffer from the "view" of the light. Because it's a depth buffer, only opaque objects can go into it (i.e. only opaque objects can cast shadows). During normal rendering, the shader takes the current pixel position, and computes the depth of that position from the point of view of the light, and then checks the depth of that position (in light space) with the corresponding shadow depth buffer pixel. If the current pixel depth is greater than the shadow depth buffer pixel, then the pixel is shadowed. If it's less, then it's not shadowed.

While only opaque objects can cast shadows, it should be possible for transparent objects to read shadows. That means that putting your object into the transparent pass (which is where it should go because it is outputting transparency) should be able to get shadow attenuation values other than one. The fact that you always get 1 is the central problem, and what you should try to figure out. If you are using deferred rendering, you should try forward rendering. That might force Unity to do the right thing for transparent shadows. If that doesn't work, do some research on transparent shadow receivers in Unity. You should be able to find a solution (I hope).

Comment
Add comment · Show 4 · 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 daterre · May 28, 2014 at 02:31 PM 0
Share

Thank you for the detailed explanation! So if I understand correctly, the shadow depth buffer is translated to camera space, and when a 'receive shadow' surface is rendered, any shadow pixel with a lower depth value is rendered as a shadow on the target surface. This would explain both the ghost shadow (applied to a nearer object) and the occlusion of background shadows (no shadow visible from the current angle).

I guess then the question is whether it is possible to compute and sample realtime shadows in light space, not in camera space. This would allow me to avoid occlusion based on camera space translation. Obviously at a performance cost but since my art is $$anonymous$$imalistic and tightly controlled this won't be a problem.

(Using the Transparent queue means the models won't cast shadows. The art style I am going for requires self shadowing, so I do not want to bypass the shadow caster. SHADOW_ATTENUATION returns 1 for the self shadow, which is non-existent - I hadn't realized that at the time of my original post.)

avatar image MakeCodeNow · May 28, 2014 at 07:26 PM 0
Share

You can do the projection from light to camera or vice versa. I forget which approach is the most common. In any event, because you're doing such a specialized process and want self shadowing, I would recommend you render a custom shadow map per character that only handles self shadowing. Then you lookup into this shadow map as well as the global shadow map to get the effect you're looking for.

avatar image daterre · Jun 03, 2014 at 12:19 PM 0
Share

That's a good point and I will consider it. An orthographic camera rendering the characters with a depth map could simulate a directional light. I'm shaky on the projection math though...

avatar image daterre · May 13, 2015 at 10:25 AM 0
Share

Some progress being made here: http://forum.unity3d.com/threads/custom-depth-buffer-rendering.323279/

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

21 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

Related Questions

Having problems with unlit shaders casting shadows 0 Answers

Tinted multiply shader? 0 Answers

Standard Shader Still Visible through Stencil Shader 0 Answers

Shaders not working 3 Answers

depth mask braking lighting 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