CreateMaterialAsyncFunc

This commit is contained in:
ousttrue
2021-02-12 21:22:46 +09:00
parent 590fec1cdc
commit c7e0191685
9 changed files with 138 additions and 156 deletions

View File

@@ -499,7 +499,7 @@ namespace UniGLTF
{
// root task. do nothing
})
.ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.LoadMaterials())
.ContinueWithCoroutine(Scheduler.MainThread, m_materialFactory.LoadMaterials)
.OnExecute(Scheduler.ThreadPool, parent =>
{
// UniGLTF does not support draco

View File

@@ -638,7 +638,7 @@ namespace UniGLTF
var result = new MeshWithMaterials
{
Mesh = mesh,
Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x).GetOrCreateAsync(ctx.GetTextureAsync).Result).ToArray()
Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray()
};
if (meshContext.BlendShapes.Count > 0)
@@ -667,7 +667,7 @@ namespace UniGLTF
var result = new MeshWithMaterials
{
Mesh = mesh,
Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x).GetOrCreateAsync(ctx.GetTextureAsync).Result).ToArray()
Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray()
};
yield return null;

View File

@@ -10,8 +10,6 @@ using UnityEditor;
namespace UniGLTF
{
public delegate MaterialItemBase MaterialImporter(int i, glTFMaterial x, bool hasVertexColor);
public class MaterialFactory : IDisposable
{
glTF m_gltf;
@@ -24,20 +22,21 @@ namespace UniGLTF
public UnityPath ImageBaseDir { get; set; }
MaterialImporter m_materialImporter;
public MaterialImporter MaterialImporter
public delegate Task<Material> CreateMaterialAsyncFunc(glTF glTF, int i, GetTextureAsyncFunc getTexture);
CreateMaterialAsyncFunc m_createMaterialAsync;
public CreateMaterialAsyncFunc CreateMaterialAsync
{
set
{
m_materialImporter = value;
m_createMaterialAsync = value;
}
get
{
if (m_materialImporter == null)
if (m_createMaterialAsync == null)
{
m_materialImporter = CreateMaterial;
m_createMaterialAsync = MaterialItemBase.DefaultCreateMaterialAsync;
}
return m_materialImporter;
return m_createMaterialAsync;
}
}
@@ -111,22 +110,19 @@ namespace UniGLTF
throw new NotImplementedException();
}
List<MaterialItemBase> m_materials = new List<MaterialItemBase>();
public void AddMaterial(MaterialItemBase material)
List<Material> m_materials = new List<Material>();
public IReadOnlyList<Material> Materials => m_materials;
public void AddMaterial(Material material)
{
var originalName = material.Name;
var originalName = material.name;
int j = 2;
while (m_materials.Any(x => x.Name == material.Name))
while (m_materials.Any(x => x.name == material.name))
{
material.Name = string.Format("{0}({1})", originalName, j++);
material.name = string.Format("{0}({1})", originalName, j++);
}
m_materials.Add(material);
}
public IList<MaterialItemBase> GetMaterials()
{
return m_materials;
}
public MaterialItemBase GetMaterial(int index)
public Material GetMaterial(int index)
{
if (index < 0) return null;
if (index >= m_materials.Count) return null;
@@ -146,37 +142,31 @@ namespace UniGLTF
{
// using (MeasureTime("LoadMaterials"))
{
if (m_gltf.materials == null || !m_gltf.materials.Any())
if (m_gltf.materials == null || m_gltf.materials.Count == 0)
{
AddMaterial(MaterialImporter(0, null, false));
var task = CreateMaterialAsync(m_gltf, 0, GetTextureAsync);
while (!task.IsCompleted)
{
yield return null;
}
AddMaterial(task.Result);
}
else
{
for (int i = 0; i < m_gltf.materials.Count; ++i)
{
AddMaterial(MaterialImporter(i, m_gltf.materials[i], m_gltf.MaterialHasVertexColor(i)));
var task = CreateMaterialAsync(m_gltf, i, GetTextureAsync);
while (!task.IsCompleted)
{
yield return null;
}
AddMaterial(task.Result);
}
}
}
yield return null;
}
public static MaterialItemBase CreateMaterial(int i, glTFMaterial x, bool hasVertexColor)
{
if (x == null)
{
UnityEngine.Debug.LogWarning("glTFMaterial is empty");
return new PBRMaterialItem(i, x);
}
if (glTF_KHR_materials_unlit.IsEnable(x))
{
return new UnlitMaterialItem(i, x, hasVertexColor);
}
return new PBRMaterialItem(i, x);
}
public void Dispose()
{
foreach (var x in ObjectsForSubAsset())
@@ -193,7 +183,7 @@ namespace UniGLTF
}
foreach (var x in m_materials)
{
yield return x.GetOrCreateAsync(GetTextureAsync).Result;
yield return x;
}
}
}

View File

@@ -4,43 +4,24 @@ using UnityEngine;
namespace UniGLTF
{
public abstract class MaterialItemBase
public static class MaterialItemBase
{
protected int m_index;
protected glTFMaterial m_src;
public string Name { get; set; }
public MaterialItemBase(int i, glTFMaterial src)
{
m_index = i;
m_src = src;
Name = src != null ? m_src.name : "";
}
public abstract Task<Material> GetOrCreateAsync(GetTextureAsyncFunc getTexture);
public Material GetOrCreateForTest()
{
return GetOrCreateAsync(null).Result;
}
protected Material CreateMaterial(string shaderName)
public static Material CreateMaterial(int index, glTFMaterial src, string shaderName)
{
var material = new Material(Shader.Find(shaderName));
#if UNITY_EDITOR
// textureImporter.SaveAndReimport(); may destroy this material
material.hideFlags = HideFlags.DontUnloadUnusedAsset;
#endif
material.name = (m_src == null || string.IsNullOrEmpty(m_src.name))
? string.Format("material_{0:00}", m_index)
: m_src.name
material.name = (src == null || string.IsNullOrEmpty(src.name))
? string.Format("material_{0:00}", index)
: src.name
;
return material;
}
protected static void SetTextureOffsetAndScale(Material material, glTFTextureInfo textureInfo, string propertyName)
public static void SetTextureOffsetAndScale(Material material, glTFTextureInfo textureInfo, string propertyName)
{
if (glTF_KHR_texture_transform.TryGet(textureInfo, out glTF_KHR_texture_transform textureTransform))
{
@@ -61,5 +42,36 @@ namespace UniGLTF
material.SetTextureScale(propertyName, scale);
}
}
public static Task<Material> DefaultCreateMaterialAsync(glTF gltf, int i, GetTextureAsyncFunc getTexture)
{
if (i < 0 || i >= gltf.materials.Count)
{
UnityEngine.Debug.LogWarning("glTFMaterial is empty");
return PBRMaterialItem.CreateAsync(i, null, getTexture);
}
var x = gltf.materials[i];
if (glTF_KHR_materials_unlit.IsEnable(x))
{
var hasVertexColor = gltf.MaterialHasVertexColor(i);
return UnlitMaterialItem.CreateAsync(i, x, getTexture, hasVertexColor);
}
return PBRMaterialItem.CreateAsync(i, x, getTexture);
}
/// <summary>
/// for unittest
/// </summary>
/// <param name="i"></param>
/// <param name="material"></param>
/// <param name="getTexture"></param>
/// <returns></returns>
public static Material CreateMaterialForTest(int i, glTFMaterial material)
{
throw new System.NotImplementedException();
}
}
}

View File

@@ -30,7 +30,7 @@ namespace UniGLTF
/// _SrcBlend
/// _DstBlend
/// _ZWrite
public class PBRMaterialItem : MaterialItemBase
public static class PBRMaterialItem
{
public const string ShaderName = "Standard";
@@ -42,45 +42,41 @@ namespace UniGLTF
Transparent
}
public PBRMaterialItem(int i, glTFMaterial src) : base(i, src)
{
}
public override async Task<Material> GetOrCreateAsync(GetTextureAsyncFunc getTexture)
public static async Task<Material> CreateAsync(int i, glTFMaterial src, GetTextureAsyncFunc getTexture)
{
if (getTexture == null)
{
getTexture = _ => Task.FromResult<Texture2D>(null);
}
var material = CreateMaterial(ShaderName);
var material = MaterialItemBase.CreateMaterial(i, src, ShaderName);
// PBR material
if (m_src != null)
if (src != null)
{
if (m_src.pbrMetallicRoughness != null)
if (src.pbrMetallicRoughness != null)
{
if (m_src.pbrMetallicRoughness.baseColorFactor != null && m_src.pbrMetallicRoughness.baseColorFactor.Length == 4)
if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
{
var color = m_src.pbrMetallicRoughness.baseColorFactor;
var color = src.pbrMetallicRoughness.baseColorFactor;
material.color = (new Color(color[0], color[1], color[2], color[3])).gamma;
}
if (m_src.pbrMetallicRoughness.baseColorTexture != null && m_src.pbrMetallicRoughness.baseColorTexture.index != -1)
if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1)
{
material.mainTexture = await getTexture(GetTextureParam.Create(m_src.pbrMetallicRoughness.baseColorTexture.index));
material.mainTexture = await getTexture(GetTextureParam.Create(src.pbrMetallicRoughness.baseColorTexture.index));
// Texture Offset and Scale
SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
MaterialItemBase.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
}
if (m_src.pbrMetallicRoughness.metallicRoughnessTexture != null && m_src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
{
material.EnableKeyword("_METALLICGLOSSMAP");
var texture = await getTexture(GetTextureParam.CreateMetallic(
m_src.pbrMetallicRoughness.metallicRoughnessTexture.index,
m_src.pbrMetallicRoughness.metallicFactor));
src.pbrMetallicRoughness.metallicRoughnessTexture.index,
src.pbrMetallicRoughness.metallicFactor));
if (texture != null)
{
material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture);
@@ -91,69 +87,69 @@ namespace UniGLTF
material.SetFloat("_GlossMapScale", 1.0f);
// Texture Offset and Scale
SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap");
MaterialItemBase.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap");
}
else
{
material.SetFloat("_Metallic", m_src.pbrMetallicRoughness.metallicFactor);
material.SetFloat("_Glossiness", 1.0f - m_src.pbrMetallicRoughness.roughnessFactor);
material.SetFloat("_Metallic", src.pbrMetallicRoughness.metallicFactor);
material.SetFloat("_Glossiness", 1.0f - src.pbrMetallicRoughness.roughnessFactor);
}
}
if (m_src.normalTexture != null && m_src.normalTexture.index != -1)
if (src.normalTexture != null && src.normalTexture.index != -1)
{
material.EnableKeyword("_NORMALMAP");
var texture = await getTexture(GetTextureParam.CreateNormal(m_src.normalTexture.index));
var texture = await getTexture(GetTextureParam.CreateNormal(src.normalTexture.index));
if (texture != null)
{
material.SetTexture(GetTextureParam.NORMAL_PROP, texture);
material.SetFloat("_BumpScale", m_src.normalTexture.scale);
material.SetFloat("_BumpScale", src.normalTexture.scale);
}
// Texture Offset and Scale
SetTextureOffsetAndScale(material, m_src.normalTexture, "_BumpMap");
MaterialItemBase.SetTextureOffsetAndScale(material, src.normalTexture, "_BumpMap");
}
if (m_src.occlusionTexture != null && m_src.occlusionTexture.index != -1)
if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
{
var texture = await getTexture(GetTextureParam.CreateOcclusion(m_src.occlusionTexture.index));
var texture = await getTexture(GetTextureParam.CreateOcclusion(src.occlusionTexture.index));
if (texture != null)
{
material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture);
material.SetFloat("_OcclusionStrength", m_src.occlusionTexture.strength);
material.SetFloat("_OcclusionStrength", src.occlusionTexture.strength);
}
// Texture Offset and Scale
SetTextureOffsetAndScale(material, m_src.occlusionTexture, "_OcclusionMap");
MaterialItemBase.SetTextureOffsetAndScale(material, src.occlusionTexture, "_OcclusionMap");
}
if (m_src.emissiveFactor != null
|| (m_src.emissiveTexture != null && m_src.emissiveTexture.index != -1))
if (src.emissiveFactor != null
|| (src.emissiveTexture != null && src.emissiveTexture.index != -1))
{
material.EnableKeyword("_EMISSION");
material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;
if (m_src.emissiveFactor != null && m_src.emissiveFactor.Length == 3)
if (src.emissiveFactor != null && src.emissiveFactor.Length == 3)
{
material.SetColor("_EmissionColor", new Color(m_src.emissiveFactor[0], m_src.emissiveFactor[1], m_src.emissiveFactor[2]));
material.SetColor("_EmissionColor", new Color(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2]));
}
if (m_src.emissiveTexture != null && m_src.emissiveTexture.index != -1)
if (src.emissiveTexture != null && src.emissiveTexture.index != -1)
{
var texture = await getTexture(GetTextureParam.Create(m_src.emissiveTexture.index));
var texture = await getTexture(GetTextureParam.Create(src.emissiveTexture.index));
if (texture != null)
{
material.SetTexture("_EmissionMap", texture);
}
// Texture Offset and Scale
SetTextureOffsetAndScale(material, m_src.emissiveTexture, "_EmissionMap");
MaterialItemBase.SetTextureOffsetAndScale(material, src.emissiveTexture, "_EmissionMap");
}
}
BlendMode blendMode = BlendMode.Opaque;
// https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980
switch (m_src.alphaMode)
switch (src.alphaMode)
{
case "BLEND":
blendMode = BlendMode.Fade;
@@ -173,7 +169,7 @@ namespace UniGLTF
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.SetFloat("_Cutoff", m_src.alphaCutoff);
material.SetFloat("_Cutoff", src.alphaCutoff);
material.EnableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");

View File

@@ -3,55 +3,48 @@ using UnityEngine;
namespace UniGLTF
{
public class UnlitMaterialItem : MaterialItemBase
public static class UnlitMaterialItem
{
public const string ShaderName = "UniGLTF/UniUnlit";
bool m_hasVertexColor;
public UnlitMaterialItem(int i, glTFMaterial src, bool hasVertexColor) : base(i, src)
{
m_hasVertexColor = hasVertexColor;
}
public override async Task<Material> GetOrCreateAsync(GetTextureAsyncFunc getTexture)
public static async Task<Material> CreateAsync(int i, glTFMaterial src, GetTextureAsyncFunc getTexture, bool hasVertexColor)
{
if (getTexture == null)
{
getTexture = _ => Task.FromResult<Texture2D>(null);
}
var material = CreateMaterial(ShaderName);
var material = MaterialItemBase.CreateMaterial(i, src, ShaderName);
// texture
if (m_src.pbrMetallicRoughness.baseColorTexture != null)
if (src.pbrMetallicRoughness.baseColorTexture != null)
{
material.mainTexture = await getTexture(GetTextureParam.Create(m_src.pbrMetallicRoughness.baseColorTexture.index));
material.mainTexture = await getTexture(GetTextureParam.Create(src.pbrMetallicRoughness.baseColorTexture.index));
// Texture Offset and Scale
SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
MaterialItemBase.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
}
// color
if (m_src.pbrMetallicRoughness.baseColorFactor != null && m_src.pbrMetallicRoughness.baseColorFactor.Length == 4)
if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
{
var color = m_src.pbrMetallicRoughness.baseColorFactor;
var color = src.pbrMetallicRoughness.baseColorFactor;
material.color = (new Color(color[0], color[1], color[2], color[3])).gamma;
}
//renderMode
if (m_src.alphaMode == "OPAQUE")
if (src.alphaMode == "OPAQUE")
{
UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
}
else if (m_src.alphaMode == "BLEND")
else if (src.alphaMode == "BLEND")
{
UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent);
}
else if (m_src.alphaMode == "MASK")
else if (src.alphaMode == "MASK")
{
UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout);
material.SetFloat("_Cutoff", m_src.alphaCutoff);
material.SetFloat("_Cutoff", src.alphaCutoff);
}
else
{
@@ -60,7 +53,7 @@ namespace UniGLTF
}
// culling
if (m_src.doubleSided)
if (src.doubleSided)
{
UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off);
}
@@ -70,7 +63,7 @@ namespace UniGLTF
}
// VColor
if (m_hasVertexColor)
if (hasVertexColor)
{
UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply);
}

View File

@@ -3,6 +3,7 @@ using NUnit.Framework;
using UnityEngine;
using UniJSON;
using System.Linq;
using System.Threading.Tasks;
namespace UniGLTF
{
@@ -32,7 +33,7 @@ namespace UniGLTF
var gltfMaterial = materialExporter.ExportMaterial(srcMaterial, textureManager);
gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions = gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions.Deserialize();
var dstMaterial = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var dstMaterial = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual(dstMaterial.mainTextureOffset.x, offset.x, 0.3f);
Assert.AreEqual(dstMaterial.mainTextureOffset.y, offset.y, 0.2f);
@@ -80,7 +81,7 @@ namespace UniGLTF
Assert.IsTrue(glTF_KHR_materials_unlit.IsEnable(gltfMaterial));
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
}
@@ -99,7 +100,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -114,7 +115,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -130,7 +131,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -145,7 +146,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -160,7 +161,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -176,7 +177,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -191,7 +192,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -207,7 +208,7 @@ namespace UniGLTF
},
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
@@ -217,7 +218,7 @@ namespace UniGLTF
{
extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
};
var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial);
Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
}
}
@@ -225,7 +226,7 @@ namespace UniGLTF
[Test]
public void MaterialImportTest()
{
var material = MaterialFactory.CreateMaterial(0, new glTFMaterial { }, false).GetOrCreateForTest();
var material = MaterialItemBase.CreateMaterialForTest(0, new glTFMaterial { });
Assert.AreEqual("Standard", material.shader.name);
}

View File

@@ -43,7 +43,7 @@ namespace VRM
{
VRM = vrm;
// override material importer
MaterialFactory.MaterialImporter = new VRMMaterialImporter(VRM.materialProperties).CreateMaterial;
MaterialFactory.CreateMaterialAsync = new VRMMaterialImporter(VRM.materialProperties).CreateMaterial;
}
else
{
@@ -198,9 +198,8 @@ namespace VRM
}
}
var material = MaterialFactory.GetMaterials()
.FirstOrDefault(y => y.Name == x.materialName)
.GetOrCreateAsync(MaterialFactory.GetTextureAsync).Result;
var material = MaterialFactory.Materials
.FirstOrDefault(y => y.name == x.materialName);
var propertyName = x.propertyName;
if (x.propertyName.FastEndsWith("_ST_S")
|| x.propertyName.FastEndsWith("_ST_T"))

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace VRM
{
public class MToonMaterialItem : MaterialItemBase
public static class MToonMaterialItem
{
static string[] VRM_SHADER_NAMES =
{
@@ -21,18 +21,9 @@ namespace VRM
"VRM/UnlitTransparentZWrite",
};
glTF_VRM_Material m_vrmMaterial;
bool m_hasVertexColor;
public MToonMaterialItem(int i, glTFMaterial src, bool hasVertexColor, glTF_VRM_Material vrmMaterial) : base(i, src)
public static async Task<Material> CreateAsync(glTF gltf, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture)
{
m_hasVertexColor = hasVertexColor;
m_vrmMaterial = vrmMaterial;
}
public override async Task<Material> GetOrCreateAsync(GetTextureAsyncFunc getTexture)
{
var item = m_vrmMaterial;
var item = vrmMaterial;
var shaderName = item.shader;
var shader = Shader.Find(shaderName);
if (shader == null)
@@ -48,7 +39,7 @@ namespace VRM
{
Debug.LogWarningFormat("unknown shader {0}.", shaderName);
}
return await MaterialFactory.CreateMaterial(m_index, m_src, m_hasVertexColor).GetOrCreateAsync(getTexture);
return await MaterialItemBase.DefaultCreateMaterialAsync(gltf, m_index, getTexture);
}
//
@@ -120,15 +111,15 @@ namespace VRM
m_materials = materials;
}
public MaterialItemBase CreateMaterial(int i, glTFMaterial src, bool hasVertexColor)
public Task<Material> CreateMaterial(glTF gltf, int i, GetTextureAsyncFunc getTexture)
{
if (i == 0 && m_materials.Count == 0)
{
// dummy
return new PBRMaterialItem(i, src);
return MaterialItemBase.DefaultCreateMaterialAsync(gltf, i, getTexture);
}
return new MToonMaterialItem(i, src, hasVertexColor, m_materials[i]);
return MToonMaterialItem.CreateAsync(gltf, i, m_materials[i], getTexture);
}
}
}