Skip to content

Commit

Permalink
Best-effort at supporting spec/glossiness gltf
Browse files Browse the repository at this point in the history
Create a best-effort metal/rough material when provided with
a spec/glossiness material.

Change-Id: Id000b0153f7be0fa8cbdf634cd22e3c4a6c8baf1
  • Loading branch information
dubois committed Feb 6, 2020
1 parent 93e7a6b commit e070e68
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -258,6 +259,7 @@ public override IEnumerable<GltfTextureBase> ReferencedTextures {
[Serializable]
public class Extensions {
public GOOGLE_tilt_brush_material GOOGLE_tilt_brush_material;
public KHR_materials_pbrSpecularGlossiness KHR_materials_pbrSpecularGlossiness;
}

[Serializable]
Expand All @@ -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[]
Expand Down
16 changes: 14 additions & 2 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e070e68

Please sign in to comment.