mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-08 03:48:19 -05:00
Migrate old unlit materials.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using UniGLTF;
|
||||
using UniJSON;
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
/// <summary>
|
||||
/// 過去の UniVRM において、KHR_materials_unlit 拡張を使わず、VRM 拡張を用いて Unlit を表現していた Material をマイグレーションする。
|
||||
/// </summary>
|
||||
public static class MigrationLegacyUnlitMaterial
|
||||
{
|
||||
public static glTFMaterial Migrate(JsonNode vrm0XMaterial)
|
||||
{
|
||||
var unlitMaterial = new glTFMaterial
|
||||
{
|
||||
pbrMetallicRoughness = new glTFPbrMetallicRoughness
|
||||
{
|
||||
metallicFactor = 0f,
|
||||
roughnessFactor = 1f,
|
||||
},
|
||||
extensions = new glTFExtensionExport()
|
||||
.Add(glTF_KHR_materials_unlit.ExtensionName, new ArraySegment<byte>(glTF_KHR_materials_unlit.Raw)),
|
||||
};
|
||||
|
||||
switch (MigrationMaterialUtil.GetShaderName(vrm0XMaterial))
|
||||
{
|
||||
case "Unlit/Color":
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorFactor = MigrationMaterialUtil.GetBaseColorFactor(vrm0XMaterial);
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorTexture = null;
|
||||
return unlitMaterial;
|
||||
case "Unlit/Texture":
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorFactor = new float[] {1, 1, 1, 1};
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
|
||||
return unlitMaterial;
|
||||
case "Unlit/Transparent":
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorFactor = new float[] {1, 1, 1, 1};
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
|
||||
unlitMaterial.alphaMode = "BLEND";
|
||||
return unlitMaterial;
|
||||
case "Unlit/Transparent Cutout":
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorFactor = new float[] {1, 1, 1, 1};
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
|
||||
unlitMaterial.alphaMode = "MASK";
|
||||
unlitMaterial.alphaCutoff = MigrationMaterialUtil.GetCutoff(vrm0XMaterial);
|
||||
return unlitMaterial;
|
||||
case "VRM/UnlitTexture":
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorFactor = new float[] {1, 1, 1, 1};
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
|
||||
return unlitMaterial;
|
||||
case "VRM/UnlitTransparent":
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorFactor = new float[] {1, 1, 1, 1};
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
|
||||
unlitMaterial.alphaMode = "BLEND";
|
||||
return unlitMaterial;
|
||||
case "VRM/UnlitCutout":
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorFactor = new float[] {1, 1, 1, 1};
|
||||
unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
|
||||
unlitMaterial.alphaMode = "MASK";
|
||||
unlitMaterial.alphaCutoff = MigrationMaterialUtil.GetCutoff(vrm0XMaterial);
|
||||
return unlitMaterial;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 46c50e244d72488dac8e26320bac782d
|
||||
timeCreated: 1635420423
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UniGLTF;
|
||||
using UniJSON;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
internal static class MigrationMaterialUtil
|
||||
{
|
||||
private const string ShaderNameKey = "shader";
|
||||
private const string VectorPropertiesKey = "vectorProperties";
|
||||
private const string FloatPropertiesKey = "floatProperties";
|
||||
private const string TexturePropertiesKey = "textureProperties";
|
||||
private const string MainTexKey = "_MainTex";
|
||||
private const string ColorKey = "_Color";
|
||||
private const string CutoffKey = "_Cutoff";
|
||||
|
||||
public static string GetShaderName(JsonNode vrm0XMaterial)
|
||||
{
|
||||
try
|
||||
{
|
||||
return vrm0XMaterial[ShaderNameKey].GetString();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.LogWarning($"Migration Warning: ShaderName fallback default.");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static float[] GetBaseColorFactor(JsonNode vrm0XMaterial)
|
||||
{
|
||||
try
|
||||
{
|
||||
var factor = vrm0XMaterial[VectorPropertiesKey][ColorKey];
|
||||
if (!factor.IsArray() || factor.GetArrayCount() != 4)
|
||||
{
|
||||
throw new Exception("not float4");
|
||||
}
|
||||
return factor.ArrayItems().Select(x => ListTreeNodeExtensions.GetSingle(x)).ToArray();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.LogWarning($"Migration Warning: BaseColorFactor fallback default.");
|
||||
return new float[] {1, 1, 1, 1};
|
||||
}
|
||||
}
|
||||
|
||||
public static glTFMaterialBaseColorTextureInfo GetBaseColorTexture(JsonNode vrm0XMaterial)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new glTFMaterialBaseColorTextureInfo
|
||||
{
|
||||
index = vrm0XMaterial[TexturePropertiesKey][MainTexKey].GetInt32(),
|
||||
};
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.LogWarning($"Migration Warning: BaseColorTexture fallback default.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static float GetCutoff(JsonNode vrm0XMaterial)
|
||||
{
|
||||
try
|
||||
{
|
||||
return vrm0XMaterial[FloatPropertiesKey][CutoffKey].GetSingle();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.LogWarning($"Migration Warning: Cutoff fallback default.");
|
||||
return 0.5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9d2327bd1774f949440b833af321d69
|
||||
timeCreated: 1635423551
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using UniGLTF;
|
||||
using UniJSON;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
public static class MigrationMaterials
|
||||
{
|
||||
private const string DontUseExtensionShaderName = "VRM_USE_GLTFSHADER";
|
||||
private const string MaterialPropertiesKey = "materialProperties";
|
||||
|
||||
public static void Migrate(glTF gltf, JsonNode vrm0)
|
||||
{
|
||||
for (var materialIdx = 0; materialIdx < gltf.materials.Count; ++materialIdx)
|
||||
{
|
||||
try
|
||||
{
|
||||
var vrm0XMaterial = vrm0[MaterialPropertiesKey][materialIdx];
|
||||
var shaderName = MigrationMaterialUtil.GetShaderName(vrm0XMaterial);
|
||||
|
||||
// 1. material 拡張を使わない場合.
|
||||
// NOTE: この ShaderName の場合、「この拡張に記載されている情報は無視する」ということを示す.
|
||||
if (shaderName == DontUseExtensionShaderName)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 2. VRM 拡張のうち、古い Unlit 情報からの取得を試みる.
|
||||
var unlitMaterial = MigrationLegacyUnlitMaterial.Migrate(vrm0XMaterial);
|
||||
if (unlitMaterial != null)
|
||||
{
|
||||
gltf.materials[materialIdx] = unlitMaterial;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. VRM 拡張のうち、MToon 情報からの取得を試みる.
|
||||
MigrationMToonMaterial.Migrate(gltf, vrm0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 431e7e81ad3b4f7e9a03ad46d85f9d07
|
||||
timeCreated: 1635418411
|
||||
@@ -105,9 +105,9 @@ namespace UniVRM10
|
||||
UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref gltf.extensions, springBone);
|
||||
}
|
||||
|
||||
// MToon
|
||||
// Material
|
||||
{
|
||||
MigrationMToonMaterial.Migrate(gltf, vrm0);
|
||||
MigrationMaterials.Migrate(gltf, vrm0);
|
||||
}
|
||||
|
||||
// Serialize whole glTF
|
||||
|
||||
Reference in New Issue
Block a user