コード整理。IMaterialValidator.EnumerateTextureProperties 追加した

Exporter がテクスチャー列挙という形式をとらずに全部展開してするという手法を取っているため、それほど共通化されない。
This commit is contained in:
ousttrue 2021-07-16 17:38:51 +09:00
parent 0d9b9cbbf7
commit 14ea4befce
6 changed files with 131 additions and 62 deletions

View File

@ -0,0 +1,58 @@
using System.Collections.Generic;
using UnityEngine;
namespace UniGLTF
{
public interface IMaterialValidator
{
/// <summary>
/// shaderName から glTF マテリアルタイプ を得る
///
/// shaderName が エクスポートできるものでないときは null を返す(gltfデフォルトの pbr として処理される)
/// </summary>
/// <param name="shaderName"></param>
/// <returns></returns>
string GetGltfMaterialTypeFromUnityShaderName(string shaderName);
/// <summary>
/// テクスチャーを使うプロパティを列挙する
/// </summary>
/// <param name="propertyName"></param>
/// <param name="m"></param>
/// <returns></returns>
IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m);
}
public class DefaultMaterialValidator : IMaterialValidator
{
public virtual string GetGltfMaterialTypeFromUnityShaderName(string shaderName)
{
if (shaderName == "Standard")
{
return "pbr";
}
if (MaterialExporter.IsUnlit(shaderName))
{
return "unlit";
}
return null;
}
public virtual IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m)
{
// main color
yield return (MaterialExporter.COLOR_TEXTURE_PROP, m.GetTexture(MaterialExporter.COLOR_TEXTURE_PROP));
if (GetGltfMaterialTypeFromUnityShaderName(m.shader.name) == "unlit")
{
yield break;
}
// PBR
yield return (MaterialExporter.METALLIC_TEX_PROP, m.GetTexture(MaterialExporter.METALLIC_TEX_PROP));
yield return (MaterialExporter.NORMAL_TEX_PROP, m.GetTexture(MaterialExporter.NORMAL_TEX_PROP));
yield return (MaterialExporter.EMISSION_TEX_PROP, m.GetTexture(MaterialExporter.EMISSION_TEX_PROP));
yield return (MaterialExporter.OCCLUSION_TEX_PROP, m.GetTexture(MaterialExporter.OCCLUSION_TEX_PROP));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ba5723a5618eac438268641047d9549
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -7,34 +7,6 @@ using UnityEngine;
namespace UniGLTF
{
public interface IMaterialValidator
{
/// <summary>
/// shaderName から glTF マテリアルタイプ を得る
///
/// shaderName が エクスポートできるものでないときは null を返す(gltfデフォルトの pbr として処理される)
/// </summary>
/// <param name="shaderName"></param>
/// <returns></returns>
string GetGltfMaterialTypeFromUnityShaderName(string shaderName);
}
public class DefaultMaterialValidator : IMaterialValidator
{
public virtual string GetGltfMaterialTypeFromUnityShaderName(string shaderName)
{
if (shaderName == "Standard")
{
return "pbr";
}
if (MaterialExporter.IsUnlit(shaderName))
{
return "unlit";
}
return null;
}
}
[Serializable]
public class MeshExportValidator : ScriptableObject
{
@ -116,34 +88,32 @@ namespace UniGLTF
yield return Validation.Warning($"{m}: unknown shader: {m.shader.name} => export as gltf default");
}
var count = ShaderUtil.GetPropertyCount(m.shader);
for (int i = 0; i < count; ++i)
var used = new HashSet<Texture>();
foreach (var (propName, texture) in MaterialValidator.EnumerateTextureProperties(m))
{
var propType = ShaderUtil.GetPropertyType(m.shader, i);
if (propType == ShaderUtil.ShaderPropertyType.TexEnv)
if (texture == null)
{
var propName = ShaderUtil.GetPropertyName(m.shader, i);
var tex = m.GetTexture(propName);
if (tex != null)
continue;
}
var assetPath = AssetDatabase.GetAssetPath(texture);
if (!string.IsNullOrEmpty(assetPath))
{
if (AssetImporter.GetAtPath(assetPath) is TextureImporter textureImporter)
{
var assetPath = AssetDatabase.GetAssetPath(tex);
if (!string.IsNullOrEmpty(assetPath))
switch (textureImporter.textureType)
{
if (AssetImporter.GetAtPath(assetPath) is TextureImporter textureImporter)
{
switch (textureImporter.textureType)
{
case TextureImporterType.Default:
case TextureImporterType.NormalMap:
break;
case TextureImporterType.Default:
case TextureImporterType.NormalMap:
break;
default:
// EditorTextureSerializer throw Exception
// エクスポート未実装
yield return Validation.Error($"{tex}: unknown texture type: {textureImporter.textureType}", ValidationContext.Create(tex));
break;
default:
// EditorTextureSerializer throw Exception
// エクスポート未実装
if (used.Add(texture))
{
yield return Validation.Error($"{texture}: unknown texture type: {textureImporter.textureType}", ValidationContext.Create(texture));
}
}
break;
}
}
}

View File

@ -30,6 +30,12 @@ namespace UniGLTF
return material;
}
public const string COLOR_TEXTURE_PROP = "_MainTex";
public const string METALLIC_TEX_PROP = "_MetallicGlossMap";
public const string NORMAL_TEX_PROP = "_BumpMap";
public const string EMISSION_TEX_PROP = "_EmissionMap";
public const string OCCLUSION_TEX_PROP = "_OcclusionMap";
static void Export_Color(Material m, ITextureExporter textureManager, glTFMaterial material)
{
if (m.HasProperty("_Color"))
@ -37,12 +43,12 @@ namespace UniGLTF
material.pbrMetallicRoughness.baseColorFactor = m.GetColor("_Color").ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
}
if (m.HasProperty("_MainTex"))
if (m.HasProperty(COLOR_TEXTURE_PROP))
{
// Don't export alpha channel if material was OPAQUE
var unnecessaryAlpha = string.Equals(material.alphaMode, "OPAQUE", StringComparison.Ordinal);
var index = textureManager.RegisterExportingAsSRgb(m.GetTexture("_MainTex"), !unnecessaryAlpha);
var index = textureManager.RegisterExportingAsSRgb(m.GetTexture(COLOR_TEXTURE_PROP), !unnecessaryAlpha);
if (index != -1)
{
material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
@ -67,20 +73,20 @@ namespace UniGLTF
float smoothness = 1.0f;
var textuerNames = m.GetTexturePropertyNames();
if (textuerNames.Contains("_MetallicGlossMap"))
if (textuerNames.Contains(METALLIC_TEX_PROP))
{
if (m.HasProperty("_GlossMapScale"))
{
smoothness = m.GetFloat("_GlossMapScale");
}
metallicSmoothTexture = m.GetTexture("_MetallicGlossMap");
metallicSmoothTexture = m.GetTexture(METALLIC_TEX_PROP);
}
Texture occlusionTexture = default;
var occlusionStrength = 1.0f;
if (textuerNames.Contains("_OcclusionMap"))
if (textuerNames.Contains(OCCLUSION_TEX_PROP))
{
occlusionTexture = m.GetTexture("_OcclusionMap");
occlusionTexture = m.GetTexture(OCCLUSION_TEX_PROP);
if (occlusionTexture != null && m.HasProperty("_OcclusionStrength"))
{
occlusionStrength = m.GetFloat("_OcclusionStrength");
@ -128,9 +134,9 @@ namespace UniGLTF
static void Export_Normal(Material m, ITextureExporter textureExporter, glTFMaterial material)
{
if (m.HasProperty("_BumpMap"))
if (m.HasProperty(NORMAL_TEX_PROP))
{
var index = textureExporter.RegisterExportingAsNormal(m.GetTexture("_BumpMap"));
var index = textureExporter.RegisterExportingAsNormal(m.GetTexture(NORMAL_TEX_PROP));
if (index != -1)
{
material.normalTexture = new glTFMaterialNormalTextureInfo()
@ -174,9 +180,9 @@ namespace UniGLTF
material.emissiveFactor = color.ToFloat3(ColorSpace.Linear, ColorSpace.Linear);
}
if (m.HasProperty("_EmissionMap"))
if (m.HasProperty(EMISSION_TEX_PROP))
{
var index = textureExporter.RegisterExportingAsSRgb(m.GetTexture("_EmissionMap"), needsAlpha: false);
var index = textureExporter.RegisterExportingAsSRgb(m.GetTexture(EMISSION_TEX_PROP), needsAlpha: false);
if (index != -1)
{
material.emissiveTexture = new glTFMaterialEmissiveTextureInfo()
@ -191,7 +197,7 @@ namespace UniGLTF
static void Export_MainTextureTransform(Material m, glTFTextureInfo textureInfo)
{
Export_TextureTransform(m, textureInfo, "_MainTex");
Export_TextureTransform(m, textureInfo, COLOR_TEXTURE_PROP);
}
static void Export_TextureTransform(Material m, glTFTextureInfo textureInfo, string propertyName)

View File

@ -124,6 +124,9 @@ namespace VRM
m_meshes = null;
}
/// <summary>
/// VRM0
/// </summary>
class VRMMaterialValidator : DefaultMaterialValidator
{
public override string GetGltfMaterialTypeFromUnityShaderName(string shaderName)
@ -135,6 +138,26 @@ namespace VRM
}
return base.GetGltfMaterialTypeFromUnityShaderName(shaderName);
}
public override IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m)
{
if (m.shader.name != "VRM/MToon")
{
foreach (var x in base.EnumerateTextureProperties(m))
{
yield return x;
}
}
var prop = UniGLTF.ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
foreach (var kv in prop.Properties)
{
if (kv.ShaderPropertyType == UniGLTF.ShaderPropExporter.ShaderPropertyType.TexEnv)
{
yield return (kv.Key, m.GetTexture(kv.Key));
}
}
}
}
protected override IEnumerable<Validator> ValidatorFactory()

View File

@ -6,7 +6,8 @@
"VRMShaders.GLTF.IO.Runtime",
"VRMShaders.GLTF.IO.Editor",
"UniGLTF",
"UniGLTF.Editor"
"UniGLTF.Editor",
"VRMShaders.VRM.IO.Runtime"
],
"optionalUnityReferences": [],
"includePlatforms": [