Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice
  2. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Help Wanted Baked shadows on transparent plane in AR view

Discussion in 'AR' started by viknesh2020, Nov 12, 2020.

  1. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    24
    This may be a long post :p,

    I wanted to have shadows for the objects that I place on top of the detected planes in the AR view. There are three objects that has been spawned at runtime. Each object have its own primitive plane for receiving shadows from their objects. Each plane have a shader which makes the plane transparent and the shadow visible at Realtime lighting.

    Now, the issue is, using Realtime lights for getting shadow, reduces the app's fps after deploying it to mobile device. So I baked the lightmaps but the shadows are not visible on the transparent plane. I feel like something has to be tweaked in the shader. Posting the code here,

    Code (CSharp):
    1.  
    2. Shader "Custom/Shadow"
    3. {
    4.     Properties  
    5.     {
    6.         _Color("Main Color", Color) = (1,1,1,1)
    7.         _MainTex("Base (RGB)", 2D) = "white" {}  
    8.         _Cutoff("Cutout", Range(0,1)) = 0.5 }
    9.  
    10.         SubShader
    11.         {
    12.             Pass
    13.             {
    14.                 Alphatest Greater[_Cutoff]
    15.                 SetTexture[_MainTex]
    16.             }  
    17.            
    18.             Pass
    19.             {
    20.                 Blend DstColor Zero Tags{ "LightMode" = "ForwardBase"
    21.             }
    22.        
    23.         CGPROGRAM
    24.  
    25.         #pragma vertex vert
    26.         #pragma fragment frag
    27.         #include "UnityCG.cginc"
    28.         #pragma multi_compile_fwdbase
    29.         #include "AutoLight.cginc"
    30.  
    31.         struct v2f
    32.         {
    33.             float4 pos : SV_POSITION; LIGHTING_COORDS(0,1)
    34.         };
    35.  
    36.         v2f vert(appdata_base v)
    37.         {
    38.             v2f o; o.pos = UnityObjectToClipPos(v.vertex); TRANSFER_VERTEX_TO_FRAGMENT(o);
    39.             return o;
    40.         }
    41.  
    42.         fixed4 frag(v2f i) : COLOR
    43.         {
    44.             float attenuation = LIGHT_ATTENUATION(i);
    45.             return attenuation;
    46.         }
    47.  
    48.         ENDCG
    49.      }
    50.        
    51.    }
    52.         Fallback "Transparent/Cutout/VertexLit"
    53. }
    54.  
    How do I make this shader work in baked lights? Any help would be appreciated. I am not using URP because of various reasons and I don't want to use URP for now, as the project is client specific and shortage of time to rectify things if something happens after porting the project to URP. To save the baked lightmaps within the prefab, I use the following

    https://github.com/Ayfel/PrefabLightmapping

    Versions used:
    Unity 2019.4.4f1
    AR Foundation preview 3 version 4.0.0
    AR Core preview 3 version 4.0.0
    AR Subsystems preview 3 version 4.0.0
     
    erkangur likes this.
unityunity