Why can't I DrawMeshInstanced with a Camera?

Still struggling with Unity’s graphics pipeline, sigh.

Trying to draw some instanced meshes from the view of a camera. I only want to render these meshes in the supplied camera, but for some obvious and clearly documented reason, the meshes won’t render when a camera is supplied.

Some simple code: Listen for the preCull event, Draw instanced meshes.

using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

[ExecuteInEditMode]
public class GraphicsTest : MonoBehaviour {

    public Mesh mesh;
    public Material material;
    public int instances = 1;
    public bool renderWithCamera = false;

    private Matrix4x4[] matrices;
    private MaterialPropertyBlock properties;

    private void OnEnable() {

        Camera.onPreCull -= DrawWithCamera;
        Camera.onPreCull += DrawWithCamera;
    }

    private void OnDisable() {

        Camera.onPreCull -= DrawWithCamera;
    }

    private void DrawWithCamera(Camera camera) {

        if (camera) {

            Draw(camera, camera.transform.localToWorldMatrix * Matrix4x4.TRS(Vector3.forward * 10, Quaternion.identity, Vector3.one));
        }
    }

    private void Draw(Camera camera, Matrix4x4 matrix) {

        if (mesh && material) {

            if (instances > 1) {

                material.enableInstancing = true;

                if (matrices == null || matrices.Length != instances) {

                    matrices = new Matrix4x4[instances];
                }

                if (properties == null) {

                    properties = new MaterialPropertyBlock();
                }

                for (int i = 0; i < instances; i++) {

                    matrix = Matrix4x4.TRS(camera.transform.TransformPoint(3 * i, 0, 5 + i * 10), Quaternion.identity, Vector3.one);
                    matrices[i] = matrix;
                }

                if (!renderWithCamera) {

                    //Will Render, but meshes are drawn in every camera :(
                    Graphics.DrawMeshInstanced(mesh, 0, material, matrices, instances, properties, ShadowCastingMode.Off, false, gameObject.layer, null);
                }
                else {

                    //Won't Render
                    Graphics.DrawMeshInstanced(mesh, 0, material, matrices, instances, properties, ShadowCastingMode.Off, false, gameObject.layer, camera);
                }
            }
            else {

                material.enableInstancing = false;

                Graphics.DrawMesh(mesh, matrix, material, gameObject.layer, camera);
            }
        }
    }
}

I’ve also tried drawing the mesh with

OnRenderObject,
OnPreRender,
OnPostRender,
Update,
LateUpdate
DrawGizmos

No matter what event I’m hooking into, when supplying a camera, the meshes won’t draw.

Ohhhhh. This is only affecting 2017.1. Works in 5.6

Fun.

We have a bug logged about this for 2017.1. Should be fixed soon. Passing a camera to this API is broken :frowning:

@richardkettlewell is this still broken in 2018.2.11f1?

It’s still broken in 2021.2.3f1

I’ve no idea what the bug report i was referring to (because it was over 4 years ago) so best report a new bug if you’d like it fixed.

any update?2021.3.5f1 still, but i can see something is flash, but if set camera to null , render is perfect, i think maybe the camera not get render command when it ready to draw