Implements fallbacked behaviour.

This commit is contained in:
Masataka SUMI 2022-11-04 16:20:55 +09:00
parent 04c14a2b86
commit b6744a653a
2 changed files with 22 additions and 2 deletions

View File

@ -40,6 +40,7 @@ namespace UniGLTF
break;
}
Debug.Log($"Material `{m.name}` fallbacks.");
return BuiltInFallbackMaterialExporter.ExportMaterial(m, textureExporter);
}
}

View File

@ -1,13 +1,18 @@
using UnityEngine;
using System;
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;
namespace UniGLTF
{
/// <summary>
/// 非対応のシェーダでも空のマテリアルを出力する.
/// 非対応のシェーダでも、baseColor だけ読み取ったマテリアルにする.
/// </summary>
public static class BuiltInFallbackMaterialExporter
{
private const string ColorPropertyName = "_Color";
private const string ColorTexturePropertyName = "_MainTex";
public static glTFMaterial ExportMaterial(Material src, ITextureExporter textureExporter)
{
var dst = new glTFMaterial
@ -16,6 +21,20 @@ namespace UniGLTF
pbrMetallicRoughness = new glTFPbrMetallicRoughness(),
};
if (src.HasProperty(ColorPropertyName))
{
dst.pbrMetallicRoughness.baseColorFactor = src.GetColor(ColorPropertyName).ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
}
if (src.HasProperty(ColorTexturePropertyName) && src.GetTexture(ColorTexturePropertyName) != null)
{
dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
{
index = textureExporter.RegisterExportingAsSRgb(src.GetTexture(ColorTexturePropertyName), false),
};
GltfMaterialExportUtils.ExportTextureTransform(src, dst.pbrMetallicRoughness.baseColorTexture, ColorTexturePropertyName);
}
return dst;
}
}