This commit is contained in:
2021-06-13 10:28:03 +02:00
parent eb70603c85
commit df2d24cbd3
7487 changed files with 943244 additions and 0 deletions

View File

@@ -0,0 +1,342 @@
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);
}
}
}
}
}

View File

@@ -0,0 +1,317 @@
using System.IO;
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 FBXArnoldSurfaceMaterialDescriptionPreprocessor : AssetPostprocessor
{
static readonly uint k_Version = 2;
static readonly int k_Order = 4;
static readonly string k_ShaderPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/ArnoldStandardSurface/ArnoldStandardSurface.shadergraph";
static readonly string k_ShaderTransparentPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/ArnoldStandardSurface/ArnoldStandardSurfaceTransparent.shadergraph";
public override uint GetVersion()
{
return k_Version;
}
public override int GetPostprocessOrder()
{
return k_Order;
}
static bool IsMayaArnoldStandardSurfaceMaterial(MaterialDescription description)
{
float typeId;
description.TryGetProperty("TypeId", out typeId);
return typeId == 1138001;
}
static bool Is3DsMaxArnoldStandardSurfaceMaterial(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 == 2121471519 && classIdB == 1660373836 && originalMtl != "PHYSICAL_MTL";
}
public void OnPreprocessMaterialDescription(MaterialDescription description, Material material,
AnimationClip[] clips)
{
var pipelineAsset = GraphicsSettings.currentRenderPipeline;
if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset))
return;
var lowerCasePath = Path.GetExtension(assetPath).ToLower();
if (lowerCasePath == ".fbx")
{
if (IsMayaArnoldStandardSurfaceMaterial(description))
CreateFromMayaArnoldStandardSurfaceMaterial(description, material, clips);
else if (Is3DsMaxArnoldStandardSurfaceMaterial(description))
CreateFrom3DsMaxArnoldStandardSurfaceMaterial(description, material, clips);
}
}
void CreateFromMayaArnoldStandardSurfaceMaterial(MaterialDescription description, Material material,
AnimationClip[] clips)
{
float floatProperty;
Vector4 vectorProperty;
TexturePropertyDescription textureProperty;
Shader shader;
float opacity = 1.0f;
Vector4 opacityColor;
TexturePropertyDescription opacityMap;
description.TryGetProperty("opacity", out opacityColor);
bool hasOpacityMap = description.TryGetProperty("opacity", out opacityMap);
opacity = Mathf.Min(Mathf.Min(opacityColor.x, opacityColor.y), opacityColor.z);
float transmission;
description.TryGetProperty("transmission", out transmission);
if (opacity == 1.0f && !hasOpacityMap)
{
opacity = 1.0f - transmission;
}
if (opacity < 1.0f || hasOpacityMap)
{
shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderTransparentPath);
if (shader == null)
return;
material.shader = shader;
if (hasOpacityMap)
{
material.SetTexture("_OPACITY_MAP", opacityMap.texture);
material.SetFloat("_OPACITY", 1.0f);
}
else
{
material.SetFloat("_OPACITY", opacity);
}
}
else
{
shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderPath);
if (shader == null)
return;
material.shader = shader;
}
foreach (var clip in clips)
{
clip.ClearCurves();
}
description.TryGetProperty("base", out floatProperty);
if (description.TryGetProperty("baseColor", out textureProperty))
{
SetMaterialTextureProperty("_BASE_COLOR_MAP", material, textureProperty);
material.SetColor("_BASE_COLOR", Color.white * floatProperty);
}
else if (description.TryGetProperty("baseColor", 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 *= floatProperty;
}
material.SetColor("_BASE_COLOR", vectorProperty * floatProperty);
}
if (description.TryGetProperty("emission", out floatProperty) && floatProperty > 0.0f)
{
remapPropertyColorOrTexture(description, material, "emissionColor", "_EMISSION_COLOR", floatProperty);
}
remapPropertyFloatOrTexture(description, material, "metalness", "_METALNESS");
description.TryGetProperty("specular", out floatProperty);
remapPropertyColorOrTexture(description, material, "specularColor", "_SPECULAR_COLOR", floatProperty);
remapPropertyFloatOrTexture(description, material, "specularRoughness", "_SPECULAR_ROUGHNESS");
remapPropertyFloatOrTexture(description, material, "specularIOR", "_SPECULAR_IOR");
remapPropertyTexture(description, material, "normalCamera", "_NORMAL_MAP");
}
void CreateFrom3DsMaxArnoldStandardSurfaceMaterial(MaterialDescription description, Material material,
AnimationClip[] clips)
{
float floatProperty;
Vector4 vectorProperty;
TexturePropertyDescription textureProperty;
var shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderPath);
if (shader == null)
return;
material.shader = shader;
foreach (var clip in clips)
{
clip.ClearCurves();
}
float opacity = 1.0f;
Vector4 opacityColor;
TexturePropertyDescription opacityMap;
description.TryGetProperty("opacity", out opacityColor);
bool hasOpacityMap = description.TryGetProperty("opacity", out opacityMap);
opacity = Mathf.Min(Mathf.Min(opacityColor.x, opacityColor.y), opacityColor.z);
if (opacity < 1.0f || hasOpacityMap)
{
if (hasOpacityMap)
{
material.SetTexture("_OPACITY_MAP", opacityMap.texture);
material.SetFloat("_OPACITY", 1.0f);
}
else
{
material.SetFloat("_OPACITY", opacity);
}
}
description.TryGetProperty("base", out floatProperty);
if (description.TryGetProperty("base_color.shader", out textureProperty))
{
SetMaterialTextureProperty("_BASE_COLOR_MAP", material, textureProperty);
material.SetColor("_BASE_COLOR", Color.white * floatProperty);
}
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);
}
material.SetColor("_BASE_COLOR", vectorProperty * floatProperty);
}
if (description.TryGetProperty("emission", out floatProperty) && floatProperty > 0.0f)
{
remapPropertyColorOrTexture3DsMax(description, material, "emission_color", "_EMISSION_COLOR",
floatProperty);
}
remapPropertyFloatOrTexture3DsMax(description, material, "metalness", "_METALNESS");
description.TryGetProperty("specular", out float specularFactor);
remapPropertyColorOrTexture3DsMax(description, material, "specular_color", "_SPECULAR_COLOR",
specularFactor);
remapPropertyFloatOrTexture3DsMax(description, material, "specular_roughness", "_SPECULAR_ROUGHNESS");
remapPropertyFloatOrTexture3DsMax(description, material, "specular_IOR", "_SPECULAR_IOR");
remapPropertyTexture(description, material, "normal_camera", "_NORMAL_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 remapPropertyColorOrTexture3DsMax(MaterialDescription description, Material material,
string inPropName, string outPropName, float multiplier = 1.0f)
{
if (description.TryGetProperty(inPropName + ".shader", out TexturePropertyDescription textureProperty))
{
material.SetTexture(outPropName + "_MAP", textureProperty.texture);
material.SetColor(outPropName, Color.white * multiplier);
}
else
{
description.TryGetProperty(inPropName, out Vector4 vectorProperty);
material.SetColor(outPropName, vectorProperty * multiplier);
}
}
static void remapPropertyFloatOrTexture3DsMax(MaterialDescription description, Material material,
string inPropName, string outPropName)
{
if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
{
material.SetTexture(outPropName + "_MAP", textureProperty.texture);
material.SetFloat(outPropName, 1.0f);
}
else
{
description.TryGetProperty(inPropName, out float floatProperty);
material.SetFloat(outPropName, floatProperty);
}
}
static void remapPropertyColorOrTexture(MaterialDescription description, Material material, string inPropName,
string outPropName, float multiplier = 1.0f)
{
if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
{
material.SetTexture(outPropName + "_MAP", textureProperty.texture);
material.SetColor(outPropName, Color.white * multiplier);
}
else
{
description.TryGetProperty(inPropName, out Vector4 vectorProperty);
material.SetColor(outPropName, vectorProperty * multiplier);
}
}
static void remapPropertyFloatOrTexture(MaterialDescription description, Material material, string inPropName,
string outPropName)
{
if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
{
material.SetTexture(outPropName + "_MAP", textureProperty.texture);
material.SetFloat(outPropName, 1.0f);
}
else
{
description.TryGetProperty(inPropName, out float floatProperty);
material.SetFloat(outPropName, floatProperty);
}
}
}
}

View File

@@ -0,0 +1,273 @@
using System.IO;
using UnityEditor.AssetImporters;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
class FBXMaterialDescriptionPreprocessor : AssetPostprocessor
{
static readonly uint k_Version = 1;
static readonly int k_Order = 2;
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;
var lowerCaseExtension = Path.GetExtension(assetPath).ToLower();
if (lowerCaseExtension != ".fbx" && lowerCaseExtension != ".obj" && lowerCaseExtension != ".blend" && lowerCaseExtension != ".mb" && lowerCaseExtension != ".ma" && lowerCaseExtension != ".max")
return;
string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit));
var shader = AssetDatabase.LoadAssetAtPath<Shader>(path);
if (shader == null)
return;
material.shader = shader;
Vector4 vectorProperty;
float floatProperty;
TexturePropertyDescription textureProperty;
bool isTransparent = false;
float opacity;
float transparencyFactor;
if (!description.TryGetProperty("Opacity", out opacity))
{
if (description.TryGetProperty("TransparencyFactor", out transparencyFactor))
{
opacity = transparencyFactor == 1.0f ? 1.0f : 1.0f - transparencyFactor;
}
if (opacity == 1.0f && description.TryGetProperty("TransparentColor", out vectorProperty))
{
opacity = vectorProperty.x == 1.0f ? 1.0f : 1.0f - vectorProperty.x;
}
}
if (opacity < 1.0f || (opacity == 1.0f && description.TryGetProperty("TransparentColor", out textureProperty)))
{
isTransparent = true;
}
else if (description.HasAnimationCurve("TransparencyFactor") || description.HasAnimationCurve("TransparentColor"))
{
isTransparent = true;
}
if (isTransparent)
{
material.SetFloat("_Mode", 3.0f);
material.SetOverrideTag("RenderType", "Transparent");
material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetFloat("_ZWrite", 0.0f);
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
material.SetFloat("_Surface", 1.0f);
}
else
{
material.SetFloat("_Mode", 0.0f);
material.SetOverrideTag("RenderType", "");
material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.Zero);
material.SetFloat("_ZWrite", 1.0f);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
material.SetFloat("_Surface", 0.0f);
}
if (description.TryGetProperty("DiffuseColor", out textureProperty) && textureProperty.texture != null)
{
Color diffuseColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
if (description.TryGetProperty("DiffuseFactor", out floatProperty))
diffuseColor *= floatProperty;
diffuseColor.a = opacity;
SetMaterialTextureProperty("_BaseMap", material, textureProperty);
material.SetColor("_BaseColor", diffuseColor);
}
else if (description.TryGetProperty("DiffuseColor", out vectorProperty))
{
Color diffuseColor = vectorProperty;
diffuseColor.a = opacity;
material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
}
if (description.TryGetProperty("Bump", out textureProperty))
{
SetMaterialTextureProperty("_BumpMap", material, textureProperty);
material.EnableKeyword("_NORMALMAP");
if (description.TryGetProperty("BumpFactor", out floatProperty))
material.SetFloat("_BumpScale", floatProperty);
}
else if (description.TryGetProperty("NormalMap", out textureProperty))
{
SetMaterialTextureProperty("_BumpMap", material, textureProperty);
material.EnableKeyword("_NORMALMAP");
if (description.TryGetProperty("BumpFactor", out floatProperty))
material.SetFloat("_BumpScale", floatProperty);
}
if (description.TryGetProperty("EmissiveColor", out textureProperty))
{
Color emissiveColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
material.SetColor("_EmissionColor", emissiveColor);
SetMaterialTextureProperty("_EmissionMap", material, textureProperty);
if (description.TryGetProperty("EmissiveFactor", out floatProperty) && floatProperty > 0.0f)
{
material.EnableKeyword("_EMISSION");
material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
}
}
else if (
description.TryGetProperty("EmissiveColor", out vectorProperty) && vectorProperty.magnitude > vectorProperty.w
|| description.HasAnimationCurve("EmissiveColor.x"))
{
if (description.TryGetProperty("EmissiveFactor", out floatProperty))
vectorProperty *= floatProperty;
material.SetColor("_EmissionColor", vectorProperty);
if (floatProperty > 0.0f)
{
material.EnableKeyword("_EMISSION");
material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
}
}
material.SetFloat("_Glossiness", 0.0f);
if (PlayerSettings.colorSpace == ColorSpace.Linear)
RemapAndTransformColorCurves(description, clips, "DiffuseColor", "_BaseColor", ConvertFloatLinearToGamma);
else
RemapColorCurves(description, clips, "DiffuseColor", "_BaseColor");
RemapTransparencyCurves(description, clips);
RemapColorCurves(description, clips, "EmissiveColor", "_EmissionColor");
}
static void RemapTransparencyCurves(MaterialDescription description, AnimationClip[] clips)
{
// For some reason, Opacity is never animated, we have to use TransparencyFactor and TransparentColor
for (int i = 0; i < clips.Length; i++)
{
bool foundTransparencyCurve = false;
AnimationCurve curve;
if (description.TryGetAnimationCurve(clips[i].name, "TransparencyFactor", out curve))
{
ConvertKeys(curve, ConvertFloatOneMinus);
clips[i].SetCurve("", typeof(Material), "_BaseColor.a", curve);
foundTransparencyCurve = true;
}
else if (description.TryGetAnimationCurve(clips[i].name, "TransparentColor.x", out curve))
{
ConvertKeys(curve, ConvertFloatOneMinus);
clips[i].SetCurve("", typeof(Material), "_BaseColor.a", curve);
foundTransparencyCurve = true;
}
if (foundTransparencyCurve && !description.HasAnimationCurveInClip(clips[i].name, "DiffuseColor"))
{
Vector4 diffuseColor;
description.TryGetProperty("DiffuseColor", out diffuseColor);
clips[i].SetCurve("", typeof(Material), "_BaseColor.r", AnimationCurve.Constant(0.0f, 1.0f, diffuseColor.x));
clips[i].SetCurve("", typeof(Material), "_BaseColor.g", AnimationCurve.Constant(0.0f, 1.0f, diffuseColor.y));
clips[i].SetCurve("", typeof(Material), "_BaseColor.b", AnimationCurve.Constant(0.0f, 1.0f, diffuseColor.z));
}
}
}
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);
}
}
}
static void RemapAndTransformColorCurves(MaterialDescription description, AnimationClip[] clips, string originalPropertyName, string newPropertyName, System.Func<float, float> converter)
{
AnimationCurve curve;
for (int i = 0; i < clips.Length; i++)
{
if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".x", out curve))
{
ConvertKeys(curve, converter);
clips[i].SetCurve("", typeof(Material), newPropertyName + ".r", curve);
}
if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".y", out curve))
{
ConvertKeys(curve, converter);
clips[i].SetCurve("", typeof(Material), newPropertyName + ".g", curve);
}
if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".z", out curve))
{
ConvertKeys(curve, converter);
clips[i].SetCurve("", typeof(Material), newPropertyName + ".b", curve);
}
}
}
static float ConvertFloatLinearToGamma(float value)
{
return Mathf.LinearToGammaSpace(value);
}
static float ConvertFloatOneMinus(float value)
{
return 1.0f - value;
}
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 SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
{
material.SetTexture(propertyName, textureProperty.texture);
material.SetTextureOffset(propertyName, textureProperty.offset);
material.SetTextureScale(propertyName, textureProperty.scale);
}
}
}

View File

@@ -0,0 +1,390 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Rendering.Universal.ShaderGUI;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
class MaterialModificationProcessor : AssetModificationProcessor
{
static void OnWillCreateAsset(string asset)
{
if (!asset.ToLowerInvariant().EndsWith(".mat"))
{
return;
}
MaterialPostprocessor.s_CreatedAssets.Add(asset);
}
}
class MaterialReimporter : Editor
{
static bool s_NeedToCheckProjSettingExistence = true;
static void ReimportAllMaterials()
{
string[] guids = AssetDatabase.FindAssets("t:material", null);
// There can be several materials subAssets per guid ( ie : FBX files ), remove duplicate guids.
var distinctGuids = guids.Distinct();
int materialIdx = 0;
int totalMaterials = distinctGuids.Count();
foreach (var asset in distinctGuids)
{
materialIdx++;
var path = AssetDatabase.GUIDToAssetPath(asset);
EditorUtility.DisplayProgressBar("Material Upgrader re-import", string.Format("({0} of {1}) {2}", materialIdx, totalMaterials, path), (float)materialIdx / (float)totalMaterials);
AssetDatabase.ImportAsset(path);
}
EditorUtility.ClearProgressBar();
MaterialPostprocessor.s_NeedsSavingAssets = true;
}
[InitializeOnLoadMethod]
static void RegisterUpgraderReimport()
{
EditorApplication.update += () =>
{
if (Time.renderedFrameCount > 0)
{
bool fileExist = true;
// We check the file existence only once to avoid IO operations every frame.
if (s_NeedToCheckProjSettingExistence)
{
fileExist = System.IO.File.Exists(UniversalProjectSettings.filePath);
s_NeedToCheckProjSettingExistence = false;
}
//This method is called at opening and when URP package change (update of manifest.json)
var curUpgradeVersion = UniversalProjectSettings.materialVersionForUpgrade;
if (curUpgradeVersion != MaterialPostprocessor.k_Upgraders.Length)
{
string commandLineOptions = Environment.CommandLine;
bool inTestSuite = commandLineOptions.Contains("-testResults");
if (!inTestSuite && fileExist)
{
EditorUtility.DisplayDialog("URP Material upgrade", "The Materials in your Project were created using an older version of the Universal Render Pipeline (URP)." +
" Unity must upgrade them to be compatible with your current version of URP. \n" +
" Unity will re-import all of the Materials in your project, save the upgraded Materials to disk, and check them out in source control if needed.\n" +
" Please see the Material upgrade guide in the URP documentation for more information.", "Ok");
}
ReimportAllMaterials();
}
if (MaterialPostprocessor.s_NeedsSavingAssets)
MaterialPostprocessor.SaveAssetsToDisk();
}
};
}
}
class MaterialPostprocessor : AssetPostprocessor
{
public static List<string> s_CreatedAssets = new List<string>();
internal static List<string> s_ImportedAssetThatNeedSaving = new List<string>();
internal static bool s_NeedsSavingAssets = false;
internal static readonly Action<Material, ShaderPathID>[] k_Upgraders = { UpgradeV1, UpgradeV2, UpgradeV3, UpgradeV4 };
static internal void SaveAssetsToDisk()
{
string commandLineOptions = System.Environment.CommandLine;
bool inTestSuite = commandLineOptions.Contains("-testResults");
if (inTestSuite)
{
// Need to update material version to prevent infinite loop in the upgrader
// when running tests.
UniversalProjectSettings.materialVersionForUpgrade = k_Upgraders.Length;
return;
}
foreach (var asset in s_ImportedAssetThatNeedSaving)
{
AssetDatabase.MakeEditable(asset);
}
AssetDatabase.SaveAssets();
//to prevent data loss, only update the saved version if user applied change and assets are written to
UniversalProjectSettings.materialVersionForUpgrade = k_Upgraders.Length;
UniversalProjectSettings.Save();
s_ImportedAssetThatNeedSaving.Clear();
s_NeedsSavingAssets = false;
}
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
var upgradeLog = "UniversalRP Material log:";
var upgradeCount = 0;
foreach (var asset in importedAssets)
{
if (!asset.EndsWith(".mat", StringComparison.InvariantCultureIgnoreCase))
continue;
var material = (Material)AssetDatabase.LoadAssetAtPath(asset, typeof(Material));
if (!ShaderUtils.IsLWShader(material.shader))
continue;
ShaderPathID id = ShaderUtils.GetEnumFromPath(material.shader.name);
var wasUpgraded = false;
var debug = "\n" + material.name;
AssetVersion assetVersion = null;
var allAssets = AssetDatabase.LoadAllAssetsAtPath(asset);
foreach (var subAsset in allAssets)
{
if (subAsset is AssetVersion sub)
{
assetVersion = sub;
}
}
if (!assetVersion)
{
wasUpgraded = true;
assetVersion = ScriptableObject.CreateInstance<AssetVersion>();
if (s_CreatedAssets.Contains(asset))
{
assetVersion.version = k_Upgraders.Length;
s_CreatedAssets.Remove(asset);
InitializeLatest(material, id);
debug += " initialized.";
}
else
{
assetVersion.version = UniversalProjectSettings.materialVersionForUpgrade;
debug += $" assumed to be version {UniversalProjectSettings.materialVersionForUpgrade} due to missing version.";
}
assetVersion.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable;
AssetDatabase.AddObjectToAsset(assetVersion, asset);
}
while (assetVersion.version < k_Upgraders.Length)
{
k_Upgraders[assetVersion.version](material, id);
debug += $" upgrading:v{assetVersion.version} to v{assetVersion.version + 1}";
assetVersion.version++;
wasUpgraded = true;
}
if (wasUpgraded)
{
upgradeLog += debug;
upgradeCount++;
EditorUtility.SetDirty(assetVersion);
s_ImportedAssetThatNeedSaving.Add(asset);
s_NeedsSavingAssets = true;
}
}
}
static void InitializeLatest(Material material, ShaderPathID id)
{
}
static void UpgradeV1(Material material, ShaderPathID shaderID)
{
var shaderPath = ShaderUtils.GetShaderPath(shaderID);
var upgradeFlag = MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound;
switch (shaderID)
{
case ShaderPathID.Unlit:
MaterialUpgrader.Upgrade(material, new UnlitUpdaterV1(shaderPath), upgradeFlag);
UnlitShader.SetMaterialKeywords(material);
break;
case ShaderPathID.SimpleLit:
MaterialUpgrader.Upgrade(material, new SimpleLitUpdaterV1(shaderPath), upgradeFlag);
SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords);
break;
case ShaderPathID.Lit:
MaterialUpgrader.Upgrade(material, new LitUpdaterV1(shaderPath), upgradeFlag);
LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords);
break;
case ShaderPathID.ParticlesLit:
MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
break;
case ShaderPathID.ParticlesSimpleLit:
MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
break;
case ShaderPathID.ParticlesUnlit:
MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords);
break;
}
}
static void UpgradeV2(Material material, ShaderPathID shaderID)
{
// fix 50 offset on shaders
if (material.HasProperty("_QueueOffset"))
BaseShaderGUI.SetupMaterialBlendMode(material);
}
static void UpgradeV3(Material material, ShaderPathID shaderID)
{
switch (shaderID)
{
case ShaderPathID.Lit:
case ShaderPathID.SimpleLit:
case ShaderPathID.ParticlesLit:
case ShaderPathID.ParticlesSimpleLit:
case ShaderPathID.ParticlesUnlit:
var propertyID = Shader.PropertyToID("_EmissionColor");
if (material.HasProperty(propertyID))
{
// In older version there was a bug that these shaders did not had HDR attribute on emission property.
// This caused emission color to be converted from gamma to linear space.
// In order to avoid visual regression on older projects we will do gamma to linear conversion here.
var emissionGamma = material.GetColor(propertyID);
var emissionLinear = emissionGamma.linear;
material.SetColor(propertyID, emissionLinear);
}
break;
}
}
static void UpgradeV4(Material material, ShaderPathID shaderID)
{}
}
// Upgraders v1
#region UpgradersV1
internal class LitUpdaterV1 : MaterialUpgrader
{
public static void UpdateLitDetails(Material material)
{
if (material == null)
throw new ArgumentNullException("material");
if (material.GetTexture("_MetallicGlossMap") || material.GetTexture("_SpecGlossMap") || material.GetFloat("_SmoothnessTextureChannel") >= 0.5f)
material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
else
material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
}
public LitUpdaterV1(string oldShaderName)
{
if (oldShaderName == null)
throw new ArgumentNullException("oldShaderName");
string standardShaderPath = ShaderUtils.GetShaderPath(ShaderPathID.Lit);
RenameShader(oldShaderName, standardShaderPath, UpdateLitDetails);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_GlossyReflections", "_EnvironmentReflections");
}
}
internal class UnlitUpdaterV1 : MaterialUpgrader
{
static Shader bakedLit = Shader.Find(ShaderUtils.GetShaderPath(ShaderPathID.BakedLit));
public static void UpgradeToUnlit(Material material)
{
if (material == null)
throw new ArgumentNullException("material");
if (material.GetFloat("_SampleGI") != 0)
{
material.shader = bakedLit;
material.EnableKeyword("_NORMALMAP");
}
}
public UnlitUpdaterV1(string oldShaderName)
{
if (oldShaderName == null)
throw new ArgumentNullException("oldShaderName");
RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.Unlit), UpgradeToUnlit);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
}
}
internal class SimpleLitUpdaterV1 : MaterialUpgrader
{
public SimpleLitUpdaterV1(string oldShaderName)
{
if (oldShaderName == null)
throw new ArgumentNullException("oldShaderName");
RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SimpleLit), UpgradeToSimpleLit);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_SpecSource", "_SpecularHighlights");
RenameFloat("_Shininess", "_Smoothness");
}
public static void UpgradeToSimpleLit(Material material)
{
if (material == null)
throw new ArgumentNullException("material");
var smoothnessSource = 1 - (int)material.GetFloat("_GlossinessSource");
material.SetFloat("_SmoothnessSource" , smoothnessSource);
if (material.GetTexture("_SpecGlossMap") == null)
{
var col = material.GetColor("_SpecColor");
var colBase = material.GetColor("_Color");
var smoothness = material.GetFloat("_Shininess");
if (material.GetFloat("_Surface") == 0)
{
if (smoothnessSource == 1)
colBase.a = smoothness;
else
col.a = smoothness;
material.SetColor("_BaseColor", colBase);
}
material.SetColor("_BaseColor", colBase);
material.SetColor("_SpecColor", col);
}
}
}
internal class ParticleUpdaterV1 : MaterialUpgrader
{
public ParticleUpdaterV1(string shaderName)
{
if (shaderName == null)
throw new ArgumentNullException("oldShaderName");
RenameShader(shaderName, shaderName, ParticleUpgrader.UpdateSurfaceBlendModes);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_FlipbookMode", "_FlipbookBlending");
switch (ShaderUtils.GetEnumFromPath(shaderName))
{
case ShaderPathID.ParticlesLit:
RenameFloat("_Glossiness", "_Smoothness");
break;
case ShaderPathID.ParticlesSimpleLit:
RenameFloat("_Glossiness", "_Smoothness");
break;
case ShaderPathID.ParticlesUnlit:
break;
}
}
}
#endregion
}

View File

@@ -0,0 +1,15 @@
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
class ModelPostprocessor : AssetPostprocessor
{
void OnPostprocessModel(GameObject go)
{
CoreEditorUtils.AddAdditionalData<Camera, UniversalAdditionalCameraData>(go);
CoreEditorUtils.AddAdditionalData<Light, UniversalAdditionalLightData>(go);
}
}
}

View File

@@ -0,0 +1,307 @@
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);
}
}
}
}

View File

@@ -0,0 +1,93 @@
using System.IO;
using UnityEngine;
using UnityEditor.AssetImporters;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
class SketchupMaterialDescriptionPreprocessor : AssetPostprocessor
{
static readonly uint k_Version = 1;
static readonly int k_Order = 2;
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;
var lowerCasePath = Path.GetExtension(assetPath).ToLower();
if (lowerCasePath != ".skp")
return;
string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit));
var shader = AssetDatabase.LoadAssetAtPath<Shader>(path);
if (shader == null)
return;
material.shader = shader;
float floatProperty;
Vector4 vectorProperty;
TexturePropertyDescription textureProperty;
if (description.TryGetProperty("DiffuseMap", out textureProperty) && textureProperty.texture != null)
{
SetMaterialTextureProperty("_BaseMap", material, textureProperty);
SetMaterialTextureProperty("_MainTex", material, textureProperty);
var color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
material.SetColor("_BaseColor", color);
material.SetColor("_Color", color);
}
else if (description.TryGetProperty("DiffuseColor", out vectorProperty))
{
Color diffuseColor = vectorProperty;
diffuseColor = PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor;
material.SetColor("_BaseColor", diffuseColor);
material.SetColor("_Color", diffuseColor);
}
if (description.TryGetProperty("IsTransparent", out floatProperty) && floatProperty == 1.0f)
{
material.SetFloat("_Mode", 3.0f); // From C# enum BlendMode
material.SetOverrideTag("RenderType", "Transparent");
material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetFloat("_ZWrite", 0.0f);
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
material.SetFloat("_Surface", 1.0f);
}
else
{
material.SetFloat("_Mode", 0.0f); // From C# enum BlendMode
material.SetOverrideTag("RenderType", "");
material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.Zero);
material.SetFloat("_ZWrite", 1.0f);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
material.SetFloat("_Surface", 0.0f);
}
}
static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
{
material.SetTexture(propertyName, textureProperty.texture);
material.SetTextureOffset(propertyName, textureProperty.offset);
material.SetTextureScale(propertyName, textureProperty.scale);
}
}
}

View File

@@ -0,0 +1,115 @@
using System;
using System.IO;
using UnityEngine;
using UnityEditor.AssetImporters;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
class ThreeDSMaterialDescriptionPreprocessor : AssetPostprocessor
{
static readonly uint k_Version = 1;
static readonly int k_Order = 2;
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;
var lowerCasePath = Path.GetExtension(assetPath).ToLower();
if (lowerCasePath != ".3ds")
return;
string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit));
var shader = AssetDatabase.LoadAssetAtPath<Shader>(path);
if (shader == null)
return;
material.shader = shader;
TexturePropertyDescription textureProperty;
float floatProperty;
Vector4 vectorProperty;
description.TryGetProperty("diffuse", out vectorProperty);
vectorProperty.x /= 255.0f;
vectorProperty.y /= 255.0f;
vectorProperty.z /= 255.0f;
vectorProperty.w /= 255.0f;
description.TryGetProperty("transparency", out floatProperty);
bool isTransparent = vectorProperty.w <= 0.99f || floatProperty > .0f;
if (isTransparent)
{
material.SetFloat("_Mode", 3.0f); // From C# enum BlendMode
material.SetOverrideTag("RenderType", "Transparent");
material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetFloat("_ZWrite", 0.0f);
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
material.SetFloat("_Surface", 1.0f);
}
else
{
material.SetFloat("_Mode", 0.0f); // From C# enum BlendMode
material.SetOverrideTag("RenderType", "");
material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.Zero);
material.SetFloat("_ZWrite", 1.0f);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
material.SetFloat("_Surface", 0.0f);
}
if (floatProperty > .0f)
vectorProperty.w = 1.0f - floatProperty;
Color diffuseColor = vectorProperty;
material.SetColor("_Color", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
if (description.TryGetProperty("EmissiveColor", out vectorProperty))
{
material.SetColor("_EmissionColor", vectorProperty);
material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
material.EnableKeyword("_EMISSION");
}
if (description.TryGetProperty("texturemap1", out textureProperty))
{
SetMaterialTextureProperty("_BaseMap", material, textureProperty);
}
if (description.TryGetProperty("bumpmap", out textureProperty))
{
if (material.HasProperty("_BumpMap"))
{
SetMaterialTextureProperty("_BumpMap", material, textureProperty);
material.EnableKeyword("_NORMALMAP");
}
}
}
static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
{
material.SetTexture(propertyName, textureProperty.texture);
material.SetTextureOffset(propertyName, textureProperty.offset);
material.SetTextureScale(propertyName, textureProperty.scale);
}
}
}