Search Unity

Any way to make this shader work?

Discussion in 'Android' started by elijad, Jun 8, 2020.

  1. elijad

    elijad

    Joined:
    Mar 19, 2020
    Posts:
    47
    I have this shader that works perfectly in editor but when built for android, it just turns pink.

    I get two GLSL link errors in logcat telling me it cannot find locations for rims and tiles. Is there a hardware feature that is required for this to work? My test device has a PowerVR GE8100 gpu which seems to support opengl es 3.2. If anyone has the answer to this or the time to actually test this thing on their device, any one of those would be greatly appreciated.


    Also, it seems to be pointing to the second line here in GLES 3.0 vertex shader, if this helps understand the issue better.

    #if UNITY_SUPPORTS_UNIFORM_LOCATION
    #define UNITY_LOCATION(x) layout(location = x)


    Code (CSharp):
    1. 'unity_ObjectToWorld'
    2.  
    3. Shader "Game/MeshFill"
    4. {
    5.  
    6.     Properties
    7.     {
    8.         _MainTex ("Texture", 2D) = "white" {}
    9.         _BGColor ("BG Color", Color) = (.5,.5,.5,1)
    10.         _Scale ("Scale", float) = 1.0
    11.         _FillAmount ("Fill Amount", Range(-1,1)) = 0.0
    12.         _ToonColor ("Toon Color", Color) = (.5,.5,.5,1)
    13.         [NoScaleOffset]_ToonShade ("Cubemap", CUBE) = "" { }
    14.         _Outline ("_Outline", Range(0,0.1)) = 0
    15.         _OutlineColor ("Color", Color) = (1, 1, 1, 1)
    16.         [Toggle(USE_UV)] _useUV ("Use UV index", Float) = 0
    17.     }
    18.     SubShader
    19.     {
    20.  
    21.         Pass
    22.         {
    23.             Stencil {
    24.                 Ref 1
    25.                 Comp always
    26.                 Pass replace
    27.             }
    28.             Tags {"RenderQueue" = "Opaque" "RenderType" = "Opaque"  "Queue" = "Geometry"}
    29.  
    30.             // Zwrite On
    31.             Lighting off
    32.  
    33.             CGPROGRAM
    34.  
    35.  
    36.  
    37.             #pragma vertex vert
    38.             #pragma fragment frag
    39.             #pragma shader_feature USE_UV
    40.  
    41.  
    42.             #include "UnityCG.cginc"
    43.  
    44.             float LevelSize;
    45.             float tiles[1023];
    46.             float rims[1023];
    47.  
    48.             struct appdata
    49.             {
    50.                 float4 vertex : POSITION;
    51.                 float2 uv : TEXCOORD0;
    52.                 float3 normal : NORMAL;
    53.             };
    54.  
    55.             struct v2f
    56.             {
    57.                 float2 uv : TEXCOORD0;
    58.                 float4 vertex : SV_POSITION;
    59.                 float fillEdge : TEXCOORD1;
    60.                 float3 cubenormal : TEXCOORD2;
    61.                 float2 fill : TEXCOORD3;
    62.             };
    63.  
    64.  
    65.             sampler2D _MainTex;
    66.             float4 _MainTex_ST;
    67.             float _FillAmount;
    68.             float4 _TopColor;
    69.             samplerCUBE _ToonShade;
    70.             float4 _ToonColor;
    71.             float4 _BGColor;
    72.             float _Scale;
    73.  
    74.             v2f vert(appdata_full v)
    75.             {
    76.                 v2f o;
    77.  
    78.                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
    79.              
    80.             #ifndef USE_UV
    81.                 o.fill.x = tiles[floor(worldPos.x) * LevelSize + floor(worldPos.z)];
    82.                 o.fill.y = rims[floor(worldPos.x) * LevelSize + floor(worldPos.z)];
    83.             #else
    84.                 o.fill.x = tiles[v.texcoord3.x * LevelSize + v.texcoord3.y];
    85.                 o.fill.y = rims[floor(worldPos.x) * LevelSize + floor(worldPos.z)];
    86.             #endif
    87.                 o.vertex = UnityObjectToClipPos(v.vertex);
    88.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    89.                 o.fillEdge = v.vertex.y + -_Scale * _FillAmount;
    90.                 o.cubenormal = UnityObjectToViewPos(float4(v.normal,0));
    91.                 return o;
    92.             }
    93.  
    94.             fixed4 frag(v2f i) : SV_Target
    95.             {
    96.                 fixed4 gradient = tex2D(_MainTex, i.uv);
    97.                 fixed4 cube = texCUBE(_ToonShade, i.cubenormal.xyz) * _ToonColor;
    98.  
    99.                 float4 result = i.fill.x;
    100.                 float4 resultColor = (cube + gradient);
    101.                 float4 color = lerp(lerp(Luminance(resultColor), resultColor, 0), result * resultColor, result) * (1, i.fill.y, 1-i.fill.y, 1);
    102.                 //color.a = lerp(_ToonColor.w, 1, i.fill);
    103.                 return color;
    104.  
    105.             }
    106.  
    107.  
    108.             ENDCG
    109.         }
    110.      
    111.  
    112.         Pass {
    113.             Stencil {
    114.                 Ref 1
    115.                 Comp notEqual
    116.                 Pass keep
    117.             }
    118.              Tags {"RenderQueue"="AlphaTest" "RenderType"="TransparentCutout"  "Queue"="Geometry"}
    119.        
    120.             Cull Front
    121.                 Ztest always
    122.             ColorMask RGB
    123.              Lighting off
    124.             Offset 10, 0
    125.             CGPROGRAM
    126.             #pragma vertex vert
    127.             #pragma fragment frag
    128.             #pragma shader_feature USE_UV
    129.             #include "UnityCG.cginc"
    130.              
    131.             float LevelSize;
    132.             float tiles[1024];
    133.             float rims[1024];
    134.  
    135.             struct v2f {
    136.                 float4 pos : SV_POSITION;
    137.             };
    138.             float _Outline;
    139.             float4 _OutlineColor;
    140.  
    141.             float4 vert(appdata_full v) : SV_POSITION {
    142.                 v2f o;
    143.                 o.pos = UnityObjectToClipPos(v.vertex);
    144.                 float3 normal = UnityObjectToViewPos(v.normal);
    145.                 normal.x *= UNITY_MATRIX_P[0][0];
    146.                 normal.y *= UNITY_MATRIX_P[1][1];
    147.                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
    148.                 #ifndef USE_UV
    149.                     o.pos.xy += normal.xy * _Outline * (1 - rims[floor(worldPos.x) * LevelSize + floor(worldPos.z)]);
    150.                 #else
    151.                     o.pos.xy += normal.xy * _Outline * (1 - rims[v.texcoord3.x * LevelSize + v.texcoord3.y]);
    152.                 #endif
    153.                 return o.pos;
    154.             }
    155.             half4 frag(v2f i, fixed facing : VFACE) :  SV_Target {
    156.                 return _OutlineColor;
    157.             }
    158.  
    159.                 ENDCG
    160.         }
    161.     }
    162.  
    163.  
    164. }
    165.  
     
    Last edited: Jun 8, 2020