Problems with Blur shader!

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…

Shader "Basic" {
    Properties {
        _MyTexture ("Texture", 2D) = "white" {} 
    }
    
    SubShader {
        Pass {
        	
        	CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_fog_exp2
			#include "UnityCG.cginc"
      		
      		sampler2D _MyTexture;
      		
      		static const int cKernelSize = 13;
      		
			//static const half4 samples[9] = {
			//	-1.0,	-1.0,	0,		1.0/16.0,
			//	-1.0,	1.0,	0,		1.0/16.0,
			//	1.0,	-1.0,	0,		1.0/16.0,
			//	1.0,	1.0,	0,		1.0/16.0,
			//	-1.0,	0.0,	0,		2.0/16.0,
			//	1.0,	0.0,	0,		2.0/16.0,
			//	0.0,	-1.0,	0,		2.0/16.0,
			//	0.0,	1.0,	0,		2.0/16.0,
			//	0.0,	0.0,	0,		4.0/16.0
			//};
			
			static const float4 samples[4] = {
				-1.0,	0.0,	0,		0.25,
				 1.0,	0.0,	0,		0.25,
				 0.0,	1.0,	0,		0.25,
				 0.0,	-1.0,	0,		0.25
			};
      		
      		struct v2f {
      			float4 vertex : POSITION; 
    			float2  textCoord : TEXCOORD0;
			};
			
			v2f vert (appdata_base v) {
    			v2f o;
    			o.textCoord = TRANSFORM_UV(0);
    			o.vertex = mul(glstate.matrix.mvp, v.vertex);
    			return o;
			}
			
			
			float4 frag (v2f vert) : COLOR {
    			
    			float4 col = float4(0,0,0,0);
    			
    			for(int i=0;i<4;i++){
    				col += samples[i].w*tex2D(_MyTexture, vert.textCoord+     float2(samples[i].x*(1/512),samples[i].x*(1/512)));
    			}    			
    			return col;
    					
    			
    			
    		//	for(int i=0; i<4; i++){
    		//		float2 sampleTexCoords=float2(samples[i].x*(1/512),samples[i].y*(1/512));
    		//		col += samples[i].w*tex2D(_MyTexture,vert.textCoord+sampleTexCoords);
    		//	}
    		//	return col;
    			
			}
      		
      		ENDCG
        }
    }
}

Any help with my code would be greatly appreciated!

Cheers,

Craig

The value (1/512) is probably always zero as you’re dividing an integer.

/P

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

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():

Vector4 offset = new vector4(1f/texture.width,1f/texture.height,0,0);
renderer.material.SetVector("_Offset", offset);

and in your shader:

//declare variable for fragment program
float4 _Offset;

//use it instead of (1/512) in fragment program

for(int i=0;i<4;i++){ 
                col += samples[i].w*tex2D(_MyTexture, vert.textCoord+  float2(samples[i].x*_Offset.x,samples[i].y*_Offset.y));

/P

Hi there,

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

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

You can add more Pass{} blocks to your shader, see docs:

/P