Search Unity

Per instance Material property without ShaderGraph

Discussion in 'Entity Component System' started by Mephesto_Khaan, Mar 9, 2021.

  1. Mephesto_Khaan

    Mephesto_Khaan

    Joined:
    Jun 2, 2013
    Posts:
    47
    I have been checking: https://forum.unity.com/threads/per-instance-material-params-support-in-entities-0-2.782207/page-2

    I want to have a per-instance property _DirtColor in my material, but I am writing the shader manually (no ShaderGraph) and it seems to fail to find the METADATA method to get the instanced value out.



    The errors:

    Shader error in 'Obstacle Shadowed': 'LoadDOTSInstancedData_float4': no matching 1 parameter function at line 225 (on d3d11)
    Shader error in 'Obstacle Shadowed': undeclared identifier 'unity_DOTSInstancing_F16_Metadata__DirtColor' at line 225 (on d3d11)



    What I am doing:

    Using URP and Unity 2020.3f1

    I add this component to my entities:

    Code (CSharp):
    1.  
    2. [MaterialProperty("_DirtColor", MaterialPropertyFormat.Float4)]
    3. public struct DirtMaterialProperty : IComponentData
    4. {
    5.   public float4 Value;
    6. }
    7.  

    And in the shader, following the thread above and , I have:

    Code (CSharp):
    1.  
    2. #pragma multi_compile _ DOTS_INSTANCING_ON
    3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    4. #ifdef UNITY_DOTS_INSTANCING_ENABLED
    5.   UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
    6.     UNITY_DEFINE_INSTANCED_PROP(float4,_DirtColor)
    7.   UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)
    8.   #define _DirtColor UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4, Metadata__DirtColor)
    9. #else
    10.    #define _DirtColor half4(0,0,0,0)
    11. #endif
    12.  
    Result is pink, so definitely going inside that #ifdef


    ---------------------
    EDIT:

    Ah! Solved, it seems I am blind

    I was using
    UNITY_DEFINE_INSTANCED_PROP

    where it should be
    UNITY_DOTS_INSTANCED_PROP
     
    Last edited: Mar 9, 2021
  2. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    I am not 100% but checking my own shader, I also have every instanced property inside the cbuffer as well:

    Code (CSharp):
    1. CBUFFER_START(UnityPerMaterial)
    2. float4 _BaseColor; //or _DirtColor in your case
    3. CBUFFER_END
    4.  
     
  3. Mephesto_Khaan

    Mephesto_Khaan

    Joined:
    Jun 2, 2013
    Posts:
    47
    Thanks, but afraid that did not work. (Redefinition of _DirtColor error, even if I change my #define to _DirtColorX).

    Maybe in your case is working without DOTs intancing?
     
  4. Mephesto_Khaan

    Mephesto_Khaan

    Joined:
    Jun 2, 2013
    Posts:
    47
    Ah! Solved, it seems I am blind

    I was using
    UNITY_DEFINE_INSTANCED_PROP

    where it should be
    UNITY_DOTS_INSTANCED_PROP