MaterialImporter.GetTextureFunc を引数に

This commit is contained in:
ousttrue 2021-02-10 17:51:15 +09:00
parent b0998eada8
commit 2ba6b4fc31
5 changed files with 23 additions and 24 deletions

View File

@ -129,7 +129,7 @@ namespace UniGLTF
{
if (m_materialImporter == null)
{
m_materialImporter = new MaterialImporter(ShaderStore, (int index) => this.GetTexture(index));
m_materialImporter = new MaterialImporter(ShaderStore);
}
return m_materialImporter;
}
@ -678,13 +678,13 @@ namespace UniGLTF
{
if (GLTF.materials == null || !GLTF.materials.Any())
{
AddMaterial(MaterialImporter.CreateMaterial(0, null, false));
AddMaterial(MaterialImporter.CreateMaterial(0, null, false, GetTexture));
}
else
{
for (int i = 0; i < GLTF.materials.Count; ++i)
{
AddMaterial(MaterialImporter.CreateMaterial(i, GLTF.materials[i], GLTF.MaterialHasVertexColor(i)));
AddMaterial(MaterialImporter.CreateMaterial(i, GLTF.materials[i], GLTF.MaterialHasVertexColor(i), GetTexture));
}
}
}

View File

@ -12,14 +12,13 @@ namespace UniGLTF
{
IShaderStore m_shaderStore;
protected Func<int, TextureItem> GetTextureFunc;
public MaterialImporter(IShaderStore shaderStore, Func<int, TextureItem> getTextureFunc)
public MaterialImporter(IShaderStore shaderStore)
{
m_shaderStore = shaderStore;
GetTextureFunc = getTextureFunc;
}
public delegate TextureItem GetTextureFunc(int index);
private enum BlendMode
{
Opaque,
@ -55,7 +54,7 @@ namespace UniGLTF
/// _SrcBlend
/// _DstBlend
/// _ZWrite
public virtual Material CreateMaterial(int i, glTFMaterial x, bool hasVertexColor)
public virtual Material CreateMaterial(int i, glTFMaterial x, bool hasVertexColor, GetTextureFunc getTexture)
{
var shader = m_shaderStore.GetShader(x);
//Debug.LogFormat("[{0}]{1}", i, shader.name);
@ -82,7 +81,7 @@ namespace UniGLTF
// texture
if (x.pbrMetallicRoughness.baseColorTexture != null)
{
var texture = GetTextureFunc(x.pbrMetallicRoughness.baseColorTexture.index);
var texture = getTexture(x.pbrMetallicRoughness.baseColorTexture.index);
if (texture != null)
{
material.mainTexture = texture.Texture;
@ -151,7 +150,7 @@ namespace UniGLTF
if (x.pbrMetallicRoughness.baseColorTexture != null && x.pbrMetallicRoughness.baseColorTexture.index != -1)
{
var texture = GetTextureFunc(x.pbrMetallicRoughness.baseColorTexture.index);
var texture = getTexture(x.pbrMetallicRoughness.baseColorTexture.index);
if (texture != null)
{
material.mainTexture = texture.Texture;
@ -164,7 +163,7 @@ namespace UniGLTF
if (x.pbrMetallicRoughness.metallicRoughnessTexture != null && x.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
{
material.EnableKeyword("_METALLICGLOSSMAP");
var texture = GetTextureFunc(x.pbrMetallicRoughness.metallicRoughnessTexture.index);
var texture = getTexture(x.pbrMetallicRoughness.metallicRoughnessTexture.index);
if (texture != null)
{
var prop = "_MetallicGlossMap";
@ -189,7 +188,7 @@ namespace UniGLTF
if (x.normalTexture != null && x.normalTexture.index != -1)
{
material.EnableKeyword("_NORMALMAP");
var texture = GetTextureFunc(x.normalTexture.index);
var texture = getTexture(x.normalTexture.index);
if (texture != null)
{
var prop = "_BumpMap";
@ -203,7 +202,7 @@ namespace UniGLTF
if (x.occlusionTexture != null && x.occlusionTexture.index != -1)
{
var texture = GetTextureFunc(x.occlusionTexture.index);
var texture = getTexture(x.occlusionTexture.index);
if (texture != null)
{
var prop = "_OcclusionMap";
@ -228,7 +227,7 @@ namespace UniGLTF
if (x.emissiveTexture != null && x.emissiveTexture.index != -1)
{
var texture = GetTextureFunc(x.emissiveTexture.index);
var texture = getTexture(x.emissiveTexture.index);
if (texture != null)
{
material.SetTexture("_EmissionMap", texture.Texture);

View File

@ -33,8 +33,8 @@ namespace UniGLTF
gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions = gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions.Deserialize();
var shaderStore = new ShaderStore(null);
var materialImporter = new MaterialImporter(shaderStore, (int index) => { return null; });
var dstMaterial = materialImporter.CreateMaterial(0, gltfMaterial, false);
var materialImporter = new MaterialImporter(shaderStore);
var dstMaterial = materialImporter.CreateMaterial(0, gltfMaterial, false, x => null);
Assert.AreEqual(dstMaterial.mainTextureOffset.x, offset.x, 0.3f);
Assert.AreEqual(dstMaterial.mainTextureOffset.y, offset.y, 0.2f);
@ -222,10 +222,10 @@ namespace UniGLTF
public void MaterialImportTest()
{
var shaderStore = new ShaderStore(null);
var materialImporter = new MaterialImporter(shaderStore, null);
var materialImporter = new MaterialImporter(shaderStore);
{
var material = materialImporter.CreateMaterial(0, new glTFMaterial { }, false);
var material = materialImporter.CreateMaterial(0, new glTFMaterial { }, false, null);
Assert.AreEqual("Standard", material.shader.name);
}
}

View File

@ -42,7 +42,7 @@ namespace VRM
{
VRM = vrm;
// override material importer
SetMaterialImporter(new VRMMaterialImporter(this, VRM.materialProperties));
SetMaterialImporter(new VRMMaterialImporter(new ShaderStore(this), VRM.materialProperties));
}
else
{

View File

@ -9,7 +9,7 @@ namespace VRM
public class VRMMaterialImporter : MaterialImporter
{
List<glTF_VRM_Material> m_materials;
public VRMMaterialImporter(ImporterContext context, List<glTF_VRM_Material> materials) : base(new ShaderStore(context), (int index) => context.GetTexture(index))
public VRMMaterialImporter(IShaderStore shaderStore, List<glTF_VRM_Material> materials) : base(shaderStore)
{
m_materials = materials;
}
@ -26,9 +26,9 @@ namespace VRM
"VRM/UnlitTransparentZWrite",
};
public override Material CreateMaterial(int i, glTFMaterial src, bool hasVertexColor)
public override Material CreateMaterial(int i, glTFMaterial src, bool hasVertexColor, GetTextureFunc getTexture)
{
if(i==0 && m_materials.Count == 0)
if (i == 0 && m_materials.Count == 0)
{
// dummy
return new Material(Shader.Find("Standard"));
@ -50,7 +50,7 @@ namespace VRM
{
Debug.LogWarningFormat("unknown shader {0}.", shaderName);
}
return base.CreateMaterial(i, src, hasVertexColor);
return base.CreateMaterial(i, src, hasVertexColor, getTexture);
}
//
@ -81,7 +81,7 @@ namespace VRM
}
foreach (var kv in item.textureProperties)
{
var texture = base.GetTextureFunc(kv.Value);
var texture = getTexture(kv.Value);
if (texture != null)
{
var converted = texture.ConvertTexture(kv.Key);