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 /
avatar image
0
Question by tcz8 · Feb 13, 2017 at 05:03 AM · shaderrendertextureshader programmingrender texturestencil

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)

  1. 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.

  2. 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?

  3. Can a variable be shared between shader instances / runs?

  4. Figure out how to make the depth render texture reliable with ortogonal cameras (has anyone done this?)

  5. Figure out how to render a custom depth texture (has anyone got info on this?)

  6. 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.

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 tanoshimi · Feb 13, 2017 at 07:06 AM

Yes, using a depth texture. There's more information and an example in the manual.

Comment
Add comment · Show 5 · 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 tcz8 · Feb 13, 2017 at 07:25 PM 0
Share

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!

avatar image tcz8 · Feb 13, 2017 at 09:33 PM 0
Share

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.

avatar image tanoshimi tcz8 · Feb 13, 2017 at 09:55 PM 0
Share

Ah, you didn't mention orthographic camera ;). Did you try https://forum.unity3d.com/threads/depth-buffer-with-orthographic-camera.355878/ ?

avatar image tcz8 tcz8 · Feb 13, 2017 at 10:23 PM 0
Share

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?

avatar image tcz8 · Feb 16, 2017 at 04:06 AM 0
Share

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.

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

90 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

Related Questions

Help me understand stencil shader logic 1 Answer

Showing hidden units - using stencil buffer with standard shader and deferred rendering 0 Answers

combining two shaders? 0 Answers

How do I make a paint trail effect in ShaderGraph? Are there better ways? 0 Answers

Using rendertextures in multiple shader passes 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