Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

CommandBuffer DrawMesh is black

Discussion in 'General Graphics' started by CDF, Jun 6, 2017.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    853
    Just trying to draw a cube with a material that gets lit. Can't be that hard can it?

    I'm using Forward Rendering

    Code (CSharp):
    1. [ExecuteInEditMode]
    2. [RequireComponent(typeof(MeshRenderer))]
    3. public sealed class CubeRenderer : MonoBehaviour {
    4.  
    5.     public Mesh mesh;
    6.     public Material material;
    7.  
    8.     private Dictionary<Camera, CommandBuffer> cameras = new Dictionary<Camera, CommandBuffer>();
    9.  
    10.     private void OnDisable() {
    11.  
    12.         foreach (var kvp in cameras) {
    13.  
    14.             if (kvp.Key) {
    15.  
    16.                 kvp.Key.RemoveCommandBuffer(CameraEvent.AfterForwardOpaque, kvp.Value);
    17.             }
    18.         }
    19.  
    20.         cameras.Clear();
    21.     }
    22.  
    23.     void OnWillRenderObject() {
    24.  
    25.         bool active = enabled && gameObject.activeInHierarchy;
    26.  
    27.         if (!active) {
    28.  
    29.             OnDisable();
    30.             return;
    31.         }
    32.  
    33.         Camera camera = Camera.current;
    34.  
    35.         if (!camera || !material || !mesh) {
    36.  
    37.             return;
    38.         }
    39.  
    40.         Matrix4x4 matrix = Matrix4x4.identity;
    41.  
    42.         CommandBuffer buffer;
    43.  
    44.         if (!cameras.TryGetValue(camera, out buffer)) {
    45.  
    46.             buffer = new CommandBuffer();
    47.             buffer.name = "Command Buffer";
    48.  
    49.             camera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, buffer);
    50.             cameras.Add(camera, buffer);
    51.  
    52.             buffer.DrawMesh(mesh, matrix, material);
    53.         }
    54.         else {
    55.  
    56.             foreach (CommandBuffer cb in cameras.Values) {
    57.  
    58.                 cb.Clear();
    59.                 cb.DrawMesh(mesh, matrix, material);
    60.             }
    61.         }
    62.     }
    63. }
    The Result I get:

    blackCube.png

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

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    853
    Wow

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

    come on Unity.
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,438
    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.)
     
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    853
    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 :(
     
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,438
    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.
     
  6. SgtOkiDoki

    SgtOkiDoki

    Joined:
    Sep 7, 2016
    Posts:
    8
    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
    https://github.com/keijiro/LightProbeUtility
     
unityunity