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.

Custom Pass Outline Effect

Discussion in 'High Definition Render Pipeline' started by Mivrel, Dec 16, 2019.

  1. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    Hello.

    In the custom pass documentation there is an example of Outline effect (with shader scripting I guess). I want to know is possible to achieve outline effect using custom passes with Shader Graph?
     
  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    221
    Hi,

    Writing an outline effect requires to do a fullscreen pass with a custom shader, currently you need to write your own by hand starting from the template (that can be created with Create > Shader > HDRP > Custom FullScreen Pass). Authoring fullscreen passes with ShaderGraph is not yet supported but we're currently working on it :)
     
  3. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    Thank you for reply.

    For now: would it be hard to write fullscreen pass (like that in documentation) with outline blur and size support for someone who don't know too much about writting shaders?
     
  4. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    221
    If you start from the documentation code, then it shouldn't be too hard, since there is already a loop with the positions to check where the object is. If you need to increase the sample count (and thus the radius) you may want to do it with poisson disk distribution. And for the blur if it's a fade of the outline from the distance of the nearest pixel to outline then you can do it with some math :)
     
  5. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    Thank you once again.

    If I understand I should generate more sampling position with Poisson Disk Sampling (with Distribution I still searched for Sampling?) and with more sampling positions the radius of outline will be higher? Then I can do the blur inside the loop with the position check?

    Sorry. English is not my native language.
     
  6. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    221
    Yes, the idea is that if you have more samples, you can increase your search radius without lacking too much of precision. If you just increase the radius but keep the 8 samples we have by default you'll have gaps in your outline.
     
  7. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    Should I implement the sampling inside shader or implement sampling and generate array outside and use in shader?
     
  8. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    221
    It depends if you want the radius to be dynamic or not, it will be more optimized to pre-compute the positions and hardcode them in the shader (like in the example). But if you want to dynamically change the sample count, then you need to compute then in C# and send the array to the shader.
    I suggest that you start with the hardcoded version since it's simpler and if you really need more performance on small outline radius, then go with a dynamic version.
     
  9. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    Ok. I checked what happen if I add 8 more samples with the multiplying by 2. Actually the size of outline has changed but If I understand there will be problems with that solution? Since the Poisson Disk Sampling is kind of new to me too, do you have suggestions what should I read first to implement it?

    By the way: why the dynamic version would have better performance with small radius?
     
    Last edited: Dec 17, 2019
  10. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    221
    If you can afford the cost of all those samples you will not have any problems with this technique. Also for poisson disk sampling, you don't actually need to implement it, you can copy/paste a list of point you find on internet (or choose another sampling pattern algorithm like the one we use for PCSS: https://github.com/Unity-Technologi...inition/Runtime/Lighting/Shadow/PCSS.hlsl#L20).

    The dynamic version allow you to use less samples for small radius, that's why it;s faster.
     
  11. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    So when I have points array like in example:

    float2( 1, 1),
    float2( 0, 1),
    float2(-1, 1),
    float2(-1, 0),
    float2(-1, -1),
    float2( 0, -1),
    float2( 1, -1),
    float2( 1, 0),

    and I change it to 16 samples like that:

    float2( 1, 1),
    float2( 0, 1),
    float2(-1, 1),
    float2(-1, 0),
    float2(-1, -1),
    float2( 0, -1),
    float2( 1, -1),
    float2( 1, 0),
    float2( 2, 2),
    float2( 0, 2),
    float2(-2, 2),
    float2(-2, 0),
    float2(-2, -2),
    float2( 0, -2),
    float2( 2, -2),
    float2( 2, 0),

    My question is what will be a difference between outline created with this solution and Poisson Disk Sampling? And as I understand the Poisson Disk Sampling points will be like something between 1 and 2 in 9-16 points, right?
     
  12. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    221
    The important thing is to keep you samples in a disk shape, if you continue to add them in a square shape with a bigger radius your outline will also have a square shape: look at these images for exmaple, let's say that the red part is the object that you want to outline. Here is the result with squared shape samples (it's a box filter):
    upload_2019-12-18_14-47-53.png

    And with disk shape samples you have this:
    upload_2019-12-18_14-49-14.png
     
  13. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    Oh! I understand.

    Two more questions about Poisson Disk Sampling: Should the generated points have integer values? What should be the maximum distance between generated points?
     
  14. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    221
    You want to leverage the hardware bilinear filtering so you want to keep the coordinates in float and for the distance, it depends. Generally it's okay if you have gaps in you sampling pattern because most of the objects you want to outline are big enough in screen space to be detected even if you're sampling one every two pixels.
    I'd say try it, it's probably faster to experiment and see how it works (or not) than trying to come up with the perfect algorithm in one shot :)
     
  15. Mivrel

    Mivrel

    Joined:
    Jun 13, 2016
    Posts:
    12
    Ok. So I will try I guess. Thank you for your answers. I appreciate that. :)
     
    antoinel_unity likes this.