Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

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

  1. tiny_xue

    tiny_xue

    Joined:
    Jun 19, 2018
    Posts:
    6
    I use CommandBuffer.DrawMeshInstanced to draw Cube mesh with Standard Shader, it shows but cannot receive shadows.
    Then I use Graphics.DrawMeshInstanced and set params to receive shadow, It's OK.
    I want to know how to use CommandBuffer to do the same work.
    Anyone help?
     
  2. tiny_xue

    tiny_xue

    Joined:
    Jun 19, 2018
    Posts:
    6
    Anyone help?
    Here is the main code for Test:
    CommandBuffer shadowCaster = new CommandBuffer();
    shadowCaster.DrawMeshInstanced(mesh, 0, material, material.FindPass("SHADOWCASTER"), matrix4X4s);
    mainLight.AddCommandBuffer(LightEvent.AfterShadowMapPass, shadowCaster);
    CommandBuffer shadowCMD = new CommandBuffer();
    shadowCMD.SetShadowSamplingMode(BuiltinRenderTextureType.CurrentActive, ShadowSamplingMode.RawDepth);
    shadowCMD.GetTemporaryRT(shadowMapID, 4096, 4096, 16, FilterMode.Bilinear, RenderTextureFormat.Shadowmap);
    shadowCMD.Blit(BuiltinRenderTextureType.CurrentActive, shadowMapID);
    mainLight.AddCommandBuffer(LightEvent.AfterShadowMapPass, shadowCMD);
    cmd = new CommandBuffer();
    material.EnableKeyword("SHADOWS_SCREEN");
    material.EnableKeyword("LIGHTPROBE_SH");
    cmd.SetGlobalTexture("_ShadowMapTexture", shadowMapID);
    cmd.DrawMeshInstanced(mesh, 0, material, 0, matrix4X4s);
    cmd.DrawMeshInstanced(mesh, 0, material, 1, matrix4X4s);
    Camera.main.AddCommandBuffer(CameraEvent.AfterForwardOpaque, cmd);