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 PsyDev · Dec 02, 2015 at 08:58 PM · shadershadersdepth-bufferfragment

How to write to Camera's Depth Buffer with a basic vertex and fragment shader?

Create the most basic, unlit shader: Project->Create->Shader->Unlit Shader. Create a material and set the shader you just created. Drag in a texture. Add a cube or plane to your scene, drag that material onto it. Everything is good so far, you should see a textured cube in your scene.

However, if you do:

 float depth = -LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, input.texCoords));

that object is not in the camera's depth buffer.

There is no ZWrite line in that default shader, but docs say it on by default. Add ZWrite On anyway, no difference.

Nothing fancy with transparency here, the object is fully opaque. Add it to the Geometry queue (I think it is by default anyway):

 Tags {"Queue" = "Geometry" "RenderType"="Opaque" }

Image effects like Depth-of-Field use that depth buffer. But for any vertex/fragment shaders I write, even with ZWrite On, are not getting included.

Does anyone know how to get a vert/frag program to write into the zbuffer in such a way that is included when sampling the camera depth texture?

Alternatively, is there some other depth buffer that has the depth of these vert/frag shaded objects? They're drawing in the correct order, depth is stored somewhere.

Thanks.

Comment
Add comment · Show 7
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 PsyDev · Dec 03, 2015 at 05:35 PM 0
Share

Anyone? How can I find the depth of pixels of the objects I just rendered with a vert/frag shader?

avatar image PsyDev · Dec 03, 2015 at 08:35 PM 0
Share

Adding Fallback "Diffuse" causes it to work. So from that I assume there is some pass being done that is not covered in the default code produced by Project->Create->Shader->Unlit Shader. Can anyone explain why adding Fallback "Diffuse" causes the depth to show up in SA$$anonymous$$PLE_DEPTH_TEXTURE(_CameraDepthTexture,.. ?

avatar image _dns_ · Dec 04, 2015 at 02:20 AM 0
Share

Hi, I think it's due to the fact that the Z pass uses a shader pass with the tag "ShadowCaster" (info here: http://docs.unity3d.com/$$anonymous$$anual/SL-RenderPipeline.html). Your shader does not have this pass/tag but the fallback one has it. To confirm, you could ask this question on the forum in the graphics/shader section, there may be more expert there. You may also be able to see that in the "frame debugger".

avatar image PsyDev _dns_ · Dec 04, 2015 at 12:23 PM 0
Share

Thanks @dns, that helps. I'm starting to get my head around the Unity rendering pipeline now. Also thanks for the advice about checking out the forums.

avatar image PsyDev _dns_ · Dec 04, 2015 at 09:34 PM 0
Share

I've gone further down the spiral now. Learned plenty about the rendering pipelines in the last 24 hours for sure. Now I've refined the question to be: How do I get the depth of objects rendered with Graphics.DrawProcedural in OnObjectRender()? The ShadowCaster pass does answer the specific question posed by this thread, so I've started a : more specific thread on the forums

avatar image rageingnonsense · Dec 04, 2015 at 06:31 PM 0
Share

Not really an answer, but I found this link that has a nice multi-part tutorial about the depth buffer. I haven;t had a chance to get through it yet but it seems very promising:

http://williamchyr.com/2013/11/unity-shaders-depth-and-normal-textures/

avatar image PsyDev rageingnonsense · Dec 04, 2015 at 06:39 PM 0
Share

Thanks @rageingnonsense . I have seen that tutorial. It was indeed helpful trying to understand how to get and use the camera's depth texture, but not so much for shedding light on how that depth texture is generated in the first place. Was that an unintended pun? ... depth is created by the Unity shadowcaster pass ... never$$anonymous$$d. That tutorial is by the guy making $$anonymous$$anifold Garden btw.

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

38 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

Related Questions

Modifying the depth buffer of an image effect? 0 Answers

Intersection Highlight Shader -1 Answers

Setting depth buffer per-fragment 2 Answers

Find out if an object is in front of a target in fragment shader? 0 Answers

2D Water top surface profile 1 Answer


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