123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using UnityEditor.Experimental;
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- #if UNITY_2020_2_OR_NEWER
- using UnityEditor.AssetImporters;
- #else
- using UnityEditor.Experimental.AssetImporters;
- #endif
- namespace UnityEditor.Rendering.Universal
- {
- class PhysicalMaterial3DsMaxPreprocessor : AssetPostprocessor
- {
- static readonly uint k_Version = 1;
- static readonly int k_Order = 4;
- static readonly string k_ShaderPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/PhysicalMaterial3DsMax/PhysicalMaterial3DsMax.shadergraph";
- static readonly string k_ShaderTransparentPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/PhysicalMaterial3DsMax/PhysicalMaterial3DsMaxTransparent.shadergraph";
- public override uint GetVersion()
- {
- return k_Version;
- }
- public override int GetPostprocessOrder()
- {
- return k_Order;
- }
-
- static bool Is3DsMaxPhysicalMaterial(MaterialDescription description)
- {
- float classIdA;
- float classIdB;
- string originalMtl;
- description.TryGetProperty("ClassIDa", out classIdA);
- description.TryGetProperty("ClassIDb", out classIdB);
- description.TryGetProperty("ORIGINAL_MTL", out originalMtl);
- return classIdA == 1030429932 && classIdB == -559038463 || originalMtl == "PHYSICAL_MTL";
- }
- static bool Is3DsMaxSimplifiedPhysicalMaterial(MaterialDescription description)
- {
- float classIdA;
- float classIdB;
- float useGlossiness;
- description.TryGetProperty("ClassIDa", out classIdA);
- description.TryGetProperty("ClassIDb", out classIdB);
- description.TryGetProperty("useGlossiness", out useGlossiness);
-
- return classIdA == -804315648 && classIdB == -1099438848 && useGlossiness == 2.0f;
- }
- public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips)
- {
- var pipelineAsset = GraphicsSettings.currentRenderPipeline;
- if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset))
- return;
- if (Is3DsMaxPhysicalMaterial(description))
- {
- CreateFrom3DsPhysicalMaterial(description, material, clips);
- }
- else if (Is3DsMaxSimplifiedPhysicalMaterial(description))
- {
- CreateFrom3DsSimplifiedPhysicalMaterial(description, material, clips);
- }
- }
- void CreateFrom3DsSimplifiedPhysicalMaterial(MaterialDescription description, Material material, AnimationClip[] clips)
- {
- float floatProperty;
- Vector4 vectorProperty;
- TexturePropertyDescription textureProperty;
- description.TryGetProperty("basecolor", out vectorProperty);
- bool hasTransparencyScalar = vectorProperty.w !=1.0f;
- var hasTransparencyMap = description.TryGetProperty("opacity_map", out textureProperty);
- bool isTransparent = hasTransparencyMap | hasTransparencyScalar;
-
- Shader shader;
- if (isTransparent)
- shader = GraphicsSettings.currentRenderPipeline.autodeskInteractiveTransparentShader;
- else
- shader = GraphicsSettings.currentRenderPipeline.autodeskInteractiveShader;
- if (shader == null)
- return;
- material.shader = shader;
- foreach (var clip in clips)
- {
- clip.ClearCurves();
- }
- if (hasTransparencyMap)
- {
- material.SetFloat("_UseOpacityMap", 1.0f);
- material.SetTexture("_OpacityMap", textureProperty.texture);
- }
- else if (hasTransparencyScalar)
- {
- material.SetFloat("_Opacity", vectorProperty.w);
- }
- if (description.TryGetProperty("basecolor", out vectorProperty))
- material.SetColor("_Color", vectorProperty);
- if (description.TryGetProperty("emit_color", out vectorProperty))
- material.SetColor("_EmissionColor", vectorProperty);
- if (description.TryGetProperty("roughness", out floatProperty))
- material.SetFloat("_Glossiness", floatProperty);
- if (description.TryGetProperty("metalness", out floatProperty))
- material.SetFloat("_Metallic", floatProperty);
- if (description.TryGetProperty("base_color_map", out textureProperty))
- {
- material.SetTexture("_MainTex", textureProperty.texture);
- material.SetFloat("_UseColorMap", 1.0f);
- material.SetColor("_UvTiling", new Vector4(textureProperty.scale.x, textureProperty.scale.y, 0.0f, 0.0f));
- material.SetColor("_UvOffset", new Vector4(textureProperty.offset.x, textureProperty.offset.y, 0.0f, 0.0f));
- }
- else
- {
- material.SetFloat("_UseColorMap", 0.0f);
- }
- if (description.TryGetProperty("norm_map", out textureProperty))
- {
- material.SetTexture("_BumpMap", textureProperty.texture);
- material.SetFloat("_UseNormalMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseNormalMap", 0.0f);
- }
- if (description.TryGetProperty("roughness_map", out textureProperty))
- {
- material.SetTexture("_SpecGlossMap", textureProperty.texture);
- material.SetFloat("_UseRoughnessMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseRoughnessMap", 0.0f);
- }
- if (description.TryGetProperty("metalness_map", out textureProperty))
- {
- material.SetTexture("_MetallicGlossMap", textureProperty.texture);
- material.SetFloat("_UseMetallicMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseMetallicMap", 0.0f);
- }
- if (description.TryGetProperty("emit_color_map", out textureProperty))
- {
- material.SetTexture("_EmissionMap", textureProperty.texture);
- material.SetFloat("_UseEmissiveMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseEmissiveMap", 0.0f);
- }
- if (description.TryGetProperty("ao_map", out textureProperty))
- {
- var tex = AssetDatabase.LoadAssetAtPath<Texture>(textureProperty.relativePath);
- material.SetTexture("AoMap", tex);
- material.SetFloat("UseAoMap", 1.0f);
- }
- else
- {
- material.SetFloat("UseAoMap", 0.0f);
- }
- }
- void CreateFrom3DsPhysicalMaterial(MaterialDescription description, Material material, AnimationClip[] clips)
- {
- float floatProperty;
- Vector4 vectorProperty;
- TexturePropertyDescription textureProperty;
- Shader shader;
- description.TryGetProperty("transparency", out floatProperty);
- bool hasTransparencyMap =
- description.TryGetProperty("transparency_map", out textureProperty);
- if (floatProperty > 0.0f || hasTransparencyMap)
- {
- shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderTransparentPath);
- if (shader == null)
- return;
- material.shader = shader;
- if (hasTransparencyMap)
- {
- material.SetTexture("_TRANSPARENCY_MAP", textureProperty.texture);
- material.SetFloat("_TRANSPARENCY", 1.0f);
- }
- else
- {
- material.SetFloat("_TRANSPARENCY", floatProperty);
- }
- }
- else
- {
- shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderPath);
- if (shader == null)
- return;
- material.shader = shader;
- }
- foreach (var clip in clips)
- {
- clip.ClearCurves();
- }
- RemapPropertyFloat(description, material, "base_weight", "_BASE_COLOR_WEIGHT");
- if (description.TryGetProperty("base_color_map", out textureProperty))
- {
- SetMaterialTextureProperty("_BASE_COLOR_MAP", material, textureProperty);
- }
- else if (description.TryGetProperty("base_color", out vectorProperty))
- {
- if (QualitySettings.activeColorSpace == ColorSpace.Gamma)
- {
- vectorProperty.x = Mathf.LinearToGammaSpace(vectorProperty.x);
- vectorProperty.y = Mathf.LinearToGammaSpace(vectorProperty.y);
- vectorProperty.z = Mathf.LinearToGammaSpace(vectorProperty.z);
- vectorProperty.w = Mathf.LinearToGammaSpace(vectorProperty.w);
- }
- material.SetColor("_BASE_COLOR", vectorProperty);
- }
- RemapPropertyFloat(description, material, "reflectivity", "_REFLECTIONS_WEIGHT");
- RemapPropertyTextureOrColor(description, material, "refl_color", "_REFLECTIONS_COLOR");
- RemapPropertyTextureOrFloat(description, material, "metalness", "_METALNESS");
- RemapPropertyTextureOrFloat(description, material, "roughness", "_REFLECTIONS_ROUGHNESS");
- RemapPropertyTextureOrFloat(description, material, "trans_ior", "_REFLECTIONS_IOR");
- RemapPropertyFloat(description, material, "emission", "_EMISSION_WEIGHT");
- RemapPropertyTextureOrColor(description, material, "emit_color", "_EMISSION_COLOR");
- RemapPropertyFloat(description, material, "bump_map_amt", "_BUMP_MAP_STRENGTH");
- RemapPropertyTexture(description, material, "bump_map", "_BUMP_MAP");
- }
- static void SetMaterialTextureProperty(string propertyName, Material material,
- TexturePropertyDescription textureProperty)
- {
- material.SetTexture(propertyName, textureProperty.texture);
- material.SetTextureOffset(propertyName, textureProperty.offset);
- material.SetTextureScale(propertyName, textureProperty.scale);
- }
- static void RemapPropertyFloat(MaterialDescription description, Material material, string inPropName,
- string outPropName)
- {
- if (description.TryGetProperty(inPropName, out float floatProperty))
- {
- material.SetFloat(outPropName, floatProperty);
- }
- }
- static void RemapPropertyTexture(MaterialDescription description, Material material, string inPropName,
- string outPropName)
- {
- if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
- {
- material.SetTexture(outPropName, textureProperty.texture);
- }
- }
- static void RemapPropertyTextureOrColor(MaterialDescription description, Material material,
- string inPropName, string outPropName)
- {
- if (description.TryGetProperty(inPropName + "_map", out TexturePropertyDescription textureProperty))
- {
- material.SetTexture(outPropName + "_MAP", textureProperty.texture);
- material.SetColor(outPropName, Color.white);
- }
- else if(description.TryGetProperty(inPropName, out Vector4 color))
- {
- material.SetColor(outPropName, color);
- }
- }
- static void RemapPropertyTextureOrFloat(MaterialDescription description, Material material,
- string inPropName, string outPropName)
- {
- if (description.TryGetProperty(inPropName + "_map", out TexturePropertyDescription textureProperty))
- {
- material.SetTexture(outPropName + "_MAP", textureProperty.texture);
- material.SetFloat(outPropName, 1.0f);
- }
- else if(description.TryGetProperty(inPropName, out float floatProperty))
- {
- material.SetFloat(outPropName, floatProperty);
- }
- }
- }
- }
|