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. Dismiss Notice

HDRP Material Conversion needs to happen for Autodesk Interactive

Discussion in 'Editor & General Support' started by siliconkgk, May 26, 2022.

  1. siliconkgk

    siliconkgk

    Joined:
    Oct 5, 2021
    Posts:
    27
    I am consistently getting materials that use a autodeks interactive shader and they do not convert to a lit HDRP material. This is so consistent with asset store materials that I'm not sure why Unity has not fixed this as a quality of life thing for developing. When switching manually to HDRP Lit it wipes all textures out of their slots.
     
    TOES likes this.
  2. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
    I had the same problem so I wrote this script to convert materials to HDRP. It works with Autodesk Interactive materials, but can easily be extended to support other materials as well.

    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. using UnityEngine;
    4. using static UnityEditor.ShaderGraph.Internal.KeywordDependentCollection;
    5.  
    6. public class MaterialToHDRP : MonoBehaviour
    7. {
    8.  
    9.     [MenuItem("Tools/Convert selected Materials to HDRP...", priority = 0)]
    10.     private static void upgradeSelected()
    11.     {
    12.         foreach(string guid in Selection.assetGUIDs)
    13.         {
    14.             string assetPath= AssetDatabase.GUIDToAssetPath(guid);
    15.             Material m=AssetDatabase.LoadAssetAtPath<Material>(assetPath);
    16.             Material mInstance=Instantiate(AssetDatabase.LoadAssetAtPath<Material>(assetPath));
    17.             mInstance.name = m.name;
    18.             if (convert(mInstance))
    19.                 EditorUtility.CopySerialized(mInstance, m); //Makes sure we keep the original GUID      
    20.         }
    21.         AssetDatabase.SaveAssets();
    22.     }
    23.  
    24.     private static bool convert(Material m)
    25.     {
    26.         string shaderName= m.shader.name;
    27.         if (shaderName.Equals("Autodesk Interactive",StringComparison.OrdinalIgnoreCase))
    28.         {
    29.             //Read
    30.             Texture albedo = m.GetTexture("_MainTex");
    31.             Texture metallic = m.GetTexture("_MetallicGlossMap");
    32.             Texture roughness = m.GetTexture("_SpecGlossMap");
    33.             Texture normal = m.GetTexture("_BumpMap");
    34.             float bumpScale = m.GetFloat("_BumpScale");
    35.             Vector2 offset = m.mainTextureOffset;
    36.             Vector2 tiling = m.mainTextureScale;
    37.  
    38.             //Convert
    39.             m.shader= Shader.Find("HDRP/Lit");
    40.             m.SetTexture("_BaseColorMap", albedo);
    41.             m.SetTexture("_NormalMap", normal);
    42.             m.SetFloat("_NormalScale", bumpScale);
    43.             m.mainTextureOffset = offset;
    44.             m.mainTextureScale = tiling;
    45.  
    46.             return true;
    47.         }
    48.         return false;
    49.     }
    50. }
    51.  
    Note that it does not copy all the material parameters, but can be extended for more detailed conversion. However, some of the maps cannot be used in the Lit shader directly anyway, as the autodesk interactive material have separate slots for roughness and metallic for example, while the Lit shader has combined multiple masks into one using the RGB components, which helps save memory.

    For a perfect conversion you'd need to make a shadergraph shader that takes the same input as the Autodesk Interactive shader, and then use the above script to convert to that new shader.

    Hope it is helpful for someone :)
     
    Last edited: Nov 30, 2022