Fix errors

This commit is contained in:
notargs 2021-10-25 17:03:47 +09:00
parent adcf43dcbe
commit 1e98f065e1
3 changed files with 59 additions and 35 deletions

View File

@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MToon;
using UniGLTF;
using UnityEngine;
@ -11,13 +14,15 @@ namespace VRM
public const string UnlitTransparentZWriteShaderName = "VRM/UnlitTransparentZWrite";
public const string UnlitTransparentZWriteMainTexturePropName = "_MainTex";
public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc)
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;
@ -34,19 +39,24 @@ namespace VRM
// use material.name, because material name may renamed in GltfParser.
var name = data.GLTF.materials[materialIdx].name;
matDesc = new MaterialDescriptor(name, MToon.Utils.ShaderName);
var textureSlots = new Dictionary<string, TextureDescriptor>();
var floatValues = new Dictionary<string, float>();
var colors = new Dictionary<string, Color>();
var vectors = new Dictionary<string, Vector4>();
var actions = new Collection<Action<Material>>();
if (vrmMaterial.textureProperties.ContainsKey(UnlitTransparentZWriteMainTexturePropName))
{
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, UnlitTransparentZWriteMainTexturePropName, out var texture))
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial,
UnlitTransparentZWriteMainTexturePropName, out var texture))
{
matDesc.TextureSlots.Add(MToon.Utils.PropMainTex, texture.Item2);
textureSlots.Add(MToon.Utils.PropMainTex, texture.Item2);
}
}
matDesc.Actions.Add(unityMaterial =>
actions.Add(unityMaterial =>
{
var mainTexture = (Texture2D) unityMaterial.GetTexture(MToon.Utils.PropMainTex);
var mainTexture = (Texture2D)unityMaterial.GetTexture(MToon.Utils.PropMainTex);
// NOTE: Unlit のフォールバックなので
// Lit/Shade 色は黒として、Alpha のために Lit にテクスチャを設定する.
@ -148,8 +158,15 @@ namespace VRM
unityMaterial.renderQueue = vrmMaterial.renderQueue;
});
matDesc = new MaterialDescriptor(name, Utils.ShaderName, null,
textureSlots,
floatValues,
colors,
vectors,
actions);
Debug.LogWarning($"fallback: {UnlitTransparentZWriteShaderName} => {MToon.Utils.ShaderName}");
return true;
}
}
}
}

View File

@ -1,4 +1,6 @@
using UniGLTF;
using System;
using System.Collections.Generic;
using UniGLTF;
using UnityEngine;
using VRMShaders;
@ -9,23 +11,23 @@ namespace UniVRM10
public MaterialDescriptor Get(GltfData data, int i)
{
// mtoon
if (!Vrm10MToonMaterialImporter.TryCreateParam(data, i, out MaterialDescriptor matDesc))
{
// unlit
if (!GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc))
{
// pbr
if (!GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc))
{
// fallback
if (Vrm10MToonMaterialImporter.TryCreateParam(data, i, out MaterialDescriptor matDesc)) return matDesc;
// unlit
if (GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
// pbr
if (GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
// fallback
#if VRM_DEVELOP
Debug.LogWarning($"material: {i} out of range. fallback");
Debug.LogWarning($"material: {i} out of range. fallback");
#endif
return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName);
}
}
}
return matDesc;
return new MaterialDescriptor(
GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName,
null,
new Dictionary<string, TextureDescriptor>(),
new Dictionary<string, float>(),
new Dictionary<string, Color>(),
new Dictionary<string, Vector4>(),
new Action<Material>[]{});
}
}

View File

@ -1,4 +1,6 @@
using UniGLTF;
using System;
using System.Collections.Generic;
using UniGLTF;
using UnityEngine;
using VRMShaders;
@ -9,17 +11,20 @@ namespace UniVRM10
public MaterialDescriptor Get(GltfData data, int i)
{
// unlit
if (!GltfUnlitMaterialImporter.TryCreateParam(data, i, out MaterialDescriptor matDesc))
{
// pbr
if (!GltfPbrUrpMaterialImporter.TryCreateParam(data, i, out matDesc))
{
// fallback
Debug.LogWarning($"material: {i} out of range. fallback");
return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName);
}
}
return matDesc;
if (GltfUnlitMaterialImporter.TryCreateParam(data, i, out MaterialDescriptor matDesc)) return matDesc;
// pbr
if (GltfPbrUrpMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
// fallback
Debug.LogWarning($"material: {i} out of range. fallback");
return new MaterialDescriptor(
GltfMaterialDescriptorGenerator.GetMaterialName(i, null),
GltfPbrMaterialImporter.ShaderName,
null,
new Dictionary<string, TextureDescriptor>(),
new Dictionary<string, float>(),
new Dictionary<string, Color>(),
new Dictionary<string, Vector4>(),
new Action<Material>[]{});
}
}