Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help on Intersection Highlight

Discussion in 'Shaders' started by deadsythe88, Jun 1, 2016.

  1. deadsythe88

    deadsythe88

    Joined:
    Jan 30, 2013
    Posts:
    2
    Hi all, I am new to the shaders. But I hope to create some special effect in my game.

    I am trying to create a special effect of Intersection Highlight when there are 2 game object intersected.

    I found a reference from this link and it works perfectly.
    https://chrismflynn.wordpress.com/2012/09/06/fun-with-shaders-and-the-depth-buffer/

    I wish to create the effect like Rocket League goals effect that only intersect with the ball but not the vehicles, like following youtube video:

    intersection2.png

    Now I have a question, I created a scene that intersecting a cube and a plane. Is there any way to ignore the plane intersections?

    Thanks!

    Here is the screenshot from my test:
    intersection.png
     
    Last edited: Jun 1, 2016
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    The problem is that all of your objects draw to the depth buffer and you use that to create your highlight effect. You will need some way of differentiating what pixels belong to highlightable objects in the shader, or a way to cull anything else out.

    I recommend using stenciling. I won't go into too much detail on how to use it, but I can help if you have questions. Basically, you'll want to write to the stencil buffer when drawing objects that can highlight, then when you go to draw the highlights, only draw to the pixels that were stenciled.

    If you're new to stenciling, don't be intimidated. Once you've got it figured out, it's just a couple lines at the top of your subshader or pass. http://docs.unity3d.com/Manual/SL-Stencil.html
     
  3. deadsythe88

    deadsythe88

    Joined:
    Jan 30, 2013
    Posts:
    2
    Thanks! I will try to figure it out. :D
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    I'm pretty sure the Rocket League setup isn't working the way you're thinking it is. It's taking advantage of the fact it only cares about a single, spherical object, and the shader is calculating the intersection with a mathematical sphere with the position (and radius) passed to the shader. Otherwise there's no way to do the intersection highlight in the areas the geometry isn't visible behind the plane. Your test is a good example where the "glow" is only visible seemingly on the box's surface. Valve did a similar trick in Portal 2 with multiple calculated spheres for when objects / the player pass through the fields.

    http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm