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

Problems with Blur shader!

Discussion in 'Shaders' started by Craig83, Apr 7, 2009.

  1. Craig83

    Craig83

    Joined:
    Nov 18, 2008
    Posts:
    15
    Hi there,

    I am trying to write my first blur shader and at the moment I am not having much luck. I can get the shader to display the texture, however for some unknown reason the blur code that I have implemented is ineffective.

    My code is as follows...

    Code (csharp):
    1.  
    2. Shader "Basic" {
    3.     Properties {
    4.         _MyTexture ("Texture", 2D) = "white" {}
    5.     }
    6.    
    7.     SubShader {
    8.         Pass {
    9.            
    10.             CGPROGRAM
    11.             #pragma vertex vert
    12.             #pragma fragment frag
    13.             #pragma fragmentoption ARB_fog_exp2
    14.             #include "UnityCG.cginc"
    15.            
    16.             sampler2D _MyTexture;
    17.            
    18.             static const int cKernelSize = 13;
    19.            
    20.             //static const half4 samples[9] = {
    21.             //  -1.0,   -1.0,   0,      1.0/16.0,
    22.             //  -1.0,   1.0,    0,      1.0/16.0,
    23.             //  1.0,    -1.0,   0,      1.0/16.0,
    24.             //  1.0,    1.0,    0,      1.0/16.0,
    25.             //  -1.0,   0.0,    0,      2.0/16.0,
    26.             //  1.0,    0.0,    0,      2.0/16.0,
    27.             //  0.0,    -1.0,   0,      2.0/16.0,
    28.             //  0.0,    1.0,    0,      2.0/16.0,
    29.             //  0.0,    0.0,    0,      4.0/16.0
    30.             //};
    31.            
    32.             static const float4 samples[4] = {
    33.                 -1.0,   0.0,    0,      0.25,
    34.                  1.0,   0.0,    0,      0.25,
    35.                  0.0,   1.0,    0,      0.25,
    36.                  0.0,   -1.0,   0,      0.25
    37.             };
    38.            
    39.             struct v2f {
    40.                 float4 vertex : POSITION;
    41.                 float2  textCoord : TEXCOORD0;
    42.             };
    43.            
    44.             v2f vert (appdata_base v) {
    45.                 v2f o;
    46.                 o.textCoord = TRANSFORM_UV(0);
    47.                 o.vertex = mul(glstate.matrix.mvp, v.vertex);
    48.                 return o;
    49.             }
    50.            
    51.            
    52.             float4 frag (v2f vert) : COLOR {
    53.                
    54.                 float4 col = float4(0,0,0,0);
    55.                
    56.                 for(int i=0;i<4;i++){
    57.                     col += samples[i].w*tex2D(_MyTexture, vert.textCoord+     float2(samples[i].x*(1/512),samples[i].x*(1/512)));
    58.                 }              
    59.                 return col;
    60.                        
    61.                
    62.                
    63.             //  for(int i=0; i<4; i++){
    64.             //      float2 sampleTexCoords=float2(samples[i].x*(1/512),samples[i].y*(1/512));
    65.             //      col += samples[i].w*tex2D(_MyTexture,vert.textCoord+sampleTexCoords);
    66.             //  }
    67.             //  return col;
    68.                
    69.             }
    70.            
    71.             ENDCG
    72.         }
    73.     }
    74. }
    75.  
    Any help with my code would be greatly appreciated!

    Cheers,

    Craig
     
  2. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    The value (1/512) is probably always zero as you're dividing an integer.

    /P
     
  3. Craig83

    Craig83

    Joined:
    Nov 18, 2008
    Posts:
    15
    Ah, i see, many thanks for your reply!

    How would you recommend I could go about getting an
    appropriate value in there for my 512x512 texture?

    kind regards

    Craig
     
  4. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Since it's a fixed value for all pixels I'd pass it in from a script. That way you can also calculate it based on the actual texture size and have the shader work with different sized textures. Like this (untested):

    In Start():
    Code (csharp):
    1. Vector4 offset = new vector4(1f/texture.width,1f/texture.height,0,0);
    2. renderer.material.SetVector("_Offset", offset);
    and in your shader:
    Code (csharp):
    1. //declare variable for fragment program
    2. float4 _Offset;
    3.  
    4. //use it instead of (1/512) in fragment program
    5.  
    6. for(int i=0;i<4;i++){
    7.                 col += samples[i].w*tex2D(_MyTexture, vert.textCoord+  float2(samples[i].x*_Offset.x,samples[i].y*_Offset.y));
    /P
     
  5. Craig83

    Craig83

    Joined:
    Nov 18, 2008
    Posts:
    15
    Hi there,

    Again many thanks for your reply, that worked perfectly and
    I managed to get the texture on my object blurring :D!

    One other question if i may, how would you go about doing
    multiple passes so that I can control how blurry my image
    can be??

    kind regards,

    Craig
     
  6. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203