Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Legacy Blur for Post Processing Stack v2

Discussion in 'Image Effects' started by spraycanmansam, Aug 15, 2017.

  1. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    Hey everyone,

    I couldn't find much info on a blur effect for the new stack so I've converted the old 'Blur (Optimized)' image effect over to work with the new Post Processing Stack v2. Just thought I'd post it here as I've seen a few requests for something similar :)

    Cheers.
     

    Attached Files:

    stonstad, kudorgyozo, Mauri and 6 others like this.
  2. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,225
    This is neat. How do you define the order in the stack?
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  4. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Would like to know about this too, creating custom stack seems too abstract for me atm
     
  5. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,225
    And how do I reuse pass, or the result of a blur, I'd like to know that.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  7. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,641
    I thank you for the shader that I am using but I have some questions in case someone can help. After starting to use it we noticed a reproducible crash happening quite often. It's a player crash, which means there is something wrong in the Unity code, but it's related to this shader.

    The crash happens inside the unityplayer.dll in the ShaderLab::TexEnv::SetTextureInfo function called by the SetupLightShaderProperties from inside ForwardRenderingLoopJob.

    I don't have the complete stack with my right now, but it should give a good idea.

    The problem is that the shader is corrupted, the memory pointed by the shader is obviously rubbish. The editor actually manages to complain as well before the crash, giving this:

    ArgumentException: Invalid shader ()
    at UnityEngine.Rendering.PostProcessing.PropertySheetFactory.Get (UnityEngine.Shader shader) [0x00018] in C:\Users\rgent\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\Utils\PropertySheetFactory.cs:30
    at UnityEngine.Rendering.PostProcessing.BlurEffect.Render (UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context) [0x00085] in C:\Users\rgent\Perforce\rgent_RGent_Release_1.53\CardLifeUnity\Assets\Standard Assets\Blur.cs:50
    at UnityEngine.Rendering.PostProcessing.PostProcessLayer.RenderList (System.Collections.Generic.List`1 list, UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context, System.String marker) [0x000ab] in C:\Users\rgent\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:752
    at UnityEngine.Rendering.PostProcessing.PostProcessLayer.RenderInjectionPoint (PostProcessEvent evt, UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context, System.String marker, Int32 releaseTargetAfterUse) [0x00049] in C:\Users\rgent\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:720
    at UnityEngine.Rendering.PostProcessing.PostProcessLayer.Render (UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context) [0x001dd] in C:\Users\rgent\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:695
    at UnityEngine.Rendering.PostProcessing.PostProcessLayer.BuildCommandBuffers () [0x003ad] in C:\Users\rgent\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:437
    at UnityEngine.Rendering.PostProcessing.PostProcessLayer.OnPreCull () [0x00096] in C:\Users\rgent\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:294

    Now I am 95% sure that this is due to the Shader.Find() failing to retrieve the right shader. I can still be wrong, I am investigating, but meanwhile I am asking this:

    The PostProcessEffectRenderer Render method is not only called every frame, it's even called when the editor is paused (why?). So

    a) Shader.Find every frame? Is it wise?
    b) why building a command buffer every frame?
     
    sarahnorthway likes this.
  8. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,641
    Anyhow, I just remember(although not tested yet) that the shader is not in the resource folder, so very likely that;s the problem, but my previous observations are still valid. Furthermore Unity should really warn about this mistake and not crash.
     
    hippocoder likes this.
  9. vmp1r3

    vmp1r3

    Joined:
    Jun 15, 2015
    Posts:
    6
    Am I only one who has this issue?
    I'm not really into shaders, but its not working -_-

    Code (CSharp):
    1. Shader error in 'Hidden/Legacy/Blur': failed to open source file: '../../PostProcessing/Shaders/StdLib.hlsl' at line 6 (on d3d11)
    2.  
    3. Compiling Vertex program
    4. Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_RGBM_ENCODING
    5.  
    Maybe code became legacy after some time? Or am I doing something wrong?

    Target platform: WebGL
     
  10. adampeled

    adampeled

    Joined:
    Dec 1, 2016
    Posts:
    1
    @plastic_block
    I had the same issue in 2018.2.10f1 with version 2.0.17-preview of the post-processing stack. Changing the line
    #include "../../PostProcessing/Shaders/StdLib.hlsl"
    to:
    #include "PostProcessing/Shaders/StdLib.hlsl"
    fixed it.

    If that doesn't work, try
    #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    instead.
     
  11. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    It works for me ! I tried first to move the files inside the package cache, but I got an error talking about missing meta files due to read only.

    btw I wanted to have the HDR support for the blur (to exceed 0-1 values), to have that you only need to add "RenderTextureFormat.DefaultHDR" on every "GetTemporaryRT" call.
     
  12. MoYing

    MoYing

    Joined:
    Dec 24, 2013
    Posts:
    8
    How did you solve the problem later?
     
  13. vmp1r3

    vmp1r3

    Joined:
    Jun 15, 2015
    Posts:
    6
    No, but I found blur shader for UI element somewhere, I'm using it instead of this one.
    Also check custom solutions at Asset Store.
     
  14. asdzxcv777

    asdzxcv777

    Joined:
    Jul 17, 2017
    Posts:
    29
    could you share that blur shader ??
     
  15. vmp1r3

    vmp1r3

    Joined:
    Jun 15, 2015
    Posts:
    6

    // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

    Shader "UI/Blur"
    {
    Properties
    {
    _Radius("Radius", Range(1, 255)) = 1
    }

    Category
    {
    Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }

    SubShader
    {
    GrabPass
    {
    Tags{ "LightMode" = "Always" }
    }

    Pass
    {
    Tags{ "LightMode" = "Always" }

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    #include "UnityCG.cginc"

    struct appdata_t
    {
    float4 vertex : POSITION;
    float2 texcoord: TEXCOORD0;
    };

    struct v2f
    {
    float4 vertex : POSITION;
    float4 uvgrab : TEXCOORD0;
    };

    v2f vert(appdata_t v)
    {
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    #if UNITY_UV_STARTS_AT_TOP
    float scale = -1.0;
    #else
    float scale = 1.0;
    #endif
    o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    o.uvgrab.zw = o.vertex.zw;
    return o;
    }

    sampler2D _GrabTexture;
    float4 _GrabTexture_TexelSize;
    float _Radius;

    half4 frag(v2f i) : COLOR
    {
    half4 sum = half4(0,0,0,0);

    #define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))

    sum += GRABXYPIXEL(0.0, 0.0);
    int measurments = 1;

    for (float range = 0.1f; range <= _Radius; range += 0.1f)
    {
    sum += GRABXYPIXEL(range, range);
    sum += GRABXYPIXEL(range, -range);
    sum += GRABXYPIXEL(-range, range);
    sum += GRABXYPIXEL(-range, -range);
    measurments += 4;
    }

    return sum / measurments;
    }
    ENDCG
    }
    GrabPass
    {
    Tags{ "LightMode" = "Always" }
    }

    Pass
    {
    Tags{ "LightMode" = "Always" }

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    #include "UnityCG.cginc"

    struct appdata_t
    {
    float4 vertex : POSITION;
    float2 texcoord: TEXCOORD0;
    };

    struct v2f
    {
    float4 vertex : POSITION;
    float4 uvgrab : TEXCOORD0;
    };

    v2f vert(appdata_t v)
    {
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    #if UNITY_UV_STARTS_AT_TOP
    float scale = -1.0;
    #else
    float scale = 1.0;
    #endif
    o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    o.uvgrab.zw = o.vertex.zw;
    return o;
    }

    sampler2D _GrabTexture;
    float4 _GrabTexture_TexelSize;
    float _Radius;

    half4 frag(v2f i) : COLOR
    {

    half4 sum = half4(0,0,0,0);
    float radius = 1.41421356237 * _Radius;

    #define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))

    sum += GRABXYPIXEL(0.0, 0.0);
    int measurments = 1;

    for (float range = 1.41421356237f; range <= radius * 1.41; range += 1.41421356237f)
    {
    sum += GRABXYPIXEL(range, 0);
    sum += GRABXYPIXEL(-range, 0);
    sum += GRABXYPIXEL(0, range);
    sum += GRABXYPIXEL(0, -range);
    measurments += 4;
    }

    return sum / measurments;
    }
    ENDCG
    }
    }
    }
    }

     
  16. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    Future here, you don't need to change the shader, just replace this line in the legacy blur shader:

    #include "PostProcessing/Shaders/StdLib.hlsl

    with this line:

    #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl
     
    Chrisdbhr and DrSharky like this.
  17. iamnexxed

    iamnexxed

    Joined:
    Jun 12, 2019
    Posts:
    4
    So cool! You are the best!