From f02d2d2ee0baf50f0e8e274e768205dacd19783e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 3 Jun 2021 17:23:04 +0900 Subject: [PATCH] MeshExportInfo.GetInfo --- .../ExportDialog/ExporterExtensions.cs | 18 -- .../ExportDialog/ExporterExtensions.cs.meta | 11 -- .../ExportDialog/MeshExportValidator.cs | 139 +--------------- .../Runtime/Extensions/UnityExtensions.cs | 12 +- .../UniGLTF/IO/MeshIO/MeshExportInfo.cs | 155 ++++++++++++++++++ 5 files changed, 167 insertions(+), 168 deletions(-) delete mode 100644 Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs delete mode 100644 Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs deleted file mode 100644 index c10c09b87..000000000 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Linq; -using UnityEngine; - -namespace UniGLTF -{ - public static class ExporterExtensions - { - public static bool EnableForExport(this Component mono) - { - if (mono.transform.Ancestors().Any(x => !x.gameObject.activeSelf)) - { - // 自分か祖先に !activeSelf がいる - return false; - } - return true; - } - } -} diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs.meta deleted file mode 100644 index 821ce7fec..000000000 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExporterExtensions.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: f76ebfda1e249cb48890de41cc221889 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs index 918eee3c0..5adf5c53d 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs @@ -35,148 +35,11 @@ namespace UniGLTF public virtual bool UseBlendShape(int index, string relativePath) => true; - static void CalcMeshSize(ref MeshExportInfo info, - string relativePath, - MeshExportSettings settings, - Func useBlendShape - ) - { - var sb = new StringBuilder(); - if (!info.IsRendererActive) - { - sb.Append("[NotActive]"); - } - - info.VertexCount = info.Mesh.vertexCount; - info.ExportVertexSize = 0; - info.TotalBlendShapeCount = 0; - info.ExportBlendShapeCount = 0; - - // float4 x 3 - // vertices - sb.Append($"(Pos"); - if (info.HasNormal) - { - sb.Append("+Nom"); - info.ExportVertexSize += 4 * 3; - } - if (info.HasUV) - { - sb.Append("+UV"); - info.ExportVertexSize += 4 * 2; - } - if (info.HasVertexColor) - { - sb.Append("+Col"); - info.ExportVertexSize += 4 * 4; - } - if (info.HasSkinning) - { - // short, float x 4 weights - sb.Append("+Skin"); - info.ExportVertexSize += (2 + 4) * 4; - } - // indices - info.IndexCount = info.Mesh.triangles.Length; - - // postion + normal ?. always tangent is ignored - info.TotalBlendShapeCount = info.Mesh.blendShapeCount; - info.ExportBlendShapeVertexSize = settings.ExportOnlyBlendShapePosition ? 4 * 3 : 4 * (3 + 3); - for (var i = 0; i < info.Mesh.blendShapeCount; ++i) - { - if (!useBlendShape(i, relativePath)) - { - continue; - } - - ++info.ExportBlendShapeCount; - } - - if (info.ExportBlendShapeCount > 0) - { - sb.Append($"+Morph x {info.ExportBlendShapeCount}"); - } - sb.Append($") x {info.Mesh.vertexCount}"); - switch (info.VertexColor) - { - case VertexColorState.ExistsAndIsUsed: - case VertexColorState.ExistsAndMixed: // エクスポートする - sb.Insert(0, "[use vcolor]"); - break; - case VertexColorState.ExistsButNotUsed: - sb.Insert(0, "[remove vcolor]"); - break; - } - if (info.ExportBlendShapeCount > 0 && !info.HasSkinning) - { - sb.Insert(0, "[morph without skin]"); - } - - // total bytes - sb.Insert(0, $"{info.ExportByteSize:#,0} Bytes = "); - info.Summary = sb.ToString(); - } - - static bool TryGetMeshInfo(GameObject root, Renderer renderer, MeshExportSettings settings, Func useBlendShape, out MeshExportInfo info) - { - info = default; - if (root == null) - { - info.Summary = ""; - return false; - } - if (renderer == null) - { - info.Summary = "no Renderer"; - return false; - } - info.Renderer = renderer; - - if (renderer is SkinnedMeshRenderer smr) - { - info.Skinned = true; - info.Mesh = smr.sharedMesh; - info.IsRendererActive = smr.EnableForExport(); - } - else if (renderer is MeshRenderer mr) - { - var filter = mr.GetComponent(); - if (filter != null) - { - info.Mesh = filter.sharedMesh; - } - info.IsRendererActive = mr.EnableForExport(); - } - else - { - info.Summary = "no Mesh"; - return false; - } - - info.VertexColor = VertexColorUtility.DetectVertexColor(info.Mesh, info.Renderer.sharedMaterials); - - var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(renderer.transform, root.transform); - CalcMeshSize(ref info, relativePath, settings, useBlendShape); - - return true; - } - public void SetRoot(GameObject ExportRoot, MeshExportSettings settings) { Settings = settings; - Meshes.Clear(); - if (ExportRoot == null) - { - return; - } - foreach (var renderer in ExportRoot.GetComponentsInChildren(true)) - { - if (TryGetMeshInfo(ExportRoot, renderer, settings, UseBlendShape, out MeshExportInfo info)) - { - Meshes.Add(info); - } - } + MeshExportInfo.GetInfo(ExportRoot, Meshes, settings, UseBlendShape); } public Func GltfMaterialFromUnityShaderName = DefaultGltfMaterialType; diff --git a/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs index f51247633..698b6898f 100644 --- a/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs @@ -213,7 +213,7 @@ namespace UniGLTF { var current = self; - var split = path.Split(new [] {'/'}, StringSplitOptions.RemoveEmptyEntries); + var split = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (var childName in split) { @@ -334,5 +334,15 @@ namespace UniGLTF } return go.AddComponent(); } + + public static bool EnableForExport(this Component mono) + { + if (mono.transform.Ancestors().Any(x => !x.gameObject.activeSelf)) + { + // 自分か祖先に !activeSelf がいる + return false; + } + return true; + } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs index 768f27ce7..1e88899d6 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs @@ -1,8 +1,13 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; using UnityEngine; namespace UniGLTF { + public delegate bool UseBlendShapeFunc(int blendShapeIndex, string relativePath); + [Serializable] public struct MeshExportInfo { @@ -46,5 +51,155 @@ namespace UniGLTF public int ExportByteSize => ExportVertexSize * VertexCount + IndicesSize + ExportBlendShapeCount * ExportBlendShapeVertexSize * VertexCount; public string Summary; + + /// + /// ヒエラルキーからエクスポートする Mesh の情報を収集する + /// + /// + /// + /// + /// blendShape の export を filtering する + public static void GetInfo(GameObject exportRoot, List list, MeshExportSettings settings, UseBlendShapeFunc useBlendShape) + { + list.Clear(); + if (exportRoot == null) + { + return; + } + + foreach (var renderer in exportRoot.GetComponentsInChildren(true)) + { + if (TryGetMeshInfo(exportRoot, renderer, settings, useBlendShape, out MeshExportInfo info)) + { + list.Add(info); + } + } + } + + static bool TryGetMeshInfo(GameObject root, Renderer renderer, MeshExportSettings settings, UseBlendShapeFunc useBlendShape, out MeshExportInfo info) + { + info = default; + if (root == null) + { + info.Summary = ""; + return false; + } + if (renderer == null) + { + info.Summary = "no Renderer"; + return false; + } + info.Renderer = renderer; + + if (renderer is SkinnedMeshRenderer smr) + { + info.Skinned = true; + info.Mesh = smr.sharedMesh; + info.IsRendererActive = smr.EnableForExport(); + } + else if (renderer is MeshRenderer mr) + { + var filter = mr.GetComponent(); + if (filter != null) + { + info.Mesh = filter.sharedMesh; + } + info.IsRendererActive = mr.EnableForExport(); + } + else + { + info.Summary = "no Mesh"; + return false; + } + + info.VertexColor = VertexColorUtility.DetectVertexColor(info.Mesh, info.Renderer.sharedMaterials); + + var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(renderer.transform, root.transform); + CalcMeshSize(ref info, relativePath, settings, useBlendShape); + + return true; + } + + static void CalcMeshSize(ref MeshExportInfo info, + string relativePath, + MeshExportSettings settings, + UseBlendShapeFunc useBlendShape + ) + { + var sb = new StringBuilder(); + if (!info.IsRendererActive) + { + sb.Append("[NotActive]"); + } + + info.VertexCount = info.Mesh.vertexCount; + info.ExportVertexSize = 0; + info.TotalBlendShapeCount = 0; + info.ExportBlendShapeCount = 0; + + // float4 x 3 + // vertices + sb.Append($"(Pos"); + if (info.HasNormal) + { + sb.Append("+Nom"); + info.ExportVertexSize += 4 * 3; + } + if (info.HasUV) + { + sb.Append("+UV"); + info.ExportVertexSize += 4 * 2; + } + if (info.HasVertexColor) + { + sb.Append("+Col"); + info.ExportVertexSize += 4 * 4; + } + if (info.HasSkinning) + { + // short, float x 4 weights + sb.Append("+Skin"); + info.ExportVertexSize += (2 + 4) * 4; + } + // indices + info.IndexCount = info.Mesh.triangles.Length; + + // postion + normal ?. always tangent is ignored + info.TotalBlendShapeCount = info.Mesh.blendShapeCount; + info.ExportBlendShapeVertexSize = settings.ExportOnlyBlendShapePosition ? 4 * 3 : 4 * (3 + 3); + for (var i = 0; i < info.Mesh.blendShapeCount; ++i) + { + if (!useBlendShape(i, relativePath)) + { + continue; + } + + ++info.ExportBlendShapeCount; + } + + if (info.ExportBlendShapeCount > 0) + { + sb.Append($"+Morph x {info.ExportBlendShapeCount}"); + } + sb.Append($") x {info.Mesh.vertexCount}"); + switch (info.VertexColor) + { + case VertexColorState.ExistsAndIsUsed: + case VertexColorState.ExistsAndMixed: // エクスポートする + sb.Insert(0, "[use vcolor]"); + break; + case VertexColorState.ExistsButNotUsed: + sb.Insert(0, "[remove vcolor]"); + break; + } + if (info.ExportBlendShapeCount > 0 && !info.HasSkinning) + { + sb.Insert(0, "[morph without skin]"); + } + + // total bytes + sb.Insert(0, $"{info.ExportByteSize:#,0} Bytes = "); + info.Summary = sb.ToString(); + } } }