Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Optimise adding shadows in scene using Unity iPhone 2 Answers

Reflect Fresnel Mobile VR 0 Answers

Strange, triangle shaped holes appearing in my applied shader. 1 Answer

Changing material with custom shader while instantiating prefab at runtime leads to change in color of all the instances 1 Answer

Mobile: Diffuse Cutout shader with NormalMap. Very bad performance. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges