Search Unity

Resolved URP Sprite Gaussian Blur Customer SubShaderGraph

Discussion in 'Shader Graph' started by Graigy1337, Aug 26, 2022.

  1. Graigy1337

    Graigy1337

    Joined:
    Oct 26, 2019
    Posts:
    9
    Hi all,

    I decided to make a Gaussian Blur SubShaderGraph to be used into 2D pipline as I couldn't find anything that was working well.

    I adopted the code from Daniel Lilett HERE but just updated it and cleaned it a bit to work in a custom function node.

    Then that was converted into a SubShader so it can be easily used in any graph moving forward.

    Custom Function Code Here:
    Code (CSharp):
    1. void GaussianBlur_float(UnityTexture2D Texture, float2 UV, float Blur, UnitySamplerState Sampler, out float3 Out_RGB, out float Out_Alpha)
    2. {
    3.     float4 col = float4(0.0, 0.0, 0.0, 0.0);
    4.     float kernelSum = 0.0;
    5.  
    6.     int upper = ((Blur - 1) / 2);
    7.     int lower = -upper;
    8.  
    9.     for (int x = lower; x <= upper; ++x)
    10.     {
    11.         for (int y = lower; y <= upper; ++y)
    12.         {
    13.             kernelSum ++;
    14.  
    15.             float2 offset = float2(_MainTex_TexelSize.x * x, _MainTex_TexelSize.y * y);
    16.             col += Texture.Sample(Sampler, UV + offset);
    17.         }
    18.     }
    19.  
    20.     col /= kernelSum;
    21.     Out_RGB = float3(col.r, col.g, col.b);
    22.     Out_Alpha = col.a;
    23. }
    This is how it looks in graph:
    upload_2022-8-26_23-5-40.png

    And finally this is how the effect looks!
    Blur.gif

    As the Depth Of Field post process doesn't work well in 2D and camera stacking is annoying this makes it much easier to get the blur effect you need.
     

    Attached Files:

    Inward, wing3s, fuzzy3d and 3 others like this.
  2. BennettFarmer

    BennettFarmer

    Joined:
    Oct 10, 2017
    Posts:
    1
    This looks exactly like what I need! However, I'm getting an error in the shader graph:
    "Shader error in 'Master': undeclared identifier '_MainTex_TexelSize' at Assets/Shaders/GaussianBlurSubShader.hlsl(15) (on d3d11)"

    Any idea how I can fix this?
     
  3. id0

    id0

    Joined:
    Nov 23, 2012
    Posts:
    455
    It is working, just open subgraph, add cging file by hand, and change precision to single. Then save, and you can use subgraph in your shader.
    Thank you Graigy1337
     
  4. crutchrepg

    crutchrepg

    Joined:
    Dec 8, 2022
    Posts:
    1
    Hi all!
    This too looks like exactly what I need. I know next to nothing about HLSL though and I'm getting the same error. I'm trying to use this in an HDRP project in Unity 2021.3.15- could that be the problem? I too get "undeclared identifier '_MainTex_TexelSize' ". I've manually added the cging file and set the precision to single - inside the subshader for the node's precision. I don't get the error inside the subshader, only when I've added the subshader to another shader graph. I've tried adding...

    Code (CSharp):
    1. sampler2D _MainTex;
    2. float4 _MainTex_TexelSize;
    earlier in the script near the initialization of col and kernelSum. (I had read that maybe you need to declare them inside the function even if they might be declared somewhere else...? But that didn't solve it.) Any help or guidance would be appreciated!
     
  5. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Hi, you can also try accessing the "yourTexture_TexelSize" by Texture Size Node and expose the texel size property in the custom blur function.

    The reason that MainTex is not defined may be because there's no texture called "_MainTex" in the shader properties panel.
     
  6. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Yea I'm getting this same error too.

    -- Update --
    Nevermind. To fix it do this in the new Shader Graph you created. Just add _MainTex in the "blackboard"
    Like in the example from Graigy
    upload_2023-3-30_18-15-27.png
     

    Attached Files:

    Last edited: Mar 30, 2023
  7. JarekKaa94

    JarekKaa94

    Joined:
    May 31, 2018
    Posts:
    10
    For me unity shader don't see GaussianBlur Custom Function. How to fix it?
     
  8. JarekKaa94

    JarekKaa94

    Joined:
    May 31, 2018
    Posts:
    10
    Fixed, ref for CustomFunction node was broken.
     
  9. InkPirate

    InkPirate

    Joined:
    May 30, 2022
    Posts:
    3
    This look AMAZING, but when I apply the shader to a material and stick it on a sprite I'd like to blur it blurs everything except for the edges of the sprite. Example below:

    upload_2023-5-2_15-27-41.png

    I set up the shader graph exactly like in the example, am I just not able to blur sprites with half transparency?
     
  10. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    It looks like you have a Tight Mesh on that sprite. Have you tried Full Rect on its Texture Import Settings? That should extend the edges.
     
  11. InkPirate

    InkPirate

    Joined:
    May 30, 2022
    Posts:
    3
    That worked! So simple, thank you SO much for getting back to me!
     

    Attached Files:

  12. Fikoljuky

    Fikoljuky

    Joined:
    Oct 3, 2016
    Posts:
    1
    I'm getting the "sampler2D object does not have methods at Assets/Art/Shaders/GaussianBlur.cginc(16) (on d3d11)" for the col += Texture.Sample(Sampler, UV + offset); part of the .cginc.

    How did you guys prevent that from happening?

    And there is also one more problem that keeps poping up, and that is

    Shader error in 'Master': undeclared identifier 'GaussianBlur_half' at line 189 (on d3d11)

    And when restarting unity, all of the errors above disappear and only this error is thrown
    Shader error in 'AS_Character': redefinition of 'GaussianBlur_float' at Assets/Art/Shaders/GaussianBlur.cginc(1) (on d3d11)
     
    Last edited: Jul 18, 2023
  13. vazana

    vazana

    Joined:
    Nov 17, 2015
    Posts:
    1
    amazing! but when i want to use something like Gradient noise to the blur input it gives an error, how should i change the shader to make it work with stuff like Gradient noise and alpha channels?
     
  14. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    This sounds pretty specific so you should make a separate thread on it and post the code and error there.
     
  15. unity_bvMQ_u6Kl92Ryw

    unity_bvMQ_u6Kl92Ryw

    Joined:
    Sep 22, 2022
    Posts:
    2
    Hi everyone! I did everything which Graigy suggested but I keep getting an error which says:
    Shader error in 'Master': 'GaussianBlur_float': cannot implicitly convert from 'float3' to 'struct UnitySamplerState' at line 282 (on d3d11). Could someone please help me with this one?
     
    Last edited: Mar 24, 2024
  16. unity_bvMQ_u6Kl92Ryw

    unity_bvMQ_u6Kl92Ryw

    Joined:
    Sep 22, 2022
    Posts:
    2
    Never mind, I fixed it. Thank you Graigy, this helped me a lot!
     
  17. Shaggy21

    Shaggy21

    Joined:
    Oct 2, 2014
    Posts:
    24
    This is pretty nice but it's a Box Blur, not Gaussian blur. I noticed it also when you showed the GIF. In order to calculate a Gaussian blur you have to apply different weights for each sample, instead of aggregating the sample count.