Search Unity

Shader not working on Android

Discussion in 'Shaders' started by elomode, Feb 20, 2018.

  1. elomode

    elomode

    Joined:
    Dec 17, 2016
    Posts:
    2
    We have a problem where our shader works fine in the editor, but if we make an android build nothing happens.

    Our shader is a fragment shader that should do a cellular automata simulation. You color exactly one pixel by clicking on it and then the shader expands the pixel in a hexagon like shape.
    The fragment shaders output is written into a RenderTexture and then fed back in for further calculations.

    Sorry if this code makes you barf, this is the first time we are doing shaders and we have zero ideas why this does not do anything on android. We already played around with minimum API settings,always included shader and set the graphics api to opengles3.

    If you have any idea why this is not working, we would really appreciate your help.

    Updating the shader
    Code (CSharp):
    1.         Graphics.Blit(texture, buffer, material);
    2.         Graphics.Blit(buffer, texture);

    Updating the click
    Code (CSharp):
    1.  foreach (Touch touch in Input.touches)
    2.             {
    3.                 if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
    4.                 {
    5.                     pos = Camera.main.ScreenToViewportPoint(touch.position);
    6.                 }
    7.             }
    8.  
    9.             int x = Mathf.RoundToInt(pos.x * pixelSize.x);
    10.             int y = Mathf.RoundToInt(pos.y * pixelSize.y);
    11.  
    12.             material.SetVector("_Click", new Vector4(x, y, 0f, 0f));
    The shader
    Code (CSharp):
    1. Shader "Unlit/GameOfLifeRound"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _PixelsX("PixelsX", Float) = 256
    7.         _PixelsY("PixelsY", Float) = 256
    8.         _Click("Click", Vector) = (0,0,0,0)
    9.         _SpreadValue("Spread", Float) = 0.1
    10.         _Color("Color", Vector) = (0,0,0,0)
    11.         [MaterialToggle] _clearToWhite("ClearToWhite", Float) = 0
    12.     }
    13.     SubShader
    14.     {
    15.         //Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
    16.         LOD 100
    17.  
    18.         Pass
    19.         {
    20.             //ZWrite Off
    21.             //Blend SrcAplha OneMinusSrcAplha
    22.  
    23.             CGPROGRAM
    24.  
    25.     #pragma vertex vert
    26.     #pragma fragment frag
    27.  
    28.     #include "UnityCG.cginc"
    29.  
    30.      #pragma target 3.0
    31.  
    32.             struct appdata
    33.         {
    34.             float4 vertex : POSITION;
    35.             float2 uv : TEXCOORD0;
    36.         };
    37.  
    38.         struct v2f
    39.         {
    40.             float2 uv : TEXCOORD0;
    41.             float4 vertex : SV_POSITION;
    42.         };
    43.  
    44.         sampler2D _MainTex;
    45.         float4 _MainTex_ST;
    46.         float _PixelsX;
    47.         float _PixelsY;
    48.         float4 _Click;
    49.         float _clearToWhite;
    50.         float _SpreadValue;
    51.         float4 _Color;
    52.  
    53.         v2f vert(appdata v)
    54.         {
    55.             v2f o;
    56.             o.vertex = UnityObjectToClipPos(v.vertex);
    57.             o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    58.             UNITY_TRANSFER_FOG(o,o.vertex);
    59.             return o;
    60.         }
    61.  
    62.         fixed4 frag(v2f i) : SV_TARGET
    63.         {
    64.             if (_clearToWhite == 1)
    65.             {
    66.                 return float4(1,1,1,0);
    67.             }
    68.  
    69.         float s_x = 1 / _PixelsX;
    70.         float s_y = 1 / _PixelsY;
    71.  
    72.         float4 left = tex2D(_MainTex, i.uv + fixed2(-s_x, 0)); // Centre Left
    73.         float4 top = tex2D(_MainTex, i.uv + fixed2(-0, -s_y)); // Top Centre
    74.         float4 center = tex2D(_MainTex, i.uv + fixed2(0, 0)); // Centre Centre
    75.         float4 bottom = tex2D(_MainTex, i.uv + fixed2(0, +s_y)); // Bottom Centre
    76.         float4 right = tex2D(_MainTex, i.uv + fixed2(+s_x, 0)); // Centre Right
    77.         float4 topleft = tex2D(_MainTex, i.uv + fixed2(-s_x, -s_y));
    78.         float4 topright = tex2D(_MainTex, i.uv + fixed2(+s_x, -s_y));
    79.         float4 bottomleft = tex2D(_MainTex, i.uv + fixed2(-s_x, +s_y));
    80.         float4 bottomright = tex2D(_MainTex, i.uv + fixed2(+s_x, +s_y));
    81.  
    82.         if (center.a >= 1)
    83.         {
    84.             return center;
    85.         }
    86.  
    87.         float spreadvalue = 0.05;
    88.  
    89.         if (center.a % spreadvalue == 0)
    90.         {
    91.             if (top.a > _SpreadValue)
    92.             {
    93.                 return float4(top.r, top.g, top.b, center.a + spreadvalue);
    94.             }
    95.             if (right.a > _SpreadValue)
    96.             {
    97.                 return float4(right.r, right.g, right.b, center.a + spreadvalue);
    98.             }
    99.             if (bottom.a > _SpreadValue)
    100.             {
    101.                 return float4(bottom.r, bottom.g, bottom.b, center.a + spreadvalue);
    102.             }
    103.             if (left.a > _SpreadValue)
    104.             {
    105.                 return float4(left.r, left.g, left.b, center.a + spreadvalue);
    106.             }
    107.         }
    108.         else
    109.         {
    110.             _SpreadValue *= 1.5;
    111.             if (topleft.a > _SpreadValue)
    112.             {
    113.                 return float4(topleft.r, topleft.g, topleft.b, center.a + spreadvalue);
    114.             }
    115.             if (topright.a > _SpreadValue)
    116.             {
    117.                 return float4(topright.r, topright.g, topright.b, center.a + spreadvalue);
    118.             }
    119.             if (bottomright.a > _SpreadValue)
    120.             {
    121.                 return float4(bottomright.r, bottomright.g, bottomright.b, center.a + spreadvalue);
    122.             }
    123.             if (bottomleft.a > _SpreadValue)
    124.             {
    125.                 return float4(bottomleft.r, bottomleft.g, bottomleft.b, center.a + spreadvalue);
    126.             }
    127.         }
    128.  
    129.         //Draw Pixels depending on _Click
    130.         float x = round(i.uv.r * _PixelsX);
    131.         float y = round(i.uv.g * _PixelsY);
    132.  
    133.         if (y == _Click.g && x == _Click.r)
    134.         {
    135.             return _Color;
    136.         }
    137.         return tex2D(_MainTex,i.uv);
    138.         }
    139.         ENDCG
    140.         }
    141.     }
    142. }
    143.  
     
  2. LukasCh

    LukasCh

    Unity Technologies

    Joined:
    Mar 9, 2015
    Posts:
    102
    Could you elaborate this part? What do you mean "android build nothing happens" is it pink (Our internal shader for error) or black (In case of NaN or other issues) or is it not rendered?
    I really recommend check logcat for some hints from shader compilation by drivers, because it is really hard to tell simply from shader.