Search Unity

Custom Unlit Shader won't work in Android build

Discussion in 'Editor & General Support' started by Adzelliper, Apr 14, 2021.

  1. Adzelliper

    Adzelliper

    Joined:
    Jul 17, 2019
    Posts:
    2
    As the title says, when I build my project the plane with the shader on it comes up as a bright pink. I have already tried putting it in a "Resource" folder, putting it in the Always Included Shaders array in the graphic settings, and putting it on multiple objects to make sure it includes the shader when building. I found that some of the calculations in the shader is causing this problem. It seems that the getForHeatPixel() is causing it to turn pink. If I comment out the second if statement and the for loop, the shader works in the build, but those are used for putting hit points onto the shader and coloring it.

    For context I am making a heatmap and running it in VR on my Oculus Quest 2.

    Code (CSharp):
    1. Shader "Unlit/HeatmapShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color0("Color 0", Color) = (0,0,0,0.1)
    7.         _Color1("Color 1", Color) = (0,.9,.2,.1)
    8.         _Color2("Color 2", Color) = (.1,1,.3,.1)
    9.         _Color3("Color 3", Color) = (.2,0.7,0.1,.1)
    10.         _Color4("Color 4", Color) = (.3,0.7,0.1,.1)
    11.         _Color5("Color 5", Color) = (.4,0.7,0.1,.1)
    12.         _Color6("Color 6", Color) = (.5,0.7,0.1,.1)
    13.         _Color7("Color 7", Color) = (.6,0.7,0.1,.1)
    14.         _Color8("Color 8", Color) = (.7,0.7,0.1,.1)
    15.         _Color9("Color 9", Color) = (.8,0.7,0.1,.1)
    16.         _Color10("Color 10", Color) = (.9,0.7,0.1,.1)
    17.         _Color11("Color 11", Color) = (0,0.1,0.1,.1)
    18.         _Color12("Color 12", Color) = (0,0.2,0.1,.1)
    19.         _Color13("Color 13", Color) = (0,0,0.3,.1)
    20.         _Color14("Color 14", Color) = (0,0,0.4,.1)
    21.         _Color15("Color 15", Color) = (1,0,0,.1)
    22.     }
    23.     SubShader
    24.     {
    25.         Tags { "RenderType"="Opaque" }
    26.         LOD 100
    27.  
    28.         Pass
    29.         {
    30.             CGPROGRAM
    31.             #pragma vertex vert
    32.             #pragma fragment frag
    33.             // make fog work
    34.             #pragma multi_compile_fog
    35.  
    36.             #include "UnityCG.cginc"
    37.  
    38.             struct appdata
    39.             {
    40.                 float4 vertex : POSITION;
    41.                 float2 uv : TEXCOORD0;
    42.             };
    43.  
    44.             struct v2f
    45.             {
    46.                 float2 uv : TEXCOORD0;
    47.                 UNITY_FOG_COORDS(1)
    48.                 float4 vertex : SV_POSITION;
    49.             };
    50.  
    51.             sampler2D _MainTex;
    52.             float4 _MainTex_ST;
    53.  
    54.             v2f vert (appdata v)
    55.             {
    56.                 v2f o;
    57.                 o.vertex = UnityObjectToClipPos(v.vertex);
    58.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    59.                 UNITY_TRANSFER_FOG(o,o.vertex);
    60.                 return o;
    61.             }
    62.  
    63.             float4 colors[16];
    64.             float pointRanges[16];
    65.  
    66.             float _HitsX[345];
    67.             float _HitsY[345];
    68.             float _HitsInt[345];
    69.             int _HitCount = 0;
    70.  
    71.  
    72.             float4 _Color0;
    73.             float4 _Color1;
    74.             float4 _Color2;
    75.             float4 _Color3;
    76.             float4 _Color4;
    77.             float4 _Color5;
    78.             float4 _Color6;
    79.             float4 _Color7;
    80.             float4 _Color8;
    81.             float4 _Color9;
    82.             float4 _Color10;
    83.             float4 _Color11;
    84.             float4 _Color12;
    85.             float4 _Color13;
    86.             float4 _Color14;
    87.             float4 _Color15;
    88.  
    89.  
    90.             void init()
    91.             {
    92.                 colors[0] = _Color0;
    93.                 colors[1] = _Color1;
    94.                 colors[2] = _Color2;
    95.                 colors[3] = _Color3;
    96.                 colors[4] = _Color4;
    97.                 colors[5] = _Color5;
    98.                 colors[6] = _Color6;
    99.                 colors[7] = _Color7;
    100.                 colors[8] = _Color8;
    101.                 colors[9] = _Color9;
    102.                 colors[10] = _Color10;
    103.                 colors[11] = _Color11;
    104.                 colors[12] = _Color12;
    105.                 colors[13] = _Color13;
    106.                 colors[14] = _Color14;
    107.                 colors[15] = _Color15;
    108.  
    109.                 pointRanges[0] = 0;
    110.                 pointRanges[1] = 0.1;
    111.                 pointRanges[2] = 0.2;
    112.                 pointRanges[3] = 0.3;
    113.                 pointRanges[4] = 0.4;
    114.                 pointRanges[5] = 0.5;
    115.                 pointRanges[6] = 0.55;
    116.                 pointRanges[7] = 0.6;
    117.                 pointRanges[8] = 0.65;
    118.                 pointRanges[9] = 0.7;
    119.                 pointRanges[10] = 0.75;
    120.                 pointRanges[11] = 0.8;
    121.                 pointRanges[12] = 0.85;
    122.                 pointRanges[13] = 0.90;
    123.                 pointRanges[14] = 0.95;
    124.                 pointRanges[15] = 1.0;
    125.                
    126.                 /*for(int i = 0; i < 345; i++)
    127.                 {
    128.                     _HitsX[i] = 0;
    129.                     _HitsY[i] = 0;
    130.                     _HitsInt[i] = 0;
    131.                 }*/
    132.  
    133.                 //-0.5 - 2 for the y position
    134.                 //-2 - 2 for the x
    135.             }
    136.  
    137.             float distsq(float2 a, float2 b)
    138.             {
    139.                
    140.                 float area_of_effect_size = 0.3f;
    141.                 float d = pow(max(0.0, 1.0 - distance(a,b) / area_of_effect_size), 2);
    142.  
    143.                 return d;
    144.  
    145.             }
    146.  
    147.             float3 getHeatForPixel(float weight)
    148.             {
    149.                
    150.                 if(weight <= pointRanges[0])
    151.                 {
    152.                     return colors[0];
    153.                 }
    154.                 if(weight >= pointRanges[15])
    155.                 {
    156.                     return colors[15];
    157.                 }
    158.  
    159.                 for(int i = 1; i < 16; i++)
    160.                 {
    161.                     if(weight < pointRanges[i]){
    162.                         float dist_from_lower_point = weight - pointRanges[i-1];
    163.                         float size_of_point_range = pointRanges[i] - pointRanges[i-1];
    164.  
    165.                         float ratio_over_lower_point = dist_from_lower_point / size_of_point_range;
    166.                        
    167.                         float3 color_range = colors[i] - colors[i-1];
    168.                         float3 color_contribution = color_range * ratio_over_lower_point;
    169.  
    170.                         float3 new_color = colors[i - 1] + color_contribution;
    171.  
    172.                         return new_color;
    173.                     }
    174.                 }
    175.  
    176.                 //Calculations breaking the shader during build?
    177.                 return colors[0];
    178.             }
    179.  
    180.             fixed4 frag (v2f i) : SV_Target
    181.             {
    182.                 init();
    183.                
    184.                 // sample the texture
    185.                 fixed4 col = tex2D(_MainTex, i.uv);
    186.  
    187.                 float2 uv = i.uv;
    188.                 uv = uv * 4 - float2(4.0, 4.0);
    189.  
    190.                 float totalWeight = 0;
    191.                 for(float i = 0; i < _HitCount; i++)
    192.                 {
    193.                     float2 work_pt = float2(_HitsX[i], _HitsY[i]);
    194.                     float pt_intensity = _HitsInt[i];
    195.  
    196.                     totalWeight += 0.08 * distsq(uv, work_pt) * pt_intensity;
    197.                 }
    198.  
    199.                 //UNITY_APPLY_FOG(i.fogCoord, col);
    200.                 return col + float4(getHeatForPixel(totalWeight), 0.8f);
    201.             }
    202.             ENDCG
    203.         }
    204.     }
    205. }
    206.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Hook up your
    adb logcat
    when launching the app and see if there are any shader errors reported.
     
  3. Adzelliper

    Adzelliper

    Joined:
    Jul 17, 2019
    Posts:
    2
    I ran Logcat and didn't get any shader errors but I did get some other errors. I'm unsure if any of these affect it. upload_2021-4-15_15-0-46.png
     

    Attached Files:

  4. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    727
    I'm getting the same error on Android, not sure what's going on. I have another shader that doesn't do this. The code looks like this (a bit nonsensical atm because just trying to test) :

    Code (CSharp):
    1. float _InstancedDetailIndices[1024];
    2. StructuredBuffer<LightmapUVData> _InstancedLightmapData;
    3.  
    4.  
    5. int lightmapDataIndex = round(_InstancedDetailIndices[unity_InstanceID]);
    6. LightmapUVData luvdata = _InstancedLightmapData[lightmapDataIndex];
    7. float3 newUV2 = float3(unity_InstanceID, lightmapDataIndex, luvdata.LightmapIndex);
    8.  

    and the error :

    Code (CSharp):
    1. 08-17 11:58:36.556  3244 12723 D Unity   : Note: Creation of internal variant of shader 'Hidden/PGCustom.Standard_RMA_World_Noise^$_BakedShadowCount3$_CustomDirectionalShadows$_LightingQuality3$FOG_EXP2$' failed.
    2. 08-17 11:58:36.558  3244 12723 I AdrenoGLES-0: Error: Uniform _InstancedDetailIndices cannot find a suitable location/component.
    3. 08-17 11:58:36.558  3244 12723 I AdrenoGLES-0: Error: Linking failed.
    4. 08-17 11:58:36.558  3244 12723 E Unity   : -------- Shader Hidden/PGCustom.Standard_RMA_World_Noise^$_BakedShadowCount3$_CustomDirectionalShadows$_LightingQuality3$FOG_EXP2$
    5. 08-17 11:58:36.558  3244 12723 E Unity   : -------- GLSL link error: Error: Uniform _InstancedDetailIndices cannot find a suitable location/component.
    6. 08-17 11:58:36.558  3244 12723 E Unity   : Error: Linking failed.
    7. 08-17 11:58:36.558  3244 12723 E Unity   :
     
    Last edited: Aug 18, 2022
  5. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    727
    I fixed it! Seems that using float4 array works. So I just use a quarter the array count and index into the array and the float4 now. I had my suspicions because DirectX requires data to be in 16 bit increments for constant buffers, so it got me thinking, maybe mobile has similar requirements for arrays. Maybe float2 would work as well (given it is 16 bit), but ofc float definitely does not.