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.

Shader Graph Outline / Metaball Effect

Discussion in 'Shaders' started by coidevoid, Mar 10, 2019.

  1. coidevoid

    coidevoid

    Joined:
    Oct 26, 2017
    Posts:
    55
    Hello there.

    I'm kinda stuck on an issue, I want to create a "metaball" shader within the shader graph.


    (The metaball effect)

    I did successfuly get all the "particles" as a single texture :
    upload_2019-3-10_23-31-25.png

    But now I'd like to make an outline. For that I probably need to write a custom node, but I tried my best all day and I really don't get how I can simply compare the neighbor pixels to see if there are or not next to a transparent pixel.

    Thanks for your help!
     
  2. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    645
    I suggest that instead of trying to detect the edges of your particles, you take an other approach.
    Not that to make the edge detection you want, you basically need to sample the texture multiple times by offsetting the UV, to check if the value of the neighbor pixels are different or not.

    My suggestions :
    - Since apparently you're rendering to a render texture, take full control of what you're writing in this texture, and :
    - Set the camera background to black
    - Assign an additive material with a radial gradient white texture to your particles
    - This will basically render a "Signed Distance Field" (go google that for more details, it can be very usefull) that kind of represent the "density" of your metaball.
    - Now you just have to test the texture for a threshold value to determine if the pixel you sampled is in/out/in the border of the metaball.

    Example:
    upload_2019-3-11_13-40-40.png

    Don't forget to set the render texture format to a Float one.
     
    andacoban likes this.
  3. coidevoid

    coidevoid

    Joined:
    Oct 26, 2017
    Posts:
    55
    Hi Remy,

    Thanks for your reply, I'm not using a Render Texture i'm using this trick :
    upload_2019-3-11_14-9-24.png

    And the "camera opaque" property doesn't work with the "transparent" sprites.
    So maybe i'm gonna change that and use a render texture instead, if you can enlighten me on few points :
    -Is it something I have to render on an other camera and use a target texture on this camera ?
    -Is the texture a parameter that I have to pass every frame to the shader and if that's the case is it not too expensive in performance ?

    Thanks again! Regards
     
  4. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    645
    - Yes, you have to render on an other camera using a render texture as a target. But you can use the layer masks to filter what is rendered.
    - You don't have to pass the render texture as parameter everyframe. One the render texture is referenced, it gets updated automatically.

    Also note that shadegraph as the "Scene Color" node that is basically what's happening with the _CameraOpaqueTexture trick.
     
  5. coidevoid

    coidevoid

    Joined:
    Oct 26, 2017
    Posts:
    55
    Hello Remy, I've tried it works like a charm! Thank you!
     
  6. Peterrific

    Peterrific

    Joined:
    Oct 10, 2019
    Posts:
    1
    Hey coidevoid. This thread is quite old now but I was hoping you could share the final result and how you ended up doing it? I been searching all over for at metaball effect made with Shader Graph and this thread is the only one that really pops up. I'd love to see and learn how you managed it!