Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 saldavonschwartz · Sep 27, 2016 at 06:37 AM · unity 5shadershadersshader programmingshaderlab

Unity equivalent to glDepthMask or alternative?

I'm trying to achieve the following setup:

alt text

  • 2 cubes. In the image attached they are the green and pink cube. They are both rendered culling their front faces, so that they are visible if a camera stands inside of them.

  • The 2 cubes intersect, as show in the image.

  • The pink cube has an opening (door, window, portal, whathaveyou) thru which a camea facing in the direction of the opening, can see the green cube.

  • Here is the interesting part: if the camera is not looking thru the opening however, I want the green cube face on the right side of the image to not be visible.

The culling is easy: just say Cull Front in a surface shader. The part that I'm not sure how to pull of in Unity is the last point.

The way I would solve this in OpenGL, in a forward rendering pipeline, would be by turning on depth testing glEnable(GL_DEPTH_TEST) and, when rendering the green box, I'd disable depth writing to the z buffer but keep testing on. i.e. glDepthMask(GL_FALSE) then render the cube and then re-enable writes to the z buffer. Essentially treating the green cube as if it were a skybox.

But I have not found anywhere in the ShaderLab documentation an equivalent of glDepthMask. there is ZWrite and ZTest but these just are equivalent to glEnable(GL_DEPTH_TEST) and glDepthFunc, not to the mask.

Any thoughts?

Alternatively, I was thinking of using clipping planes . In GL this would be using gl_ClipDistance in the shaders. I would render the green cube with a clipping distance equal to the intersection point of both cubes - 1, so it chops off that face. And the render the other cube without the clipping plane. But this would require updating the shader each time the clipping plane (intersection between the cubes) changes.

UPDATE:

I actually think that ShaderLab's ZWrite <On|Off> is the equivalent of glDepthMask. The issue is that I don't seem to be able to control rendering the green cube after Unity's skybox. If I were, the approach I'd take would be:

  1. Unity renders it's skybox.

  2. I render my green cube with ZWrite Off. This implies z testing takes place against the fragments of the cube, but the buffer won't be updated with the z values of the cube after this pass.

  3. Render the rest of the geometry as usual.

Since step 2 did not write to the z buffer, the geometry should cover everything as it normally does with respect to the engine's skybox. This means it would cover the green cube too. But, if the geometry (pink cube) had any openings, the pink cube would not write to the z-buffer since there is nothing there. The buffer in those areas should be 1 (the default for infinitely away).

I tried the above, which just meant creating a new surface shader with ZWrite Off and a tag "Queue" = "Background+1", hoping it would mean "render after the skybox". But while it does prevent the green cube rendering on top of the pink cube, it also makes Unity's skybox render on top of the green cube. So not sure how to tell it to render after the skybox.

img-1661.jpg (76.7 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

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

133 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

Related Questions

Getting Color Generated from Shader to Script to Shader 0 Answers

Very basic shader problem 0 Answers

_Time in Image Effects 0 Answers

Operate the shader, rotate the material 90 degrees 0 Answers

How to map letters on mesh with 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