From 96b386def17efe76cc146d91e07b6f79084912d5 Mon Sep 17 00:00:00 2001 From: Paul Du Bois Date: Mon, 21 Oct 2019 17:25:55 -0700 Subject: [PATCH] gltf: honor the material name in the .gltf Previously we were throwing it away, for no good reason. Change-Id: Id000b0150ebe5cb9224bfad1ec3176e03efc6319 --- .../Scripts/Gltf/GltfMaterialConverter.cs | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs b/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs index 3d57c295..be81b04c 100644 --- a/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs +++ b/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs @@ -258,7 +258,7 @@ private static Material LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) { } // Tilt Brush doesn't support metallicRoughnessTexture (yet?) } - return CreateNewPbrMaterial(desc.Material, pbr); + return CreateNewPbrMaterial(desc.Material, gltfMat.name, pbr); } /// @@ -295,31 +295,37 @@ private static Material LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) { return null; } - return CreateNewPbrMaterial(baseMaterial, gltfMat.pbrMetallicRoughness); + return CreateNewPbrMaterial(baseMaterial, gltfMat.name, gltfMat.pbrMetallicRoughness); } // Helper for ConvertGltf{1,2}Material private UnityMaterial CreateNewPbrMaterial( - Material baseMaterial, Gltf2Material.PbrMetallicRoughness pbr) { + Material baseMaterial, string gltfMatName, Gltf2Material.PbrMetallicRoughness pbr) { Material mat = UnityEngine.Object.Instantiate(baseMaterial); - string matName = baseMaterial.name; - if (matName.StartsWith("Base")) { - matName = matName.Substring(4); - } - - mat.SetColor("_BaseColorFactor", pbr.baseColorFactor); Texture tex = null; if (pbr.baseColorTexture != null) { tex = pbr.baseColorTexture.texture.unityTexture; mat.SetTexture("_BaseColorTex", tex); } - if (tex != null) { - matName = string.Format("{0}_{1}", matName, tex.name); + + if (gltfMatName != null) { + // The gltf has a name it wants us to use + mat.name = gltfMatName; + } else { + // No name in the gltf; make up something reasonable + string matName = baseMaterial.name.StartsWith("Base") + ? baseMaterial.name.Substring(4) + : baseMaterial.name; + if (tex != null) { + matName = string.Format("{0}_{1}", matName, tex.name); + } + mat.name = matName; } + + mat.SetColor("_BaseColorFactor", pbr.baseColorFactor); mat.SetFloat("_MetallicFactor", pbr.metallicFactor); mat.SetFloat("_RoughnessFactor", pbr.roughnessFactor); - mat.name = matName; return new UnityMaterial { material = mat, template = baseMaterial }; }