Search Unity

Blur shaders not working on iOS

Discussion in 'Shaders' started by Biggix, Apr 18, 2015.

  1. Biggix

    Biggix

    Joined:
    Dec 8, 2014
    Posts:
    44
    Hi guys,

    I have made 2 different blur shaders for my project. They work in the editor, but are not visible when I try to run the project on iOS. And when I switch to Android, I get a black rectangle instead of the first shader.

    Does anyone know how to remedy the situation? I can't actually upgrade to Unity 5 with this project as too many things get broken. So for now I'm running Unity 4.6.3f1. I tried to select Automatic & Metal in iOS graphic settings with no positive effect. Selecting OpenGL ES 3.0 produces black screen on iPhone - iOS 8.3, xCode 6.3.

    GaussianBlur shader (works on RenderTexture as it purpose is to blur everything behind it)

    Code (CSharp):
    1. Shader "Custom/BlurGauss" {
    2.     Properties {
    3.     _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    4.     _blurSizeXY("BlurSizeXY", Range(0,10)) = 0
    5. }
    6.     SubShader {
    7.  
    8.         // Draw ourselves after all opaque geometry
    9.         Tags { "Queue" = "Transparent" }
    10.  
    11.         // Grab the screen behind the object into _GrabTexture
    12.         GrabPass { }
    13.  
    14.         // Render the object with the texture generated above
    15.         Pass {
    16.  
    17.  
    18. CGPROGRAM
    19. #pragma target 3.0
    20. #pragma vertex vert
    21. #pragma fragment frag
    22. #include "UnityCG.cginc"
    23.  
    24.  
    25.  
    26.             sampler2D _GrabTexture : register(s0);
    27.             float _blurSizeXY;
    28.  
    29. struct data {
    30.  
    31.     float4 vertex : POSITION;
    32.  
    33.     float3 normal : NORMAL;
    34.  
    35. };
    36.  
    37.  
    38. struct v2f {
    39.  
    40.     float4 position : POSITION;
    41.  
    42.     float4 screenPos : TEXCOORD0;
    43.  
    44. };
    45.  
    46.  
    47. v2f vert(data i){
    48.  
    49.     v2f o;
    50.  
    51.     o.position = mul(UNITY_MATRIX_MVP, i.vertex);
    52.  
    53.     o.screenPos = o.position;
    54.  
    55.     return o;
    56.  
    57. }
    58.  
    59.  
    60. half4 frag( v2f i ) : COLOR
    61.  
    62. {
    63.  
    64.     float2 screenPos = i.screenPos.xy / i.screenPos.w;
    65.     float depth= _blurSizeXY*0.0005;
    66.  
    67.     screenPos.x = (screenPos.x + 1) * 0.5;
    68.  
    69.     screenPos.y = (screenPos.y + 1) * 0.5; //was 1-
    70.  
    71.     half4 sum = half4(0.0h,0.0h,0.0h,0.0h);
    72.     sum += tex2D( _GrabTexture, float2(screenPos.x-5.0 * depth, screenPos.y+5.0 * depth)) * 0.025;
    73.     sum += tex2D( _GrabTexture, float2(screenPos.x+5.0 * depth, screenPos.y-5.0 * depth)) * 0.025;
    74.  
    75.     sum += tex2D( _GrabTexture, float2(screenPos.x-4.0 * depth, screenPos.y+4.0 * depth)) * 0.05;
    76.     sum += tex2D( _GrabTexture, float2(screenPos.x+4.0 * depth, screenPos.y-4.0 * depth)) * 0.05;
    77.  
    78.  
    79.     sum += tex2D( _GrabTexture, float2(screenPos.x-3.0 * depth, screenPos.y+3.0 * depth)) * 0.09;
    80.     sum += tex2D( _GrabTexture, float2(screenPos.x+3.0 * depth, screenPos.y-3.0 * depth)) * 0.09;
    81.  
    82.     sum += tex2D( _GrabTexture, float2(screenPos.x-2.0 * depth, screenPos.y+2.0 * depth)) * 0.12;
    83.     sum += tex2D( _GrabTexture, float2(screenPos.x+2.0 * depth, screenPos.y-2.0 * depth)) * 0.12;
    84.  
    85.     sum += tex2D( _GrabTexture, float2(screenPos.x-1.0 * depth, screenPos.y+1.0 * depth)) *  0.15;
    86.     sum += tex2D( _GrabTexture, float2(screenPos.x+1.0 * depth, screenPos.y-1.0 * depth)) *  0.15;
    87.  
    88.     sum += tex2D( _GrabTexture, screenPos-5.0 * depth) * 0.025;
    89.     sum += tex2D( _GrabTexture, screenPos-4.0 * depth) * 0.05;
    90.     sum += tex2D( _GrabTexture, screenPos-3.0 * depth) * 0.09;
    91.     sum += tex2D( _GrabTexture, screenPos-2.0 * depth) * 0.12;
    92.     sum += tex2D( _GrabTexture, screenPos-1.0 * depth) * 0.15;
    93.     sum += tex2D( _GrabTexture, screenPos) * 0.16;
    94.     sum += tex2D( _GrabTexture, screenPos+5.0 * depth) * 0.15;
    95.     sum += tex2D( _GrabTexture, screenPos+4.0 * depth) * 0.12;
    96.     sum += tex2D( _GrabTexture, screenPos+3.0 * depth) * 0.09;
    97.     sum += tex2D( _GrabTexture, screenPos+2.0 * depth) * 0.05;
    98.     sum += tex2D( _GrabTexture, screenPos+1.0 * depth) * 0.025;
    99.    
    100.     return sum/1.8;
    101.  
    102. }
    103. ENDCG
    104.         }
    105.     }
    106.  
    107. Fallback Off
    108. }
    Texture Directional Blur (works on a texture - supposed to blur a selected sprite vertically)

    Code (CSharp):
    1.  
    2. Shader"Unlit/TransparentColoredBlurred"
    3. {
    4. Properties {
    5.  
    6.     _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
    7.  
    8.     _blurSizeXY("BlurSizeXY", Range(0,10)) = 0
    9.   }
    10.   SubShader
    11.   {
    12.     LOD 100
    13.     Tags
    14.     {
    15.       "Queue" = "Transparent"
    16.       "IgnoreProjector" = "True"
    17.       "RenderType" = "Transparent"
    18.     }
    19.     Cull Off
    20.     Lighting Off
    21.     ZWrite Off
    22.     Fog { Mode Off }
    23.     Offset -1, -1
    24.     Blend SrcAlpha OneMinusSrcAlpha
    25.     Pass
    26.     {
    27.       CGPROGRAM
    28.       #pragma vertex vertexProgram
    29.       #pragma fragment fragmentProgram
    30.         #pragma target 3.0
    31.       #include "UnityCG.cginc"
    32.  
    33.       float _blurSizeXY;
    34.       struct appdata_t
    35.       {
    36.         float4 vertex : POSITION;
    37.         float2 textureCoordinate : TEXCOORD0;
    38.         fixed4 color : COLOR;
    39.       };
    40.       struct vertexToFragment
    41.       {
    42.         float4 vertex : SV_POSITION;
    43.         half2 textureCoordinate : TEXCOORD0;
    44.         fixed4 color : COLOR;
    45.       };
    46.       sampler2D _MainTex;
    47.       float4 _MainTex_ST;
    48.       float _Distance;
    49.       vertexToFragment vertexProgram (appdata_t vertexData)
    50.       {
    51.         vertexToFragment output;
    52.         output.vertex = mul(UNITY_MATRIX_MVP, vertexData.vertex);
    53.         output.textureCoordinate = TRANSFORM_TEX(vertexData.textureCoordinate, _MainTex);
    54.         output.color = vertexData.color;
    55.         return output;
    56.       }
    57.       fixed4 fragmentProgram (vertexToFragment input) : COLOR
    58.       {
    59.         float distance = _blurSizeXY * 0.00032;
    60.    
    61.         float depth= _blurSizeXY*0.0009;
    62.    
    63.    
    64.         fixed4 computedColor = tex2D(_MainTex, input.textureCoordinate) * 0.3;
    65.    
    66.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y + distance * 2)) * 0.15;
    67.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y - distance * 2)) * 0.15;
    68.  
    69.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y + distance * 1.5)) * 0.35;
    70.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y - distance * 1.5)) * 0.35;
    71.    
    72.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y + 5.0 * depth)) * 0.025;
    73.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y - 5.0 * depth)) * 0.025;
    74.    
    75.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y + 4.0 * depth)) * 0.05;
    76.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y - 4.0 * depth)) * 0.05;
    77.    
    78.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y + 3.0 * depth)) * 0.09;
    79.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y - 3.0 * depth)) * 0.09;
    80.    
    81.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y + 2.0 * depth)) * 0.12;
    82.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y - 2.0 * depth)) * 0.12;
    83.    
    84.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y + 1.0 * depth)) *  0.15;
    85.         computedColor += tex2D(_MainTex, half2(input.textureCoordinate.x, input.textureCoordinate.y - 1.0 * depth)) *  0.15;
    86.    
    87.  
    88.         return computedColor*(0.50+(depth*10));
    89.      
    90.    
    91.       }
    92.       ENDCG
    93.     }
    94.   }
    95. }
     
  2. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    did u try target at OpenGL ES 2.0?

    #pragma target 2.0

    player setting , graphics API also OpenGL ES 2.0
     
  3. Biggix

    Biggix

    Joined:
    Dec 8, 2014
    Posts:
    44
    Yes indeed it helped! thanks! I had to modify the first one a bit since it exceeded 64 instructions.
    Is it possible to increase the instruction limit with a setting in the shader? Would it have serious performance impact?