Search Unity

Custom shader works in editor, displays in all pink on Android. Fragment shader shows in logcat

Discussion in 'Shaders' started by KayH, Sep 6, 2017.

  1. KayH

    KayH

    Joined:
    Jan 6, 2017
    Posts:
    110
    Here's the shader I wrote:

    Code (CSharp):
    1.     Shader "Custom/InstancedContourShader"
    2.     {
    3.        Properties
    4.        {
    5.            _Color ("Color", Color) = (1,1,1,1)
    6.            _LineWidthInverse ("LineWidthInverse", Float) = 0.98
    7.            _MainTex ("Texture", 2D) = "white" {}
    8.        }
    9.        SubShader
    10.        {
    11.             Tags {"RenderType"="Opaque" "LightMode"="ForwardBase"}
    12.            LOD 100
    13.  
    14.            Pass
    15.            {
    16.                CGPROGRAM
    17.            #pragma debug
    18.                #pragma vertex vert
    19.                #pragma fragment frag
    20.                 #pragma multi_compile_instancing
    21.  
    22.                #include "UnityCG.cginc"
    23.                 #include "UnityLightingCommon.cginc"
    24.  
    25.                 struct appdata
    26.                 {
    27.                     float4 vertex : POSITION;
    28.                     float4 normal : NORMAL;
    29.                     float2 texcoord: TEXCOORD;
    30.                     UNITY_VERTEX_INPUT_INSTANCE_ID
    31.                 };
    32.  
    33.                struct v2f
    34.                {
    35.                    float2 uv : TEXCOORD0;
    36.                     fixed4 diff : COLOR0;
    37.                    float4 vertex : SV_POSITION;
    38.                     UNITY_VERTEX_INPUT_INSTANCE_ID
    39.                };
    40.  
    41.                sampler2D _MainTex;
    42.                float4 _MainTex_ST;
    43.              
    44.                // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    45.                // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    46.                #pragma instancing_options assumeuniformscaling
    47.                UNITY_INSTANCING_CBUFFER_START(Props)
    48.                    UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
    49.                    UNITY_DEFINE_INSTANCED_PROP(float1, _LineWidthInverse)
    50.        //           fixed4 _Color;
    51.                UNITY_INSTANCING_CBUFFER_END
    52.  
    53.                 v2f vert (appdata v)
    54.                 {
    55.                     v2f o;
    56.                     UNITY_SETUP_INSTANCE_ID(v);
    57.                     UNITY_TRANSFER_INSTANCE_ID(v, o); // necessary only if you want to access instanced properties in the fragment Shader.
    58.                     o.vertex = UnityObjectToClipPos(v.vertex);
    59.                     o.uv = v.texcoord;
    60.                     half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    61.                     half nl = abs(dot(worldNormal, _WorldSpaceLightPos0.xyz));
    62.                     o.diff = nl * _LightColor0;
    63.  
    64.                     // the only difference from previous shader:
    65.                     // in addition to the diffuse lighting from the main light,
    66.                     // add illumination from ambient or light probes
    67.                     // ShadeSH9 function from UnityCG.cginc evaluates it,
    68.                     // using world space normal
    69.                     o.diff.rgb += ShadeSH9(half4(worldNormal,1));
    70.                     return o;
    71.                 }
    72.  
    73.                fixed4 frag (v2f i) : SV_Target
    74.                {
    75.                     UNITY_SETUP_INSTANCE_ID(i); // necessary only if any instanced properties are going to be accessed in the fragment Shader.
    76.                    // sample the texture
    77.                    fixed4 col = tex2D (_MainTex, i.uv) * UNITY_ACCESS_INSTANCED_PROP(_Color);
    78.                     col *= i.diff;
    79.                    float t = max (abs(i.uv.x), abs(i.uv.y));
    80.                    t = min(t - UNITY_ACCESS_INSTANCED_PROP(_LineWidthInverse), 0);
    81.                    t = -sign(t);
    82.                    col *= t;
    83.                    return col;
    84.                }
    85.                ENDCG
    86.            }
    87.        }
    88.     }
    89.  
    The shader is used in several materials, all are directly attached to gameobjects. I tried applying fixes suggested for similar questions like adding the shader to always include in the project settings and disabling stripping for instanced variants. But to no avail.

    Here's the adb logcat output regarding shaders:


    Since the compiled fragment shader part is output several times (once for each material I'm guessing) there should be a problem with the fragment shader specifically but it doesn't tell me what the problem is.

    It's a Gear VR app running on an S7.
     
  2. mjo5501

    mjo5501

    Joined:
    Oct 18, 2018
    Posts:
    1
    Running into the same problem. The shader is loaded, so "Make sure it's loading" isn't a solution.
     
  3. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    This means the shader failed to compile or link on the device.
    Can you provide device logs (from a development build)? The exact error should be shown there.