Search Unity

Shader Replacement not working with Graphics.DrawMeshInstancedIndirect()

Discussion in 'Shaders' started by Ramphic, May 15, 2018.

  1. Ramphic

    Ramphic

    Joined:
    Jun 21, 2017
    Posts:
    60
    Hi,
    Shader replacement is not working for material of mesh rendered using Graphics.DrawMeshInstancedIndirect() . But it's working for normal gameobjects in scene with any shader.

    How to make it work for Graphics.DrawMeshInstancedIndirect() ?

    Thankyou !!
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Does the shader you replace with have the same instanced indirect setup function?
     
  3. Ramphic

    Ramphic

    Joined:
    Jun 21, 2017
    Posts:
    60
    Thankyou for the reply.
    Both the replacement shader and already present shader for instanced indirect mesh are vert frag shaders. So I am not using any setup function as per unity doc here.

    Finally what I want is bloom effect for only the objects (which are drawn with Graphics.DrawMeshInstancedIndirect()) with particular shader. So, I am trying to achieve it using replacement shaders.

    Thankyou !!
     
  4. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    when drawing instanced indirect each instance does not have the unity_ObjectToWorld and unity_WorldToObject set. This you need to do in a function spesified by the
    #pragma instancing_options procedural:setup

    in this case a function called setup.

    This function has to be present on the replacement shaders you try to use. If not it will render all instances with "empty" matrixes.

    Lennart
     
  5. Ramphic

    Ramphic

    Joined:
    Jun 21, 2017
    Posts:
    60
    Ohh, I din't know that. Thankyou !. But what values should I set to them in replacement shaders?
     
  6. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    the setup function needs to set the same values as the original shader you are replacing does. The materials also need to to have the same compute buffers etc.
     
  7. Ramphic

    Ramphic

    Joined:
    Jun 21, 2017
    Posts:
    60
    How can I access what values I assigned to original shader? If possible , can you please provide sample replacement shader that works for unity example of Graphics.DrawMeshInstancedIndirect() given here.

    Thankyou!
     
  8. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    If you wanted to make another shader replacing the one in your link it needs this section.
    In addition to what the shader normally would do.

    But remember this will change depending on what the original shader does and how it calculates the matrixes.
    You can not make an generic replacement shader that works for all instanced indirect implementations.

    Code (csharp):
    1.  
    2.  #pragma instancing_options procedural:setup
    3.  
    4.    #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    5.        StructuredBuffer<float4> positionBuffer;
    6.    #endif
    7.  
    8. void setup()
    9.        {
    10.        #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    11.            float4 data = positionBuffer[unity_InstanceID];
    12.  
    13.             float rotation = data.w * data.w * _Time.y * 0.5f;
    14.            rotate2D(data.xz, rotation);
    15.  
    16.             unity_ObjectToWorld._11_21_31_41 = float4(data.w, 0, 0, 0);
    17.            unity_ObjectToWorld._12_22_32_42 = float4(0, data.w, 0, 0);
    18.            unity_ObjectToWorld._13_23_33_43 = float4(0, 0, data.w, 0);
    19.            unity_ObjectToWorld._14_24_34_44 = float4(data.xyz, 1);
    20.            unity_WorldToObject = unity_ObjectToWorld;
    21.            unity_WorldToObject._14_24_34 *= -1;
    22.            unity_WorldToObject._11_22_33 = 1.0f / unity_WorldToObject._11_22_33;
    23.        #endif
    24.        }
    25.  
    26.  
     
  9. Ramphic

    Ramphic

    Joined:
    Jun 21, 2017
    Posts:
    60
    Thankyou ,it worked !! :)
     
  10. Ramphic

    Ramphic

    Joined:
    Jun 21, 2017
    Posts:
    60
    Hi, It worked with camera.setReplacementShader() function. But not working with camera.RenderWithShader(). Why so?