Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

CommandBuffer DrawMeshInstancedIndirect

Discussion in 'General Graphics' started by AlcogGMS, Jun 25, 2019.

  1. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
    Hi all, sry for my english.

    I need to render objects with CommandBuffer DrawMeshInstancedIndirect. Albedo rendered fine, but objects havent ambient light and not receive shadows, they cast shadows only on another objects that rendered without commandBuffers.
    All works fine with Graphics.DrawMeshInstancedIndirect, but how enable shadows and ambient light with CommandBuffers ?
    Light model - forward, no SRP used.

    Code (CSharp):
    1.         m_buffer = new CommandBuffer();
    2.         m_buffer.name = "FORWARD";
    3.         m_buffer.SetRenderTarget(BuiltinRenderTextureType.CurrentActive);
    4.         m_buffer.DrawMeshInstancedIndirect(m_GroupMesh, 0, m_GroupMaterial, m_GroupMaterial.FindPass("FORWARD"), m_indirArgsBuffer);
    5.         agmsCam.m_Camera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, m_buffer);
    6.         Light light = FindObjectOfType<Light>();
    7.  
    8.         //CAST SHADFOWS ???
    9.         m_buffer = new CommandBuffer();
    10.         m_buffer.name = "CAST SHADOWS";
    11.         m_buffer.DrawMeshInstancedIndirect(m_GroupMesh, 0, m_GroupMaterial, m_GroupMaterial.FindPass("ShadowCaster"), m_indirArgsBuffer);
    12.         light.AddCommandBuffer(LightEvent.BeforeShadowMapPass, m_buffer);
    13.  
    shader code:
    Code (CSharp):
    1. Shader "AGMS/IndirectInstanced"{
    2.     Properties{
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.  
    9.     SubShader{
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.  
    13.         CGPROGRAM
    14.         #pragma surface surf Standard addshadow fullforwardshadows
    15.         #pragma multi_compile_instancing
    16.         #pragma instancing_options assumeuniformscaling procedural:setup
    17.         #pragma target 5.0
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input{
    22.             float2 uv_MainTex;
    23.         };
    24.  
    25. #if defined(SHADER_API_D3D11) || defined(SHADER_API_D3D12)
    26.         StructuredBuffer<float4x4> _matrixBuffer;
    27. #endif
    28.         void setup(){
    29. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    30.             unity_ObjectToWorld = _matrixBuffer[unity_InstanceID];      
    31.             unity_WorldToObject = unity_ObjectToWorld;
    32.             unity_WorldToObject._14_24_34 *= -1;
    33.             unity_WorldToObject._11_22_33 = 1.0f / unity_WorldToObject._11_22_33;
    34. #endif
    35.         }
    36.    
    37.         half _Glossiness;
    38.         half _Metallic;
    39.         fixed4 _Color;
    40.  
    41.         void surf (Input IN, inout SurfaceOutputStandard o){
    42.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    43.             o.Albedo = c.rgb;
    44.             o.Metallic = _Metallic;
    45.             o.Smoothness = _Glossiness;
    46.             o.Alpha = c.a;
    47.         }
    48.         ENDCG
    49.     }
    50. }


    trees and ground that rendered with DrawMeshInstancedIndirect have not shadows and ambient light
     
    Last edited: Jun 25, 2019
  2. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
  3. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
  4. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
    is someone alive here?
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  6. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
    I have already done using Graphics.DrawMeshInstancedIndirect and everything works, I want to remake using CommandBuffer DrawMeshInstancedIndirect, but objects do not accept shadows
     
  7. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
  8. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
  9. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
  10. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
  11. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
    Are Unity representatives on the forum?
    Can no one give an answer?
    It's been 3 weeks now!
    Where is the tutorial on using CommandBuffer DrawMeshInstancedIndirect with shadows?
     
  12. AlcogGMS

    AlcogGMS

    Joined:
    May 18, 2016
    Posts:
    24
    uuppppp
     
  13. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Hi, sometimes no-one really knows the answer - maybe you're doing something no-one has tried before, for example. There are many ways to use our API and not all of them are familiar to us.

    Perhaps try adding a 3rd command buffer pass:
    Code (CSharp):
    1. agmsCam.m_Camera.AddCommandBuffer(CameraEvent.BeforeDepthTexture, m_buffer);
    That is probably needed to get the objects into the buffer that is used for shadow receiving.
    There might be other missing pieces to this puzzle, though.
     
    Baggers_ and karl_jones like this.