MeshExportList を追加。GetUniqueMaterials を共通化

This commit is contained in:
ousttrue 2021-07-16 16:47:21 +09:00
parent da4eff1318
commit 0d9b9cbbf7
4 changed files with 77 additions and 57 deletions

View File

@ -55,7 +55,7 @@ namespace UniGLTF
return null;
}
public List<MeshExportInfo> Meshes = new List<MeshExportInfo>();
public MeshExportList Meshes = new MeshExportList();
public int ExpectedExportByteSize => Meshes.Where(x => x.IsRendererActive).Sum(x => x.ExportByteSize);
@ -65,7 +65,7 @@ namespace UniGLTF
{
return;
}
MeshExportInfo.GetInfo(ExportRoot.transform.Traverse().Skip(1), Meshes, settings);
Meshes.GetInfo(ExportRoot.transform.Traverse().Skip(1), settings);
foreach (var info in Meshes)
{
info.CalcMeshSize(ExportRoot, info.Renderers[0].Item1, settings, blendShapeFilter);
@ -108,12 +108,8 @@ namespace UniGLTF
}
}
foreach (var m in Meshes.SelectMany(x => x.Materials).Distinct())
foreach (var m in Meshes.GetUniqueMaterials())
{
if (m == null)
{
continue;
}
var gltfMaterial = MaterialValidator.GetGltfMaterialTypeFromUnityShaderName(m.shader.name);
if (string.IsNullOrEmpty(gltfMaterial))
{

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -50,7 +51,7 @@ namespace UniGLTF
}
}
public static bool TryGetSameMeshIndex(List<MeshExportInfo> meshWithRenderers, Mesh mesh, Material[] materials, out int meshIndex)
public static bool TryGetSameMeshIndex(IReadOnlyList<MeshExportInfo> meshWithRenderers, Mesh mesh, Material[] materials, out int meshIndex)
{
for (var i = 0; i < meshWithRenderers.Count; i++)
{
@ -183,7 +184,7 @@ namespace UniGLTF
public string Summary;
#endregion
MeshExportInfo(Renderer renderer, GltfExportSettings settings)
public MeshExportInfo(Renderer renderer, GltfExportSettings settings)
{
if (renderer == null)
{
@ -220,7 +221,7 @@ namespace UniGLTF
PushRenderer(renderer);
}
void PushRenderer(Renderer renderer)
public void PushRenderer(Renderer renderer)
{
if (renderer is SkinnedMeshRenderer smr)
{
@ -261,53 +262,12 @@ namespace UniGLTF
}
}
public static MeshExportInfo Create(GameObject go)
{
var list = new List<MeshExportInfo>();
GetInfo(go.transform.Traverse(), list, new GltfExportSettings());
return list[0];
}
/// <summary>
/// ヒエラルキーからエクスポートする Mesh の情報を収集する
/// </summary>
/// <param name="root"></param>
/// <param name="list"></param>
/// <param name="settings"></param>
/// <param name="blendShapeFilter"> blendShape の export を filtering する </param>
public static void GetInfo(IEnumerable<Transform> nodes, List<MeshExportInfo> list, GltfExportSettings settings)
{
list.Clear();
foreach (var node in nodes)
{
var renderer = node.GetComponent<Renderer>();
if (renderer == null)
{
continue;
}
var found = list.FirstOrDefault(x => x.IsSameMeshAndMaterials(renderer));
if (found != null)
{
found.PushRenderer(renderer);
continue;
}
var info = new MeshExportInfo(renderer, settings);
if (info.Mesh != null)
{
list.Add(info);
}
}
}
static bool TryGetMeshInfo()
{
return true;
}
public void CalcMeshSize(
GameObject root,
Renderer renderer,
@ -409,4 +369,68 @@ namespace UniGLTF
Summary = sb.ToString();
}
}
public class MeshExportList : IReadOnlyList<MeshExportInfo>
{
List<MeshExportInfo> m_list = new List<MeshExportInfo>();
public int Count => m_list.Count;
public MeshExportInfo this[int index] => m_list[index];
public IEnumerator<MeshExportInfo> GetEnumerator()
{
return m_list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerable<Material> GetUniqueMaterials()
{
return m_list.SelectMany(x => x.Materials).Where(x => x != null).Distinct();
}
/// <summary>
/// ヒエラルキーからエクスポートする Mesh の情報を収集する
/// </summary>
/// <param name="root"></param>
/// <param name="list"></param>
/// <param name="settings"></param>
/// <param name="blendShapeFilter"> blendShape の export を filtering する </param>
public void GetInfo(IEnumerable<Transform> nodes, GltfExportSettings settings)
{
m_list.Clear();
foreach (var node in nodes)
{
var renderer = node.GetComponent<Renderer>();
if (renderer == null)
{
continue;
}
var found = m_list.FirstOrDefault(x => x.IsSameMeshAndMaterials(renderer));
if (found != null)
{
found.PushRenderer(renderer);
continue;
}
var info = new MeshExportInfo(renderer, settings);
if (info.Mesh != null)
{
m_list.Add(info);
}
}
}
public static MeshExportInfo Create(GameObject go)
{
var list = new MeshExportList();
list.GetInfo(go.transform.Traverse(), new GltfExportSettings());
return list.m_list[0];
}
}
}

View File

@ -142,7 +142,7 @@ namespace UniGLTF
}
#region Export
static glTFNode ExportNode(Transform x, List<Transform> nodes, List<MeshExportInfo> meshWithRenderers, List<SkinnedMeshRenderer> skins)
static glTFNode ExportNode(Transform x, List<Transform> nodes, IReadOnlyList<MeshExportInfo> meshWithRenderers, List<SkinnedMeshRenderer> skins)
{
var node = new glTFNode
{
@ -231,11 +231,11 @@ namespace UniGLTF
.Skip(1) // exclude root object for the symmetry with the importer
.ToList();
var uniqueUnityMeshes = new List<MeshExportInfo>();
MeshExportInfo.GetInfo(Nodes, uniqueUnityMeshes, meshExportSettings);
var uniqueUnityMeshes = new MeshExportList();
uniqueUnityMeshes.GetInfo(Nodes, meshExportSettings);
#region Materials and Textures
Materials = uniqueUnityMeshes.SelectMany(x => x.Materials).Where(x => x != null).Distinct().ToList();
Materials = uniqueUnityMeshes.GetUniqueMaterials().ToList();
m_textureExporter = new TextureExporter(textureSerializer);

View File

@ -134,7 +134,7 @@ namespace UniGLTF
};
var axisInverter = Axes.X.Create();
var unityMesh = MeshExportInfo.Create(go);
var unityMesh = MeshExportList.Create(go);
var (gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer
? MeshExporter_DividedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials, axisInverter, meshExportSettings)
: MeshExporter_SharedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials,axisInverter, meshExportSettings)
@ -182,7 +182,7 @@ namespace UniGLTF
};
var axisInverter = Axes.X.Create();
var unityMesh = MeshExportInfo.Create(go);
var unityMesh = MeshExportList.Create(go);
var (gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer
? MeshExporter_DividedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials, axisInverter, meshExportSettings)
: MeshExporter_SharedVertexBuffer.Export(glTF, bufferIndex, unityMesh, Materials,axisInverter, meshExportSettings)