Meshサイズの事前計算。ReduceBlendShape反映

This commit is contained in:
ousttrue
2020-09-11 18:11:51 +09:00
parent 75484b1785
commit a153f30898

View File

@@ -22,6 +22,7 @@ namespace VRM
List<Validation> m_validations = new List<Validation>();
public IEnumerable<Validation> Validations => m_validations;
public int ExpectedByteSize = 0;
/// <summary>
/// ボーン名の重複を確認
@@ -187,6 +188,11 @@ namespace VRM
public void Validate(GameObject ExportRoot, VRMExportSettings m_settings, VRMMetaObject meta)
{
m_validations.Clear();
if (ExportRoot == null)
{
return;
}
m_validations.AddRange(_Validate(ExportRoot, m_settings));
if (ExportRoot != null)
{
@@ -198,6 +204,103 @@ namespace VRM
}
}
MetaHasError = meta.Validate().Any();
// サイズ の 計算
var proxy = ExportRoot.GetComponent<VRMBlendShapeProxy>();
var clips = new List<BlendShapeClip>();
if (proxy != null && proxy.BlendShapeAvatar != null)
{
clips.AddRange(proxy.BlendShapeAvatar.Clips);
}
ExpectedByteSize = 0;
foreach (var renderer in ExportRoot.GetComponentsInChildren<Renderer>())
{
var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(renderer.transform, ExportRoot.transform);
var mesh = GetMesh(renderer);
ExpectedByteSize += CalcMeshSize(relativePath, mesh, m_settings, clips);
}
}
static bool ClipsContainsName(List<BlendShapeClip> clips, bool onlyPreset, BlendShapeBinding binding)
{
foreach (var c in clips)
{
if (onlyPreset)
{
if (c.Preset == BlendShapePreset.Unknown)
{
continue;
}
}
foreach (var b in c.Values)
{
if (b.RelativePath == binding.RelativePath && b.Index == binding.Index)
{
return true;
}
}
}
return false;
}
static int CalcMeshSize(string relativePath, Mesh m, VRMExportSettings m_settings, List<BlendShapeClip> clips)
{
int size = 0;
// vertices
size += m.vertexCount * 4 * 3; // vector3
if (m.normals != null)
{
size += m.vertexCount * 4 * 3;
}
if (m.uv != null)
{
size += m.vertexCount * 4 * 2;
}
if (m.colors != null)
{
size += m.vertexCount * 4 * 4;
}
// indices
size += m.triangles.Length * 4; // int ?
// blendshapes
for (var i = 0; i < m.blendShapeCount; ++i)
{
// var name = m.GetBlendShapeName(i);
if (m_settings.ReduceBlendshape)
{
if (!ClipsContainsName(clips, m_settings.ReduceBlendshapeClip, new BlendShapeBinding
{
Index = i,
RelativePath = relativePath,
}))
{
// skip
continue;
}
}
size += m.vertexCount * 4 * (3 + 3);
}
return size;
}
static Mesh GetMesh(Renderer r)
{
if (r is SkinnedMeshRenderer smr)
{
return smr.sharedMesh;
}
if (r is MeshRenderer)
{
MeshFilter f = r.GetComponent<MeshFilter>();
if (f != null)
{
return f.sharedMesh;
}
}
return null;
}
IEnumerable<Validation> _Validate(GameObject ExportRoot, VRMExportSettings m_settings)