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

Question How do I change material values of Lit shader at runtime?

Discussion in 'High Definition Render Pipeline' started by jjejj87, Dec 26, 2021.

  1. jjejj87

    jjejj87

    Joined:
    Feb 2, 2013
    Posts:
    1,115
    Hi, I am trying to disable/enable pixel displacement of some materials at runtime (for performance reasons).

    And I've come this far

    Code (CSharp):
    1. matWithHeight.EnableKeyword("_DisplacementMode");
    2. matWithHeight.SetInt("_DisplacementMode", 0);
    Now, it updates in the inspector but nothing in the actual game. I am guessing I am missing renderqueue or some keywords. Can anyone help?

    Thanks!
     
  2. jjejj87

    jjejj87

    Joined:
    Feb 2, 2013
    Posts:
    1,115
    Please, can anyone help? I am really stuck with this. Any help will be appreciated.

    Thanks
     
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    I believe the correct keyword is _PIXEL_DISPLACEMENT

    Also since this is a shader feature you might have issues at build time if both variations of the shader don’t pre exist at build time, but that’s a different issue.
     
  4. jjejj87

    jjejj87

    Joined:
    Feb 2, 2013
    Posts:
    1,115
    Acid, thanks for the help. I did try to add that keyword, but same thing but no joy. It changes in the inspector but nothing in the render. I really wonder what I have to do to get this to work.

    Still looking for ways to do this.
     
  5. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    760
    In latest HDRP, there's a function called HDMaterials.ValidateMaterial(Material) that adds/removes keywords (the same way inspector does when you change shader features)

    This should fix your issue.