- Home /
In Unity 2D how can I make a shader aware of the Z coordinate (depth) of the object it is about to draw over?
Before you say Z coordinates are not used in 2D, know that I sync the Y coordinate with the Z value which is what make my sprites draw behind or above others in my top down rpg which I read is pretty common.
Here is a list of things I was thinking about trying:
(please help eliminate whats impossible)
When drawing a vertex, use its coordinate with WorldSpaceViewDir to extract its distance from the camera and assign it a value from 1 to 200 which would represent a rough depth estimate. Store the number in the stencil buffer to be retreived by shaders drawing other objects. Leaves me with 55 extra stencil values for other shader effects. Hopefully it will be enough for a 2D game. Will have to deal with weird value jumps if an object's depth is changes.
Find another way to store data that can be shared between shaders running on different objects. Is there anything else than the stancil buffer available to do this?
Can a variable be shared between shader instances / runs?
Figure out how to make the depth render texture reliable with ortogonal cameras (has anyone done this?)
Figure out how to render a custom depth texture (has anyone got info on this?)
How about the depth buffer? I read about some shaders using this, is it the depth texture? or is it allways accessible like the stencil?
Thank you for your help.
Answer by tanoshimi · Feb 13, 2017 at 07:06 AM
Yes, using a depth texture. There's more information and an example in the manual.
Wow, this will be so usefull!
If you have another $$anonymous$$ute I would like your advice on the following:
Is using a fullscreen render texture a huge performance hit? and is it accurate enough to not cause issues were the depth does not allways match the scene perfectly?
How would this compare vs the technique described here: https://forum.unity3d.com/threads/how-to-get-distance-from-camera-to-pixel-in-fragment-shader.440136/
Thank you very much!
Hmm, I have been reading more on the subject and I am not sure this can work with an Ortographic camera in 2D. I need to figure out the distance between the sprite we are drawing and the one we are drawing onto.
Ah, you didn't mention orthographic camera ;). Did you try https://forum.unity3d.com/threads/depth-buffer-with-orthographic-camera.355878/ ?
I have seen it from reading it it seems unreliable. I need to know when a sprite os 0.01 away on the z axis or y since i am syncronizing the values for all my sprites. Isn't there any other way to pass that kind of info to a sprite? or somewere else than the stencil buffer were I can store a value per pixel or vertex?
Well turns out I was able to make the render texture work in 2d after all.
I added made sure zwrite was on and added a fallback to the standard shader then I was still getting fully red image in my render texture. I adjusted the near and far clipping pane to a much lower range and it started working. I set Near -2 and Far to 5.