Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by JSNuttall · Aug 04, 2020 at 07:32 AM · shadershadersmaskdepthmasking

Depth Mask Shader Not Working As Intended

Okay, so for a project I'm working on I wanted to have one room that looks like it's in space, where if the player looks around all they see (aside from objects inside the room) is the starry sky—except through the passages to another room. To accomplish this, I thought maybe I could use two cameras, the back camera in an enclosure with just the starry background, and the front, main camera showing the player's surroundings, and then I'd give the exterior walls of the room a depth mask shader.

Basically, like this: note that the skybox is visible through the circular opening in the wall, which is exactly what I want. (Eventually there's going to be a connection to another room or corridor there, so it won't just be open to the skybox, but that's beside the point for now. The blue circle is intentional; that's there to indicate to the player the boundaries of where they can walk.)

alt text

I'm having trouble getting this to work as intended, though. The instructions on this page do seem to work:

http://wiki.unity3d.com/index.php/DepthMask

....as long as I put that "SetRenderQueue" script on every object outside the astronomy room. The problem is, well, that's a lot of objects, and it seems like a less-than-ideal solution to have to put a script on every single object in the entire scene except the handful of objects in this one room.

That same page does have a suggestion about what to do if you want to mask a lot of objects:

"In most common scenarios, you will only need a few objects to be masked. If, however, you find that you have more objects that need to be masked than objects that do not, you might find it useful to attach the SetRenderQueue script to the masks themselves, and set their queues to 2090 (just before regular geometry). Objects that you don't want masked should have their queues set to 2080 (just before the mask shader)."​

So I tried that, and I got... well, I got what's shown in the above image. Looks great... until I turn around. And then I see this:

alt text

All that geometry there, that tall rectangular column and the smaller corridor, is outside the room with the Depth Mask shader on its walls, and therefore it should be masked out... or so I thought. But it's not. I don't want all that stuff to be visible from inside the room; I just want the starry sky to show there.

I did a lot of web searching to see if I could find a solution to this, and I found a number of other depth mask shaders online... but they all had the same issue; they didn't mask out the objects outside the room. Here are some of the other sites I found with shaders that I tried, with similar results:

https://github.com/doomlaser/DepthMask-Unity-Shader

http://www.pixelplacement.com/2011/02/15/masking-in-unity/

https://forum.unity.com/threads/solved-mask-shader-with-ztest-always.519094/

I admit I don't know much about shaders in Unity yet; I'm trying to learn, but it seems like there's a lot to absorb there. But if anyone could point me in the right direction and let me know how I could create a depth mask that masks out the objects outside this room, I'd really appreciate it. (Or am I taking the wrong approach entirely, and is there a better way to do this that doesn't involve depth masks at all?)

Thanks.

(I posted this question earlier today in the Unity forum before finding Unity Answers and realizing it was probably more appropriate here... sorry for the double post; I'm relatively new to Unity and still trying to find my way around.)

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
0

Answer by JSNuttall · Aug 04, 2020 at 07:47 AM

And after struggling with this for hours, I stumble across a solution right after posting this... so I guess I'll post the answer here for the benefit of anyone else who runs into a similar problem.

As it turns out, there was nothing wrong with the shader itself (or in fact with any of the five different shaders I tried)... the problem was with the camera. More specifically, the camera's rendering path, which was set to Deferred—apparently the Deferred rendering path has some issues with masking, but once I set the rendering path to Forward the depth shader started working just the way I'd wanted it to.

I still don't really know what I'm doing and don't fully understand the depth shader or the rendering paths, so if anyone else knows more about it and feels like adding a better explanation (or correcting something I may have stated incorrectly), feel free, but at least that seems to have solved my problem.

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
avatar image
0

Answer by unity_Mah2fCQ9gALMSw · Feb 15, 2021 at 11:07 AM

@JSNuttall The depth mask shader and the SetRenderQueue.cs script are using out of date API's from 2013, would you be able to add a bit more detail on how you got this working? Im useing URP and I dont have an option of of changeing rendering path to or from deferred.

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

210 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Adapt alpha mask shader to gradual alpha increase with distance to object center 0 Answers

Using an object as a mask for another object (using Spine) 0 Answers

Mask Shader and Shadow 2 Answers

How to make effect between scenes with UI Mask or Shader 0 Answers

Merge 2 shaders 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