Search Unity

CommandBuffer.DrawMeshInstanced Can Not Received Shadows in unity2018.4 forward rendering

Discussion in 'General Graphics' started by tiny_xue, Jul 13, 2021.

  1. tiny_xue

    tiny_xue

    Joined:
    Jun 19, 2018
    Posts:
    6
    Hi, man. I meet the shadow problem, with 2018.4 , Commandbuffer.DrawMeshInstanced, unity standard shader, forward pass, no screenspace shadow.(when use screenspace shadow it can work, just copy texture in AfterScreenspaceMask event, other process is the same)
    I Copied the ShadowTexture from the AfterShadowMapPass(or AfterShadowMap) event, and use cmd.SetGlobalTexture("_ShadowMapTexture", shadowMapID) in my defined CMD which executed in CameraEvent.AfterForwardOpaque event.
    I checked the framebuffer infos, and make sure my object shader infos were as same as the object in Unity self Opaque pipeline, but no shadow shown!
    Am i missing some keywords?
    @zeroyao @antoinel_unity
    Codes like this:
    Code (CSharp):
    1. shadowMapID = Shader.PropertyToID("_ShadowMapTexture");
    2.  
    3.         Transform tr1 = GameObject.Find("Cube").transform;
    4.         Transform tr2 = GameObject.Find("Cube (1)").transform;
    5.         matrix4X4s = new Matrix4x4[2] { tr1.localToWorldMatrix, tr2.localToWorldMatrix, };
    6.         mesh = tr1.GetComponent<MeshFilter>().sharedMesh;
    7.         material = tr1.GetComponent<Renderer>().sharedMaterial;
    8.         material.enableInstancing = true;
    9.  
    10.         CommandBuffer shadowCaster = new CommandBuffer();
    11.         shadowCaster.DrawMeshInstanced(mesh, 0, material, material.FindPass("SHADOWCASTER"), matrix4X4s);
    12.         mainLight.AddCommandBuffer(LightEvent.AfterShadowMapPass, shadowCaster);
    13.  
    14.         CommandBuffer shadowCMD = new CommandBuffer();
    15.         shadowCMD.SetShadowSamplingMode(BuiltinRenderTextureType.CurrentActive, ShadowSamplingMode.RawDepth);
    16.         shadowCMD.GetTemporaryRT(shadowMapID, 4096, 4096, 32, FilterMode.Bilinear, RenderTextureFormat.Depth);
    17.         shadowCMD.Blit(BuiltinRenderTextureType.CurrentActive, shadowMapID);
    18.         mainLight.AddCommandBuffer(LightEvent.AfterShadowMap, shadowCMD);
    19.  
    20.         cmd = new CommandBuffer();
    21.         material.EnableKeyword("SHADOWS_SCREEN");
    22.         cmd.SetGlobalTexture("_ShadowMapTexture", shadowMapID);
    23.         cmd.DrawMeshInstanced(mesh, 0, material, 0, matrix4X4s);
    24.         cmd.DrawMeshInstanced(mesh, 0, material, 1, matrix4X4s);
    25.         Camera.main.AddCommandBuffer(CameraEvent.AfterForwardOpaque, cmd);
     
  2. Stormer2020

    Stormer2020

    Joined:
    Sep 14, 2020
    Posts:
    92
    Hi~ The same issue, did you fix it? Thank you~
     
  3. wechat_os_Qy00ZsXVumo1o8syrISdbhhOI

    wechat_os_Qy00ZsXVumo1o8syrISdbhhOI

    Joined:
    Apr 6, 2020
    Posts:
    3
    did you fix it?QAQ,Thank you!
     
  4. Stormer2020

    Stormer2020

    Joined:
    Sep 14, 2020
    Posts:
    92
    I changed the MainCamera to Deferred Rendering.