Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Lightweight Shaders work in Editor, but not on Android?!

Discussion in 'Shaders' started by battou, Sep 28, 2018.

  1. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    197
    So I have switched to Lightweight Pipeline. Made some shaders. Assambled level. Tested it on PC. Everything works great.

    I switch to Android platform. Build. Again, no errors, all looks great.

    I install my apk on my phone, launch it, and get all abjects in pink! Like shaders are not compiled.

    I added Reporter from asset store and checked debug console ingame, no errors.

    What can be wrong???

    Here is my shader code. What am I missing here??(

    Code (CSharp):
    1. Shader "LightweightPipeline/Physically Based Example"
    2. {
    3.     Properties
    4.     {
    5.         // Specular vs Metallic workflow
    6.         [HideInInspector] _WorkflowMode("WorkflowMode", Float) = 0.0
    7.  
    8.         [HDR]
    9.         _Color("Color", Color) = (0.5,0.5,0.5,1)
    10.         [Toggle]_IsSpec("Is Specular", Float) = 0.0
    11.         _Gloss("Spec Gloss", Range(0.0, 1.0)) = 0.5
    12.        
    13.         _MainTex("Albedo", 2D) = "white" {}
    14.  
    15.         [NoScaleOffset]
    16.         _AlphaMask("Alpha Mask", 2D) = "white" {}
    17.         _AMaskTileOffset("Alpha mask tiling offset", Vector) = (1,1,0,0)
    18.  
    19.  
    20.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    21.  
    22.         [KeywordEnum(Off, Front, Back)] _Cull("Cull mode", Float) = 0.0
    23.  
    24.        
    25.         [HideInInspector] _Surface("__surface", Float) = 0.0
    26.         [HideInInspector] _Blend("__blend", Float) = 0.0
    27.         [HideInInspector] _AlphaClip("__clip", Float) = 0.0
    28.     }
    29.  
    30.     SubShader
    31.     {
    32.         Tags{"RenderType" = "Opaque" "RenderPipeline" = "LightweightPipeline" "IgnoreProjector" = "True"}
    33.         LOD 300
    34.  
    35.         Pass
    36.         {
    37.             Name "StandardLit"
    38.             Tags{"LightMode" = "LightweightForward"}
    39.        
    40.             Cull [_Cull]
    41.        
    42.             HLSLPROGRAM
    43.             #pragma prefer_hlslcc gles
    44.             #pragma exclude_renderers d3d11_9x
    45.             #pragma target 2.0
    46.        
    47.             // -------------------------------------
    48.             // Material Keywords
    49.             #pragma shader_feature _ALPHATEST_ON
    50.             #pragma shader_feature _ALPHAPREMULTIPLY_ON
    51.        
    52.             // -------------------------------------
    53.             // Lightweight Pipeline keywords
    54.             #pragma multi_compile _ _SHADOWS_ENABLED
    55.  
    56.             #pragma multi_compile _ _CULL_FRONT
    57.             #pragma multi_compile _ _CULL_BACK
    58.        
    59.             // -------------------------------------
    60.             // Unity defined keywords
    61.             #pragma multi_compile _ DIRLIGHTMAP_COMBINED
    62.             #pragma multi_compile _ LIGHTMAP_ON
    63.             #pragma multi_compile_fog
    64.        
    65.             //--------------------------------------
    66.             // GPU Instancing
    67.             #pragma multi_compile_instancing
    68.        
    69.             #pragma vertex LitPassVertex
    70.             #pragma fragment LitPassFragment
    71.        
    72.             #include "LWRP/ShaderLibrary/Core.hlsl"
    73.             #include "LWRP/ShaderLibrary/Lighting.hlsl"
    74.        
    75.        
    76.             TEXTURE2D(_MainTex);            SAMPLER(sampler_MainTex);
    77.             float4 _MainTex_ST;
    78.        
    79.             TEXTURE2D(_AlphaMask);            SAMPLER(sampler_AlphaMask);
    80.             float4 _AlphaMask_ST;
    81.        
    82.             half4 _Color;
    83.             half _Cutoff;
    84.             float _IsSpec;
    85.             float _Gloss;
    86.             float4 _AMaskTileOffset;
    87.  
    88.             half Alpha(half albedoAlpha, half4 color, half cutoff)
    89.             {
    90.                         half alpha = albedoAlpha * color.a;
    91.                         clip(alpha - cutoff);
    92.        
    93.                 return alpha;
    94.             }
    95.        
    96.        
    97.        
    98.             struct VertexInput
    99.             {
    100.                 float4 vertex       : POSITION;
    101.                 float3 normal       : NORMAL;
    102.                 float4 tangent      : TANGENT;
    103.                 float2 texcoord     : TEXCOORD0;
    104.                 float2 lightmapUV   : TEXCOORD1;
    105.             };
    106.        
    107.             struct VertexOutput
    108.             {
    109.                 float2 uv                       : TEXCOORD0;
    110.                 float2 lightmapUV               : TEXCOORD1;
    111.                 float4 positionWSAndFogFactor   : TEXCOORD2;
    112.                 half3  normal                   : TEXCOORD3;
    113.                 float2 uvMask                   : TEXCOORD4;
    114.        
    115.                 float4 shadowCoord              : TEXCOORD6;
    116.                 float4 clipPos                  : SV_POSITION;
    117.             };
    118.        
    119.             VertexOutput LitPassVertex(VertexInput v)
    120.             {
    121.                 VertexOutput o = (VertexOutput)0;
    122.        
    123.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    124.                 o.uvMask = TRANSFORM_TEX(v.texcoord, _AlphaMask);
    125.                 o.lightmapUV = v.lightmapUV.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    126.        
    127.                
    128.                 float3 positionWS = TransformObjectToWorld(v.vertex.xyz);
    129.                 o.clipPos = TransformWorldToHClip(positionWS);
    130.                
    131.                 float fogFactor = ComputeFogFactor(o.clipPos.z);
    132.                 o.positionWSAndFogFactor = float4(positionWS, fogFactor);
    133.        
    134.  
    135.  
    136.                 #ifdef _CULL_FRONT
    137.                 o.normal = TransformObjectToWorldNormal(-v.normal);
    138.                 #endif
    139.  
    140.                 #ifdef _CULL_BACK
    141.                 o.normal = TransformObjectToWorldNormal(v.normal);
    142.                 #endif
    143.                 o.shadowCoord = TransformWorldToShadowCoord(positionWS);
    144.                 return o;
    145.             }
    146.        
    147.             half4 LitPassFragment(VertexOutput IN) : SV_Target
    148.             {
    149.                 half3 normalWS = normalize(IN.normal);
    150.        
    151.                 float3 positionWS = IN.positionWSAndFogFactor.xyz;
    152.                 half3 viewDirectionWS = SafeNormalize(GetCameraPositionWS() - positionWS);
    153.  
    154.                 Light mainLight = GetMainLight();
    155.                 mainLight.attenuation = MainLightRealtimeShadowAttenuation(IN.shadowCoord);
    156.        
    157.                 half NdotL = saturate(dot(normalWS, mainLight.direction))>0;
    158.                 half3 radiance = mainLight.color * max(0.5, mainLight.attenuation * NdotL);
    159.                 half3 color = radiance;
    160.        
    161.                 int pixelLightCount = GetPixelLightCount();
    162.                 for (int i = 0; i < pixelLightCount; ++i)
    163.                 {
    164.                     Light light = GetLight(i, positionWS);
    165.                     light.attenuation *= LocalLightRealtimeShadowAttenuation(light.index, positionWS);
    166.                     color += light.color * (light.attenuation * NdotL);
    167.                 }
    168.  
    169.                 color*= SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
    170.        
    171.                 float2 maskUV = (IN.uvMask * 2 - 1);
    172.                 maskUV = maskUV * float2(_AMaskTileOffset.x, _AMaskTileOffset.y);
    173.                 maskUV = maskUV + float2(_AMaskTileOffset.z, _AMaskTileOffset.w);
    174.                 maskUV = (maskUV * 0.5 + 0.5);
    175.  
    176.                 half4 MaskAlpha = SAMPLE_TEXTURE2D(_AlphaMask, sampler_AlphaMask, maskUV);
    177.                 half alpha = Alpha(MaskAlpha.a, _Color, _Cutoff);
    178.        
    179.                 float fogFactor = IN.positionWSAndFogFactor.w;
    180.  
    181.                 float3 camDir = normalize(GetCameraPositionWS() - IN.positionWSAndFogFactor);
    182.                 half3 specDir = normalize(camDir + mainLight.direction);
    183.                 half AdotN = dot(specDir, normalWS);
    184.  
    185.                 ApplyFog(color, fogFactor);
    186.  
    187.                 return half4(color*_Color +(AdotN>0.98+0.02*_Gloss)*_IsSpec, alpha);
    188.             }
    189.             ENDHLSL
    190.         }
    191.  
    192.  
    193.        
    194.  
    195.         // Used for shadows
    196.         UsePass "LightweightPipeline/Standard (Physically Based)/ShadowCaster"
    197.        
    198.         // Used for depth prepass
    199.         // If shadows cascade are enabled we need to perform a depth prepass.
    200.         // We also need to use a depth prepass in some cases camera require depth texture
    201.         // (e.g, MSAA is enabled and we can't resolve with Texture2DMS
    202.         UsePass "LightweightPipeline/Standard (Physically Based)/DepthOnly"
    203.  
    204.         // Used for Baking GI. This pass is stripped from build.
    205.         UsePass "LightweightPipeline/Standard (Physically Based)/Meta"
    206.     }
    207.  
    208.     FallBack "Hidden/InternalErrorShader"
    209. }
     
    Last edited: Sep 28, 2018
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,864
    Hi!
    Please check the device logs (and make sure you're in Development mode, not Release).
     
  3. Wahooney

    Wahooney

    Joined:
    Mar 8, 2010
    Posts:
    281
    @aleksandrk I'm having the same issue, my game runs fine on desktop but goes pink on mobile. Unity 2018.3b12 LWRP 4.3.0. I get these errors on startup:

    AndroidPlayer(Huawei_Nexus_6P@192.168.21.29)
    The referenced script (Unknown) on this Behaviour is missing!

    AndroidPlayer(Huawei_Nexus_6P@192.168.21.29) The referenced script on this Behaviour (Game Object '<null>') is missing!

    AndroidPlayer(Huawei_Nexus_6P@192.168.21.29) A scripted object (probably UnityEngine.Experimental.Rendering.LightweightPipeline.LightweightRenderPipelineAsset?) has a different serialization layout when loading. (Read 48 bytes but expected 184 bytes)
    Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?

    AndroidPlayer(Huawei_Nexus_6P@192.168.21.29) The referenced script (Unknown) on this Behaviour is missing!

    AndroidPlayer(Huawei_Nexus_6P@192.168.21.29) The referenced script on this Behaviour (Game Object '<null>') is missing!

    AndroidPlayer(Huawei_Nexus_6P@192.168.21.29) A scripted object (probably UnityEngine.Experimental.Rendering.LightweightPipeline.LightweightRenderPipelineResources?) has a different serialization layout when loading. (Read 68 bytes but expected 116 bytes)
    Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?

    AFAIK none of my behaviours are missing, so it could be related.

    Thanks.
     
    Last edited: Dec 5, 2018
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,864
    Hi @Wahooney,
    I'd try and kill the Library folder contents, since this just says "I'm trying to read a file and I'm failing". Did you update to a newer Unity version recently?
     
  5. Wahooney

    Wahooney

    Joined:
    Mar 8, 2010
    Posts:
    281
    Hey @aleksandrk,

    Thanks for getting back to me.
    I upgraded from 2018.2 to 2018.3b12, but when I converted I hadn't written any scripts or included any packages.

    After deleting the library I'm still getting an all pink render with LWRP + Android and have all the same errors/warnings.

    EDIT:
    I deleted everything except my Assets and rebuilt my Project Settings from scratch, seems to be working now, good thing I'm working on an incredibly simple game :D Thanks for pointing me in the right direction.
     
    Last edited: Dec 5, 2018
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    Bump. Custom LWRP shaders works fine in editor but on android all pink (release\development builds both)
     
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,864
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    I’m out of office today :( I’ll can tell you tomorrow with logs attached :) but I checked it before and not saw any errors
     
  9. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    Sorry for long responce, ver ybusy at work! Here log captured from simple build with my custom shader, worked in Editor and not worked on android (in MEmu emulator in this case, but the same problem on all android devices)
    I send you sample project in to private messages. Thanks! Waiting for you help!
    Just couple of screenshots, all fine in Editor, without any errors:
    upload_2019-1-19_13-51-18.png

    And pink on android :(
    upload_2019-1-19_13-59-4.png

    Also, as we can see, shader variants of my custom shader, used in scene, compiles when we build.

    upload_2019-1-19_13-43-31.png
     

    Attached Files:

  10. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    @aleksandrk just to confirm, so as not to miss the message above :)
     
  11. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,864
    Hi!
    Can you please provide a log from a real device? :)
    This one doesn't contain anything useful.
     
  12. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    I can get android device tomorrow and send logs here! If you want you can check repro and apk sended to you in private message before :)
     
  13. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    logcat by
    upload_2019-1-21_19-41-58.png

    from real device.

    Edit:
    And second log with development build checked.
     

    Attached Files:

    Last edited: Jan 21, 2019
  14. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,864
    @eizenhorn the logcat output looks fine. Please submit a bug report for further investigation.
     
  15. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    Done
    Case 1119437
     
  16. francesko2005

    francesko2005

    Joined:
    Jun 24, 2014
    Posts:
    2
    If comment Meta Pass it will work on android. Looks like this pass isn't stripped.
    Code (CSharp):
    1.         // Used for Baking GI. This pass is stripped from build.
    2.         //UsePass "LightweightPipeline/Standard (Physically Based)/Meta"
     
    Last edited: Feb 7, 2019
  17. francesko2005

    francesko2005

    Joined:
    Jun 24, 2014
    Posts:
    2
    Temporary solution - replace usepass by code
    Code (CSharp):
    1.      
    2.  
    3.         // Used for Baking GI. This pass is stripped from build.
    4.         //UsePass "LightweightPipeline/Standard (Physically Based)/Meta"
    5.         Pass
    6.         {
    7.             Name "Meta"
    8.             Tags{"LightMode" = "Meta"}
    9.  
    10.             Cull Off
    11.  
    12.             HLSLPROGRAM
    13.             // Required to compile gles 2.0 with standard srp library
    14.             #pragma prefer_hlslcc gles
    15.             #pragma exclude_renderers d3d11_9x
    16.  
    17.             #pragma vertex LightweightVertexMeta
    18.             #pragma fragment LightweightFragmentMeta
    19.  
    20.             #pragma shader_feature _SPECULAR_SETUP
    21.             #pragma shader_feature _EMISSION
    22.             #pragma shader_feature _METALLICSPECGLOSSMAP
    23.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    24.  
    25.             #pragma shader_feature _SPECGLOSSMAP
    26.  
    27.             #include "Packages/com.unity.render-pipelines.lightweight/Shaders/LitInput.hlsl"
    28.             #include "Packages/com.unity.render-pipelines.lightweight/Shaders/LitMetaPass.hlsl"
    29.  
    30.             ENDHLSL
    31.         }
     
    Last edited: Feb 8, 2019
  18. STD3DInmersion

    STD3DInmersion

    Joined:
    Aug 12, 2018
    Posts:
    1
    Hello francesko! I have the same problem but the includes of the replace:
    #include "Packages/com.unity.render-pipelines.lightweight/Shaders/LitInput.hlsl"
    #include "Packages/com.unity.render-pipelines.lightweight/Shaders/LitMetaPass.hlsl"
    give me errors, because unity cant find the files, do you know how to solve it?
     
    Last edited: Feb 10, 2019
  19. dheerajbreddy304

    dheerajbreddy304

    Joined:
    Feb 18, 2018
    Posts:
    6
    Hi,

    I am new to Unity game development. I making a mobile game. In LWRP I made a custom shader graph (glow shader). It works fine in unity in my pc. But when I build a .APK and install and play on my android device it shows a plain block with no shaders.

    Am I missing something?
    a.png f2916204-6364-4622-8e9c-.jpg
     
  20. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    Might need to add it to the "Always included shaders" section of Projects Settings -> Grpahics
     
    DankalApps and Anton-Bogodvid like this.