mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-08 03:48:19 -05:00
Implement VRMZWriteMaterialImporter
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UniGLTF;
|
||||
using UniGLTF;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
@@ -16,24 +14,35 @@ namespace VRM
|
||||
|
||||
public MaterialDescriptor Get(GltfData data, int i)
|
||||
{
|
||||
// mtoon
|
||||
if (!VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out MaterialDescriptor matDesc))
|
||||
MaterialDescriptor matDesc;
|
||||
|
||||
// legacy "VRM/UnlitTransparentZWrite"
|
||||
if (VRMZWriteMaterialImporter.TryCreateParam(data, m_vrm, i, out matDesc))
|
||||
{
|
||||
// unlit
|
||||
if (!GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc))
|
||||
{
|
||||
// pbr
|
||||
if (!GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc))
|
||||
{
|
||||
// fallback
|
||||
#if VRM_DEVELOP
|
||||
Debug.LogWarning($"material: {i} out of range. fallback");
|
||||
#endif
|
||||
return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName);
|
||||
}
|
||||
}
|
||||
return matDesc;
|
||||
}
|
||||
return matDesc;
|
||||
|
||||
// mtoon
|
||||
if (VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out matDesc))
|
||||
{
|
||||
return matDesc;
|
||||
}
|
||||
|
||||
// unlit
|
||||
if (GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc))
|
||||
{
|
||||
return matDesc;
|
||||
}
|
||||
|
||||
// pbr
|
||||
if (GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc))
|
||||
{
|
||||
return matDesc;
|
||||
}
|
||||
|
||||
// fallback
|
||||
Debug.LogWarning($"fallback");
|
||||
return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
87
Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs
Normal file
87
Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using UniGLTF;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRM
|
||||
{
|
||||
public static class VRMZWriteMaterialImporter
|
||||
{
|
||||
public const string ShaderName = "VRM/UnlitTransparentZWrite";
|
||||
|
||||
public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc)
|
||||
{
|
||||
if (vrm?.materialProperties == null || vrm.materialProperties.Count == 0)
|
||||
{
|
||||
matDesc = default;
|
||||
return false;
|
||||
}
|
||||
if (materialIdx < 0 || materialIdx >= vrm.materialProperties.Count)
|
||||
{
|
||||
matDesc = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var vrmMaterial = vrm.materialProperties[materialIdx];
|
||||
if (vrmMaterial.shader != ShaderName)
|
||||
{
|
||||
// fallback to gltf
|
||||
matDesc = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// use material.name, because material name may renamed in GltfParser.
|
||||
var name = data.GLTF.materials[materialIdx].name;
|
||||
|
||||
//
|
||||
// import as MToon
|
||||
//
|
||||
matDesc = new MaterialDescriptor(name, MToon.Utils.ShaderName);
|
||||
|
||||
matDesc.RenderQueue = vrmMaterial.renderQueue;
|
||||
|
||||
if (vrmMaterial.textureProperties.ContainsKey(MToon.Utils.PropMainTex))
|
||||
{
|
||||
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, MToon.Utils.PropMainTex, out var texture))
|
||||
{
|
||||
matDesc.TextureSlots.Add(MToon.Utils.PropMainTex, texture.Item2);
|
||||
matDesc.TextureSlots.Add(MToon.Utils.PropShadeTexture, texture.Item2);
|
||||
}
|
||||
}
|
||||
|
||||
matDesc.Colors[MToon.Utils.PropColor] = Color.white;
|
||||
matDesc.Colors[MToon.Utils.PropShadeColor] = Color.white;
|
||||
|
||||
foreach (var kv in vrmMaterial.keywordMap)
|
||||
{
|
||||
if (kv.Value)
|
||||
{
|
||||
matDesc.Actions.Add(material => material.EnableKeyword(kv.Key));
|
||||
}
|
||||
else
|
||||
{
|
||||
matDesc.Actions.Add(material => material.DisableKeyword(kv.Key));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var kv in vrmMaterial.tagMap)
|
||||
{
|
||||
matDesc.Actions.Add(material => material.SetOverrideTag(kv.Key, kv.Value));
|
||||
}
|
||||
|
||||
if (vrmMaterial.shader == MToon.Utils.ShaderName)
|
||||
{
|
||||
// TODO: Material拡張にMToonの項目が追加されたら旧バージョンのshaderPropから変換をかける
|
||||
// インポート時にUniVRMに含まれるMToonのバージョンに上書きする
|
||||
matDesc.FloatValues[MToon.Utils.PropVersion] = MToon.Utils.VersionNumber;
|
||||
}
|
||||
|
||||
matDesc.Actions.Add(m =>
|
||||
{
|
||||
m.SetFloat(MToon.Utils.PropBlendMode, (float)MToon.RenderMode.TransparentWithZWrite);
|
||||
MToon.Utils.ValidateProperties(m, true);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs.meta
Normal file
11
Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51208fe94c21c4b4abb16fd89a3aa7f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user