Search Unity

AR Shadow in AR Foundation not working first time Android

Discussion in 'AR' started by Sound-Master, Apr 23, 2020.

  1. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    48
    Hello all,

    I am sorry if this sounds like a noob question.

    I am using Ar Foundation 4.0.0
    Universal RP 7.1.8
    Unity 2019.3.7f1

    I am experiencing a strange issue with shadows in AR Foundation. The scene is developed from the UX example and I place a custom prefab.

    The prefab includes a shader which allows me to have AR shadows while using the URP.

    When I start the app the first time, I have to give permission to use the camera. The the app opens but when I place the prefab, there is no shadow.

    If I reopen the app this time the shadow would appear.

    is the issue linked to the permission request? What is preventing the shader from working correctly on first launch?

    Any help would be appreciated!

    Here is the shader code

    Code (CSharp):
    1. Shader "Custom/AR Shadows"
    2. {
    3.     Properties
    4.     {
    5.  
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags { "LightMode" = "ForwardBase" "RenderType"="Opaque" "Queue"="Geometry+1" "ForceNoShadowCasting"="True"  }
    11.         LOD 300
    12.  
    13.         Pass
    14.         {
    15.             Name "AR Shadows"
    16.             Tags
    17.             {
    18.                 "LightMode" = "UniversalForward"
    19.             }
    20.  
    21.             Blend SrcAlpha OneMinusSrcAlpha
    22.             ZWrite On
    23.             Cull Off
    24.  
    25.             HLSLPROGRAM
    26.             //#pragma prefer_hlslcc gles
    27.             #pragma exclude_renderers d3d11_9x
    28.             #pragma target 2.0
    29.  
    30.             // -------------------------------------
    31.             // Material Keywords
    32.             #pragma shader_feature _ALPHATEST_ON
    33.             #pragma shader_feature _ALPHAPREMULTIPLY_ON
    34.  
    35.             // -------------------------------------
    36.             // Lightweight Pipeline keywords
    37.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    38.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    39.             #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
    40.             #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
    41.             #pragma multi_compile _ _SHADOWS_SOFT
    42.  
    43.             // -------------------------------------
    44.             // Unity defined keywords
    45.             #pragma multi_compile _ DIRLIGHTMAP_COMBINED
    46.             #pragma multi_compile _ LIGHTMAP_ON
    47.  
    48.             //--------------------------------------
    49.             // GPU Instancing
    50.             #pragma multi_compile_instancing
    51.  
    52.             #pragma vertex HiddenVertex
    53.             #pragma fragment HiddenFragment
    54.  
    55.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    56.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    57.  
    58.             struct Attributes
    59.             {
    60.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    61.                 float4 positionOS   : POSITION;
    62.             };
    63.  
    64.             struct Varyings
    65.             {
    66.                 float4 positionCS   : SV_POSITION;
    67.                 float4 shadowCoord  : TEXCOORD0;
    68.             };
    69.  
    70.             Varyings HiddenVertex(Attributes input)
    71.             {
    72.                 Varyings output = (Varyings)0;
    73.  
    74.                 UNITY_SETUP_INSTANCE_ID(input);
    75.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    76.  
    77.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    78.  
    79.                 output.shadowCoord = GetShadowCoord(vertexInput);
    80.                 output.positionCS = vertexInput.positionCS;
    81.  
    82.                 return output;
    83.             }
    84.  
    85.             half4 HiddenFragment(Varyings input) : SV_Target
    86.             {
    87.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    88.  
    89.                 half s = MainLightRealtimeShadow(input.shadowCoord);
    90.  
    91.                 return half4(0, 0, 0, 1 - s);
    92.             }
    93.             ENDHLSL
    94.         }
    95.  
    96.         UsePass "Universal Render Pipeline/Lit/DepthOnly"
    97.     }
    98.  
    99.     FallBack "Hidden/InternalErrorShader"
    100. }
    101.  
    It was modified from this link.

    Many Thanks!