Search Unity

Cause Materials to update its properties Material Editor / Standard Shader

Discussion in 'General Graphics' started by daxiongmao, Sep 5, 2021.

  1. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    I am writing a editor script to assign materials and make some changes to materials of a model.
    When i set these in code the actual material is not being updated.
    Say having an empty metallic texture, now i set it. through the editor i assuming this would set a keyword and cause it to recompile. but setting it through material.SetTexture is not causing the same changes.

    I don't see any changes in the view port until i expand the actual material editor of the material i have changed.
    I assuming this is due to the custom Material editor not updating shader keywords etc.

    How can i force this to happen after i have modified a material? Without having to go through each material.
    Re-imports etc don't help.
    MaterialEditor.ApplyMaterialPropertyDrawers doesnt do anything.

    Thanks
     
  2. hypnoslave

    hypnoslave

    Joined:
    Sep 8, 2009
    Posts:
    439
    Not trying to highjack, but I'm seeing a probably related issue. I'm trying to update the values of a material in game using a coroutine (to slowly make something glow, in my case). Point is - it doesn't work, UNTIL I pause the editor, and expand the material in the inspector. Then, all of a sudden, the value pops to what it should be at that point in time.
     
  3. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    Probably the same issue but I am only worried about it for editor scripts.

    Your fix might be little more complicated than mine since your doing it at runtime and the material editors are editor classes.

    I am pretty sure depending on what is changing, new shader keywords have to be set. Which the custom editor does.

    But something like glow i would think would work unless you are setting the texture where it was not set before.
    But the editor could even be doing something like glow == 0 then don't compile glow logic.

    Look at the shader source for the standard shader if thats what your using make sure your setting the right property names. Just in case.

    If it don't find a way then i may have to look at the source for the custom editor and create a function i can call.
    This is probably what you would need to do if you want it to happen at runtime. Or at least look at what that editor is using to enable glow and set values to make it just on enough.
     
  4. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    Having an idea overnight I just tried out and it worked.

    It is hacky solution for me since i am in the editor only.
    Is that once i update the materials over several editor window updates i select each material i modified.
    Code (CSharp):
    1. Selection.activeObject = material;
    and my materials update.

    This has to be done once per frame and not in a single frame. Can use EditorWindow::Update() or probably editor coroutines.
     
  5. hypnoslave

    hypnoslave

    Joined:
    Sep 8, 2009
    Posts:
    439
    Hey that's brilliant! good, simple solution!

    Thanks for your thoughts RE: my situation. maybe I can hack something together. barf.
     
  6. Chen_Zonglin

    Chen_Zonglin

    Joined:
    Dec 30, 2015
    Posts:
    7
    Thanks for your thoughts!!!!!!!!!!!!!!!!!!!!!
     
  7. migwellian

    migwellian

    Joined:
    Mar 20, 2018
    Posts:
    16
    I had this issue too. Great workaround @daxiongmao, thank you!
     
  8. migwellian

    migwellian

    Joined:
    Mar 20, 2018
    Posts:
    16
    I can confirm that when you open your material in the Inspector, it causes any new keywords to be enabled. I was able to work around this issue by calling
    material.EnableKeyword
    for each of these keywords' names. To get the correct keyword names I looked at the contents of
    material.shaderKeywords
    . In contrast to @daxiongmao 's workaround, this one works in a single frame and doesn't require a coroutine.
     
    daxiongmao likes this.
  9. hypnoslave

    hypnoslave

    Joined:
    Sep 8, 2009
    Posts:
    439
    hey thanks for sharing. I forgot about this thread.

    I wasn't able to solve my particular issue with the Standard shader, so I just wrote a surface shader with the functionality I needed and animated the emissive that way.
     
  10. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    Maybe you take it into account already but the inspector I think looks at what is assigned. So if say no emissive texture it won't enable that keyword vs just enabling every keyword.

    Doing your way would probably work at runtime as well.