From e070e6884b34b1078175fbfaf9a56c9852f7ad75 Mon Sep 17 00:00:00 2001 From: Paul Du Bois Date: Thu, 6 Feb 2020 15:09:35 -0800 Subject: [PATCH] Best-effort at supporting spec/glossiness gltf Create a best-effort metal/rough material when provided with a spec/glossiness material. Change-Id: Id000b0153f7be0fa8cbdf634cd22e3c4a6c8baf1 --- .../Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs | 10 ++++++++++ .../Scripts/Gltf/GltfMaterialConverter.cs | 16 ++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs b/UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs index a3576cc7..899337b6 100644 --- a/UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs +++ b/UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs @@ -137,6 +137,7 @@ public override void Dereference(IUriLoader uriLoader = null) { DereferenceTextureInfo(mat.pbrMetallicRoughness.baseColorTexture); DereferenceTextureInfo(mat.pbrMetallicRoughness.metallicRoughnessTexture); } + DereferenceTextureInfo(mat.extensions?.KHR_materials_pbrSpecularGlossiness?.diffuseTexture); } for (int i = 0; i < nodes.Count; i++) { nodes[i].gltfIndex = i; @@ -258,6 +259,7 @@ public override IEnumerable ReferencedTextures { [Serializable] public class Extensions { public GOOGLE_tilt_brush_material GOOGLE_tilt_brush_material; + public KHR_materials_pbrSpecularGlossiness KHR_materials_pbrSpecularGlossiness; } [Serializable] @@ -269,6 +271,14 @@ public class PbrMetallicRoughness { public TextureInfo metallicRoughnessTexture; } + [Serializable] + public class KHR_materials_pbrSpecularGlossiness { + public Color diffuseFactor = Color.white; + public TextureInfo diffuseTexture; + // public Vector3 specularFactor; not supported + public float glossinessFactor = 1.0f; + } + [Serializable] public class TextureInfo { public int index; // indexes into root.textures[] diff --git a/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs b/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs index 924f8d4b..5acce7a6 100644 --- a/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs +++ b/UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs @@ -273,8 +273,20 @@ private static Material LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) { } if (gltfMat.pbrMetallicRoughness == null) { - Debug.LogWarningFormat("Material #{0} has no PBR info.", gltfMat.gltfIndex); - return null; + var specGloss = gltfMat.extensions?.KHR_materials_pbrSpecularGlossiness; + if (specGloss != null) { + // Try and make the best of pbrSpecularGlossiness. + // Maybe it would be better to support "extensionsRequired" and raise an error + // if the asset requires pbrSpecularGlossiness. + gltfMat.pbrMetallicRoughness = new Gltf2Material.PbrMetallicRoughness { + baseColorFactor = specGloss.diffuseFactor, + baseColorTexture = specGloss.diffuseTexture, + roughnessFactor = 1f - specGloss.glossinessFactor + }; + } else { + Debug.LogWarningFormat("Material #{0} has no PBR info.", gltfMat.gltfIndex); + return null; + } } return CreateNewPbrMaterial(baseMaterial, gltfMat.name, gltfMat.pbrMetallicRoughness);