Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

CommandBuffer DrawMesh is black

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

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,324
    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:
    1,324
    Wow

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

    come on Unity.
     
    jason_skillman likes this.
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    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:
    1,324
    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,520
    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:
    13
    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