Search Unity

Radial blur?

Discussion in 'Editor & General Support' started by Dr.Pure, Oct 12, 2009.

  1. Dr.Pure

    Dr.Pure

    Joined:
    Sep 20, 2009
    Posts:
    136
    Is there any radial blur effect hiding in the world wide unity web somewhere? ^^
    Would be nice to use it with anti aliasing together, maybe in Unity 2.6?
    Or how I could simulate a camera, flying with high velocity, which is actually looking and feeling fast too?
     
  2. screenhogdotcom

    screenhogdotcom

    Joined:
    Aug 23, 2009
    Posts:
    126
    Do you mean a spinning blur (like a fan moving really fast) or do you mean a blur where the center of the screen is clear, but it gets blurrier as you get close to the edges?

    I haven't played with motion blur much, I'm just asking because you could mean a few things by "radial blur".
     
  3. sybixsus2

    sybixsus2

    Joined:
    Feb 16, 2009
    Posts:
    943
    What's wrong with the inbuilt motion blur? (http://unity3d.com/support/documentation/Components/script-MotionBlur.html)

    If your camera is moving forward fast, the blur is - by its very nature - going to be radial. If you're asking because you have Indie, and not Pro, then I think radial blur won't help as that would also require rendering to a texture.

    The only other trick I can think of offhand to increase the sense of speed is to increase the FOV of your camera.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I think the desired effect is like the zoom blur option on Photoshop's radial blur - there is no such effect script that ships with Unity and I couldn't find one on the wiki either. There might be a third party effect script somewhere.
     
  5. Dr.Pure

    Dr.Pure

    Joined:
    Sep 20, 2009
    Posts:
    136
    Example:
    http://www.borisfx.com/images/bcc3/radial_blur.jpg

    @sybixsus2 Yea, the built-in motionblur is cool but it blurs with a dynamic blur shape, the radial thing is static and blurs always in the same way. So it's really a different looking blur, especially when you move the camera.

    More info:
    http://www.gamerendering.com/2008/12/20/radial-blur-filter/

    It seems that this effect isn't too hard to accomplish, so I'll look into some shader scripting but I guess it's too expensive for my project time :( Or maybe someone wants to do it?

    This topic may belong to the ShaderLab now :oops:
     
  6. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    I quickly converted the shader for Unity...
    The the ImageEffects class from "Pro Standard Asset/Image Based" is required.

    Hope it works... I didn't test openGL.

    RadialBlur.shader
    Code (csharp):
    1. Shader "Hidden/radialBlur" {
    2. Properties {
    3.     _MainTex ("Input", RECT) = "white" {}
    4.     _BlurStrength ("", Float) = 0.5
    5.     _BlurWidth ("", Float) = 0.5
    6. }
    7.     SubShader {
    8.         Pass {
    9.             ZTest Always Cull Off ZWrite Off
    10.             Fog { Mode off }
    11.        
    12.     CGPROGRAM
    13.    
    14.     #pragma vertex vert_img
    15.     #pragma fragment frag
    16.     #pragma fragmentoption ARB_precision_hint_fastest
    17.  
    18.     #include "UnityCG.cginc"
    19.  
    20.     uniform samplerRECT _MainTex;
    21.     uniform half _BlurStrength;
    22.     uniform half _BlurWidth;
    23.  
    24.     half4 frag (v2f_img i) : COLOR {
    25.         half4 color = texRECT(_MainTex, i.uv);
    26.        
    27.         // some sample positions
    28.         half samples[10] = half[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08);
    29.        
    30.         //vector to the middle of the screen
    31.         half2 dir = 0.5 - i.uv;
    32.        
    33.         //distance to center
    34.         half dist = sqrt(dir.x*dir.x + dir.y*dir.y);
    35.        
    36.         //normalize direction
    37.         dir = dir/dist;
    38.        
    39.         //additional samples towards center of screen
    40.         half4 sum = color;
    41.         for(int n = 0; n < 10; n++)
    42.         {
    43.             sum += texRECT(_MainTex, i.uv + dir * samples[n] * _BlurWidth);
    44.         }
    45.        
    46.         //eleven samples...
    47.         sum *= 1.0/11.0;
    48.        
    49.         //weighten blur depending on distance to screen center
    50.         half t = dist * _BlurStrength;
    51.         t = clamp(t, 0.0, 1.0);
    52.        
    53.         //blend original with blur
    54.         return mix(color, sum, t);
    55.     }
    56.     ENDCG
    57.         }
    58.     }
    59. }

    RadialBlur.cs
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class RadialBlur : MonoBehaviour
    5. {
    6.     public Shader rbShader;
    7.    
    8.     public float blurStrength = 2.2f;
    9.     public float blurWidth = 1.0f;
    10.  
    11.     private Material rbMaterial = null;
    12.  
    13.     private Material GetMaterial()
    14.     {
    15.         if (rbMaterial == null)
    16.         {
    17.             rbMaterial = new Material(rbShader);
    18.             rbMaterial.hideFlags = HideFlags.HideAndDontSave;
    19.         }
    20.         return rbMaterial;
    21.     }
    22.  
    23.     void Start()
    24.     {
    25.         if (rbShader == null)
    26.         {
    27.             Debug.LogError("shader missing!", this);
    28.         }
    29.     }
    30.  
    31.     void OnRenderImage(RenderTexture source, RenderTexture dest)
    32.     {
    33.         GetMaterial().SetFloat("_BlurStrength", blurStrength);
    34.         GetMaterial().SetFloat("_BlurWidth", blurWidth);
    35.         ImageEffects.BlitWithMaterial(GetMaterial(), source, dest);
    36.     }
    37. }
    38.  
     
  7. Dr.Pure

    Dr.Pure

    Joined:
    Sep 20, 2009
    Posts:
    136
    Thanks! Now I have a .shader and a .cs file, but how I'm supposed to use these files in unity now? :oops:

    The built-in image effects have a script in addition in order to work, I need it too?
     
  8. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    Add the RadialBlur script to the main Camera and in the inspector add the shader into the Rb Shader field...
    If you get an error message like "The name `ImageEffects` does not exist in the current context" you probably need to add the ProStandardAssets.
     
  9. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    @DerWoDaSo - Doesn't seem to work in OpenGL, tested on my Macbook Pro with ATI X1600 graphics. I didn't get any errors reported and I tried different values for the two variables with no apparent change.
     
  10. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    Thanks bigkahuna, the problem is that the shader uses a texRECT sampler and in OpenGL mode. This means the UV Range is not from 0-1, but from 0-textureSize. So several values have to be scaled accordingly.

    Here is a little work around for that. But isn't there a ... more elegant way?

    radialBlur.shader
    Code (csharp):
    1. Shader "Hidden/radialBlur" {
    2. Properties {
    3.     _MainTex ("Input", RECT) = "white" {}
    4.     _BlurStrength ("", Float) = 0.5
    5.     _BlurWidth ("", Float) = 0.5
    6. }
    7.     SubShader {
    8.         Pass {
    9.             ZTest Always Cull Off ZWrite Off
    10.             Fog { Mode off }
    11.        
    12.     CGPROGRAM
    13.    
    14.     #pragma vertex vert_img
    15.     #pragma fragment frag
    16.     #pragma fragmentoption ARB_precision_hint_fastest
    17.  
    18.     #include "UnityCG.cginc"
    19.  
    20.     uniform samplerRECT _MainTex;
    21.     uniform half _BlurStrength;
    22.     uniform half _BlurWidth;
    23.     uniform half _iWidth;
    24.     uniform half _iHeight;
    25.  
    26.     half4 frag (v2f_img i) : COLOR {
    27.         half4 color = texRECT(_MainTex, i.uv);
    28.        
    29.         // some sample positions
    30.         half samples[10] = half[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08);
    31.        
    32.         //vector to the middle of the screen
    33.         half2 dir = 0.5 * half2(_iHeight,_iWidth) - i.uv;
    34.        
    35.         //distance to center
    36.         half dist = sqrt(dir.x*dir.x + dir.y*dir.y);
    37.        
    38.         //normalize direction
    39.         dir = dir/dist;
    40.        
    41.         //additional samples towards center of screen
    42.         half4 sum = color;
    43.         for(int n = 0; n < 10; n++)
    44.         {
    45.             sum += texRECT(_MainTex, i.uv + dir * samples[n] * _BlurWidth * _iWidth);
    46.         }
    47.        
    48.         //eleven samples...
    49.         sum *= 1.0/11.0;
    50.        
    51.         //weighten blur depending on distance to screen center
    52.         half t = dist * _BlurStrength / _iWidth;
    53.         t = clamp(t, 0.0, 1.0);
    54.        
    55.         //blend original with blur
    56.         return mix(color, sum, t);
    57.     }
    58.     ENDCG
    59.         }
    60.     }
    61. }

    radialBlur.cs
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class RadialBlur : MonoBehaviour
    5. {
    6.     public Shader rbShader;
    7.    
    8.     public float blurStrength = 2.2f;
    9.     public float blurWidth = 1.0f;
    10.  
    11.     private Material rbMaterial = null;
    12.     private bool isOpenGL;
    13.  
    14.     private Material GetMaterial()
    15.     {
    16.         if (rbMaterial == null)
    17.         {
    18.             rbMaterial = new Material(rbShader);
    19.             rbMaterial.hideFlags = HideFlags.HideAndDontSave;
    20.         }
    21.         return rbMaterial;
    22.     }
    23.  
    24.     void Start()
    25.     {
    26.         if (rbShader == null)
    27.         {
    28.             Debug.LogError("shader missing!", this);
    29.         }
    30.         isOpenGL = SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL");
    31.     }
    32.  
    33.     void OnRenderImage(RenderTexture source, RenderTexture dest)
    34.     {
    35.         //If we run in OpenGL mode, our UV coords are
    36.         //not in 0-1 range, because of the texRECT sampler
    37.         float ImageWidth = 1;
    38.         float ImageHeight = 1;
    39.         if (isOpenGL)
    40.         {
    41.             ImageWidth = source.width;
    42.             ImageHeight = source.height;
    43.         }
    44.  
    45.         GetMaterial().SetFloat("_BlurStrength", blurStrength);
    46.         GetMaterial().SetFloat("_BlurWidth", blurWidth);
    47.         GetMaterial().SetFloat("_iHeight",ImageWidth);
    48.         GetMaterial().SetFloat("_iWidth", ImageHeight);
    49.         ImageEffects.BlitWithMaterial(GetMaterial(), source, dest);
    50.     }
    51. }
     
  11. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    That fixed it! :)

    This would make a very nice addition to the wiki...
     

    Attached Files:

  12. Dr.Pure

    Dr.Pure

    Joined:
    Sep 20, 2009
    Posts:
    136
    Yeah, really nice =D

    I searched for this first in the wiki, so there it would be good placed :idea:
     
  13. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Is this effect fully compatible with Unity 3.0?

    thanks
     
  14. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    Do you have unity3 compatible version?
     
  15. GennadiyKorol

    GennadiyKorol

    Joined:
    Jul 10, 2010
    Posts:
    107
    radialBlur.shader
    Code (csharp):
    1. Shader "Hidden/radialBlur" {
    2. Properties {
    3.     _MainTex ("Input", RECT) = "white" {}
    4.     _BlurStrength ("", Float) = 0.5
    5.     _BlurWidth ("", Float) = 0.5
    6. }
    7.     SubShader {
    8.         Pass {
    9.             ZTest Always Cull Off ZWrite Off
    10.             Fog { Mode off }
    11.        
    12.     CGPROGRAM
    13.    
    14.     #pragma vertex vert_img
    15.     #pragma fragment frag
    16.     #pragma fragmentoption ARB_precision_hint_fastest
    17.  
    18.     #include "UnityCG.cginc"
    19.  
    20.     uniform samplerRECT _MainTex;
    21.     uniform half _BlurStrength;
    22.     uniform half _BlurWidth;
    23.     uniform half _iWidth;
    24.     uniform half _iHeight;
    25.  
    26.     half4 frag (v2f_img i) : COLOR {
    27.         half4 color = texRECT(_MainTex, i.uv);
    28.        
    29.         // some sample positions
    30.         half samples[10] = half[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08);
    31.        
    32.         //vector to the middle of the screen
    33.         half2 dir = 0.5 * half2(_iHeight,_iWidth) - i.uv;
    34.        
    35.         //distance to center
    36.         half dist = sqrt(dir.x*dir.x + dir.y*dir.y);
    37.        
    38.         //normalize direction
    39.         dir = dir/dist;
    40.        
    41.         //additional samples towards center of screen
    42.         half4 sum = color;
    43.         for(int n = 0; n < 10; n++)
    44.         {
    45.             sum += texRECT(_MainTex, i.uv + dir * samples[n] * _BlurWidth * _iWidth);
    46.         }
    47.        
    48.         //eleven samples...
    49.         sum *= 1.0/11.0;
    50.        
    51.         //weighten blur depending on distance to screen center
    52.         half t = dist * _BlurStrength / _iWidth;
    53.         t = clamp(t, 0.0, 1.0);
    54.        
    55.         //blend original with blur
    56.         return lerp(color, sum, t);
    57.     }
    58.     ENDCG
    59.         }
    60.     }
    61. }


    This works for me in 3.0. Just changed the mix to lerp and it seems to work well.
     
  16. razormax

    razormax

    Joined:
    Nov 2, 2010
    Posts:
    151

    how to make this work on iPad2 ?
     
  17. razormax

    razormax

    Joined:
    Nov 2, 2010
    Posts:
    151

    how to make this work on iPad2 with Unity3.4.1 Pro?
     
  18. njp16330

    njp16330

    Joined:
    Jan 15, 2012
    Posts:
    3
    Thanks man.... Your shader works great for me....
     
  19. daminos

    daminos

    Joined:
    Jun 24, 2012
    Posts:
    3
    on OpenGL mode it is not work :S, any idea ?
     
  20. will_jones

    will_jones

    Joined:
    Dec 20, 2011
    Posts:
    80
    can this be edited to include only motion? It seems to stay on for me and I could use it for just motion
     
  21. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    You control shader aperance from script which is above.