Search Unity

Shaders on mobile not working

Discussion in 'Shaders' started by Skunkynator, Jan 22, 2020.

  1. Skunkynator

    Skunkynator

    Joined:
    Jan 22, 2020
    Posts:
    6
    I've recently started learning how to write shaders and how to apply them to a camera
    i tried to use a few of my custom shaders on android but they just returned a black screen or flickering black/white screen with a few pixel being the other shade, or rarely a random colour
    even the default invert preset does it

    i can only think of the tex2d function not returning the colour of the screen pixel or _mainTex not being set to the rendered image of the camera (it wouldnt explain the rare random coloured pixels tho)

    this is one of my custom shaders thats acting weird on android for example
    Code (CSharp):
    1. Shader "Camera/circleInvert"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _speed ("Circle Speed", float) = 1
    7.         _size ("Circle Size", float) = 1
    8.     }
    9.     SubShader
    10.     {
    11.         // No culling or depth
    12.         Cull Off ZWrite Off ZTest Always
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.  
    34.             v2f vert (appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 o.uv = v.uv;
    39.                 return o;
    40.             }
    41.  
    42.             sampler2D _MainTex;
    43.             float _size;
    44.             float _speed;
    45.  
    46.             fixed4 frag (v2f i) : SV_Target
    47.             {
    48.                 float2 uvMid = i.uv - 0.5;
    49.                 float dist = sqrt(uvMid.x * uvMid.x + uvMid.y * uvMid.y);
    50.                 float timing = ( sign( sin(_Time[1] * _speed+dist * _size)) + 1)/2.;
    51.                
    52.                 i.uv.y = timing * i.uv.y + (1-timing) * (1-i.uv.y);
    53.  
    54.                 fixed4 col = tex2D(_MainTex, i.uv);
    55.                 col.rgb = timing * col.rgb + (1-timing)*(1-col.rgb);
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61. }
    does anyone know why it wont work on android?
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Try commenting out everything in your frag function and leave just lines 54 and 56. If this doesn't work, then there's indeed some problem outside of the shader.
     
  3. Skunkynator

    Skunkynator

    Joined:
    Jan 22, 2020
    Posts:
    6
    this just returns a black screen

    as said in my question, even the default template (which is just colour inversion) returns a flashing screen with random pixel not being inverted and some rare pixels even having a random color
    if needed i can upload screenshots of the different expectations and outcomes

    tried with unity versions 2019.3.0 and 2019.1.6
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Is there anything in the logcat on the device?
     
  5. Skunkynator

    Skunkynator

    Joined:
    Jan 22, 2020
    Posts:
    6
    ive not known of logcat yet but this is the error: (the shader is still the above with only get colour and return)
    upload_2020-1-23_10-40-34.png
     

    Attached Files:

  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    ok, and how exactly are you rendering stuff with this shader?
     
  7. Skunkynator

    Skunkynator

    Joined:
    Jan 22, 2020
    Posts:
    6
    i have a monobehavior class in which i use graphic.blit in onRenderImage
    code(it works normaly in editor and windows builds):
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class CameraMaterials : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     List<Material> materials = new List<Material>();
    6.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    7.     {
    8.         foreach(Material shader in materials)
    9.         {
    10.             Graphics.Blit(source, source, shader);
    11.         }
    12.         Graphics.Blit(source, destination);
    13.     }
    14. }
     
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Alright :)
    Can you please make a bug report?
    This will help figure out, what's exactly incorrect there.
     
  9. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    The problem is caused by using the same render texture as a source and destination for blit
    Graphics.Blit(source, source, shader);
     
    aleksandrk and Skunkynator like this.