From 80761cc01a3e771c7690cbc54162a8ca3ddd9f6c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 11 Jul 2022 20:03:59 +0900 Subject: [PATCH] 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) {