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

Fade a mesh's surface by distance

Discussion in 'Shaders' started by HankySpanky, Jun 27, 2016.

  1. HankySpanky

    HankySpanky

    Joined:
    Feb 3, 2015
    Posts:
    77
    This is more of a general inquiry than an outright technical question, seeing as I'm a novice in both theory and practice, but any angle would be appreciated really.

    Basically, what I want to do is. Have a mesh object's opaqueness kind of fade in, depending on how close its individual surfaces, or vertices are to a given target, using given distance values. So that if a mesh is positioned close enough to the target so that it can be partly seen, but its farthest point is outside the given maximum distance, it will appear to be partly opaque, partly invisible.

    Like so:
    http://i.imgur.com/D9SdARV.png
    Where the mesh in question a sphere, and is partly within a given visibility radius from a target.

    I'm guessing I have to do something roughly along these lines:
    1. Feed the shader the current position of the target and and any relevant distance values
    2. Use the values and vertex world positions to calculate transparency across the mesh's surfaces.

    This is, at the very least, doable, right?
     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,460
    yes, in the frag you calculate distance to camera and use that to compute alpha of the point.
     
  3. HankySpanky

    HankySpanky

    Joined:
    Feb 3, 2015
    Posts:
    77
    But say that I needed it to be an arbitrary point in space, for example a gameobject that isn't the player. Would it be any more complex to compute?
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,460
    nope make a property that pass a 3 vector to the shader by script, instead of the camera it will be any objects.
     
  5. HankySpanky

    HankySpanky

    Joined:
    Feb 3, 2015
    Posts:
    77
    Any ideas on where to start?

    At this point I'd love any technical, or beginner tips...or anything really.

    And to be honest these kinds of drive by answers tend to be rather frustrating. I was hoping for slightly more elaborate response rather than just essentially "yes, it is indeed possible".
    Though I admit that my question was phrased pretty open endedly.
     
    Last edited: Jun 27, 2016
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Make a script that calls SetVector() on your object's material using a custom shader. Have it pass a Vector4 with the world space position and radius.
    https://docs.unity3d.com/ScriptReference/Material.SetVector.html

    In the shader you'll need the world space position at each pixel. If you're using a vertex / fragment shader you'll need to calculate that in the vertex shader and pass it to the pixel shader. If you're using a surface shader just add worldPos to your input struct.

    Then you just have to compare the distance between the world space position of the surface in the shader to the world space position you're passing in against the distance you want the cutoff.

    If where you're stuck is writing the shader then I recommend going through all 5 parts of this guide:
    http://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/
     
    HankySpanky likes this.
  7. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,460
    It's because you have enough keyword to start searching the doc and google it at large, we won't make the job at your place. I don't have the time personally, so if there another charitable mind to take my place :p
     
  8. HankySpanky

    HankySpanky

    Joined:
    Feb 3, 2015
    Posts:
    77
    That's not really a reason. I do appreciate the answer but if you're just starting out, these keywords, of which there are few, and barely put into context, won't really save you rummaging through a lot of irrelevant shader topics that will pop up in a search containing said few keywords.
     
    That_Dude likes this.
  9. HankySpanky

    HankySpanky

    Joined:
    Feb 3, 2015
    Posts:
    77
    I'm inching my way through that exact guide as we speak. Trying to unravel the syntax before I go too far ahead of myself, albeit asking this question kind of was.

    But thanks for the input.

    Would you really use a surface shader in this case? I've gotten the impression from the online material I've found that surface shaders are generally used for coloring an object across the board, or consistently rather than differently color individual bits of it, like this mixed fading presumably would do.
    If that attempt at an explanation makes any sense.
    So if that were true, you'd fade the entire object with one consistent alpha value rather than differently for its vertices.

    But if not, how would you say that it differed from using a fragment/vertex shader to do it?
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Surface shaders are vertex / fragment shader generators. They allow you to easily use Unity's built in lighting functionality without having to manually write the many shader passes the lighting system requires.

    If all you care about is an unlit object that fades in and out, vertex / fragment shaders are the way to go.

    Get to part 2 of that link and it'll go into surface shaders.
     
    HankySpanky likes this.
  11. HankySpanky

    HankySpanky

    Joined:
    Feb 3, 2015
    Posts:
    77
    Oh, that's actually the opposite of what I want. Assuming you're talking about an object having one consistent alpha for all of its vertices. I want an object to be able to be varying levels of transparent, ranging from invisible to fully opaque. Different parts of the object have different alpha values and so forth.

    But if that's something I can achieve with either surface or fragment shaders then that's all the better I guess.
     
  12. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    It is not.

    Both surface shaders and custom vert / frag shaders are fully capable of doing per vertex or per pixel transparency changes. It's just a question of if you want the area that is visible to be unit (either a solid color or a texture) or lit with light color, intensity, direction, and shadowing effecting it. You can have all of that lighting in vert / frag shaders too, it's just easier with surface shaders.
     
    HankySpanky likes this.
  13. HankySpanky

    HankySpanky

    Joined:
    Feb 3, 2015
    Posts:
    77
    Ah, right. I'll have to take your word for it, but it sounds logical enough. Thanks for elaborating.