CommandBuffer DrawMesh is black

Just trying to draw a cube with a material that gets lit. Can’t be that hard can it?

I’m using Forward Rendering

[ExecuteInEditMode]
[RequireComponent(typeof(MeshRenderer))]
public sealed class CubeRenderer : MonoBehaviour {

    public Mesh mesh;
    public Material material;

    private Dictionary<Camera, CommandBuffer> cameras = new Dictionary<Camera, CommandBuffer>();

    private void OnDisable() {

        foreach (var kvp in cameras) {

            if (kvp.Key) {

                kvp.Key.RemoveCommandBuffer(CameraEvent.AfterForwardOpaque, kvp.Value);
            }
        }

        cameras.Clear();
    }

    void OnWillRenderObject() {

        bool active = enabled && gameObject.activeInHierarchy;

        if (!active) {

            OnDisable();
            return;
        }

        Camera camera = Camera.current;

        if (!camera || !material || !mesh) {

            return;
        }

        Matrix4x4 matrix = Matrix4x4.identity;

        CommandBuffer buffer;

        if (!cameras.TryGetValue(camera, out buffer)) {

            buffer = new CommandBuffer();
            buffer.name = "Command Buffer";

            camera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, buffer);
            cameras.Add(camera, buffer);

            buffer.DrawMesh(mesh, matrix, material);
        }
        else {

            foreach (CommandBuffer cb in cameras.Values) {

                cb.Clear();
                cb.DrawMesh(mesh, matrix, material);
            }
        }
    }
}

The Result I get:

3096541--233721--blackCube.png

I have tried every single CameraEvent value. Nothing works, the cube is either black or doesn’t show up at all

Wow

cb.DrawMesh(mesh, matrix, material); //no lighting
cb.DrawMesh(mesh, matrix, material, 0, 0); //lighting

come on Unity.

1 Like

When using a CommandBuffer, you are in between the built in Unity passes. So you need to specify a specific shader pass to use. (That last 0.)

I wish there were some simple examples on the Docs about this stuff.

Currently struggling with getting my cube to receive shadows.
I have managed to get it casting shadows by adding a Command Buffer to a Directional light.

No idea how to get it to receive shadows though :frowning:

To work with CommandBuffers you really should know the ins and outs of the Unity rendering pipeline. I agree that unfortunately, many details are not documented. (Which built in render target contains what at each stage and which stencil buffer parts are being used by the render pipeline itself for example.) There is some documentation, but it’s not 100% correct or up to date.

I have found a solution.
It’s a Unity issue and they provide a solution around this. (Took me a while to find the solution)

—What is the problem—
Unity doesn’t assign the ‘SH coefficients’ to the target material and that leads to no ambient lighting.

—How to solve it—
Calculate it in CPU once → Assing it to ‘MaterialPropertyBlock’, → Assing that to the material.

Github link for the solution example

1 Like