Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Material Overrides not doing anything?

Discussion in 'Entity Component System' started by DizzyTornado, Sep 2, 2021.

  1. DizzyTornado

    DizzyTornado

    Joined:
    Nov 17, 2013
    Posts:
    134
    Basically, I am spawning a bunch of cubes and I want them to share a material but I want each to have a different color.

    The cube prefab has a material with a HDRP shadergraph on it with an exposed property with reference "_Color".

    Then I have a component like this to modify color,
    Code (CSharp):
    1. [Serializable]
    2. [MaterialProperty("_Color", MaterialPropertyFormat.Float4)]
    3. public struct ColorOverride : IComponentData
    4. {
    5.     public float4 Value;
    6. }
    When spawning all the cubes I do this,
    Code (CSharp):
    1. Entity newNodeEntity = entityManager.Instantiate( nodeEntityPrefab );
    2.  
    3.         ColorOverride colorOverride = new ColorOverride()
    4.         {
    5.             Value = colorF
    6.         };
    7.         entityManager.AddComponentData( newNodeEntity, colorOverride );
    For some reason this doesn't do anything and the color is just the same as the default for the material.

    Can anyone help with this? The only documentation for current versions is for doing it with the premade material override object which does not allow you to modify color via code.
     
    Last edited: Sep 2, 2021
  2. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    Try "_BaseColor" instead of "_Color". Your code looks good for me just check if the property name is correct.
     
  3. DizzyTornado

    DizzyTornado

    Joined:
    Nov 17, 2013
    Posts:
    134
    I'll give it a shot but I don't think this is the issue because the property/reference name is "_Color"
     
    Krajca likes this.
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,987
    There's are poorly-named checkbox in the shader graph property that I think is called "Override References" or something like that. If you check it, it adds more settings, one of which lets you choose the property to be per hybrid instance. Also make sure that the reference name isn't some weird hash but the actual _Color. That is different from the property name.
     
    Krajca likes this.
  5. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    Your code looks good (as long as your shader color name matches the _Color). Here's a step that I have had to do each time I start a new build.

    Right-click in your assets folder
    -Create
    -Rendering
    -High Definition Render Pipeline Asset

    Name it whatever you want
    In Project Settings
    -Graphics
    Add this new asset to the "Scriptable Render Pipeline Settings"

    Restart Unity
     
    DizzyTornado likes this.
  6. DizzyTornado

    DizzyTornado

    Joined:
    Nov 17, 2013
    Posts:
    134
    This did it!!!! I had to check "Override Property Declaration" and set "Shader Declaration" to "Hybrid Per Instance". Been having headaches about this issue for over a month...