123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- using UnityEngine;
- using UnityEditor.AssetImporters;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- namespace UnityEditor.Rendering.Universal
- {
- class AutodeskInteractiveMaterialImport : AssetPostprocessor
- {
- static readonly uint k_Version = 1;
- static readonly int k_Order = 3;
- public override uint GetVersion()
- {
- return k_Version;
- }
- public override int GetPostprocessOrder()
- {
- return k_Order;
- }
- public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips)
- {
- var pipelineAsset = GraphicsSettings.currentRenderPipeline;
- if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset))
- return;
-
- if (IsAutodeskInteractiveMaterial(description))
- {
- float floatProperty;
- Vector4 vectorProperty;
- TexturePropertyDescription textureProperty;
- bool isMasked = description.TryGetProperty("mask_threshold",out floatProperty);
- bool isTransparent = description.TryGetProperty("opacity",out floatProperty);
- Shader shader;
- if (isMasked)
- shader = GraphicsSettings.currentRenderPipeline.autodeskInteractiveMaskedShader;
- else 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 (description.TryGetProperty("base_color", out vectorProperty))
- material.SetColor("_Color", vectorProperty);
- if (description.TryGetProperty("emissive", out vectorProperty))
- material.SetColor("_EmissionColor", vectorProperty);
- if (description.TryGetProperty("roughness", out floatProperty))
- material.SetFloat("_Roughness", floatProperty);
- if (description.TryGetProperty("metallic", out floatProperty))
- material.SetFloat("_Metallic", floatProperty);
- if (description.TryGetProperty("uvTransform", out vectorProperty))
- {
- material.SetVector("_UvOffset", new Vector4(vectorProperty.x, vectorProperty.y, .0f, .0f));
- material.SetVector("_UvTiling", new Vector4(vectorProperty.w, vectorProperty.z, .0f, .0f));
- }
- if (description.TryGetProperty("TEX_color_map", out textureProperty))
- {
- material.SetTexture("_MainTex", textureProperty.texture);
- material.SetFloat("_UseColorMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseColorMap", 0.0f);
- }
- if (description.TryGetProperty("TEX_normal_map", out textureProperty))
- {
- material.SetTexture("_BumpMap", textureProperty.texture);
- material.SetFloat("_UseNormalMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseNormalMap", 0.0f);
- }
- if (description.TryGetProperty("TEX_roughness_map", out textureProperty))
- {
- material.SetTexture("RoughnessMap", textureProperty.texture);
- material.SetFloat("_UseRoughnessMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseRoughnessMap", 0.0f);
- }
- if (description.TryGetProperty("TEX_metallic_map", out textureProperty))
- {
- material.SetTexture("_MetallicMap", textureProperty.texture);
- material.SetFloat("_UseMetallicMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseMetallicMap", 0.0f);
- }
- if (description.TryGetProperty("TEX_emissive_map", out textureProperty))
- {
- material.SetTexture("_EmissionMap", textureProperty.texture);
- material.SetFloat("_UseEmissiveMap", 1.0f);
- }
- else
- {
- material.SetFloat("_UseEmissiveMap", 0.0f);
- }
- if (description.TryGetProperty("hasTransparencyTexture", out floatProperty))
- material.SetFloat("_UseOpacityMap", floatProperty);
- if (description.TryGetProperty("transparencyMaskThreshold", out floatProperty))
- material.SetFloat("_OpacityThreshold", floatProperty);
- if (description.TryGetProperty("TEX_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);
- }
- RemapColorCurves(description, clips, "base_color", "_Color");
- RemapCurve(description, clips, "mask_threshold", "_Cutoff");
- RemapCurve(description, clips, "metallic", "_Metallic");
- RemapCurve(description, clips, "roughness", "_Glossiness");
- for (int i = 0; i < clips.Length; i++)
- {
- if (description.HasAnimationCurveInClip(clips[i].name, "uv_scale.x") || description.HasAnimationCurveInClip(clips[i].name, "uv_scale.y"))
- {
- AnimationCurve curve;
- if (description.TryGetAnimationCurve(clips[i].name, "uv_scale.x", out curve))
- clips[i].SetCurve("", typeof(Material), "_UvTiling.x", curve);
- else
- clips[i].SetCurve("", typeof(Material), "_UvTiling.x", AnimationCurve.Constant(0.0f, 1.0f, 1.0f));
- if (description.TryGetAnimationCurve(clips[i].name, "uv_scale.y", out curve))
- clips[i].SetCurve("", typeof(Material), "_UvTiling.y", curve);
- else
- clips[i].SetCurve("", typeof(Material), "_UvTiling.y", AnimationCurve.Constant(0.0f, 1.0f, 1.0f));
- }
- if (description.HasAnimationCurveInClip(clips[i].name, "uv_offset.x") || description.HasAnimationCurveInClip(clips[i].name, "uv_offset.y"))
- {
- AnimationCurve curve;
- if (description.TryGetAnimationCurve(clips[i].name, "uv_offset.x", out curve))
- clips[i].SetCurve("", typeof(Material), "_UvOffset.x", curve);
- else
- clips[i].SetCurve("", typeof(Material), "_UvOffset.x", AnimationCurve.Constant(0.0f, 1.0f, 0.0f));
- if (description.TryGetAnimationCurve(clips[i].name, "uv_offset.y", out curve))
- {
- ConvertKeys(curve, ConvertFloatNegate);
- clips[i].SetCurve("", typeof(Material), "_UvOffset.y", curve);
- }
- else
- clips[i].SetCurve("", typeof(Material), "_UvOffset.y", AnimationCurve.Constant(0.0f, 1.0f, 0.0f));
- }
- }
- if (description.HasAnimationCurve("emissive_intensity"))
- {
- Vector4 emissiveColor;
- description.TryGetProperty("emissive", out emissiveColor);
- for (int i = 0; i < clips.Length; i++)
- {
- AnimationCurve curve;
- description.TryGetAnimationCurve(clips[i].name, "emissive_intensity", out curve);
- // remap emissive intensity to emission color
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.r", curve);
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.g", curve);
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.b", curve);
- }
- }
- else if (description.TryGetProperty("emissive", out vectorProperty))
- {
- if (vectorProperty.x > 0.0f || vectorProperty.y > 0.0f || vectorProperty.z > 0.0f)
- {
- material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
- material.EnableKeyword("_EMISSION");
- }
- if (description.TryGetProperty("emissive_intensity", out floatProperty))
- {
- vectorProperty *= floatProperty;
- }
- material.SetColor("_EmissionColor", vectorProperty);
- if (description.HasAnimationCurve("emissive.x"))
- {
- if (description.HasAnimationCurve("emissive_intensity"))
- {
- // combine color and intensity.
- for (int i = 0; i < clips.Length; i++)
- {
- AnimationCurve curve;
- AnimationCurve intensityCurve;
- description.TryGetAnimationCurve(clips[i].name, "emissive_intensity", out intensityCurve);
- description.TryGetAnimationCurve(clips[i].name, "emissive.x", out curve);
- MultiplyCurves(curve, intensityCurve);
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.r", curve);
- description.TryGetAnimationCurve(clips[i].name, "emissive.y", out curve);
- MultiplyCurves(curve, intensityCurve);
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.g", curve);
- description.TryGetAnimationCurve(clips[i].name, "emissive.z", out curve);
- MultiplyCurves(curve, intensityCurve);
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.b", curve);
- }
- }
- else
- {
- RemapColorCurves(description, clips, "emissive", "_EmissionColor");
- }
- }
- else if (description.HasAnimationCurve("emissive_intensity"))
- {
- Vector4 emissiveColor;
- description.TryGetProperty("emissive", out emissiveColor);
- for (int i = 0; i < clips.Length; i++)
- {
- AnimationCurve curve;
- description.TryGetAnimationCurve(clips[i].name, "emissive_intensity", out curve);
- // remap emissive intensity to emission color
- AnimationCurve curveR = new AnimationCurve();
- ConvertAndCopyKeys(curveR, curve, value => ConvertFloatMultiply(emissiveColor.x, value));
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.r", curveR);
- AnimationCurve curveG = new AnimationCurve();
- ConvertAndCopyKeys(curveG, curve, value => ConvertFloatMultiply(emissiveColor.y, value));
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.g", curveG);
- AnimationCurve curveB = new AnimationCurve();
- ConvertAndCopyKeys(curveB, curve, value => ConvertFloatMultiply(emissiveColor.z, value));
- clips[i].SetCurve("", typeof(Material), "_EmissionColor.b", curveB);
- }
- }
- }
- }
- }
- static bool IsAutodeskInteractiveMaterial(MaterialDescription description)
- {
- return description.TryGetProperty("renderAPI", out string stringValue) && stringValue == "SFX_PBS_SHADER";
- }
- static void ConvertKeys(AnimationCurve curve, System.Func<float, float> convertionDelegate)
- {
- Keyframe[] keyframes = curve.keys;
- for (int i = 0; i < keyframes.Length; i++)
- {
- keyframes[i].value = convertionDelegate(keyframes[i].value);
- }
- curve.keys = keyframes;
- }
- static void ConvertAndCopyKeys(AnimationCurve curveDest, AnimationCurve curveSource, System.Func<float, float> convertionDelegate)
- {
- for (int i = 0; i < curveSource.keys.Length; i++)
- {
- var sourceKey = curveSource.keys[i];
- curveDest.AddKey(new Keyframe(sourceKey.time, convertionDelegate(sourceKey.value), sourceKey.inTangent, sourceKey.outTangent, sourceKey.inWeight, sourceKey.outWeight));
- }
- }
- static float ConvertFloatNegate(float value)
- {
- return -value;
- }
- static float ConvertFloatMultiply(float value, float multiplier)
- {
- return value * multiplier;
- }
- static void MultiplyCurves(AnimationCurve curve, AnimationCurve curveMultiplier)
- {
- Keyframe[] keyframes = curve.keys;
- for (int i = 0; i < keyframes.Length; i++)
- {
- keyframes[i].value *= curveMultiplier.Evaluate(keyframes[i].time);
- }
- curve.keys = keyframes;
- }
- static void RemapCurve(MaterialDescription description, AnimationClip[] clips, string originalPropertyName, string newPropertyName)
- {
- AnimationCurve curve;
- for (int i = 0; i < clips.Length; i++)
- {
- if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName, out curve))
- {
- clips[i].SetCurve("", typeof(Material), newPropertyName, curve);
- }
- }
- }
- static void RemapColorCurves(MaterialDescription description, AnimationClip[] clips, string originalPropertyName, string newPropertyName)
- {
- AnimationCurve curve;
- for (int i = 0; i < clips.Length; i++)
- {
- if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".x", out curve))
- {
- clips[i].SetCurve("", typeof(Material), newPropertyName + ".r", curve);
- }
- if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".y", out curve))
- {
- clips[i].SetCurve("", typeof(Material), newPropertyName + ".g", curve);
- }
- if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".z", out curve))
- {
- clips[i].SetCurve("", typeof(Material), newPropertyName + ".b", curve);
- }
- }
- }
- }
- }
|