Search Unity

HDRP materials created by editor script do not reflect the settings set on them

Discussion in 'High Definition Render Pipeline' started by dantman, Dec 24, 2019.

  1. dantman

    dantman

    Joined:
    Jan 3, 2017
    Posts:
    4
    I'm having trouble writing an editor tool for batch creating a bunch of HDRP materials.

    Typically this code should work for creating a HDRP/Lit material:
    Code (CSharp):
    1. Material mat = new Material(Shader.Find("HDRP/Lit"));
    2. mat.SetInt(HDMaterialProperties.kMaterialID, (int)Lit.MaterialFeatureFlags.LitSpecularColor);
    3. mat.SetFloat(kSurfaceType, (float)SurfaceType.Transparent);
    4. mat.SetFloat(kBlendMode, (float)BlendMode.Additive);
    5. mat.SetFloat(kEnableBlendModePreserveSpecularLighting, 0);
    6. mat.SetTexture("_BaseColorMap", baseTexture);
    7. AssetDatabase.CreateAsset(mat, "Assets/Materials/Test.mat");
    However while the values do get set and you "see" them in the editor, the material does not actually reflect the settings that are set. i.e. The editor says "Surface Type: Transparent" but the material isn't actually transparent until you manually change settings in the GUI for each material.

    I understand this is because there are other internal configurations that get set when you change things in the GUI. But from what I can tell all the classes related to this like LitGUI are left as default private classes meaning none of the functions used to re-initialize these internal settings or set things can be called from editor scripts outside the package.

    How do I ensure material settings are set correctly when I create them from scripts?
     
  2. Shinao

    Shinao

    Joined:
    Mar 7, 2014
    Posts:
    36
    Got the same problem. Did you resolve your issue ?
     
  3. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    934
    Hi, there is currently no helper tool provided to do that.
    The GUI is doing a lot of thing under the hood (settings renderstate, setting Keyword, setting renderqueue etc...). When you try to create a Material yourself you require to do all those step too, and as you mention there is no public API provide for that.
    This is a know problem and we hope to be able to invest time in it in the future. We have no ETA for it for now. Only workaround is to look at the GUI code and replicated the behavior. It is not public because we are still changing the behavior.
     
  4. Shinao

    Shinao

    Joined:
    Mar 7, 2014
    Posts:
    36
    Indeed I have looked to the HDRP shader directly and saw that I missed some keywords to enable which the Editor set automatically if there was a texture linked to it.
    To use a NormalMap I needed to set the texture and enable both "_NORMALMAP" and "_NORMALMAP_TANGENT_SPACE" keywords.
     
  5. Guillaume-atVolumiq

    Guillaume-atVolumiq

    Joined:
    Sep 14, 2018
    Posts:
    36
    Same problem here, we have HDRP materials made from scratch based on Scriptable Objects but the textures won't show until the inspector dropdown is clicked.



    Enabling the propers keywords doesn't help either (Pillow on the left not yet "refreshed")

     
  6. BigKnockDown

    BigKnockDown

    Joined:
    Mar 23, 2013
    Posts:
    16
    Is the issue sorted in any of the current or upcoming version of Unity ? Or is there any workaround for this ?
     
  7. Brody-TOG

    Brody-TOG

    Joined:
    May 7, 2020
    Posts:
    15
    Hi! I'm also stumbling upon this thread trying to create HDRP/Lit materials through editor script. All I want to do is hook up Albedo / Normal / Mask textures. Is this feasible, or is that still WIP?
     
    lacas8282 likes this.
  8. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,266
    Last edited: Sep 24, 2020
  9. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    Well the easiest solution right now is to use a template material with the correct shader and create the new one with ->
    new Material(_templateMaterial);
    .
     
  10. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,266
    That is a starting point.
    Then you have to learn the names of all the material parts to set things -
    material.SetFloat( "_Metallic", metallic );
    material.SetTexture( "_BaseColorMap", texture );

    This was never documented and there were no new properties to make it easy and if you make a new Shader Graph it will not init with all the same exact named properties because you have to start from scratch... not good
    Any new doc section on this?
     
  11. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    why not create an empty material in the editor then use that one as starting point at runtime?