diff --git a/Assets/UniGLTF/Runtime/Extensions/KeyValuePairExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/KeyValuePairExtensions.cs new file mode 100644 index 000000000..5f94c8101 --- /dev/null +++ b/Assets/UniGLTF/Runtime/Extensions/KeyValuePairExtensions.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace UniGLTF +{ + public static class KeyValuePairExtensions + { + public static void Deconstruct(this KeyValuePair pair, out T key, out U value) + { + key = pair.Key; + value = pair.Value; + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs.meta b/Assets/UniGLTF/Runtime/Extensions/KeyValuePairExtensions.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs.meta rename to Assets/UniGLTF/Runtime/Extensions/KeyValuePairExtensions.cs.meta index ec2ce3377..022151d5f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs.meta +++ b/Assets/UniGLTF/Runtime/Extensions/KeyValuePairExtensions.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 8ef774aa861dc6f46a94330c7169bf04 +guid: be00d5fb3e17dbf4e87ec4a7aae8dbc3 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs deleted file mode 100644 index 71ce55e5f..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs +++ /dev/null @@ -1,492 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Runtime.InteropServices; - -namespace UniGLTF -{ - static class BitWriter - { - public static void Write(byte[] bytes, int i, Int32 value) - { - var tmp = BitConverter.GetBytes(value); - bytes[i++] = tmp[0]; - bytes[i++] = tmp[1]; - bytes[i++] = tmp[2]; - bytes[i++] = tmp[3]; - } - - #region UINT - public static void Write(byte[] bytes, int i, Byte value) - { - bytes[i++] = value; - } - - public static void Write(byte[] bytes, int i, UInt16 value) - { - var tmp = BitConverter.GetBytes(value); - bytes[i++] = tmp[0]; - bytes[i++] = tmp[1]; - } - - public static void Write(byte[] bytes, int i, UInt32 value) - { - var tmp = BitConverter.GetBytes(value); - bytes[i++] = tmp[0]; - bytes[i++] = tmp[1]; - bytes[i++] = tmp[2]; - bytes[i++] = tmp[3]; - } - #endregion - - public static void Write(byte[] bytes, int i, Single value) - { - var tmp = BitConverter.GetBytes(value); - bytes[i++] = tmp[0]; - bytes[i++] = tmp[1]; - bytes[i++] = tmp[2]; - bytes[i++] = tmp[3]; - } - - public static void Write(byte[] bytes, int i, System.Numerics.Vector2 value) - { - Write(bytes, i, value.X); - Write(bytes, i + 4, value.Y); - } - - public static void Write(byte[] bytes, int i, System.Numerics.Vector3 value) - { - Write(bytes, i, value.X); - Write(bytes, i + 4, value.Y); - Write(bytes, i + 8, value.Z); - } - - public static void Write(byte[] bytes, int i, System.Numerics.Vector4 value) - { - Write(bytes, i, value.X); - Write(bytes, i + 4, value.Y); - Write(bytes, i + 8, value.Z); - Write(bytes, i + 12, value.W); - } - - public static void Write(byte[] bytes, int i, System.Numerics.Quaternion value) - { - Write(bytes, i, value.X); - Write(bytes, i + 4, value.Y); - Write(bytes, i + 8, value.Z); - Write(bytes, i + 12, value.W); - } - - public static void Write(byte[] bytes, int i, System.Numerics.Matrix4x4 value) - { - Write(bytes, i, value.M11); - Write(bytes, i + 4, value.M12); - Write(bytes, i + 8, value.M13); - Write(bytes, i + 12, value.M14); - Write(bytes, i + 16, value.M21); - Write(bytes, i + 20, value.M22); - Write(bytes, i + 24, value.M23); - Write(bytes, i + 28, value.M24); - Write(bytes, i + 32, value.M31); - Write(bytes, i + 36, value.M32); - Write(bytes, i + 40, value.M33); - Write(bytes, i + 44, value.M34); - Write(bytes, i + 48, value.M41); - Write(bytes, i + 52, value.M42); - Write(bytes, i + 56, value.M43); - Write(bytes, i + 60, value.M44); - } - - public static void Write(byte[] bytes, int i, UShort4 value) - { - Write(bytes, i, value.x); - Write(bytes, i + 2, value.y); - Write(bytes, i + 4, value.z); - Write(bytes, i + 6, value.w); - } - - public static void Write(byte[] bytes, int i, SkinJoints value) - { - Write(bytes, i, value.Joint0); - Write(bytes, i + 2, value.Joint1); - Write(bytes, i + 4, value.Joint2); - Write(bytes, i + 6, value.Joint3); - } - - public static void Write(byte[] bytes, int i, Byte4 value) - { - bytes[i++] = value.x; - bytes[i++] = value.y; - bytes[i++] = value.z; - bytes[i++] = value.w; - } - - public static void Write(byte[] bytes, int i, UnityEngine.Vector2 value) - { - Write(bytes, i, value.x); - Write(bytes, i + 4, value.y); - } - - public static void Write(byte[] bytes, int i, UnityEngine.Vector3 value) - { - Write(bytes, i, value.x); - Write(bytes, i + 4, value.y); - Write(bytes, i + 8, value.z); - } - - public static void Write(byte[] bytes, int i, UnityEngine.Vector4 value) - { - Write(bytes, i, value.x); - Write(bytes, i + 4, value.y); - Write(bytes, i + 8, value.z); - Write(bytes, i + 12, value.w); - } - - public static void Write(byte[] bytes, int i, UnityEngine.Color value) - { - Write(bytes, i, value.r); - Write(bytes, i + 4, value.g); - Write(bytes, i + 8, value.b); - Write(bytes, i + 12, value.a); - } - - public static void Write(byte[] bytes, int i, UnityEngine.Matrix4x4 value) - { - Write(bytes, i, value.m00); - Write(bytes, i + 4, value.m01); - Write(bytes, i + 8, value.m02); - Write(bytes, i + 12, value.m03); - Write(bytes, i + 16, value.m10); - Write(bytes, i + 20, value.m11); - Write(bytes, i + 24, value.m12); - Write(bytes, i + 28, value.m13); - Write(bytes, i + 32, value.m20); - Write(bytes, i + 36, value.m21); - Write(bytes, i + 40, value.m22); - Write(bytes, i + 44, value.m23); - Write(bytes, i + 48, value.m30); - Write(bytes, i + 52, value.m31); - Write(bytes, i + 56, value.m32); - Write(bytes, i + 60, value.m33); - } - } - - public static class KeyValuePariExtensions - { - public static void Deconstruct(this KeyValuePair pair, out T key, out U value) - { - key = pair.Key; - value = pair.Value; - } - } - - public struct SpanLike : IEquatable>, IEnumerable - where T : struct - { - public readonly ArraySegment Bytes; - - readonly int m_itemSize; - public readonly int Length; - - public delegate T Getter(byte[] bytes, int start); - readonly Getter m_getter; - - public delegate void Setter(byte[] bytes, int i, T value); - readonly Setter m_setter; - - public T this[int i] - { - set - { - m_setter(Bytes.Array, Bytes.Offset + i * m_itemSize, value); - } - get - { - return m_getter(Bytes.Array, Bytes.Offset + i * m_itemSize); - } - } - - public SpanLike(ArraySegment bytes, int itemSize, Getter getter, Setter setter) - { - Bytes = bytes; - m_itemSize = itemSize; - Length = Bytes.Count / m_itemSize; - m_getter = getter; - m_setter = setter; - } - - public SpanLike Slice(int offset, int count) - { - var bytesOffset = offset * m_itemSize; - var bytesLength = count * m_itemSize; - if (bytesOffset + bytesLength > Bytes.Count) - { - throw new ArgumentOutOfRangeException(); - } - return new SpanLike(new ArraySegment( - Bytes.Array, - Bytes.Offset + bytesOffset, - bytesLength - ), m_itemSize, m_getter, m_setter); - } - - public SpanLike Slice(int offset) - { - var bytesOffset = offset * m_itemSize; - if (bytesOffset > Bytes.Count) - { - throw new ArgumentOutOfRangeException(); - } - var bytesLength = Bytes.Count - bytesOffset; - return new SpanLike(new ArraySegment( - Bytes.Array, - Bytes.Offset + bytesOffset, - bytesLength - ), m_itemSize, m_getter, m_setter); - } - - public T[] ToArray() - { - var array = new T[Length]; - Bytes.FromBytes(array); - return array; - } - - public bool Equals(SpanLike other) - { - if (Length != other.Length) - { - return false; - } - - var end = Length; - for (int i = 0; i < end; ++i) - { - if (!this[i].Equals(other[i])) - { - return false; - } - } - - return true; - } - - public IEnumerator GetEnumerator() - { - for (int i = 0; i < Length; ++i) - { - yield return this[i]; - } - } - - IEnumerator IEnumerable.GetEnumerator() - { - return (IEnumerator)GetEnumerator(); - } - } - - public static class SpanLike - { - struct GetSet where T : struct - { - public SpanLike.Getter Getter; - public SpanLike.Setter Setter; - } - - public static readonly Dictionary Map = new Dictionary() - { - {typeof(Byte), new GetSet{ - Getter = (array, start) => array[start], - Setter = BitWriter.Write}}, - {typeof(UInt16), new GetSet{ - Getter = BitConverter.ToUInt16, - Setter = BitWriter.Write}}, - {typeof(UInt32), new GetSet{ - Getter = BitConverter.ToUInt32, - Setter = BitWriter.Write}}, - {typeof(Int32), new GetSet{ - Getter = BitConverter.ToInt32, - Setter = BitWriter.Write}}, - {typeof(Single), new GetSet{ - Getter = BitConverter.ToSingle, - Setter = BitWriter.Write}}, - {typeof(System.Numerics.Vector2), new GetSet{ - Getter = (array, start) => - new System.Numerics.Vector2( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4) - ), - Setter = BitWriter.Write - }}, - {typeof(System.Numerics.Vector3), new GetSet{ - Getter = (array, start) => - new System.Numerics.Vector3( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4), - BitConverter.ToSingle(array, start + 8) - ), - Setter = BitWriter.Write - }}, - {typeof(System.Numerics.Vector4), new GetSet{ - Getter = (array, start) => - new System.Numerics.Vector4( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4), - BitConverter.ToSingle(array, start + 8), - BitConverter.ToSingle(array, start + 12) - ), - Setter = BitWriter.Write - }}, - {typeof(System.Numerics.Quaternion), new GetSet{ - Getter = (array, start) => - new System.Numerics.Quaternion( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4), - BitConverter.ToSingle(array, start + 8), - BitConverter.ToSingle(array, start + 12) - ), - Setter = BitWriter.Write - }}, - {typeof(System.Numerics.Matrix4x4), new GetSet{ - Getter = (array, start) => - new System.Numerics.Matrix4x4( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4), - BitConverter.ToSingle(array, start + 8), - BitConverter.ToSingle(array, start + 12), - BitConverter.ToSingle(array, start + 16), - BitConverter.ToSingle(array, start + 20), - BitConverter.ToSingle(array, start + 24), - BitConverter.ToSingle(array, start + 28), - BitConverter.ToSingle(array, start + 32), - BitConverter.ToSingle(array, start + 36), - BitConverter.ToSingle(array, start + 40), - BitConverter.ToSingle(array, start + 44), - BitConverter.ToSingle(array, start + 48), - BitConverter.ToSingle(array, start + 52), - BitConverter.ToSingle(array, start + 56), - BitConverter.ToSingle(array, start + 60) - ), - Setter = BitWriter.Write - }}, - {typeof(Byte4), new GetSet{ - Getter = (array, start) => - new Byte4( - array[start], - array[start + 1], - array[start + 2], - array[start + 3] - ), - Setter = BitWriter.Write - }}, - {typeof(UShort4), new GetSet{ - Getter = (array, start) => - new UShort4( - BitConverter.ToUInt16(array, start), - BitConverter.ToUInt16(array, start + 2), - BitConverter.ToUInt16(array, start + 4), - BitConverter.ToUInt16(array, start + 6) - ), - Setter = BitWriter.Write - }}, - {typeof(SkinJoints), new GetSet{ - Getter = (array, start) => - new SkinJoints - { - Joint0 = BitConverter.ToUInt16(array, start), - Joint1 = BitConverter.ToUInt16(array, start + 2), - Joint2 = BitConverter.ToUInt16(array, start + 4), - Joint3 = BitConverter.ToUInt16(array, start + 6), - }, - Setter = BitWriter.Write - }}, - {typeof(UnityEngine.Vector2), new GetSet{ - Getter = (array, start) => - new UnityEngine.Vector2( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4) - ), - Setter = BitWriter.Write - }}, - {typeof(UnityEngine.Vector3), new GetSet{ - Getter = (array, start) => - new UnityEngine.Vector3( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4), - BitConverter.ToSingle(array, start + 8) - ), - Setter = BitWriter.Write - }}, - {typeof(UnityEngine.Vector4), new GetSet{ - Getter = (array, start) => - new UnityEngine.Vector4( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4), - BitConverter.ToSingle(array, start + 8), - BitConverter.ToSingle(array, start + 12) - ), - Setter = BitWriter.Write - }}, - {typeof(UnityEngine.Color), new GetSet{ - Getter = (array, start) => - new UnityEngine.Color( - BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 4), - BitConverter.ToSingle(array, start + 8), - BitConverter.ToSingle(array, start + 12) - ), - Setter = BitWriter.Write - }}, - {typeof(UnityEngine.Matrix4x4), new GetSet{ - Getter = (array, start) => - // UnityEngine.Matrix4x4 - // * 列優先メモリレイアウト(col0, col1, col2, col3) - // * Constructor 引き数が行優先 new Matrix4x4(row0, row1, row2, row3); - new UnityEngine.Matrix4x4( - new UnityEngine.Vector4(BitConverter.ToSingle(array, start), - BitConverter.ToSingle(array, start + 16), - BitConverter.ToSingle(array, start + 32), - BitConverter.ToSingle(array, start + 48)), - new UnityEngine.Vector4(BitConverter.ToSingle(array, start+4), - BitConverter.ToSingle(array, start + 16+4), - BitConverter.ToSingle(array, start + 32+4), - BitConverter.ToSingle(array, start + 48+4)), - new UnityEngine.Vector4(BitConverter.ToSingle(array, start+8), - BitConverter.ToSingle(array, start + 16+8), - BitConverter.ToSingle(array, start + 32+8), - BitConverter.ToSingle(array, start + 48+8)), - new UnityEngine.Vector4(BitConverter.ToSingle(array, start+12), - BitConverter.ToSingle(array, start + 16+12), - BitConverter.ToSingle(array, start + 32+12), - BitConverter.ToSingle(array, start + 48+12)) - ), - Setter = BitWriter.Write - }}, - - }; - - public static SpanLike Wrap(ArraySegment bytes) where T : struct - { - if (!Map.TryGetValue(typeof(T), out object value)) - { - throw new KeyNotFoundException($"{typeof(T)}"); - } - var getset = (GetSet)value; - return new SpanLike(bytes, Marshal.SizeOf(), getset.Getter, getset.Setter); - } - - public static SpanLike Create(int itemCount) where T : struct - { - var itemSize = Marshal.SizeOf(); - var array = new byte[itemCount * itemSize]; - return Wrap(new ArraySegment(array)); - } - - public static SpanLike CopyFrom(T[] src) where T : struct - { - var buffer = new byte[src.Length * Marshal.SizeOf()]; - src.ToBytes(new ArraySegment(buffer)); - return Wrap(new ArraySegment(buffer)); - } - } -} diff --git a/Assets/UniGLTF/Tests/UniGLTF/NumericTests.cs b/Assets/UniGLTF/Tests/UniGLTF/NumericTests.cs index 8f75a5c93..05d0c8266 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/NumericTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/NumericTests.cs @@ -19,27 +19,6 @@ namespace UniGLTF Assert.AreEqual(orgAngle, angle); } - [Test] - [Category("Numerics")] - public void Vector4Test() - { - var bytes = new byte[4 * 4]; - { - var span = SpanLike.Wrap(new ArraySegment(bytes)); - span[0] = 1.0f; - } - - { - var span = SpanLike.Wrap(new ArraySegment(bytes)); - Assert.AreEqual(1.0f, span[0].X); - } - - { - var span = SpanLike.Wrap(new ArraySegment(bytes)); - Assert.AreEqual(1.0f, span[0].X); - } - } - [Test] [Category("Numerics")] public void Vector3Test() diff --git a/Assets/UniGLTF/Tests/UniGLTF/SpanLikeTests.cs b/Assets/UniGLTF/Tests/UniGLTF/SpanLikeTests.cs deleted file mode 100644 index 472201b06..000000000 --- a/Assets/UniGLTF/Tests/UniGLTF/SpanLikeTests.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using NUnit.Framework; - -namespace UniGLTF -{ - public class SpanLikeTests - { - [Test] - [Category("SpanLike")] - public void CopyTest() - { - var src = new int[] { 255 }; - var dst = new byte[4]; - - src.ToBytes(new ArraySegment(dst)); - - Assert.AreEqual(0xFF, dst[0]); - Assert.AreEqual(0, dst[1]); - Assert.AreEqual(0, dst[2]); - Assert.AreEqual(0, dst[3]); - } - - [Test] - [Category("SpanLike")] - public void SpanLikeTest() - { - var v0 = new UnityEngine.Vector3(1, 2, 3); - var v1 = new UnityEngine.Vector3(4, 5, 6); - var positions = new UnityEngine.Vector3[] - { - v0, - v1, - }; - var span = SpanLike.CopyFrom(positions); - - Assert.AreEqual(2, span.Length); - Assert.AreEqual(v0, span[0]); - Assert.AreEqual(v1, span[1]); - } - } -} diff --git a/Assets/UniGLTF/Tests/UniGLTF/SpanLikeTests.cs.meta b/Assets/UniGLTF/Tests/UniGLTF/SpanLikeTests.cs.meta deleted file mode 100644 index b3756ed6d..000000000 --- a/Assets/UniGLTF/Tests/UniGLTF/SpanLikeTests.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: f70b7312ba0d32b4493231c6f8a42e4c -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs b/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs index d63bba48d..7a7ef16f7 100644 --- a/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs +++ b/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Numerics; using UniGLTF; using Unity.Collections; using VrmLib; @@ -13,48 +12,6 @@ namespace UniVRM10 /// public static class MeshWriter { - static void Vec3MinMax(ArraySegment bytes, glTFAccessor accessor) - { - var positions = SpanLike.Wrap(bytes); - var min = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); - var max = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); - foreach (var p in positions) - { - min = Vector3.Min(min, p); - max = Vector3.Max(max, p); - } - accessor.min = min.ToFloat3(); - accessor.max = max.ToFloat3(); - } - - // static int ExportIndices(ExportingGltfData data, BufferAccessor x, int offset, int count, ExportArgs option) - // { - // if (x.Count <= ushort.MaxValue) - // { - // if (x.ComponentType == AccessorValueType.UNSIGNED_INT) - // { - // // ensure ushort - // var src = x.GetSpan().Slice(offset, count); - // var bytes = new byte[src.Length * 2]; - // var dst = SpanLike.Wrap(new ArraySegment(bytes)); - // for (int i = 0; i < src.Length; ++i) - // { - // dst[i] = (ushort)src[i]; - // } - // var accessor = new BufferAccessor(new ArraySegment(bytes), AccessorValueType.UNSIGNED_SHORT, AccessorVectorType.SCALAR, count); - // return accessor.AddAccessorTo(data, 0, option.sparse, null, 0, count); - // } - // else - // { - // return x.AddAccessorTo(data, 0, option.sparse, null, offset, count); - // } - // } - // else - // { - // return x.AddAccessorTo(data, 0, option.sparse, null, offset, count); - // } - // } - /// /// https://github.com/vrm-c/UniVRM/issues/800 /// @@ -67,7 +24,7 @@ namespace UniVRM10 /// /// static IEnumerable ExportMeshDivided(this VrmLib.Mesh mesh, List materials, - ExportingGltfData writer, ExportArgs option) + ExportingGltfData writer, ExportArgs option) { var usedIndices = new List(); var meshIndices = mesh.IndexBuffer.GetAsIntArray(); @@ -161,7 +118,7 @@ namespace UniVRM10 /// /// /// - public static glTFMesh ExportMeshGroup(this MeshGroup src, List materials, ExportingGltfData writer, ExportArgs option) + public static glTFMesh ExportMeshGroup(this MeshGroup src, List materials, ExportingGltfData writer, ExportArgs option) { var gltfMesh = new glTFMesh { diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs index c14d85281..6311ccb2f 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using UniGLTF; using UniJSON; +using Unity.Collections; namespace UniVRM10 { @@ -18,7 +19,7 @@ namespace UniVRM10 } } - static (int, int) GetVertexRange(SpanLike indices) + static (int, int) GetVertexRange(NativeArray indices) { var min = int.MaxValue; ; var max = 0;