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 Piflik · Jan 20, 2021 at 10:02 AM · shaderdepth buffer

Per object Depth Test

I am currently trying to implement an effect, but my google-fu isn't strong enough.

I have a shader that is rendered additively. It is transparent, but I don't want to have the typical artefacts with concave or self-intersecting geometry, so I added a Depth-only pass. This works great as long as the objects are all nicely sortable, but in my case I have objects contained in other objects.

I can mostly fix it by forcing a render order using the RenderQueue, but in some cases, the order is not really deterministic.

Is there a way emulate a depth test in a way that it only applies to the currently rendered object, or reset the depth buffer after an object is drawn?

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

Answer by Bunny83 · Jan 20, 2021 at 10:58 AM

What you'Re asking is simply not possible. Transparent object / triangles have to be rendered with the painter's algorithm to get the correct blending between the different parts. Most engines can only sort on a per object basis which is what Unity does as well. However objects can only be sorted based on their pivot position. You can not somehow determine a single depth value for a whole object from the current view position that ensures the correct order. To implement the painter's algorithm properly it's not even enough to sort on a per triangle basis as intersections or circular overlaps are edge cases which would make it necessary to split a triangle / polygon into several to be able to sort them correctly.


For most cases this is completely impractical in a real time rendering setup. Keep in mind that the order of objects / triangles would also change depending on the view. You haven't said what kind of game / application you're creating. In 2.5D or 2D setups this becomes a bit simpler. However in a full 3D environment there's no generally valid approach.


The first problem is that the individual triangles of a mesh are rendered in the order they are defind in the mesh. That's also why self occluding geometry can look so weird depending on the angle you look at it. Because from one point the triangles may be rendered in the right order because the first triangles in the mesh are the furtherst from your view point. So they would follow the painter's algorithm. However viewing the same object from the otherside would of course reverse the order. Since most more complex imported objects may have an arbitrary triangle order you almost never get the right sorting. This is just one single object. Of course you don't want any self overlapping so you first do a depth pass so only the closest triangles are rendered.


The next issue is sorting of the individual objects is difficult since you would need to take into account every part of each object in order to sort them properly. For example imagine having a slanted long wall-like object which has its pivot in the center. So you look along the wall. Further aways there's an object right before the wall. So you should render the wall first and that object last. However just looking at the pivot makes the wall much closer to the camera than that other object, so they will draw in the wrong order. When objects are intersecting it gets even more complex, as mentioned above since you can not really determine which object is before which since intersecting objects technically are both at the same time. At least a part is closer while another part is further away. There is no patent solution.


Doing rendering on a per triangle basis is simply impossible for a game engine as you would need to draw each triangle on its own. So you loose a lot of the benefits of having a pipeline GPU. Also the sorting is really expensive. Also to solve the cyclic issues you would need to dynamically split geometry to solve this properly.


So the best thing you can do is put constraints on the objects placements and different object configurations to ensure you can determine the sorting order. If you use a scriptable rendering pipeline it may make sense to create a modified one that takes care of the sorting based on some of your own metadata. Though this is not really trivial to get good performance.

Comment
Add comment · Show 1 · 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 Piflik · Jan 20, 2021 at 11:27 AM 0
Share

Thanks for taking the time to write such a long answer, but I know all that and I, too, don't want to sort triangles. $$anonymous$$y idea was to use some temporary (or the stencil) buffer for the pseudo-depth-test, or reset the depth buffer to the state it was before rendering an object, but I don't know how this is possible, if at all.

I can force the order via the queue, and since I am rendering the objects additively from a orthographic camera I can also move the objects along the z-Axis to eli$$anonymous$$ate overlap completely and circumvent the depth test issues. However, I am not using a fixed camera, so I'd have to reposition all objects whenever the camera moves. Not an unsurmoutable problem, not even particularly complex, but a shader solution would have been preferrable, if it existed.

The easiest solution, of course, would be to just not have the depth pass in the shader and ignore the artifacts.

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

172 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

Related Questions

Inverted Depth Mask? 0 Answers

16 bits Depth Map to file using a RenderTexture 0 Answers

How to access Tilemap Tile's x-y-z positions from shader, to mask or move tiles 0 Answers

Alpha blended depth masking of sprites in the shape of the sprite, not the internal mesh? 0 Answers

How to use shader depth buffer offset in android on unity 5 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