Search Unity

Creating Material Assets with Metallic / Smoothness Channels

Discussion in 'Shaders' started by cynex, Aug 29, 2017.

  1. cynex

    cynex

    Joined:
    May 5, 2015
    Posts:
    10
    Hey there, I've been struggling with what seems to be a simple problem, and I'm hoping the answer is just as simple.
    I've been working on a tool to merge texture sets and automatically create a new material asset for those texture sets, and everything works so far, aside from the metallic smoothness channel. After looking around yesterday, I read that you need to use EnableKeyword to indicate shader variants.

    I've broken down a small part of my code to give an overvierw:
    Code (CSharp):
    1. Texture2D MetallicGlossMap = (Texture2D)AssetDatabase.LoadAssetAtPath("/PathOf/RecentlySavedPNGAsset.png", typeof(Texture2D));  
    2.  
    3. Material newMaterial = new Material(Shader.Find("Standard"));
    4.  
    5. // Enable the reqired Shader Variants
    6. newMaterial.EnableKeyword ("_METALLICGLOSSMAP");
    7. newMaterial.EnableKeyword ("_NORMALMAP");  
    8. newMaterial.EnableKeyword ("_DETAIL_MULX2");
    9. newMaterial.EnableKeyword ("_PARALLAXMAP");  
    10.  
    11. newMaterial.SetTexture ("_MetallicGlossMap",MetallicGlossMap);
    12.  
    13. // And obviously the other required Textures.
    14. //newMaterial.SetTexture ("_BumpMap",..Normal Map...);
    15. //newMaterial.SetTexture ("_EmissionMap",...Emission Map...);
    16.  
    17. AssetDatabase.CreateAsset (newMaterial,"/PathOfTheMaterial.mat");
    18. AssetDatabase.ImportAsset("/PathOfTheMaterial.mat", ImportAssetOptions.ForceUpdate);
    The result is a newly created .mat file with every channel I've needed so far, with exception of metallic smoothness. on first Inspection using debug mode, I do see the shader keywords applied as I've illustrated in the screenshot attached, however, when I use the normal inspector and return to debug inspector, those keywords have been removed.

    If anyone has any pointers as to what I am doing wrong, I'd really appreciate it.
     

    Attached Files:

  2. cynex

    cynex

    Joined:
    May 5, 2015
    Posts:
    10
    I suppose I should mention that I am using Unity 5.6.3f1