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

Question Custom ShadowMap Not Working On Mobile

Discussion in 'Shaders' started by samy99, May 31, 2023.

  1. samy99

    samy99

    Joined:
    Mar 4, 2020
    Posts:
    7
    Unity 2022.2.16
    URP 14.0.7

    I make a custom scriptable renderer pass to render a showmap, then on shader i get shadow via
    SAMPLE_TEXTURE2D_SHADOW, is work OK in Editor and PC,but when i build to a Android run on mobile, the sample result is all black. I check the shadowmap renderTexture on the Frame Debuger and it is correct rendering on mobile, so just sample incorrect i guess. it maybe something wrong in other space.

    shadowmap on framer debuger,it look ok


    half GetHardShadow(float4 shadowCoord){
    float3 uv = shadowCoord.xyz / shadowCoord.w;
    uv.xy = uv.xy * 0.5 + 0.5;

    if(uv.x<0 || uv.x>1 || uv.y<0 || uv.y>1) return 1.0f;

    //SAMPLE_TEXTURE2D_X can sample correct,but not the way i need
    // float atten = SAMPLE_TEXTURE2D_X(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture, uv);

    half atten = SAMPLE_TEXTURE2D_SHADOW(_MainLightShadowmapTexture, sampler_LinearClampCompare, uv ).r;
    return atten; //this alway return 0 on Android mobile,but correct value on PC
    atten = LerpWhiteTo(atten, _shadowStrength);
    return atten;
    }
    [ATTACH=full]1250218[/ATTACH]


    Code (CSharp):
    1. var format = UnityEngine.Experimental.Rendering.GraphicsFormatUtility.GetDepthStencilFormat(16, 0);
    2.  
    3. rtd = new RenderTextureDescriptor(mainLightShadowResolution, mainLightShadowResolution, UnityEngine.Experimental.Rendering.GraphicsFormat.None, format);
    4.  
    5. rtd.shadowSamplingMode =(RenderingUtils.SupportsRenderTextureFormat(RenderTextureFormat.Shadowmap)&& (SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES2)) ?ShadowSamplingMode.RawDepth : ShadowSamplingMode.None;
    6.  
    7. cmd.SetRenderTarget(ri_main_ID, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
    8. cmd.ClearRenderTarget(true, true, Color.clear);
     
  2. samy99

    samy99

    Joined:
    Mar 4, 2020
    Posts:
    7
    and i got another truble on build to mobile,is my shadowcaster pass shader variants may all strip on build. i got a log like:
    STRIPPING: Universal Render Pipeline/CustomPBRLit (CustomShadowCaster pass) (Vertex) - Remaining shader variants = 0/2 = 0% - Total = 1175/79756 = 1.473243% TimeMs=0.2085

    it means all the shadowcaster pass variants striped, it's no variants genetate!
     
  3. samy99

    samy99

    Joined:
    Mar 4, 2020
    Posts:
    7
    i test with a new Phone (xiaomi k20 api:Vulkan) today and it's run OK now. i'm confuse if it the last phone's(nexus 6p api:OpenGl) problem, the different of Graphic API ?
     
  4. samy99

    samy99

    Joined:
    Mar 4, 2020
    Posts:
    7
    i guess the problem is only happen on a
    not UNITY_REVERSED_Z platform