MeshIntegrator use MeshIntegrationGroup

This commit is contained in:
ousttrue 2023-10-27 15:15:27 +09:00
parent 000662aeb0
commit cd3d0573d6
3 changed files with 64 additions and 57 deletions

View File

@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using UnityEngine;
namespace UniGLTF.MeshUtility
{
public class MeshIntegrator
{
public const string INTEGRATED_MESH_WITHOUT_BLENDSHAPE_NAME = "Integrated(WithoutBlendShape)";
public const string INTEGRATED_MESH_WITH_BLENDSHAPE_NAME = "Integrated(WithBlendShape)";
public const string INTEGRATED_MESH_ALL_NAME = "Integrated(All)";
private MeshIntegrator()
{
}
struct SubMesh
{
@ -81,7 +85,7 @@ namespace UniGLTF.MeshUtility
return bw;
}
public void Push(MeshRenderer renderer)
void Push(MeshRenderer renderer)
{
var meshFilter = renderer.GetComponent<MeshFilter>();
if (meshFilter == null)
@ -232,7 +236,24 @@ namespace UniGLTF.MeshUtility
}
}
public MeshIntegrationResult Integrate(MeshEnumerateOption onlyBlendShapeRenderers)
public static MeshIntegrationResult Integrate(MeshIntegrationGroup group, bool useBlendShape)
{
var integrator = new MeshUtility.MeshIntegrator();
foreach (var x in group.Renderers)
{
if (x is SkinnedMeshRenderer smr)
{
integrator.Push(smr);
}
else if (x is MeshRenderer mr)
{
integrator.Push(mr);
}
}
return integrator.Integrate(group.Name, useBlendShape);
}
public MeshIntegrationResult Integrate(string name, bool useBlendShape)
{
var mesh = new Mesh();
@ -253,51 +274,15 @@ namespace UniGLTF.MeshUtility
mesh.SetIndices(SubMeshes[i].Indices.ToArray(), MeshTopology.Triangles, i);
}
mesh.bindposes = BindPoses.ToArray();
// blendshape
switch (onlyBlendShapeRenderers)
if (useBlendShape)
{
case MeshEnumerateOption.OnlyWithBlendShape:
{
AddBlendShapesToMesh(mesh);
mesh.name = INTEGRATED_MESH_WITH_BLENDSHAPE_NAME;
break;
}
case MeshEnumerateOption.All:
{
AddBlendShapesToMesh(mesh);
mesh.name = INTEGRATED_MESH_ALL_NAME;
break;
}
case MeshEnumerateOption.OnlyWithoutBlendShape:
{
mesh.name = INTEGRATED_MESH_WITHOUT_BLENDSHAPE_NAME;
break;
}
AddBlendShapesToMesh(mesh);
}
mesh.name = name;
// meshName
var meshNode = new GameObject();
switch (onlyBlendShapeRenderers)
{
case MeshEnumerateOption.OnlyWithBlendShape:
{
meshNode.name = INTEGRATED_MESH_WITH_BLENDSHAPE_NAME;
break;
}
case MeshEnumerateOption.OnlyWithoutBlendShape:
{
meshNode.name = INTEGRATED_MESH_WITHOUT_BLENDSHAPE_NAME;
break;
}
case MeshEnumerateOption.All:
{
meshNode.name = INTEGRATED_MESH_ALL_NAME;
break;
}
}
meshNode.name = name;
var integrated = meshNode.AddComponent<SkinnedMeshRenderer>();
integrated.sharedMesh = mesh;

View File

@ -1,11 +1,14 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace UniGLTF.MeshUtility
{
public static class MeshIntegratorUtility
{
public const string INTEGRATED_MESH_WITHOUT_BLENDSHAPE_NAME = "Integrated(WithoutBlendShape)";
public const string INTEGRATED_MESH_WITH_BLENDSHAPE_NAME = "Integrated(WithBlendShape)";
public const string INTEGRATED_MESH_ALL_NAME = "Integrated(All)";
/// <summary>
/// go を root としたヒエラルキーから Renderer を集めて、統合された Mesh 作成する
/// </summary>
@ -22,32 +25,39 @@ namespace UniGLTF.MeshUtility
{
var exclude = new MeshExclude(excludes);
var integrator = new MeshUtility.MeshIntegrator();
var group = new MeshIntegrationGroup();
bool useBlendShape = false;
switch (onlyBlendShapeRenderers)
{
case MeshEnumerateOption.OnlyWithBlendShape:
{
group.Name = INTEGRATED_MESH_WITH_BLENDSHAPE_NAME;
useBlendShape = true;
foreach (var x in EnumerateSkinnedMeshRenderer(go.transform, onlyBlendShapeRenderers))
{
if (exclude.IsExcluded(x))
{
continue;
}
integrator.Push(x);
group.Renderers.Add(x);
}
break;
}
case MeshEnumerateOption.OnlyWithoutBlendShape:
{
group.Name = INTEGRATED_MESH_WITHOUT_BLENDSHAPE_NAME;
useBlendShape = false;
foreach (var x in EnumerateSkinnedMeshRenderer(go.transform, onlyBlendShapeRenderers))
{
if (exclude.IsExcluded(x))
{
continue;
}
integrator.Push(x);
group.Renderers.Add(x);
}
foreach (var x in EnumerateMeshRenderer(go.transform))
@ -56,7 +66,7 @@ namespace UniGLTF.MeshUtility
{
continue;
}
integrator.Push(x);
group.Renderers.Add(x);
}
break;
@ -64,13 +74,16 @@ namespace UniGLTF.MeshUtility
case MeshEnumerateOption.All:
{
group.Name = INTEGRATED_MESH_ALL_NAME;
useBlendShape = true;
foreach (var x in EnumerateSkinnedMeshRenderer(go.transform, onlyBlendShapeRenderers))
{
if (exclude.IsExcluded(x))
{
continue;
}
integrator.Push(x);
group.Renderers.Add(x);
}
foreach (var x in EnumerateMeshRenderer(go.transform))
@ -79,14 +92,14 @@ namespace UniGLTF.MeshUtility
{
continue;
}
integrator.Push(x);
group.Renderers.Add(x);
}
break;
}
}
return integrator.Integrate(onlyBlendShapeRenderers);
return MeshIntegrator.Integrate(group, useBlendShape);
}
public static IEnumerable<SkinnedMeshRenderer> EnumerateSkinnedMeshRenderer(Transform root, MeshEnumerateOption hasBlendShape)

View File

@ -145,13 +145,22 @@ namespace UniVRM10
// TODO: integration
foreach (var group in MeshIntegrationGroups)
{
foreach (var renderer in group.Renderers)
{
var newMesh = MeshIntegrator.Integrate(group, true);
// TODO: firstperson
// TODO: split
if (SplitByBlendShape)
{
// var withBlendShape, withoutBlendShape
// TODO: remove old renderer
// TODO: add new renderer to root
}
else
{
// TODO: remove old renderer
// TODO: add new renderer to root
}
}
// TODO: split
}
}
}