- Home /
Question by
AtahanTugraKiltan · Mar 31, 2016 at 12:21 PM ·
shaderoptimizationcustom-shader
Improving (a Blur) Shader Performance
Based on the shader offered in the following asset store item: Blur Shader (Pro Only)
I have modified the blur shader to offer Colour tint and texture capabilities. I have close to 0 knowledge about shaders and the fact that I got it working was a surprise for myself.
Due to the lack of my knowledge of how it works, I am afraid that it may be an unnecessarily expensive shader to use. If anyone could advise me on what steps I could take in order to improve the shader's performance, I would be grateful.
I should add that my intended use is for the UI system of Unity. (If by any chance you think this is already a good shader, feel free to use it, or if you manage to improve it, again, use it but please also let me know)
Shader "Custom/WaterBlurColour" {
Properties {
_BlurSizeXY("BlurSizeXY", Range(0,10)) = 0
_Multiply("Multiply", Range(0,1)) = 1
_Brightness("Brightness", Range(0,1)) = 1
_TintColor ("Tint Color", COLOR) = (1, 1, 1, 1)
_MainTex ("Texture", 2D) = "white" { }
_AlphaBoost ("Texture Alpha Boost", Range(1,3)) = 1
}
SubShader {
// Draw ourselves after all opaque geometry
Tags { "Queue" = "Transparent" }
//Allow the use of uGUI colour field
BindChannels {
Bind "Color", color
}
// Grab the screen behind the object into _GrabTexture
GrabPass { }
// Render the object with the texture generated above
Pass {
CGPROGRAM
#pragma debug
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
sampler2D _GrabTexture : register(s0);
float _BlurSizeXY;
float _Multiply;
float _Brightness;
float4 _TintColor : COLOR;
sampler2D _MainTex;
float _AlphaBoost;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 uv : TEXCOORD1;
};
struct v2f {
float4 position : POSITION;
fixed4 color : COLOR;
float4 screenPos : TEXCOORD0;
float2 uv : TEXCOORD1;
};
float4 _MainTex_ST;
v2f vert(appdata_t v){
v2f o;
o.position = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.screenPos = o.position;
o.color = v.color;
return o;
}
half4 frag( v2f i ) : COLOR
{
float2 screenPos = i.screenPos.xy / i.screenPos.w;
float depth= _BlurSizeXY*0.0005;
screenPos.x = (screenPos.x + 1) * 0.5;
screenPos.y = 1-(screenPos.y + 1) * 0.5;
// The user defined Texture, or the UI Sprite
fixed4 texcol = tex2D(_MainTex, i.uv);
// Unmodified GrabPass
fixed4 orig = tex2D(_GrabTexture, screenPos);
half4 sum = half4(0.0h,0.0h,0.0h,0.0h);
//From what I understand, this is responsible for the "BLUR"
sum += tex2D( _GrabTexture, float2(screenPos.x-5.0 * depth, screenPos.y+5.0 * depth)) * 0.025;
sum += tex2D( _GrabTexture, float2(screenPos.x+5.0 * depth, screenPos.y-5.0 * depth)) * 0.025;
sum += tex2D( _GrabTexture, float2(screenPos.x-4.5 * depth, screenPos.y+4.5 * depth)) * 0.0375;
sum += tex2D( _GrabTexture, float2(screenPos.x+4.5 * depth, screenPos.y-4.5 * depth)) * 0.0375;
sum += tex2D( _GrabTexture, float2(screenPos.x-4.0 * depth, screenPos.y+4.0 * depth)) * 0.05;
sum += tex2D( _GrabTexture, float2(screenPos.x+4.0 * depth, screenPos.y-4.0 * depth)) * 0.05;
sum += tex2D( _GrabTexture, float2(screenPos.x-3.5 * depth, screenPos.y+3.5 * depth)) * 0.07;
sum += tex2D( _GrabTexture, float2(screenPos.x+3.5 * depth, screenPos.y-3.5 * depth)) * 0.07;
sum += tex2D( _GrabTexture, float2(screenPos.x-3.0 * depth, screenPos.y+3.0 * depth)) * 0.09;
sum += tex2D( _GrabTexture, float2(screenPos.x+3.0 * depth, screenPos.y-3.0 * depth)) * 0.09;
sum += tex2D( _GrabTexture, float2(screenPos.x-2.5 * depth, screenPos.y+2.5 * depth)) * 0.105;
sum += tex2D( _GrabTexture, float2(screenPos.x+2.5 * depth, screenPos.y-2.5 * depth)) * 0.105;
sum += tex2D( _GrabTexture, float2(screenPos.x-2.0 * depth, screenPos.y+2.0 * depth)) * 0.12;
sum += tex2D( _GrabTexture, float2(screenPos.x+2.0 * depth, screenPos.y-2.0 * depth)) * 0.12;
sum += tex2D( _GrabTexture, float2(screenPos.x-1.5 * depth, screenPos.y+1.5 * depth)) * 0.1325;
sum += tex2D( _GrabTexture, float2(screenPos.x+1.5 * depth, screenPos.y-1.5 * depth)) * 0.1325;
sum += tex2D( _GrabTexture, float2(screenPos.x-1.0 * depth, screenPos.y+1.0 * depth)) * 0.15;
sum += tex2D( _GrabTexture, float2(screenPos.x+1.0 * depth, screenPos.y-1.0 * depth)) * 0.15;
sum += tex2D( _GrabTexture, float2(screenPos.x-0.5 * depth, screenPos.y+0.5 * depth)) * 0.155;
sum += tex2D( _GrabTexture, float2(screenPos.x+0.5 * depth, screenPos.y-0.5 * depth)) * 0.155;
sum += tex2D( _GrabTexture, screenPos-5.0 * depth) * 0.025;
sum += tex2D( _GrabTexture, screenPos-4.5 * depth) * 0.0375;
sum += tex2D( _GrabTexture, screenPos-4.0 * depth) * 0.05;
sum += tex2D( _GrabTexture, screenPos-3.5 * depth) * 0.07;
sum += tex2D( _GrabTexture, screenPos-3.0 * depth) * 0.09;
sum += tex2D( _GrabTexture, screenPos-2.5 * depth) * 0.105;
sum += tex2D( _GrabTexture, screenPos-2.0 * depth) * 0.12;
sum += tex2D( _GrabTexture, screenPos-1.5 * depth) * 0.1325;
sum += tex2D( _GrabTexture, screenPos-1.0 * depth) * 0.15;
sum += tex2D( _GrabTexture, screenPos-0.5 * depth) * 0.155;
sum += tex2D( _GrabTexture, screenPos) * 0.16;
sum += tex2D( _GrabTexture, screenPos+5.0 * depth) * 0.025;
sum += tex2D( _GrabTexture, screenPos+4.5 * depth) * 0.0375;
sum += tex2D( _GrabTexture, screenPos+4.0 * depth) * 0.05;
sum += tex2D( _GrabTexture, screenPos+3.5 * depth) * 0.07;
sum += tex2D( _GrabTexture, screenPos+3.0 * depth) * 0.09;
sum += tex2D( _GrabTexture, screenPos+2.5 * depth) * 0.105;
sum += tex2D( _GrabTexture, screenPos+2.0 * depth) * 0.12;
sum += tex2D( _GrabTexture, screenPos+1.5 * depth) * 0.1325;
sum += tex2D( _GrabTexture, screenPos+1.0 * depth) * 0.15;
sum += tex2D( _GrabTexture, screenPos+0.5 * depth) * 0.155;
// Added this myself, for extra colour of the "blurred" colours
sum *= _Multiply;
/* Using this to Blend the GrabPass Colours, with the tint colour and the texture colour. Based on _Brightness also blends between a multiplicative and additive method (_brightness: 0 = add, 1 = Multiply) */
half4 sumend = (( (sum) + (_TintColor *_Brightness * texcol * i.color)) ) + ( (sum) * (_TintColor * (1-_Brightness) * texcol * i.color) );
/* this allows for the "transparent" regions of the texture to let through the original GrabPass, without "Blur" */
return (orig * (1-(texcol.a/_AlphaBoost))) + sumend*texcol.a*_AlphaBoost;
}
ENDCG
}
}
Fallback Off
}
Comment