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 /
avatar image
2
Question by rtrimble · May 20, 2016 at 05:07 AM · shaderrenderingmobileshadowstencil

How to implement stencil shadows/shadow volumes

Hey all,

For the mobile game we are developing, we would like to implement shadow volumes to allow us to have point lights that cast shadows. Right now I have silhouette detection and shadow volume extrusion working. A separate GameObject is created for each shadow volume - I want to eventually do this all on the GPU, but this seemed like the most straight-forward way to get started.

This has been new territory for me and has left me with a few questions.

  1. When "rendering" the shadow volume mesh to perform the stencil operations, do I render this at the same time as the scene, or do I have to render it after the full scene renders?

  2. Once the stencil buffer is populated, how do I actually render the shadows? Do I have to use render textures? Do I apply some type of shader to the camera? Something else? Thank you for any help!

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 Bunny83 · May 20, 2016 at 06:53 AM

The order is usually as follows:

  • Render the scene normally

  • Render the shadow geometry with your shaddow shader which usually has two passes if you do "Carmack's reverse".

  • Render the "shadow" by rendering a fullscreen quad with your shadow colour with a special shader that does a stencil test.

I've once implemented stencil shadows in a C++ directX project and it worked pretty well.

ps: If you have trouble understanding how the depth fail algorithm works, i suggest reading this amazing letter of John Carmack where he describes the process of how he discovered it.

edit

The actual rendering can be done using one camera and use a queue offset in your shaders to make sure they are rendered after each other. The fullscreen quad could be rendered the same way or by using the GL class inside OnPostRender in a script attached to the camera. You can use pretty much the sample script that is shown in the docs.

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 Bunny83 · May 20, 2016 at 07:21 AM 0
Share

ps: $$anonymous$$eep in $$anonymous$$d that your shadow geometry need to be "capped" on both ends. The easiest way is to split the original object into two parts across the shadow edge and use both halfs as caps. So you basically use the original mesh but extruding it at the edge by inserting some quads between the original edge loops of the caps.

Alternatively you could construct caps out of the edge information, but that's usually much more difficult and requires more performance.

If you're using shadow volumes just for 2d / 2.5d games you probably don't need caps as those are usually offscreen.

avatar image rtrimble · May 23, 2016 at 02:39 PM 0
Share

I still have a few kinks to work out, but I got it working overall.

$$anonymous$$y biggest hurdle was figuring out how to render it all to the screen which I accomplished through OnRenderImage which I'm surprised worked since I've seen other people had issue with stencil testing when doing that. Is there an advantage to one or the other?

Anyway, this helped immensely. Thank you!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shadow Support in Custom Shader 0 Answers

Shadows don't render on materials with rendering mode set to "fade" or "transparent" 1 Answer

Shadows being received by non-shadow collectors that are depth culled by a shadow collector. 1 Answer

Render a sprite only when it's on the parent but not other sprites. 0 Answers

Stencil on iOS 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