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:
I have tried every single CameraEvent value. Nothing works, the cube is either black or doesn’t show up at all