Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Highlight (Intersection) Shader

Discussion in 'Shader Graph' started by romanpapush, Aug 20, 2019.

  1. romanpapush

    romanpapush

    Joined:
    Sep 20, 2012
    Posts:
    40
    Hello,

    Is it possible in current Shader Graph state to create a shader, that would highlight intersecting geometry?
    To elaborate: the shader would go on a sphere, and would highlight any surface it touches.



    It can use either the mesh of the sphere itself, or a SphereMask.
    I've tried everything, but can't seem to get anywhere close.
     
  2. nikefootbag

    nikefootbag

    Joined:
    Jun 13, 2017
    Posts:
    28
  3. romanpapush

    romanpapush

    Joined:
    Sep 20, 2012
    Posts:
    40
    @nikefootbag Thank you for replying!
    Sadly both of the solutions are a bit different still. First one uses the position of the sphere inside the shader, which means, I must have all shaders in my scene have that logic, which is not ideal.
    What I'm looking is sort of an omnidirectional decal. The object that projects color/image to any surface, not just the ones with built-in logic.
     
  4. nikefootbag

    nikefootbag

    Joined:
    Jun 13, 2017
    Posts:
    28
    @romanpapush ah I see, goodluck in finding a solution!
    I just realised I follow you on youtube and twitter, your tutorials are great (and also real funny), keep it up!
     
  5. romanpapush

    romanpapush

    Joined:
    Sep 20, 2012
    Posts:
    40
    Thank you for saying that @nikefootbag :)
    Yeah, I guess will be using the alternative methods for now. Cheers!
     
  6. planetmatrix

    planetmatrix

    Joined:
    Dec 8, 2015
    Posts:
    38
    @romanpapush I am trying the same as you, could you please share what alternative methods you used?
    Many thanks!
     
  7. Elizabeth_LeGros

    Elizabeth_LeGros

    Unity Technologies

    Joined:
    Jan 7, 2020
    Posts:
    50
    Hello! Which SRP are you using? Also, would it always be on a sphere or other simple mesh?
    So long as you have access to scene depth (deffered or forward rendering with the flag checked for depth buffer) and are doing simple shapes I can think of a few ways to achieve this.
     
  8. LandonTownsend

    LandonTownsend

    Unity Technologies

    Joined:
    Aug 21, 2019
    Posts:
    35
    As long as you have access to scene depth, and the shader is set to be transparent, this should work:
    upload_2020-1-9_14-46-23.png upload_2020-1-9_14-47-10.png
    Will match the scale of the sphere too, as long as it's not scaled non-uniformly. It will only work on spheres, however.

    I find an inner radius of 0.493 and an outer radius of 0.495 looks the best.
     
    Last edited: Jan 9, 2020
  9. adrian-taylor09

    adrian-taylor09

    Joined:
    Dec 22, 2016
    Posts:
    63
    Thanks for posting this, works great in editor but doesn't work in VR (single pass rendering mode) Is there any way to make it work with VR
     
  10. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    If there was a way to make this work with a box collider, non uniformly scaled, you'd be a hero.
     
    StrangeWays777 and JamesArndt like this.
  11. JukeBoxDelta

    JukeBoxDelta

    Joined:
    Mar 21, 2019
    Posts:
    3
    This is amazing! I've been looking everywhere for something like this! Thank you so much.

    Would it be possible to have this work with any mesh instead of only a sphere?
     
  12. LandonTownsend

    LandonTownsend

    Unity Technologies

    Joined:
    Aug 21, 2019
    Posts:
    35
    Unfortunately that would not be as simple to implement, as the reason the shader works is it's checking the distance in the depth buffer from the center of the sphere in order to know where to draw.
     
  13. Madix

    Madix

    Joined:
    Nov 22, 2013
    Posts:
    16
    Kind of on a similar vein, how would one go about making something like this in Shader Graph? I've been struggling with it. It's essentially a decal shader for URP, would be nice to be able to hook something up like this for shader graph so you can tweak and do scrolling effects and such.

    https://github.com/ColinLeung-NiloCat/UnityURPUnlitScreenSpaceDecalShader
     
  14. romanpapush

    romanpapush

    Joined:
    Sep 20, 2012
    Posts:
    40
    @LandonTownsend Thank you so much for your answer, this is exactly what I was looking and it is working wonderfully!
    But I can't seem to make it work with transparent objects as well. I've tried writing them to the depth buffer through Renderer Features, but with no luck. Can it be achieved with the current setup?

    Once again, a million thanks for the shader graph solution you posted above
     
    DDAAACCC likes this.
  15. DDAAACCC

    DDAAACCC

    Joined:
    Jul 8, 2020
    Posts:
    9
    I had the same problem with transparent objects not being applied.
    Do you know how to solve that problem?
     
  16. romanpapush

    romanpapush

    Joined:
    Sep 20, 2012
    Posts:
    40
    Hi! I've actually resorted to rendering another material with a spherical mask on top of my objects using URP's Renderer Features material override. Did the trick for my case. Cheers!
     
  17. DDAAACCC

    DDAAACCC

    Joined:
    Jul 8, 2020
    Posts:
    9
    Thanks for replying to you and found this.
    https://docs.unity3d.com/Packages/c...ures/how-to-custom-effect-render-objects.html

    it work

    upload_2021-11-9_13-14-44.png

    Just use the shader above and the method you described.

    But the problem with this is that whenever the camera position changes, the overlap of the stationary red sphere is not constant.
    In my opinion, this method prioritizes the rendering order (zbuffer) of objects with a specific layer in the camera.
    I don't think this has anything to do with the surface changing whenever the camera position changes, isn't it?
     
    sewy likes this.
  18. romanpapush

    romanpapush

    Joined:
    Sep 20, 2012
    Posts:
    40
    Hi!

    Have you checked your "Depth > Write depth" checkbox settings on your sphere vs the rest of the objects in your Render Objects Renderer Feature?
    And if you're using newer versions of Shader Graph there's a Depth Test setting in Graph Settings you might want to check also for your shaders.

    I don't know how your scene is set up, but to a lesser extent double check if you're excluding layers from Filtering setting (in Universal Render Data) which you are manually rendering with Render Objects feature. Also changing the event to, say, "AfterRenderingSkyBox", might solve the issue if your objects are using transparent materials and being rendered after the highlight object.
     
  19. DDAAACCC

    DDAAACCC

    Joined:
    Jul 8, 2020
    Posts:
    9
    thankyou

    upload_2021-11-15_17-4-7.png

    The color is faded, but it seems to have deviated from the wrong application. Thank you...
     

    Attached Files:

    Last edited: Nov 15, 2021
  20. FlippyKat

    FlippyKat

    Joined:
    Jan 28, 2021
    Posts:
    1
    Does anyone know how to get this to work with an orthographic camera?
     
  21. carmofin

    carmofin

    Joined:
    May 13, 2017
    Posts:
    116
    I have an ortographic camera too! From the past I know the depth buffer is broken in orthe: It's always either 0 or 1.
    What can I do to make this work? I really need it...
     
  22. carmofin

    carmofin

    Joined:
    May 13, 2017
    Posts:
    116