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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Lexoka · Jun 27, 2013 at 04:19 PM · shaderdepth-buffercustom-shaderdepth of fielddof

How to get Unity's DOF effect with custom shaders?

Hi,

I'm using a custom shader for rendering certain types of objects, and I'd like to use Unity's built-in Depth of Field effect (scatter, i.e. the new one) in combination with it. The DOF effect works fine if I use the standard diffuse shader for my objects, but not with my custom shader. That is, every object seems to receive the same amount of blurring, regardless of the focal distance.

If I toggle the "visualize" option for the DOF effect, every object appears white with my shader. My fragment shader does update the depth buffer, though:

 struct fragment_out {
   float4 Color : COLOR0;
   float depth  : DEPTH;
 };
 
 fragment_out my_f_shader (v2p i) {
   fragment_out OUT;
   […]
   OUT.Color = color;
   OUT.depth = depth;
   return OUT;
 }

It's a pretty complicated raycasting shader and I'm not the one who wrote it, nor do I fully understand it, but as far as I can tell from the rendering, this depth value seems to be correct. I wanted to get a sense of what these depth values where, and since I couldn't print them, I decided to color my objects with float4(depth, depth, depth, 1). I don't know if there's a simpler way to do that, but this is what I came up with.

Anyway, they all appeared white, which seemed strange since it implied a constant depth of 1, but when I raised the depth value to the power of 1000 before creating the color, I got a nice spectrum from almost black to almost white, with objects closest to the camera being darkest, and vice versa.

So at least the depth value is consistent. But why does the DOF effect fail to take it under account? Do I need to somehow make sure the depth is updated to somewhere else in Unity?

This is with OpenGL on MacOS, if that's relevant.

Any clue as to what might be going wrong would be much appreciated.

Comment
Add comment · Show 3
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 Lexoka · Aug 08, 2013 at 09:58 PM 0
Share

I'm surprised that no one has answered this yet. I figured this case would be pretty common and that I was probably missing something simple, or even obvious.

Anyway, if anyone was wondering, I'm still looking for a solution.

avatar image OP_toss · Aug 08, 2013 at 11:09 PM 0
Share

Sounds like you've already found the problem. The DOF post process is heavily dependent on the depth pass... $$anonymous$$akes sense right? So if you're shader is altering the depth pass, that's screwing up the DOF shader.

What is the custom shader doing exactly?

Does it need to be editing the depth value?

Can you replace the depth value with another value for output?

The shader is the problem, not the DOF effect. So that's what you need to understand in order to know how to fix it.

Hope this helps!

avatar image Lexoka · Aug 30, 2013 at 03:38 PM 0
Share

The shader in question does volumetric raycasting for objects defined analytically (by equations). Without editing the depth value, it can't seem to deter$$anonymous$$e the correct order in which objects are to be rendered.

That said, when the depth value is edited, everything is rendered in the right order, including other objects rendered with Unity's built-in shader. To me, this suggests that the depth value is correct, but I could be missing something. Since the DOF doesn't work, I probably am.

I'll try to find a way to compare the depth values written by the custom shader with those produced by the standard diffuse one to confirm this.

Thanks for your help!

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

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

Problems with depth values in _CameraDepthTexture 4 Answers

Dynamically alter shader parameters? 1 Answer

The meaning of the depth map value 1 Answer

Better shader effects on terrain - Bumpmapping or Specular? 2 Answers

Mesh z-fighting to itself 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