Search Unity

Mobile Shader that swaps in an image

Discussion in 'Shaders' started by davidmurdoch, May 27, 2017.

  1. davidmurdoch

    davidmurdoch

    Joined:
    Feb 3, 2017
    Posts:
    1
    I'm using a shader similar to the following. It works on desktop but not on Android (2013 Moto X and some others tested).

    Code (csharp):
    1.  
    2.     Shader "Infinity Code/Online Maps/Tileset"
    3.     {
    4.         Properties
    5.         {
    6.             _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    7.             _GrassTexture("Grass Texture", 2D) = "green" {}
    8.         }
    9.         SubShader
    10.         {
    11.             Tags {"Queue"="Transparent-100" "IgnoreProjector"="True" "RenderType"="Transparent"}
    12.             LOD 200
    13.             CGPROGRAM
    14.             #pragma surface surf Lambert alpha
    15.             sampler2D _MainTex;
    16.             sampler2D _GrassTexture;
    17.             struct Input
    18.             {
    19.                 float2 uv_MainTex;
    20.             };
    21.             void surf (Input IN, inout SurfaceOutput o)
    22.             {
    23.                 o.Albedo = tex2D(_GrassTexture, IN.uv_MainTex);
    24.                 o.Alpha = 1;
    25.             }
    26.             ENDCG
    27.         }
    28.         Fallback "Transparent/VertexLit"
    29.     }
    `_GrassTexture` is set to an image file that should be used instead of what's in the `IN`. An desktop this works great. On Android it renders just grey.

    Is this sort of thing just not going to be possible on Android or am I doing something wrong?
     
  2. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
    try getting rid of that -100 looks dodgy
    otherwise looks like its a fairly standars transsparent/alpha shader that ignores alpha. ignores the main texture and just uses the grass texture.

    usually simple shaders like this work fine on iOS/Android unless some obscure shader function is used, or too many instruction... which yours obviously doesn't have too many.

    you could also try adding this,,, before CGPROGRAM to set the transparency mode.
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    or if you dont need any alpha... try this to replace your Tags line
    Tags{ "RenderType" = "Opaque" }