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
0
Question by miningold · Oct 27, 2012 at 08:26 AM · layerscullingz-buffer

Only render objects behind specific object

I have a sphere on one layer and a bunch of objects on an other layer. I would like to only render the objects that are behind the sphere with respect to the camera. This would all be rendered to a texture for later use.

I assume the steps would have to be something like:

 Render sphere layer with ZWrite on and ColorMask 0
 Render objects layer using ZTest Greater or GEqual

I will also probably need to use Shader Replacement when rendering the objects layer.

I have tried a couple of things but I have failed to produce any desirable results.

Let me know if you need any more information.

Edit: After further thought I think the steps should rather be:

 Render depth of sphere layer to depth texture
 Render objects with a depth greater than that in the depth texture

This would involve setting depthTextureMode to Depth when rendering the sphere then using the generated _CameraDepthTexture when rendering the objects for depth testing. But I am not sure what the shader would look like that was using the _CameraDepthTexture.

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
0
Best Answer

Answer by miningold · Oct 31, 2012 at 09:34 PM

I ended up rendering the sphere to the depth texture at _CameraDepthTexture

Then I wrote a surface shader that draws the other objects. This shader would simply draw an alpha of 0 if it were in front of the sphere (using the objects screenPos to map to the depth texture and retrieve the depth).

There was one small caveat, I began to run into transparency render order problems. So I told unity to compile the surf shader into a vertex/fragment pair. Then I overrode the AlphaTest to write to the Z-buffer only if the alpha was greater than 0. I couldn't do this in a surface shader because Unity compiles there own alpha testing.

 AlphaTest Greater 0 ZWrite on ColorMask RBG
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 maloya8 · May 30, 2013 at 09:18 PM 0
Share

would you please show you did it? I mean the code

avatar image Marrt · Jul 23, 2013 at 09:10 AM 0
Share

i guess your above explanation would be enough to reproduce your solution, but since i am a shader noob... would you please share your shaders? thanks in advance :D

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

11 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

Related Questions

Dynamically render/hide object individually for each camera in scene 0 Answers

Prevent material from rendering behind itself 0 Answers

How to Choose Camera for Game Window 2 Answers

Seperate Shadow Strengths from different objects? 0 Answers

Apply Camera.layerCullDistances to certain/specific layers? 2 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