diff --git a/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs b/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs index 90cd2cf84..8ec486aee 100644 --- a/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs +++ b/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs @@ -4,6 +4,9 @@ using Unity.Collections; using Unity.Jobs; using UnityEngine; using UnityEngine.Profiling; +using UnityEngine.Rendering; +using VrmLib; +using Mesh = UnityEngine.Mesh; namespace UniVRM10 { @@ -19,17 +22,13 @@ namespace UniVRM10 { Profiler.BeginSample("MeshImporter.LoadSharedMesh"); var mesh = new Mesh(); - if (src.IndexBuffer.Count > ushort.MaxValue) - { - mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; - } - var positions = src.VertexBuffer.Positions.GetNativeArray(Allocator.TempJob); - var normals = src.VertexBuffer.Normals?.GetNativeArray(Allocator.TempJob) ?? default; - var texCoords = src.VertexBuffer.TexCoords?.GetNativeArray(Allocator.TempJob) ?? default; - var colors = src.VertexBuffer.Colors?.GetNativeArray(Allocator.TempJob) ?? default; - var weights = src.VertexBuffer.Weights?.GetNativeArray(Allocator.TempJob) ?? default; - var joints = src.VertexBuffer.Joints?.GetNativeArray(Allocator.TempJob) ?? default; + var positions = src.VertexBuffer.Positions.GetAsNativeArray(Allocator.TempJob); + var normals = src.VertexBuffer.Normals?.GetAsNativeArray(Allocator.TempJob) ?? default; + var texCoords = src.VertexBuffer.TexCoords?.GetAsNativeArray(Allocator.TempJob) ?? default; + var colors = src.VertexBuffer.Colors?.GetAsNativeArray(Allocator.TempJob) ?? default; + var weights = src.VertexBuffer.Weights?.GetAsNativeArray(Allocator.TempJob) ?? default; + var joints = src.VertexBuffer.Joints?.GetAsNativeArray(Allocator.TempJob) ?? default; var vertices = new NativeArray(positions.Length, Allocator.TempJob); @@ -69,15 +68,34 @@ namespace UniVRM10 // 出力のNativeArrayを開放 vertices.Dispose(); - // submesh 方式 - mesh.subMeshCount = src.Submeshes.Count; - var triangles = src.IndexBuffer.GetAsIntList(); - for (var i = 0; i < src.Submeshes.Count; ++i) + // Indexを更新 + switch (src.IndexBuffer.ComponentType) { - var submesh = src.Submeshes[i]; - mesh.SetTriangles(triangles.GetRange(submesh.Offset, submesh.DrawCount), i); + case AccessorValueType.UNSIGNED_SHORT: + var shortIndices = src.IndexBuffer.GetAsNativeArray(Allocator.Temp); + mesh.SetIndexBufferParams(shortIndices.Length, IndexFormat.UInt16); + mesh.SetIndexBufferData(shortIndices, 0, 0, shortIndices.Length); + shortIndices.Dispose(); + break; + case AccessorValueType.UNSIGNED_INT: + var intIndices = src.IndexBuffer.GetAsNativeArray(Allocator.Temp); + mesh.SetIndexBufferParams(intIndices.Length, IndexFormat.UInt32); + mesh.SetIndexBufferData(intIndices, 0, 0, intIndices.Length); + intIndices.Dispose(); + break; + default: + throw new NotImplementedException(); } + // SubMeshを更新 + mesh.subMeshCount = src.Submeshes.Count; + for (var i = 0; i < src.Submeshes.Count; ++i) + { + var subMesh = src.Submeshes[i]; + mesh.SetSubMesh(i, new SubMeshDescriptor(subMesh.Offset, subMesh.DrawCount)); + } + + // MorphTargetを更新 foreach (var morphTarget in src.MorphTargets) { var morphTargetPositions = @@ -88,6 +106,7 @@ namespace UniVRM10 mesh.AddBlendShapeFrame(morphTarget.Name, 100.0f, morphTargetPositions, null, null); } + // 各種パラメーターを再計算 mesh.RecalculateBounds(); mesh.RecalculateTangents(); diff --git a/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs b/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs index 90b6e5405..ac46dab04 100644 --- a/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs +++ b/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs @@ -109,7 +109,7 @@ namespace VrmLib /// バッファをNativeArrayに変換して返す /// 開放の責務は使い手側にある点に注意 /// - public unsafe NativeArray GetNativeArray(Allocator allocator, bool checkStride = true) where T : struct + public unsafe NativeArray GetAsNativeArray(Allocator allocator, bool checkStride = true) where T : struct { if (checkStride && Marshal.SizeOf(typeof(T)) != Stride) {