From 35359a6dc350856012a9354fc8f4409d373e4f72 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 16:04:59 +0900 Subject: [PATCH 01/10] add GltfValidator test --- Assets/VRM10/Tests/MigrationTests.cs | 41 ++++++++++++++++++- .../VRMShaders/GLTF/IO/Editor/PathObject.cs | 14 +++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Assets/VRM10/Tests/MigrationTests.cs b/Assets/VRM10/Tests/MigrationTests.cs index 84c35e32c..815d36197 100644 --- a/Assets/VRM10/Tests/MigrationTests.cs +++ b/Assets/VRM10/Tests/MigrationTests.cs @@ -257,11 +257,48 @@ namespace UniVRM10 [Test] public void MigrateMeta() { + using (var data = new GlbFileParser(AliciaPath).Parse()) + { + using (var migrated = Vrm10Data.Migrate(data, out Vrm10Data vrm, out MigrationData migration)) + { + Assert.NotNull(vrm); + Assert.NotNull(migration); + } + } + } + + [Test] + public void GltfValidator() + { + if (!VRMShaders.PathObject.TryGetFromEnvironmentVariable("GLTF_VALIDATOR", out var exe)) + { + return; + } + if (!exe.Exists) + { + return; + } using (var data = new GlbFileParser(AliciaPath).Parse()) using (var migrated = Vrm10Data.Migrate(data, out Vrm10Data vrm, out MigrationData migration)) { - Assert.NotNull(vrm); - Assert.NotNull(migration); + var glb = Glb.Create(migrated.Json, new ArraySegment(migrated.Bin.ToArray())).ToBytes(); + var glbPath = "tmp.glb"; + File.WriteAllBytes(glbPath, glb); + + var processStartInfo = new System.Diagnostics.ProcessStartInfo(exe.FullPath, $"{glbPath} -o") + { + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + + var process = System.Diagnostics.Process.Start(processStartInfo); + string standardOutput = process.StandardOutput.ReadToEnd(); + string standardError = process.StandardError.ReadToEnd(); + int exitCode = process.ExitCode; + Debug.Log($"{exitCode}\n{standardOutput}\n{standardError}\n"); + Assert.AreEqual(0, exitCode); } } } diff --git a/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs b/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs index 56efe3a9c..7ed44346b 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs @@ -27,6 +27,8 @@ namespace VRMShaders public bool IsUnderAsset => IsDescendantOf(UnityAssets); + public bool Exists => System.IO.File.Exists(FullPath); + /// /// AssetDatabase の引き数になるパスを想定。 /// Assets のひとつ上を 基準とする相対パス。 @@ -136,6 +138,18 @@ namespace VRMShaders return true; } + public static bool TryGetFromEnvironmentVariable(string key, out PathObject dst) + { + var value = System.Environment.GetEnvironmentVariable(key); + if (string.IsNullOrEmpty(value)) + { + dst = default; + return false; + } + dst = PathObject.FromFullPath(value); + return true; + } + public PathObject Child(string child) { return FromFullPath(Path.Combine(FullPath, child)); From fa454b226aab3a19050ba75917e141877f6faad1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 17:13:37 +0900 Subject: [PATCH 02/10] fix extensionUsed to extensionsUsed --- .../Runtime/UniGLTF/IO/GltfJsonUtil.cs | 20 +++---- .../Runtime/UniGLTF/IO/gltfExporter.cs | 16 +----- .../Tests/UniGLTF/GltfJsonUtilTests.cs | 23 ++++---- Assets/VRM10/Tests/MigrationTests.cs | 54 +++++++++++++------ 4 files changed, 60 insertions(+), 53 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs index c96e33e84..3ee9c9cf6 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs @@ -9,7 +9,7 @@ namespace UniGLTF { public static class GltfJsonUtil { - const string EXTENSION_USED_KEY = "extensionUsed"; + public const string EXTENSION_USED_KEY = "extensionsUsed"; /// /// JsonPath を 再帰的に列挙する @@ -96,14 +96,14 @@ namespace UniGLTF return false; } - static void CopyJson(IReadOnlyList extensionUsed, JsonFormatter dst, JsonNode src, int level) + static void CopyJson(IReadOnlyList extensionsUsed, JsonFormatter dst, JsonNode src, int level) { if (src.IsArray()) { dst.BeginList(); foreach (var v in src.ArrayItems()) { - CopyJson(extensionUsed, dst, v, level + 1); + CopyJson(extensionsUsed, dst, v, level + 1); } dst.EndList(); } @@ -119,7 +119,7 @@ namespace UniGLTF var key = kv.Key.GetString(); if (key == EXTENSION_USED_KEY) { - if (extensionUsed.Count == 0) + if (extensionsUsed.Count == 0) { // skip } @@ -128,7 +128,7 @@ namespace UniGLTF dst.Key(key); // replace dst.BeginList(); - foreach (var ex in extensionUsed) + foreach (var ex in extensionsUsed) { dst.Value(ex); } @@ -140,15 +140,15 @@ namespace UniGLTF else { dst.Key(key); - CopyJson(extensionUsed, dst, kv.Value, level + 1); + CopyJson(extensionsUsed, dst, kv.Value, level + 1); } } - if (!done && level == 0 && extensionUsed.Count > 0) + if (!done && level == 0 && extensionsUsed.Count > 0) { // add dst.Key(EXTENSION_USED_KEY); dst.BeginList(); - foreach (var ex in extensionUsed) + foreach (var ex in extensionsUsed) { dst.Value(ex); } @@ -162,7 +162,7 @@ namespace UniGLTF foreach (var kv in src.ObjectItems()) { dst.Key(kv.Key.GetUtf8String()); - CopyJson(extensionUsed, dst, kv.Value, level + 1); + CopyJson(extensionsUsed, dst, kv.Value, level + 1); } dst.EndMap(); } @@ -177,7 +177,7 @@ namespace UniGLTF /// /// https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/glTF.schema.json /// - /// extensionUsed の更新を各拡張自身にやらせるのは無駄だし、手動でコントロールするのも間違いの元である。 + /// extensionsUsed の更新を各拡張自身にやらせるのは無駄だし、手動でコントロールするのも間違いの元である。 /// 完成品の JSON から後付けで作ることにした。 /// /// * Exporter しか使わない処理なので、GC, 処理速度は気にしてない diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 03db82d3c..8adeef7e1 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -51,18 +51,6 @@ namespace UniGLTF return new MaterialExporter(); } - /// - /// このエクスポーターがサポートするExtension - /// - protected virtual IEnumerable ExtensionUsed - { - get - { - yield return glTF_KHR_materials_unlit.ExtensionName; - yield return glTF_KHR_texture_transform.ExtensionName; - } - } - protected ITextureExporter TextureExporter => _textureExporter; private TextureExporter _textureExporter; @@ -86,8 +74,6 @@ namespace UniGLTF { _data = data; - _gltf.extensionsUsed.AddRange(ExtensionUsed); - _gltf.asset = new glTFAssets { generator = "UniGLTF-" + UniGLTFVersion.VERSION, @@ -353,7 +339,7 @@ namespace UniGLTF } /// - /// GlbLowPevelParser.FixNameUnique で付与した Suffix を remove + /// GlbLowLevelParser.FixNameUnique で付与した Suffix を remove /// public static void FixName(glTF gltf) { diff --git a/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs index cd806db21..40034b8c8 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs @@ -7,7 +7,7 @@ namespace UniGLTF public class GltfJsonUtilTests { [Test] - public void Update_extensionUsed() + public void Update_extensionsUsed() { var dst = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(@"{ ""asset"": { @@ -44,11 +44,11 @@ namespace UniGLTF var parsed = dst.ParseAsJson(); Assert.AreEqual(new string[] { "KHR_materials_unlit" }, - parsed["extensionUsed"].ArrayItems().Select(x => x.GetString()).ToArray()); + parsed[GltfJsonUtil.EXTENSION_USED_KEY].ArrayItems().Select(x => x.GetString()).ToArray()); } [Test] - public void Replace_extensionUsed() + public void Replace_extensionsUsed() { var dst = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(@"{ ""asset"": { @@ -63,7 +63,7 @@ namespace UniGLTF ] } ], - ""extensionUsed"": [""dummy""], + ""extensionsUsed"": [""dummy""], ""materials"": [ { ""pbrMetallicRoughness"": { @@ -86,11 +86,11 @@ namespace UniGLTF var parsed = dst.ParseAsJson(); Assert.AreEqual(new string[] { "KHR_materials_unlit" }, - parsed["extensionUsed"].ArrayItems().Select(x => x.GetString()).ToArray()); + parsed[GltfJsonUtil.EXTENSION_USED_KEY].ArrayItems().Select(x => x.GetString()).ToArray()); } [Test] - public void Empty_extensionUsed() + public void Empty_extensionsUsed() { var dst = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(@"{ ""asset"": { @@ -105,7 +105,7 @@ namespace UniGLTF ] } ], - ""extensionUsed"": [""dummy""] , + ""extensionsUsed"": [""dummy""] , ""materials"": [ { ""pbrMetallicRoughness"": { @@ -123,11 +123,11 @@ namespace UniGLTF }"); var parsed = dst.ParseAsJson(); - Assert.False(parsed.ContainsKey("extensionUsed")); + Assert.False(parsed.ContainsKey(GltfJsonUtil.EXTENSION_USED_KEY)); } [Test] - public void Empty2_extensionUsed() + public void Empty2_extensionsUsed() { var dst = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(@"{ ""asset"": { @@ -156,12 +156,11 @@ namespace UniGLTF ""name"": ""Red"" } ], - ""extensionUsed"": [""dummy""] + ""extensionsUsed"": [""dummy""] }"); var parsed = dst.ParseAsJson(); - Assert.False(parsed.ContainsKey("extensionUsed")); + Assert.False(parsed.ContainsKey(GltfJsonUtil.EXTENSION_USED_KEY)); } - } } diff --git a/Assets/VRM10/Tests/MigrationTests.cs b/Assets/VRM10/Tests/MigrationTests.cs index 815d36197..7e26b7099 100644 --- a/Assets/VRM10/Tests/MigrationTests.cs +++ b/Assets/VRM10/Tests/MigrationTests.cs @@ -267,6 +267,27 @@ namespace UniVRM10 } } + class TempFile : IDisposable + { + public string Path { get; } + + TempFile(string path) + { + Path = path; + } + + public void Dispose() + { + // File.Delete(Path); + } + + public static TempFile Create(string path, byte[] bytes) + { + File.WriteAllBytes(path, bytes); + return new TempFile(path); + } + } + [Test] public void GltfValidator() { @@ -281,24 +302,25 @@ namespace UniVRM10 using (var data = new GlbFileParser(AliciaPath).Parse()) using (var migrated = Vrm10Data.Migrate(data, out Vrm10Data vrm, out MigrationData migration)) { - var glb = Glb.Create(migrated.Json, new ArraySegment(migrated.Bin.ToArray())).ToBytes(); - var glbPath = "tmp.glb"; - File.WriteAllBytes(glbPath, glb); - - var processStartInfo = new System.Diagnostics.ProcessStartInfo(exe.FullPath, $"{glbPath} -o") + var json = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(migrated.Json); + var glb = Glb.Create(json, new ArraySegment(migrated.Bin.ToArray())).ToBytes(); + using (var tmp = TempFile.Create("GltfValidator_tmp.glb", glb)) { - CreateNoWindow = true, - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true, - }; + var processStartInfo = new System.Diagnostics.ProcessStartInfo(exe.FullPath, $"{tmp.Path} -o") + { + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; - var process = System.Diagnostics.Process.Start(processStartInfo); - string standardOutput = process.StandardOutput.ReadToEnd(); - string standardError = process.StandardError.ReadToEnd(); - int exitCode = process.ExitCode; - Debug.Log($"{exitCode}\n{standardOutput}\n{standardError}\n"); - Assert.AreEqual(0, exitCode); + var process = System.Diagnostics.Process.Start(processStartInfo); + string standardOutput = process.StandardOutput.ReadToEnd(); + string standardError = process.StandardError.ReadToEnd(); + int exitCode = process.ExitCode; + Debug.Log($"{exitCode}\n{standardOutput}\n{standardError}\n"); + Assert.AreEqual(0, exitCode); + } } } } From eccb12f645fcf0e00c62abf631b5e2c4d208be61 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 17:27:07 +0900 Subject: [PATCH 03/10] fix ACCESSOR_TOTAL_OFFSET_ALIGNMENT --- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index 1ca4c8d10..ade421550 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -46,6 +46,12 @@ namespace UniVRM10 var bufferView = _buffer.Extend(bytes); var index = _bufferViews.Count; _bufferViews.Add(bufferView); + var mod = bytes.Length % 4; + if (mod != 0) + { + // padding for 4byte alignment + _buffer.Extend(new ArraySegment(new byte[4 - mod])); + } return index; } From 71ca64b10d29c11e78b0f59a3a7f3a1695335213 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 17:39:05 +0900 Subject: [PATCH 04/10] fix BUFFER_VIEW_TARGET_MISSING --- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index ade421550..c205ea9e9 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -41,9 +41,9 @@ namespace UniVRM10 return new MeshUpdater(data).Update(model); } - int AddBuffer(NativeArray bytes) + int AddBuffer(NativeArray bytes, glBufferTarget target) { - var bufferView = _buffer.Extend(bytes); + var bufferView = _buffer.Extend(bytes, target); var index = _bufferViews.Count; _bufferViews.Add(bufferView); var mod = bytes.Length % 4; @@ -55,9 +55,9 @@ namespace UniVRM10 return index; } - int AddAccessor(NativeArray span) where T : struct + int AddAccessor(NativeArray span, glBufferTarget target) where T : struct { - var bufferViewIndex = AddBuffer(span.Reinterpret(Marshal.SizeOf())); + var bufferViewIndex = AddBuffer(span.Reinterpret(Marshal.SizeOf()), target); var accessor = new glTFAccessor { bufferView = bufferViewIndex, @@ -71,13 +71,13 @@ namespace UniVRM10 return index; } - int? AddAccessor(BufferAccessor buffer) where T : struct + int? AddAccessor(BufferAccessor buffer, glBufferTarget target) where T : struct { if (buffer == null) { return default; } - return AddAccessor(buffer.GetSpan()); + return AddAccessor(buffer.GetSpan(), target); } struct MorphAccessor @@ -99,7 +99,7 @@ namespace UniVRM10 foreach (var image in gltf.images) { var bytes = _data.GetBytesFromBufferView(image.bufferView); - image.bufferView = AddBuffer(bytes); + image.bufferView = AddBuffer(bytes, default); } // copy mesh @@ -136,7 +136,7 @@ namespace UniVRM10 if (skinIndex == -1) { skinIndex = gltf.skins.Count; - gltfSkin.inverseBindMatrices = AddAccessor(node.MeshGroup.Skin.InverseMatrices.GetSpan()); + gltfSkin.inverseBindMatrices = AddAccessor(node.MeshGroup.Skin.InverseMatrices.GetSpan(), default); gltf.skins.Add(gltfSkin); } else @@ -222,20 +222,20 @@ namespace UniVRM10 default: throw new NotImplementedException(); } - var position = AddAccessor(mesh.VertexBuffer.Positions); - var normal = AddAccessor(mesh.VertexBuffer.Normals); - var uv = AddAccessor(mesh.VertexBuffer.TexCoords); - var weights = AddAccessor(mesh.VertexBuffer.Weights); - var joints = AddAccessor(mesh.VertexBuffer.Joints); - var color = AddAccessor(mesh.VertexBuffer.Colors); + var position = AddAccessor(mesh.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER); + var normal = AddAccessor(mesh.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER); + var uv = AddAccessor(mesh.VertexBuffer.TexCoords, glBufferTarget.ARRAY_BUFFER); + var weights = AddAccessor(mesh.VertexBuffer.Weights, glBufferTarget.ARRAY_BUFFER); + var joints = AddAccessor(mesh.VertexBuffer.Joints, glBufferTarget.ARRAY_BUFFER); + var color = AddAccessor(mesh.VertexBuffer.Colors, glBufferTarget.ARRAY_BUFFER); var morphTargets = new MorphAccessor[] { }; if (mesh.MorphTargets != null) { morphTargets = mesh.MorphTargets.Select(x => new MorphAccessor { - Position = AddAccessor(x.VertexBuffer.Positions), - Normal = AddAccessor(x.VertexBuffer.Normals), + Position = AddAccessor(x.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER), + Normal = AddAccessor(x.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER), }).ToArray(); } @@ -243,7 +243,7 @@ namespace UniVRM10 foreach (var (gltfPrim, submesh) in Enumerable.Zip(gltfMesh.primitives, mesh.Submeshes, (l, r) => (l, r))) { var subIndices = indices.GetSubArray(submesh.Offset, submesh.DrawCount); - gltfPrim.indices = AddAccessor(subIndices); + gltfPrim.indices = AddAccessor(subIndices, glBufferTarget.ELEMENT_ARRAY_BUFFER); gltfPrim.attributes.POSITION = position.Value; gltfPrim.attributes.NORMAL = normal.GetValueOrDefault(-1); // たぶん、ありえる gltfPrim.attributes.TANGENT = -1; @@ -306,24 +306,24 @@ namespace UniVRM10 throw new NotImplementedException(); } - var position = AddAccessor(mesh.VertexBuffer.Positions); - var normal = AddAccessor(mesh.VertexBuffer.Normals); - var uv = AddAccessor(mesh.VertexBuffer.TexCoords); - var weights = AddAccessor(mesh.VertexBuffer.Weights); - var joints = AddAccessor(mesh.VertexBuffer.Joints); - var color = AddAccessor(mesh.VertexBuffer.Colors); + var position = AddAccessor(mesh.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER); + var normal = AddAccessor(mesh.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER); + var uv = AddAccessor(mesh.VertexBuffer.TexCoords, glBufferTarget.ARRAY_BUFFER); + var weights = AddAccessor(mesh.VertexBuffer.Weights, glBufferTarget.ARRAY_BUFFER); + var joints = AddAccessor(mesh.VertexBuffer.Joints, glBufferTarget.ARRAY_BUFFER); + var color = AddAccessor(mesh.VertexBuffer.Colors, glBufferTarget.ARRAY_BUFFER); var morphTargets = new MorphAccessor[] { }; if (mesh.MorphTargets != null) { morphTargets = mesh.MorphTargets.Select(x => new MorphAccessor { - Position = AddAccessor(x.VertexBuffer.Positions), - Normal = AddAccessor(x.VertexBuffer.Normals), + Position = AddAccessor(x.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER), + Normal = AddAccessor(x.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER), }).ToArray(); } - gltfPrim.indices = AddAccessor(indices); + gltfPrim.indices = AddAccessor(indices, glBufferTarget.ELEMENT_ARRAY_BUFFER); gltfPrim.attributes.POSITION = position.Value; gltfPrim.attributes.NORMAL = normal.GetValueOrDefault(-1); // たぶん、ありえる gltfPrim.attributes.TANGENT = -1; From 3e14b73cb4d5ad849edf8878d638fdef2ac97345 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 18:08:52 +0900 Subject: [PATCH 05/10] fix MESH_PRIMITIVE_POSITION_ACCESSOR_WITHOUT_BOUNDS --- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 77 +++++++++++++------ 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index c205ea9e9..97158b313 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -46,16 +46,38 @@ namespace UniVRM10 var bufferView = _buffer.Extend(bytes, target); var index = _bufferViews.Count; _bufferViews.Add(bufferView); + + // padding for 4byte alignment var mod = bytes.Length % 4; if (mod != 0) { - // padding for 4byte alignment _buffer.Extend(new ArraySegment(new byte[4 - mod])); } + return index; } - int AddAccessor(NativeArray span, glBufferTarget target) where T : struct + static (float[], float[]) GetMinMax(NativeArray v) + { + var minX = float.PositiveInfinity; + var minY = float.PositiveInfinity; + var minZ = float.PositiveInfinity; + var maxX = float.NegativeInfinity; + var maxY = float.NegativeInfinity; + var maxZ = float.NegativeInfinity; + for (int i = 1; i < v.Length; ++i) + { + if (v[i].x < minX) minX = v[i].x; + if (v[i].y < minY) minY = v[i].y; + if (v[i].z < minZ) minZ = v[i].z; + if (v[i].x > maxX) maxX = v[i].x; + if (v[i].y > maxY) maxY = v[i].y; + if (v[i].z > maxZ) maxZ = v[i].z; + } + return (new float[] { minX, minY, minZ }, new float[] { maxX, maxY, maxZ }); + } + + int AddAccessor(NativeArray span, glBufferTarget target, bool minMaxBounds) where T : struct { var bufferViewIndex = AddBuffer(span.Reinterpret(Marshal.SizeOf()), target); var accessor = new glTFAccessor @@ -66,18 +88,27 @@ namespace UniVRM10 componentType = glTFExtensions.GetComponentType(), type = glTFExtensions.GetAccessorType(), }; + if (minMaxBounds) + { + if (span is NativeArray positions) + { + var (min, max) = GetMinMax(positions); + accessor.min = min; + accessor.max = max; + } + } var index = _accessors.Count; _accessors.Add(accessor); return index; } - int? AddAccessor(BufferAccessor buffer, glBufferTarget target) where T : struct + int? AddAccessor(BufferAccessor buffer, glBufferTarget target, bool minMaxBounds) where T : struct { if (buffer == null) { return default; } - return AddAccessor(buffer.GetSpan(), target); + return AddAccessor(buffer.GetSpan(), target, minMaxBounds); } struct MorphAccessor @@ -136,7 +167,7 @@ namespace UniVRM10 if (skinIndex == -1) { skinIndex = gltf.skins.Count; - gltfSkin.inverseBindMatrices = AddAccessor(node.MeshGroup.Skin.InverseMatrices.GetSpan(), default); + gltfSkin.inverseBindMatrices = AddAccessor(node.MeshGroup.Skin.InverseMatrices.GetSpan(), default, false); gltf.skins.Add(gltfSkin); } else @@ -222,20 +253,20 @@ namespace UniVRM10 default: throw new NotImplementedException(); } - var position = AddAccessor(mesh.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER); - var normal = AddAccessor(mesh.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER); - var uv = AddAccessor(mesh.VertexBuffer.TexCoords, glBufferTarget.ARRAY_BUFFER); - var weights = AddAccessor(mesh.VertexBuffer.Weights, glBufferTarget.ARRAY_BUFFER); - var joints = AddAccessor(mesh.VertexBuffer.Joints, glBufferTarget.ARRAY_BUFFER); - var color = AddAccessor(mesh.VertexBuffer.Colors, glBufferTarget.ARRAY_BUFFER); + var position = AddAccessor(mesh.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER, true); + var normal = AddAccessor(mesh.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER, false); + var uv = AddAccessor(mesh.VertexBuffer.TexCoords, glBufferTarget.ARRAY_BUFFER, false); + var weights = AddAccessor(mesh.VertexBuffer.Weights, glBufferTarget.ARRAY_BUFFER, false); + var joints = AddAccessor(mesh.VertexBuffer.Joints, glBufferTarget.ARRAY_BUFFER, false); + var color = AddAccessor(mesh.VertexBuffer.Colors, glBufferTarget.ARRAY_BUFFER, false); var morphTargets = new MorphAccessor[] { }; if (mesh.MorphTargets != null) { morphTargets = mesh.MorphTargets.Select(x => new MorphAccessor { - Position = AddAccessor(x.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER), - Normal = AddAccessor(x.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER), + Position = AddAccessor(x.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER, true), + Normal = AddAccessor(x.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER, false), }).ToArray(); } @@ -243,7 +274,7 @@ namespace UniVRM10 foreach (var (gltfPrim, submesh) in Enumerable.Zip(gltfMesh.primitives, mesh.Submeshes, (l, r) => (l, r))) { var subIndices = indices.GetSubArray(submesh.Offset, submesh.DrawCount); - gltfPrim.indices = AddAccessor(subIndices, glBufferTarget.ELEMENT_ARRAY_BUFFER); + gltfPrim.indices = AddAccessor(subIndices, glBufferTarget.ELEMENT_ARRAY_BUFFER, false); gltfPrim.attributes.POSITION = position.Value; gltfPrim.attributes.NORMAL = normal.GetValueOrDefault(-1); // たぶん、ありえる gltfPrim.attributes.TANGENT = -1; @@ -306,24 +337,24 @@ namespace UniVRM10 throw new NotImplementedException(); } - var position = AddAccessor(mesh.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER); - var normal = AddAccessor(mesh.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER); - var uv = AddAccessor(mesh.VertexBuffer.TexCoords, glBufferTarget.ARRAY_BUFFER); - var weights = AddAccessor(mesh.VertexBuffer.Weights, glBufferTarget.ARRAY_BUFFER); - var joints = AddAccessor(mesh.VertexBuffer.Joints, glBufferTarget.ARRAY_BUFFER); - var color = AddAccessor(mesh.VertexBuffer.Colors, glBufferTarget.ARRAY_BUFFER); + var position = AddAccessor(mesh.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER, true); + var normal = AddAccessor(mesh.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER, false); + var uv = AddAccessor(mesh.VertexBuffer.TexCoords, glBufferTarget.ARRAY_BUFFER, false); + var weights = AddAccessor(mesh.VertexBuffer.Weights, glBufferTarget.ARRAY_BUFFER, false); + var joints = AddAccessor(mesh.VertexBuffer.Joints, glBufferTarget.ARRAY_BUFFER, false); + var color = AddAccessor(mesh.VertexBuffer.Colors, glBufferTarget.ARRAY_BUFFER, false); var morphTargets = new MorphAccessor[] { }; if (mesh.MorphTargets != null) { morphTargets = mesh.MorphTargets.Select(x => new MorphAccessor { - Position = AddAccessor(x.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER), - Normal = AddAccessor(x.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER), + Position = AddAccessor(x.VertexBuffer.Positions, glBufferTarget.ARRAY_BUFFER, true), + Normal = AddAccessor(x.VertexBuffer.Normals, glBufferTarget.ARRAY_BUFFER, false), }).ToArray(); } - gltfPrim.indices = AddAccessor(indices, glBufferTarget.ELEMENT_ARRAY_BUFFER); + gltfPrim.indices = AddAccessor(indices, glBufferTarget.ELEMENT_ARRAY_BUFFER, false); gltfPrim.attributes.POSITION = position.Value; gltfPrim.attributes.NORMAL = normal.GetValueOrDefault(-1); // たぶん、ありえる gltfPrim.attributes.TANGENT = -1; From 787039ea56ee29e15f28233cda60b182b135821a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 18:27:56 +0900 Subject: [PATCH 06/10] fix bufferLength --- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index 97158b313..325c83b1b 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -65,7 +65,7 @@ namespace UniVRM10 var maxX = float.NegativeInfinity; var maxY = float.NegativeInfinity; var maxZ = float.NegativeInfinity; - for (int i = 1; i < v.Length; ++i) + for (int i = 0; i < v.Length; ++i) { if (v[i].x < minX) minX = v[i].x; if (v[i].y < minY) minY = v[i].y; @@ -206,6 +206,7 @@ namespace UniVRM10 } // replace + gltf.buffers[0].byteLength = _buffer.Bytes.Count; gltf.bufferViews = _bufferViews; gltf.accessors = _accessors; From 80761cc01a3e771c7690cbc54162a8ca3ddd9f6c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 20:03:59 +0900 Subject: [PATCH 07/10] add NullableSerialization * Nullable only --- .../Serialization/FieldSerializationInfo.cs | 4 ++ .../Serialization/NullableSerialization.cs | 41 +++++++++++++++++++ .../NullableSerialization.cs.meta | 11 +++++ .../UniGLTF/Format/GltfSerializer.g.cs | 4 +- .../Runtime/UniGLTF/Format/glTFBuffer.cs | 2 +- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 10 ++--- Assets/VRM10/Runtime/IO/IndexExtensions.cs | 22 ++++++++++ Assets/VRM10/Runtime/IO/Vrm10ImportData.cs | 8 ++-- Assets/VRM10/Runtime/Migration/RotateY180.cs | 6 +-- 9 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs create mode 100644 Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/Serialization/FieldSerializationInfo.cs b/Assets/UniGLTF/Editor/UniGLTF/Serialization/FieldSerializationInfo.cs index 9e9c86ffa..49bf61af4 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/Serialization/FieldSerializationInfo.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/Serialization/FieldSerializationInfo.cs @@ -75,6 +75,10 @@ namespace UniGLTF return new StringKeyDictionarySerialization(t, GetSerialization(t.GetGenericArguments()[1], path, attr, prefix)); } + else if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + return new NullableSerialization(t.GetGenericArguments()[0], path, attr, prefix); + } // GetCollectionType(fi.FieldType, out suffix, out t); if (t == typeof(sbyte)) diff --git a/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs b/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs new file mode 100644 index 000000000..7b2ecda26 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; + +namespace UniGLTF +{ + public class NullableSerialization : IValueSerialization + { + public NullableSerialization(Type t, string path, JsonSchemaAttribute attr, string prefix) + { + + } + + public Type ValueType => typeof(Int32); + public bool IsInline => true; + + public string CreateSerializationCondition(string argName, JsonSchemaAttribute t) + { + return $"{argName}.HasValue"; + } + + public string GenerateSerializerCall(string callName, string argName) + { + return $"f.Value({argName}.Value)"; + } + + public void GenerateSerializer(StreamWriter writer, string callName) + { + throw new NotImplementedException(); + } + + public string GenerateDeserializerCall(string callName, string argName) + { + return argName + ".GetInt32()"; + } + + public void GenerateDeserializer(StreamWriter writer, string callName) + { + throw new NotImplementedException(); + } + } +} diff --git a/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs.meta new file mode 100644 index 000000000..f589dab5f --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 84c7673bb94af9f419ca9839910fd8be +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs index 6268a60ee..7d609c04d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs @@ -272,9 +272,9 @@ public static void Serialize_gltf_accessors_ITEM(JsonFormatter f, glTFAccessor v f.BeginMap(); - if(value.bufferView>=0){ + if(value.bufferView.HasValue){ f.Key("bufferView"); - f.Value(value.bufferView); + f.Value(value.bufferView.Value); } if(value.byteOffset>=0){ diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs index 1721a95b8..e153baf7c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs @@ -93,7 +93,7 @@ namespace UniGLTF public class glTFAccessor { [JsonSchema(Minimum = 0)] - public int bufferView = -1; + public int? bufferView; [JsonSchema(Minimum = 0, Dependencies = new string[] { "bufferView" })] public int byteOffset; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 3a7527e96..2d1de97b7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -208,7 +208,7 @@ namespace UniGLTF public BufferAccessor GetIndicesFromAccessorIndex(int accessorIndex) { var accessor = GLTF.accessors[accessorIndex]; - var view = GLTF.bufferViews[accessor.bufferView]; + var view = GLTF.bufferViews[accessor.bufferView.Value]; return GetIntIndicesFromView(view, accessor.count, accessor.byteOffset, accessor.componentType); } @@ -218,8 +218,8 @@ namespace UniGLTF if (vertexAccessor.count <= 0) return NativeArrayManager.CreateNativeArray(0); - var result = (vertexAccessor.bufferView != -1) - ? GetTypedFromAccessor(vertexAccessor, GLTF.bufferViews[vertexAccessor.bufferView]) + var result = (vertexAccessor.bufferView.HasValue) + ? GetTypedFromAccessor(vertexAccessor, GLTF.bufferViews[vertexAccessor.bufferView.Value]) : NativeArrayManager.CreateNativeArray(vertexAccessor.count) ; @@ -277,9 +277,9 @@ namespace UniGLTF var bufferCount = vertexAccessor.count * vertexAccessor.TypeCount; NativeArray result = default; - if (vertexAccessor.bufferView != -1) + if (vertexAccessor.bufferView.HasValue) { - var view = GLTF.bufferViews[vertexAccessor.bufferView]; + var view = GLTF.bufferViews[vertexAccessor.bufferView.Value]; var segment = GetBytesFromBuffer(view.buffer); result = segment.GetSubArray(view.byteOffset + vertexAccessor.byteOffset, vertexAccessor.count * 4 * vertexAccessor.TypeCount).Reinterpret(1); } diff --git a/Assets/VRM10/Runtime/IO/IndexExtensions.cs b/Assets/VRM10/Runtime/IO/IndexExtensions.cs index 56b323369..d305bd25d 100644 --- a/Assets/VRM10/Runtime/IO/IndexExtensions.cs +++ b/Assets/VRM10/Runtime/IO/IndexExtensions.cs @@ -18,5 +18,27 @@ namespace UniVRM10 index = value; return true; } + + public static bool TryGetValidIndex(this int? value, int count, out int index) + { + if (!value.HasValue) + { + index = -1; + return false; + } + if (value < 0) + { + index = -1; + return false; + } + if (value >= count) + { + index = -1; + return false; + } + + index = value.Value; + return true; + } } } diff --git a/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs b/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs index ada4c16f8..10a6bcd42 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs @@ -226,7 +226,7 @@ namespace UniVRM10 bool AccessorsIsContinuous(int[] accessorIndices) { var firstAccessor = Gltf.accessors[accessorIndices[0]]; - var firstView = Gltf.bufferViews[firstAccessor.bufferView]; + var firstView = Gltf.bufferViews[firstAccessor.bufferView.Value]; var start = firstView.byteOffset + firstAccessor.byteOffset; var pos = start; foreach (var i in accessorIndices) @@ -241,7 +241,7 @@ namespace UniVRM10 return false; } - var view = Gltf.bufferViews[current.bufferView]; + var view = Gltf.bufferViews[current.bufferView.Value]; if (pos != view.byteOffset + current.byteOffset) { return false; @@ -267,7 +267,7 @@ namespace UniVRM10 { // IndexBufferが連続して格納されている => Slice でいける var firstAccessor = Gltf.accessors[accessorIndices[0]]; - var firstView = Gltf.bufferViews[firstAccessor.bufferView]; + var firstView = Gltf.bufferViews[firstAccessor.bufferView.Value]; var start = firstView.byteOffset + firstAccessor.byteOffset; if (!firstView.buffer.TryGetValidIndex(Gltf.buffers.Count, out int firstViewBufferIndex)) { @@ -295,7 +295,7 @@ namespace UniVRM10 { throw new ArgumentException($"accessor.type: {accessor.type}"); } - var view = Gltf.bufferViews[accessor.bufferView]; + var view = Gltf.bufferViews[accessor.bufferView.Value]; if (!view.buffer.TryGetValidIndex(Gltf.buffers.Count, out int viewBufferIndex)) { throw new Exception(); diff --git a/Assets/VRM10/Runtime/Migration/RotateY180.cs b/Assets/VRM10/Runtime/Migration/RotateY180.cs index 93bb4203c..c0b1890a7 100644 --- a/Assets/VRM10/Runtime/Migration/RotateY180.cs +++ b/Assets/VRM10/Runtime/Migration/RotateY180.cs @@ -61,9 +61,9 @@ namespace UniVRM10 var accessor = data.GLTF.accessors[accessorIndex]; var bufferViewIndex = -1; - if (accessor.bufferView != -1) + if (accessor.bufferView.HasValue) { - bufferViewIndex = accessor.bufferView; + bufferViewIndex = accessor.bufferView.Value; } else if (accessor.sparse?.values != null && accessor.sparse.values.bufferView != -1) { @@ -113,7 +113,7 @@ namespace UniVRM10 if (used.Add(skin.inverseBindMatrices)) { var accessor = data.GLTF.accessors[skin.inverseBindMatrices]; - var buffer = data.GetBytesFromBufferView(accessor.bufferView); + var buffer = data.GetBytesFromBufferView(accessor.bufferView.Value); var span = buffer.Reinterpret(1); for (int i = 0; i < span.Length; ++i) { From 9c92be5f3e880c4e06ac80852e03ee420ea795d1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 20:14:03 +0900 Subject: [PATCH 08/10] glTFAccessor.byteOffset to Nullable * fix no byteoffset when sparse --- Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs | 4 ++-- Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs | 2 +- Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs | 2 +- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 6 +++--- Assets/VRM10/Runtime/IO/Vrm10ImportData.cs | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs index 7d609c04d..502aec107 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs @@ -277,9 +277,9 @@ public static void Serialize_gltf_accessors_ITEM(JsonFormatter f, glTFAccessor v f.Value(value.bufferView.Value); } - if(value.byteOffset>=0){ + if(value.byteOffset.HasValue){ f.Key("byteOffset"); - f.Value(value.byteOffset); + f.Value(value.byteOffset.Value); } if(!string.IsNullOrEmpty(value.type)){ diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs index e153baf7c..99b5b8a4c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs @@ -96,7 +96,7 @@ namespace UniGLTF public int? bufferView; [JsonSchema(Minimum = 0, Dependencies = new string[] { "bufferView" })] - public int byteOffset; + public int? byteOffset; [JsonSchema(Required = true, EnumValues = new object[] { "SCALAR", "VEC2", "VEC3", "VEC4", "MAT2", "MAT3", "MAT4" }, EnumSerializationType = EnumSerializationType.AsString)] public string type; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs index b5d535cc5..1788c7cd1 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs @@ -115,7 +115,7 @@ namespace UniGLTF var accessorIndex = Gltf.accessors.Count; Gltf.accessors.Add(new glTFAccessor { - byteOffset = 0, + byteOffset = default, componentType = glTFExtensions.GetComponentType(), type = glTFExtensions.GetAccessorType(), count = accessorCount, diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 2d1de97b7..6e4feffe4 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -166,7 +166,7 @@ namespace UniGLTF NativeArray GetTypedFromAccessor(glTFAccessor accessor, glTFBufferView view) where T : struct { var bytes = GetBytesFromBufferView(view); - return bytes.GetSubArray(accessor.byteOffset, bytes.Length - accessor.byteOffset).Reinterpret(1).GetSubArray(0, accessor.count); + return bytes.GetSubArray(accessor.byteOffset.GetValueOrDefault(), bytes.Length - accessor.byteOffset.GetValueOrDefault()).Reinterpret(1).GetSubArray(0, accessor.count); } /// @@ -209,7 +209,7 @@ namespace UniGLTF { var accessor = GLTF.accessors[accessorIndex]; var view = GLTF.bufferViews[accessor.bufferView.Value]; - return GetIntIndicesFromView(view, accessor.count, accessor.byteOffset, accessor.componentType); + return GetIntIndicesFromView(view, accessor.count, accessor.byteOffset.GetValueOrDefault(), accessor.componentType); } public NativeArray GetArrayFromAccessor(int accessorIndex) where T : struct @@ -281,7 +281,7 @@ namespace UniGLTF { var view = GLTF.bufferViews[vertexAccessor.bufferView.Value]; var segment = GetBytesFromBuffer(view.buffer); - result = segment.GetSubArray(view.byteOffset + vertexAccessor.byteOffset, vertexAccessor.count * 4 * vertexAccessor.TypeCount).Reinterpret(1); + result = segment.GetSubArray(view.byteOffset + vertexAccessor.byteOffset.GetValueOrDefault(), vertexAccessor.count * 4 * vertexAccessor.TypeCount).Reinterpret(1); } else { diff --git a/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs b/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs index 10a6bcd42..c829ffadc 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10ImportData.cs @@ -136,7 +136,7 @@ namespace UniVRM10 { var buffer = m_data.Bin; var byteSize = accessor.CalcByteSize(); - bytes = m_data.Bin.GetSubArray(view.byteOffset, view.byteLength).GetSubArray(accessor.byteOffset, byteSize); + bytes = m_data.Bin.GetSubArray(view.byteOffset, view.byteLength).GetSubArray(accessor.byteOffset.GetValueOrDefault(), byteSize); } } @@ -268,7 +268,7 @@ namespace UniVRM10 // IndexBufferが連続して格納されている => Slice でいける var firstAccessor = Gltf.accessors[accessorIndices[0]]; var firstView = Gltf.bufferViews[firstAccessor.bufferView.Value]; - var start = firstView.byteOffset + firstAccessor.byteOffset; + var start = firstView.byteOffset + firstAccessor.byteOffset.GetValueOrDefault(); if (!firstView.buffer.TryGetValidIndex(Gltf.buffers.Count, out int firstViewBufferIndex)) { throw new Exception(); @@ -302,7 +302,7 @@ namespace UniVRM10 } var buffer = Gltf.buffers[viewBufferIndex]; var bin = GetBufferBytes(buffer); - var start = view.byteOffset + accessor.byteOffset; + var start = view.byteOffset + accessor.byteOffset.GetValueOrDefault(); var bytes = bin.GetSubArray(start, accessor.count * accessor.GetStride()); var dst = indices.Reinterpret(1).GetSubArray(offset, accessor.count); offset += accessor.count; From 05d7c1d8ea3ae0f9e5d7c9afb6877a57dd3631c1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 20:19:54 +0900 Subject: [PATCH 09/10] fix sparse blendShape position accessor min and max --- Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs index 8276891ba..2f2d6f3e4 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs @@ -48,6 +48,8 @@ namespace UniGLTF positionAccessorIndex = data.ExtendSparseBufferAndGetAccessorIndex(accessorCount, sparseIndices.Select(x => positions[x]).ToArray(), sparseIndices, sparseIndicesViewIndex, glBufferTarget.NONE); + data.Gltf.accessors[positionAccessorIndex].min = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Min(a.x, b.x), Math.Min(a.y, b.y), Mathf.Min(a.z, b.z))).ToArray(); + data.Gltf.accessors[positionAccessorIndex].max = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Max(a.x, b.x), Math.Max(a.y, b.y), Mathf.Max(a.z, b.z))).ToArray(); } // normals From 9589cb145f27f06d49ec1f2f65934daed8e5658b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 20:28:12 +0900 Subject: [PATCH 10/10] =?UTF-8?q?=E6=9C=AA=E5=AE=9F=E8=A3=85=E3=81=A7?= =?UTF-8?q?=E3=81=82=E3=82=8B=E3=81=93=E3=81=A8=E3=81=8C=E5=88=86=E3=81=8B?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/UniGLTF/Serialization/NullableSerialization.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs b/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs index 7b2ecda26..d129bbec1 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/Serialization/NullableSerialization.cs @@ -7,7 +7,10 @@ namespace UniGLTF { public NullableSerialization(Type t, string path, JsonSchemaAttribute attr, string prefix) { - + if (t != typeof(int)) + { + throw new NotImplementedException(); + } } public Type ValueType => typeof(Int32);