• 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
Question by xLinearx · Apr 18, 2013 at 04:31 PM · shadershadersdepth-bufferdisplacement

Setting depth buffer per-fragment

Hi, what I'm trying to do is simple in theory, but in practice I do not know how to go about it.

I wrote a nice steep parallax mapping shader that allows me to make materials that make a basic Unity cylinder look like this:

pretty picture look at it

My issue is that the parallax mapping is so steep that when I try to place things next to the surface (especially if I am using this shader for the ground and need to place, say, chairs or other objects "on the ground"), the things start to intersect the surface's base geometry instead of my displaced surface.

The standard pipeline for shaders is, as far as I know:

Vertex Shader -> Depth Write/Test -> Fragment Shader

Is there any way, short of disabling all depth test and making my own depth buffer out of a render texture (with blackjack and hookers) that I can write to the depth buffer per-fragment in the second stage so that my bumpy wall can properly intersect other objects in the scene? I'm using a Surface Shader but I do not mind going back to the old shader model to do this.

-edit-

Here is the intersection problem I am facing (for which I am trying to find a way to draw to the depth buffer):

alt text

inter.jpg (219.5 kB)
here.jpg (479.9 kB)
Comment
DaveA
Bunny83
whydoidoit
Loius
amitshah

People who like this

5 Show 4
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 hoy_smallfry · Apr 18, 2013 at 06:27 PM 0
Share

Do you have an image of your current intersection?

avatar image DaveA · Apr 18, 2013 at 06:47 PM 0
Share
  • for Futurama reference and stunning effect. Hope you are going to share/sell this.

avatar image xLinearx · Apr 20, 2013 at 07:16 PM 1
Share

I will share/sell this at some point, perhaps. I'm actually looking to write a series of tutorials for Unity shader-writing so that people can learn to do this sort of thing themselves too.

avatar image whydoidoit · Apr 20, 2013 at 09:05 PM 0
Share

Would love to have those tutorial on Unity Gems if you want to post/cross post there - a lot of traffic...

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by whydoidoit · Apr 20, 2013 at 09:13 PM

This is one of those examples of why we need geometry shaders I guess. I concur, it's not easy to do that.

The only thing I can think of is rendering the depth as a render texture and then reading from that with your shader very late in the queue.

Comment

People who like this

0 Show 0 · 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

Answer by DemianSPb · Mar 28, 2017 at 07:18 AM

at least in Unity 5.4+ it's possible:

 // inside CGPROGRAM
 struct FragmentOutput
 {
     fixed4 color : COLOR;
     half depth : DEPTH;
 };
 FragmentOutput frag(VertexOutput i) {
     FragmentOutput output;
     output.color = ...;
     output.depth = (1.0 / worldSpaceDepth - _ZBufferParams.w) / _ZBufferParams.z;
     return output;
 }
Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

vertex displacement shader and ssao 0 Answers

Modifying the depth buffer of an image effect? 0 Answers

'Venetian Blinds' effect using depth buffer 0 Answers

Intersection Highlight Shader -1 Answers

Displace and show normals in fragment shader 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