From 9ad556abcc0a546a8baff6c1dfcb8e4351dc327e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:09:11 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=E3=83=A1=E3=83=B3=E3=83=90=E3=83=BC?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E3=82=8F=E3=81=AA=E3=81=84=20MeshImporter=20?= =?UTF-8?q?=E3=82=92=20static=20class=20=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs | 3 +-- Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 003c8063e..cb281bf96 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -167,7 +167,6 @@ namespace UniGLTF { var inverter = InvertAxis.Create(); - var meshImporter = new MeshImporter(); if (GLTF.meshes.Count > 0) { for (var i = 0; i < GLTF.meshes.Count; ++i) @@ -175,7 +174,7 @@ namespace UniGLTF var index = i; using (MeasureTime("ReadMesh")) { - var meshContext = await awaitCaller.Run(() => meshImporter.ReadMesh(Data, index, inverter)); + var meshContext = await awaitCaller.Run(() => MeshImporter.ReadMesh(Data, index, inverter)); var meshWithMaterials = await BuildMeshAsync(awaitCaller, MeasureTime, meshContext, index); Meshes.Add(meshWithMaterials); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs index 5fcb06ca1..aa726b807 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs @@ -7,7 +7,7 @@ using VRMShaders; namespace UniGLTF { - public class MeshImporter + public static class MeshImporter { private const float FrameWeight = 100.0f; @@ -29,7 +29,7 @@ namespace UniGLTF return sharedAttributes; } - internal MeshContext ReadMesh(GltfData data, int meshIndex, IAxisInverter inverter) + internal static MeshContext ReadMesh(GltfData data, int meshIndex, IAxisInverter inverter) { Profiler.BeginSample("ReadMesh"); var gltfMesh = data.GLTF.meshes[meshIndex]; From 94b0928d5acbcef78950a226077e07521495bd6e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:20:14 +0900 Subject: [PATCH 2/9] =?UTF-8?q?MeshImporter=20=E3=82=92=E6=95=B4=E7=90=86?= =?UTF-8?q?=E3=80=82=E6=A9=9F=E8=83=BD=E3=82=92=20ImporterContext=20?= =?UTF-8?q?=E3=81=A8=20MeshContext=20=E3=81=AB=E5=A7=94=E8=AD=B2=E3=81=97?= =?UTF-8?q?=E3=81=A6=E6=B6=88=E6=BB=85=E3=81=95=E3=81=9B=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO/ImporterContext.cs | 47 ++++- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 114 ++++++++++++ .../Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs | 165 ------------------ .../UniGLTF/IO/MeshIO/MeshImporter.cs.meta | 11 -- 4 files changed, 158 insertions(+), 179 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index cb281bf96..0efb50d7a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -163,6 +163,47 @@ namespace UniGLTF await awaitCaller.NextFrame(); } + private static bool HasSharedVertexBuffer(glTFMesh gltfMesh) + { + glTFAttributes lastAttributes = null; + var sharedAttributes = true; + foreach (var prim in gltfMesh.primitives) + { + if (lastAttributes != null && !prim.attributes.Equals(lastAttributes)) + { + sharedAttributes = false; + break; + } + + lastAttributes = prim.attributes; + } + + return sharedAttributes; + } + + private static MeshContext ReadMesh(GltfData data, int meshIndex, IAxisInverter inverter) + { + Profiler.BeginSample("ReadMesh"); + var gltfMesh = data.GLTF.meshes[meshIndex]; + + var meshContext = new MeshContext(gltfMesh.name, meshIndex); + if (HasSharedVertexBuffer(gltfMesh)) + { + meshContext.ImportMeshSharingVertexBuffer(data, gltfMesh, inverter); + } + else + { + meshContext.ImportMeshIndependentVertexBuffer(data, gltfMesh, inverter); + } + + meshContext.RenameBlendShape(gltfMesh); + + meshContext.DropUnusedVertices(); + + Profiler.EndSample(); + return meshContext; + } + protected virtual async Task LoadGeometryAsync(IAwaitCaller awaitCaller, Func MeasureTime) { var inverter = InvertAxis.Create(); @@ -174,7 +215,7 @@ namespace UniGLTF var index = i; using (MeasureTime("ReadMesh")) { - var meshContext = await awaitCaller.Run(() => MeshImporter.ReadMesh(Data, index, inverter)); + var meshContext = await awaitCaller.Run(() => ReadMesh(Data, index, inverter)); var meshWithMaterials = await BuildMeshAsync(awaitCaller, MeasureTime, meshContext, index); Meshes.Add(meshWithMaterials); } @@ -289,11 +330,11 @@ namespace UniGLTF return Task.FromResult(null); } - async Task BuildMeshAsync(IAwaitCaller awaitCaller, Func MeasureTime, MeshContext x, int i) + async Task BuildMeshAsync(IAwaitCaller awaitCaller, Func MeasureTime, MeshContext meshContext, int i) { using (MeasureTime("BuildMesh")) { - var meshWithMaterials = await MeshImporter.BuildMeshAsync(awaitCaller, MaterialFactory.GetMaterial, x); + var meshWithMaterials = await meshContext.BuildMeshAsync(awaitCaller, MaterialFactory.GetMaterial); var mesh = meshWithMaterials.Mesh; // mesh name diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index 48e54d0a9..b4fb885e3 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -1,14 +1,18 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using UnityEngine; using UnityEngine.Profiling; using UnityEngine.Rendering; +using VRMShaders; namespace UniGLTF { internal class MeshContext { + private const float FrameWeight = 100.0f; + private readonly List _vertices = new List(); private readonly List _skinnedMeshVertices = new List(); private readonly List _indices = new List(); @@ -537,5 +541,115 @@ namespace UniGLTF Profiler.EndSample(); } + + private (Mesh, bool) BuildMesh() + { + this.AddDefaultMaterial(); + + //Debug.Log(prims.ToJson()); + var mesh = new Mesh + { + name = this.Name + }; + + this.UploadMeshVertices(mesh); + this.UploadMeshIndices(mesh); + + // NOTE: mesh.vertices では自動的に行われていたが、SetVertexBuffer では行われないため、明示的に呼び出す. + mesh.RecalculateBounds(); + + if (!this.HasNormal) + { + mesh.RecalculateNormals(); + } + + return (mesh, true); + } + + private static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, BlendShape blendShape, + Vector3[] emptyVertices) + { + Vector3[] positions = null; + Vector3[] normals = null; + await awaitCaller.Run(() => + { + positions = blendShape.Positions.ToArray(); + if (blendShape.Normals != null) + { + normals = blendShape.Normals.ToArray(); + } + }); + + Profiler.BeginSample("MeshImporter.BuildBlendShapeAsync"); + if (blendShape.Positions.Count > 0) + { + if (blendShape.Positions.Count == mesh.vertexCount) + { + mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight, + blendShape.Positions.ToArray(), + normals.Length == mesh.vertexCount && normals.Length == positions.Length ? normals : null, + null + ); + } + else + { + Debug.LogWarningFormat( + "May be partial primitive has blendShape. Require separate mesh or extend blend shape, but not implemented: {0}", + blendShape.Name); + } + } + else + { + // Debug.LogFormat("empty blendshape: {0}.{1}", mesh.name, blendShape.Name); + // add empty blend shape for keep blend shape index + mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight, + emptyVertices, + null, + null + ); + } + + Profiler.EndSample(); + } + + public async Task BuildMeshAsync( + IAwaitCaller awaitCaller, + Func ctx) + { + Profiler.BeginSample("MeshImporter.BuildMesh"); + var (mesh, recalculateTangents) = this.BuildMesh(); + Profiler.EndSample(); + + if (recalculateTangents) + { + await awaitCaller.NextFrame(); + mesh.RecalculateTangents(); + await awaitCaller.NextFrame(); + } + + // 先にすべてのマテリアルを作成済みなのでテクスチャーは生成済み。Resultを使ってよい + var result = new MeshWithMaterials + { + Mesh = mesh, + Materials = this.MaterialIndices.Select(ctx).ToArray() + }; + + await awaitCaller.NextFrame(); + if (this.BlendShapes.Count > 0) + { + var emptyVertices = new Vector3[mesh.vertexCount]; + foreach (var blendShape in this.BlendShapes) + { + await BuildBlendShapeAsync(awaitCaller, mesh, blendShape, emptyVertices); + } + } + + Profiler.BeginSample("Mesh.UploadMeshData"); + mesh.UploadMeshData(false); + Profiler.EndSample(); + + return result; + } + } } \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs deleted file mode 100644 index aa726b807..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs +++ /dev/null @@ -1,165 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using UnityEngine; -using UnityEngine.Profiling; -using VRMShaders; - -namespace UniGLTF -{ - public static class MeshImporter - { - private const float FrameWeight = 100.0f; - - private static bool HasSharedVertexBuffer(glTFMesh gltfMesh) - { - glTFAttributes lastAttributes = null; - var sharedAttributes = true; - foreach (var prim in gltfMesh.primitives) - { - if (lastAttributes != null && !prim.attributes.Equals(lastAttributes)) - { - sharedAttributes = false; - break; - } - - lastAttributes = prim.attributes; - } - - return sharedAttributes; - } - - internal static MeshContext ReadMesh(GltfData data, int meshIndex, IAxisInverter inverter) - { - Profiler.BeginSample("ReadMesh"); - var gltfMesh = data.GLTF.meshes[meshIndex]; - - var meshContext = new MeshContext(gltfMesh.name, meshIndex); - if (HasSharedVertexBuffer(gltfMesh)) - { - meshContext.ImportMeshSharingVertexBuffer(data, gltfMesh, inverter); - } - else - { - meshContext.ImportMeshIndependentVertexBuffer(data, gltfMesh, inverter); - } - - meshContext.RenameBlendShape(gltfMesh); - - meshContext.DropUnusedVertices(); - - Profiler.EndSample(); - return meshContext; - } - - private static (Mesh, bool) BuildMesh(MeshContext meshContext) - { - meshContext.AddDefaultMaterial(); - - //Debug.Log(prims.ToJson()); - var mesh = new Mesh - { - name = meshContext.Name - }; - - meshContext.UploadMeshVertices(mesh); - meshContext.UploadMeshIndices(mesh); - - // NOTE: mesh.vertices では自動的に行われていたが、SetVertexBuffer では行われないため、明示的に呼び出す. - mesh.RecalculateBounds(); - - if (!meshContext.HasNormal) - { - mesh.RecalculateNormals(); - } - - return (mesh, true); - } - - private static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, BlendShape blendShape, - Vector3[] emptyVertices) - { - Vector3[] positions = null; - Vector3[] normals = null; - await awaitCaller.Run(() => - { - positions = blendShape.Positions.ToArray(); - if (blendShape.Normals != null) - { - normals = blendShape.Normals.ToArray(); - } - }); - - Profiler.BeginSample("MeshImporter.BuildBlendShapeAsync"); - if (blendShape.Positions.Count > 0) - { - if (blendShape.Positions.Count == mesh.vertexCount) - { - mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight, - blendShape.Positions.ToArray(), - normals.Length == mesh.vertexCount && normals.Length == positions.Length ? normals : null, - null - ); - } - else - { - Debug.LogWarningFormat( - "May be partial primitive has blendShape. Require separate mesh or extend blend shape, but not implemented: {0}", - blendShape.Name); - } - } - else - { - // Debug.LogFormat("empty blendshape: {0}.{1}", mesh.name, blendShape.Name); - // add empty blend shape for keep blend shape index - mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight, - emptyVertices, - null, - null - ); - } - - Profiler.EndSample(); - } - - internal static async Task BuildMeshAsync( - IAwaitCaller awaitCaller, - Func ctx, - MeshContext meshContext) - { - Profiler.BeginSample("MeshImporter.BuildMesh"); - var (mesh, recalculateTangents) = BuildMesh(meshContext); - Profiler.EndSample(); - - if (recalculateTangents) - { - await awaitCaller.NextFrame(); - mesh.RecalculateTangents(); - await awaitCaller.NextFrame(); - } - - // 先にすべてのマテリアルを作成済みなのでテクスチャーは生成済み。Resultを使ってよい - var result = new MeshWithMaterials - { - Mesh = mesh, - Materials = meshContext.MaterialIndices.Select(ctx).ToArray() - }; - - await awaitCaller.NextFrame(); - if (meshContext.BlendShapes.Count > 0) - { - var emptyVertices = new Vector3[mesh.vertexCount]; - foreach (var blendShape in meshContext.BlendShapes) - { - await BuildBlendShapeAsync(awaitCaller, mesh, blendShape, emptyVertices); - } - } - - Profiler.BeginSample("Mesh.UploadMeshData"); - mesh.UploadMeshData(false); - Profiler.EndSample(); - - return result; - } - } -} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs.meta deleted file mode 100644 index 89f3ce9b8..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshImporter.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: d9658f6e46942b146ad7e752c6592401 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: From a8277c45e7d844ffbf79039cb172c34393a9ce75 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:30:26 +0900 Subject: [PATCH 3/9] =?UTF-8?q?MeshContext=20=E3=81=AB=E9=96=A2=E9=80=A3?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92?= =?UTF-8?q?=E9=9B=86=E7=B4=84=E3=80=82=E4=B8=8D=E8=A6=81=E3=81=AA=20public?= =?UTF-8?q?=20=E3=82=92=20private=20=E3=81=AB=E3=81=97=E3=81=9F=E3=80=82?= =?UTF-8?q?=E8=B2=AC=E5=8B=99=E3=81=8C=E6=98=8E=E7=A2=BA=E3=81=AB=E3=81=AA?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * public CreateFromGltf * public BuildMeshAsync --- .../Runtime/UniGLTF/IO/ImporterContext.cs | 43 +--------- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 85 ++++++++++++++----- 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 0efb50d7a..8ff75af30 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -163,47 +163,6 @@ namespace UniGLTF await awaitCaller.NextFrame(); } - private static bool HasSharedVertexBuffer(glTFMesh gltfMesh) - { - glTFAttributes lastAttributes = null; - var sharedAttributes = true; - foreach (var prim in gltfMesh.primitives) - { - if (lastAttributes != null && !prim.attributes.Equals(lastAttributes)) - { - sharedAttributes = false; - break; - } - - lastAttributes = prim.attributes; - } - - return sharedAttributes; - } - - private static MeshContext ReadMesh(GltfData data, int meshIndex, IAxisInverter inverter) - { - Profiler.BeginSample("ReadMesh"); - var gltfMesh = data.GLTF.meshes[meshIndex]; - - var meshContext = new MeshContext(gltfMesh.name, meshIndex); - if (HasSharedVertexBuffer(gltfMesh)) - { - meshContext.ImportMeshSharingVertexBuffer(data, gltfMesh, inverter); - } - else - { - meshContext.ImportMeshIndependentVertexBuffer(data, gltfMesh, inverter); - } - - meshContext.RenameBlendShape(gltfMesh); - - meshContext.DropUnusedVertices(); - - Profiler.EndSample(); - return meshContext; - } - protected virtual async Task LoadGeometryAsync(IAwaitCaller awaitCaller, Func MeasureTime) { var inverter = InvertAxis.Create(); @@ -215,7 +174,7 @@ namespace UniGLTF var index = i; using (MeasureTime("ReadMesh")) { - var meshContext = await awaitCaller.Run(() => ReadMesh(Data, index, inverter)); + var meshContext = await awaitCaller.Run(() => MeshContext.CreateFromGltf(Data, index, inverter)); var meshWithMaterials = await BuildMeshAsync(awaitCaller, MeasureTime, meshContext, index); Meshes.Add(meshWithMaterials); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index b4fb885e3..a30dab94d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -20,12 +20,63 @@ namespace UniGLTF private readonly List _materialIndices = new List(); private readonly List _blendShapes = new List(); - public IReadOnlyList MaterialIndices => _materialIndices; - public IReadOnlyList BlendShapes => _blendShapes; + private IReadOnlyList MaterialIndices => _materialIndices; + private IReadOnlyList BlendShapes => _blendShapes; - public bool HasNormal { get; private set; } = true; + private bool HasNormal { get; set; } = true; - public string Name { get; } + private string Name { get; } + + MeshContext(string name, int meshIndex) + { + if (string.IsNullOrEmpty(name)) + { + name = $"UniGLTF import#{meshIndex}"; + } + + Name = name; + } + + private static bool HasSharedVertexBuffer(glTFMesh gltfMesh) + { + glTFAttributes lastAttributes = null; + var sharedAttributes = true; + foreach (var prim in gltfMesh.primitives) + { + if (lastAttributes != null && !prim.attributes.Equals(lastAttributes)) + { + sharedAttributes = false; + break; + } + + lastAttributes = prim.attributes; + } + + return sharedAttributes; + } + + public static MeshContext CreateFromGltf(GltfData data, int meshIndex, IAxisInverter inverter) + { + Profiler.BeginSample("ReadMesh"); + var gltfMesh = data.GLTF.meshes[meshIndex]; + + var meshContext = new MeshContext(gltfMesh.name, meshIndex); + if (HasSharedVertexBuffer(gltfMesh)) + { + meshContext.ImportMeshSharingVertexBuffer(data, gltfMesh, inverter); + } + else + { + meshContext.ImportMeshIndependentVertexBuffer(data, gltfMesh, inverter); + } + + meshContext.RenameBlendShape(gltfMesh); + + meshContext.DropUnusedVertices(); + + Profiler.EndSample(); + return meshContext; + } /// /// * flip triangle @@ -33,7 +84,7 @@ namespace UniGLTF /// /// /// - void PushIndices(BufferAccessor src, int offset) + private void PushIndices(BufferAccessor src, int offset) { switch (src.ComponentType) { @@ -83,7 +134,7 @@ namespace UniGLTF /// 頂点情報をMeshに対して送る /// /// - public void UploadMeshVertices(Mesh mesh) + private void UploadMeshVertices(Mesh mesh) { var vertexAttributeDescriptor = MeshVertex.GetVertexAttributeDescriptor(); @@ -113,7 +164,7 @@ namespace UniGLTF /// インデックス情報をMeshに対して送る /// /// - public void UploadMeshIndices(Mesh mesh) + private void UploadMeshIndices(Mesh mesh) { mesh.SetIndexBufferParams(_indices.Count, IndexFormat.UInt32); mesh.SetIndexBufferData(_indices, 0, 0, _indices.Count); @@ -141,16 +192,6 @@ namespace UniGLTF return blendShape; } - public MeshContext(string name, int meshIndex) - { - if (string.IsNullOrEmpty(name)) - { - name = $"UniGLTF import#{meshIndex}"; - } - - Name = name; - } - private static (float x, float y, float z, float w) NormalizeBoneWeight( (float x, float y, float z, float w) src) { @@ -199,7 +240,7 @@ namespace UniGLTF /// /// /// - public void ImportMeshIndependentVertexBuffer(GltfData data, glTFMesh gltfMesh, IAxisInverter inverter) + private void ImportMeshIndependentVertexBuffer(GltfData data, glTFMesh gltfMesh, IAxisInverter inverter) { (_vertices.Capacity, _indices.Capacity) = GetCapacity(data, gltfMesh); @@ -348,7 +389,7 @@ namespace UniGLTF /// /// /// - public void ImportMeshSharingVertexBuffer(GltfData data, glTFMesh gltfMesh, IAxisInverter inverter) + private void ImportMeshSharingVertexBuffer(GltfData data, glTFMesh gltfMesh, IAxisInverter inverter) { (_vertices.Capacity, _indices.Capacity) = GetCapacity(data, gltfMesh); @@ -482,7 +523,7 @@ namespace UniGLTF } } - public void RenameBlendShape(glTFMesh gltfMesh) + private void RenameBlendShape(glTFMesh gltfMesh) { if (!gltf_mesh_extras_targetNames.TryGet(gltfMesh, out var targetNames)) return; for (var i = 0; i < BlendShapes.Count; i++) @@ -512,7 +553,7 @@ namespace UniGLTF } } - public void AddDefaultMaterial() + private void AddDefaultMaterial() { if (!_materialIndices.Any()) { @@ -526,7 +567,7 @@ namespace UniGLTF /// /// VertexBuffer の後ろに未使用頂点がある場合に削除する /// - public void DropUnusedVertices() + private void DropUnusedVertices() { Profiler.BeginSample("MeshContext.DropUnusedVertices"); var maxIndex = _indices.Max(); From 4e654a9ca144e257cba936e681ac2447dd3d3d79 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:34:54 +0900 Subject: [PATCH 4/9] remove this. --- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index a30dab94d..a9271c5ec 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -585,21 +585,21 @@ namespace UniGLTF private (Mesh, bool) BuildMesh() { - this.AddDefaultMaterial(); + AddDefaultMaterial(); //Debug.Log(prims.ToJson()); var mesh = new Mesh { - name = this.Name + name = Name }; - this.UploadMeshVertices(mesh); - this.UploadMeshIndices(mesh); + UploadMeshVertices(mesh); + UploadMeshIndices(mesh); // NOTE: mesh.vertices では自動的に行われていたが、SetVertexBuffer では行われないため、明示的に呼び出す. mesh.RecalculateBounds(); - if (!this.HasNormal) + if (!HasNormal) { mesh.RecalculateNormals(); } @@ -658,7 +658,7 @@ namespace UniGLTF Func ctx) { Profiler.BeginSample("MeshImporter.BuildMesh"); - var (mesh, recalculateTangents) = this.BuildMesh(); + var (mesh, recalculateTangents) = BuildMesh(); Profiler.EndSample(); if (recalculateTangents) @@ -672,14 +672,14 @@ namespace UniGLTF var result = new MeshWithMaterials { Mesh = mesh, - Materials = this.MaterialIndices.Select(ctx).ToArray() + Materials = MaterialIndices.Select(ctx).ToArray() }; await awaitCaller.NextFrame(); - if (this.BlendShapes.Count > 0) + if (BlendShapes.Count > 0) { var emptyVertices = new Vector3[mesh.vertexCount]; - foreach (var blendShape in this.BlendShapes) + foreach (var blendShape in BlendShapes) { await BuildBlendShapeAsync(awaitCaller, mesh, blendShape, emptyVertices); } From 66ad6b372e920faf6245ddd14aaddc4feb02421b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:38:09 +0900 Subject: [PATCH 5/9] =?UTF-8?q?BuildMesh=20=E9=96=A2=E6=95=B0=E5=B1=95?= =?UTF-8?q?=E9=96=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index a9271c5ec..f9451a115 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -583,30 +583,6 @@ namespace UniGLTF Profiler.EndSample(); } - private (Mesh, bool) BuildMesh() - { - AddDefaultMaterial(); - - //Debug.Log(prims.ToJson()); - var mesh = new Mesh - { - name = Name - }; - - UploadMeshVertices(mesh); - UploadMeshIndices(mesh); - - // NOTE: mesh.vertices では自動的に行われていたが、SetVertexBuffer では行われないため、明示的に呼び出す. - mesh.RecalculateBounds(); - - if (!HasNormal) - { - mesh.RecalculateNormals(); - } - - return (mesh, true); - } - private static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, BlendShape blendShape, Vector3[] emptyVertices) { @@ -658,15 +634,30 @@ namespace UniGLTF Func ctx) { Profiler.BeginSample("MeshImporter.BuildMesh"); - var (mesh, recalculateTangents) = BuildMesh(); + AddDefaultMaterial(); + + //Debug.Log(prims.ToJson()); + var mesh = new Mesh + { + name = Name + }; + + UploadMeshVertices(mesh); + UploadMeshIndices(mesh); + + // NOTE: mesh.vertices では自動的に行われていたが、SetVertexBuffer では行われないため、明示的に呼び出す. + mesh.RecalculateBounds(); + + if (!HasNormal) + { + mesh.RecalculateNormals(); + } Profiler.EndSample(); - if (recalculateTangents) - { - await awaitCaller.NextFrame(); - mesh.RecalculateTangents(); - await awaitCaller.NextFrame(); - } + // RecalculateTangents + await awaitCaller.NextFrame(); + mesh.RecalculateTangents(); + await awaitCaller.NextFrame(); // 先にすべてのマテリアルを作成済みなのでテクスチャーは生成済み。Resultを使ってよい var result = new MeshWithMaterials From 696b0c8e9d2c638bbf4011305b6616f4840f042e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:40:28 +0900 Subject: [PATCH 6/9] =?UTF-8?q?await=20awaitCaller.NextFrame()=E3=80=80?= =?UTF-8?q?=E3=82=92=E5=A2=97=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index f9451a115..7acfa7435 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -643,19 +643,22 @@ namespace UniGLTF }; UploadMeshVertices(mesh); + await awaitCaller.NextFrame(); + UploadMeshIndices(mesh); + await awaitCaller.NextFrame(); // NOTE: mesh.vertices では自動的に行われていたが、SetVertexBuffer では行われないため、明示的に呼び出す. mesh.RecalculateBounds(); + await awaitCaller.NextFrame(); if (!HasNormal) { mesh.RecalculateNormals(); + await awaitCaller.NextFrame(); } - Profiler.EndSample(); // RecalculateTangents - await awaitCaller.NextFrame(); mesh.RecalculateTangents(); await awaitCaller.NextFrame(); @@ -665,8 +668,8 @@ namespace UniGLTF Mesh = mesh, Materials = MaterialIndices.Select(ctx).ToArray() }; - await awaitCaller.NextFrame(); + if (BlendShapes.Count > 0) { var emptyVertices = new Vector3[mesh.vertexCount]; @@ -675,6 +678,7 @@ namespace UniGLTF await BuildBlendShapeAsync(awaitCaller, mesh, blendShape, emptyVertices); } } + Profiler.EndSample(); Profiler.BeginSample("Mesh.UploadMeshData"); mesh.UploadMeshData(false); From a4bcc3b7b09822eafe0d43d83e45ea68de306a3b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:47:07 +0900 Subject: [PATCH 7/9] comment --- Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index 7acfa7435..58f8f7035 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -658,7 +658,6 @@ namespace UniGLTF await awaitCaller.NextFrame(); } - // RecalculateTangents mesh.RecalculateTangents(); await awaitCaller.NextFrame(); From 4688ba7c1b1d3c3dcc65646f06aec4b196175d65 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Jun 2022 16:49:03 +0900 Subject: [PATCH 8/9] =?UTF-8?q?=E5=A4=89=E6=95=B0=E5=90=8D=E3=80=82?= =?UTF-8?q?=E3=82=88=E3=81=8F=E3=82=8F=E3=81=8B=E3=82=89=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index 58f8f7035..ee291fd7c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -631,7 +631,7 @@ namespace UniGLTF public async Task BuildMeshAsync( IAwaitCaller awaitCaller, - Func ctx) + Func materialFromIndex) { Profiler.BeginSample("MeshImporter.BuildMesh"); AddDefaultMaterial(); @@ -661,11 +661,10 @@ namespace UniGLTF mesh.RecalculateTangents(); await awaitCaller.NextFrame(); - // 先にすべてのマテリアルを作成済みなのでテクスチャーは生成済み。Resultを使ってよい var result = new MeshWithMaterials { Mesh = mesh, - Materials = MaterialIndices.Select(ctx).ToArray() + Materials = MaterialIndices.Select(materialFromIndex).ToArray() }; await awaitCaller.NextFrame(); @@ -685,6 +684,5 @@ namespace UniGLTF return result; } - } -} \ No newline at end of file +} From 5263d6a890c0e3fe892dc9c9349ee19872d3f6b1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 8 Jun 2022 14:45:07 +0900 Subject: [PATCH 9/9] remove MaterialIndices, BlendShapes --- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index ee291fd7c..7794c92c2 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -20,9 +20,6 @@ namespace UniGLTF private readonly List _materialIndices = new List(); private readonly List _blendShapes = new List(); - private IReadOnlyList MaterialIndices => _materialIndices; - private IReadOnlyList BlendShapes => _blendShapes; - private bool HasNormal { get; set; } = true; private string Name { get; } @@ -526,7 +523,7 @@ namespace UniGLTF private void RenameBlendShape(glTFMesh gltfMesh) { if (!gltf_mesh_extras_targetNames.TryGet(gltfMesh, out var targetNames)) return; - for (var i = 0; i < BlendShapes.Count; i++) + for (var i = 0; i < _blendShapes.Count; i++) { if (i >= targetNames.Count) { @@ -534,7 +531,7 @@ namespace UniGLTF break; } - BlendShapes[i].Name = targetNames[i]; + _blendShapes[i].Name = targetNames[i]; } } @@ -664,14 +661,14 @@ namespace UniGLTF var result = new MeshWithMaterials { Mesh = mesh, - Materials = MaterialIndices.Select(materialFromIndex).ToArray() + Materials = _materialIndices.Select(materialFromIndex).ToArray() }; await awaitCaller.NextFrame(); - if (BlendShapes.Count > 0) + if (_blendShapes.Count > 0) { var emptyVertices = new Vector3[mesh.vertexCount]; - foreach (var blendShape in BlendShapes) + foreach (var blendShape in _blendShapes) { await BuildBlendShapeAsync(awaitCaller, mesh, blendShape, emptyVertices); }