diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs
deleted file mode 100644
index 5788b59b3..000000000
--- a/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs
+++ /dev/null
@@ -1,36 +0,0 @@
-using System;
-
-
-namespace UniGLTF
-{
- ///
- /// for glb chunk buffer read
- ///
- public class ArraySegmentByteBuffer : IBytesBuffer
- {
- ArraySegment m_bytes;
-
- public ArraySegmentByteBuffer(ArraySegment bytes)
- {
- m_bytes = bytes;
- }
-
- public string Uri
- {
- get;
- private set;
- }
-
- public glTFBufferView Extend(ArraySegment array, glBufferTarget target) where T : struct
- {
- throw new NotImplementedException();
- }
-
- public void ExtendCapacity(int capacity)
- {
- throw new NotImplementedException();
- }
-
- public ArraySegment Bytes => m_bytes;
- }
-}
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs.meta
deleted file mode 100644
index 3236a085e..000000000
--- a/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs.meta
+++ /dev/null
@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: f857b8b7ca45b1746af90c7fb86b2398
-MonoImporter:
- externalObjects: {}
- serializedVersion: 2
- defaultReferences: []
- executionOrder: 0
- icon: {instanceID: 0}
- userData:
- assetBundleName:
- assetBundleVariant:
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs
deleted file mode 100644
index 6ec19069a..000000000
--- a/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-using System;
-
-namespace UniGLTF
-{
- public interface IBytesBuffer
- {
- string Uri { get; }
- ArraySegment Bytes { get; }
- glTFBufferView Extend(ArraySegment array, glBufferTarget target = glBufferTarget.NONE) where T : struct;
- void ExtendCapacity(int capacity);
- }
-}
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs.meta
deleted file mode 100644
index 2e4f187c0..000000000
--- a/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs.meta
+++ /dev/null
@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 2ce69a95857efe041a1e3dd9d2e6c98b
-MonoImporter:
- externalObjects: {}
- serializedVersion: 2
- defaultReferences: []
- executionOrder: 0
- icon: {instanceID: 0}
- userData:
- assetBundleName:
- assetBundleVariant:
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs
index a29dde5e1..722c1f8c2 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs
@@ -1,14 +1,15 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
-
+using Unity.Collections;
+using Unity.Collections.LowLevel.Unsafe;
namespace UniGLTF
{
///
/// for exporter
///
- public class ArrayByteBuffer : IBytesBuffer
+ public class ArrayByteBuffer
{
public string Uri
{
@@ -25,6 +26,11 @@ namespace UniGLTF
m_bytes = bytes;
}
+ public glTFBufferView Extend(NativeArray array, glBufferTarget target = default) where T : struct
+ {
+ return Extend(new ArraySegment(array.ToArray()), target);
+ }
+
public glTFBufferView Extend(ArraySegment array, glBufferTarget target = default) where T : struct
{
using (var pin = Pin.Create(array))
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs
index a5b29d16c..b5d535cc5 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs
@@ -2,14 +2,15 @@ using System;
using System.Collections.Generic;
using System.IO;
using UniJSON;
+using Unity.Collections;
namespace UniGLTF
{
public class ExportingGltfData
{
- public glTF GLTF { get; } = new glTF();
+ public glTF Gltf { get; } = new glTF();
- protected IBytesBuffer _buffer;
+ protected ArrayByteBuffer _buffer;
///
/// bin chunk
///
@@ -23,7 +24,7 @@ namespace UniGLTF
}
// buffers[0] is export target
- GLTF.buffers.Add(new glTFBuffer());
+ Gltf.buffers.Add(new glTFBuffer());
_buffer = new ArrayByteBuffer(new byte[reserved]);
}
@@ -31,7 +32,7 @@ namespace UniGLTF
public glTFBufferView ExtendBufferAndGetView(ArraySegment segment, glBufferTarget target) where T : struct
{
var view = _buffer.Extend(segment, target);
- GLTF.buffers[0].byteLength = _buffer.Bytes.Count;
+ Gltf.buffers[0].byteLength = _buffer.Bytes.Count;
return view;
}
@@ -44,8 +45,8 @@ namespace UniGLTF
return -1;
}
var view = ExtendBufferAndGetView(array, target);
- var viewIndex = GLTF.bufferViews.Count;
- GLTF.bufferViews.Add(view);
+ var viewIndex = Gltf.bufferViews.Count;
+ Gltf.bufferViews.Add(view);
return viewIndex;
}
@@ -67,10 +68,10 @@ namespace UniGLTF
var viewIndex = ExtendBufferAndGetViewIndex(array, target);
// index buffer's byteStride is unnecessary
- GLTF.bufferViews[viewIndex].byteStride = 0;
+ Gltf.bufferViews[viewIndex].byteStride = 0;
- var accessorIndex = GLTF.accessors.Count;
- GLTF.accessors.Add(new glTFAccessor
+ var accessorIndex = Gltf.accessors.Count;
+ Gltf.accessors.Add(new glTFAccessor
{
bufferView = viewIndex,
byteOffset = 0,
@@ -111,8 +112,8 @@ namespace UniGLTF
return -1;
}
var sparseValuesViewIndex = ExtendBufferAndGetViewIndex(sparseValues, target);
- var accessorIndex = GLTF.accessors.Count;
- GLTF.accessors.Add(new glTFAccessor
+ var accessorIndex = Gltf.accessors.Count;
+ Gltf.accessors.Add(new glTFAccessor
{
byteOffset = 0,
componentType = glTFExtensions.GetComponentType(),
@@ -135,6 +136,20 @@ namespace UniGLTF
});
return accessorIndex;
}
+
+ public void Reserve(int bytesLength)
+ {
+ _buffer.ExtendCapacity(bytesLength);
+ }
+
+ public int AppendToBuffer(NativeArray segment)
+ {
+ var gltfBufferView = _buffer.Extend(segment);
+ var viewIndex = Gltf.bufferViews.Count;
+ Gltf.bufferViews.Add(gltfBufferView);
+ return viewIndex;
+ }
+
#endregion
#region ToGltf & ToGlb
@@ -145,7 +160,7 @@ namespace UniGLTF
public byte[] ToGlbBytes()
{
var f = new JsonFormatter();
- GltfSerializer.Serialize(f, GLTF);
+ GltfSerializer.Serialize(f, Gltf);
var json = f.ToString().ParseAsJson().ToString(" ");
@@ -162,10 +177,10 @@ namespace UniGLTF
public (string, glTFBuffer) ToGltf(string gltfPath)
{
// fix buffer path
- if (GLTF.buffers.Count == 1)
+ if (Gltf.buffers.Count == 1)
{
var withoutExt = Path.GetFileNameWithoutExtension(gltfPath);
- GLTF.buffers[0].uri = $"{withoutExt}.bin";
+ Gltf.buffers[0].uri = $"{withoutExt}.bin";
}
else
{
@@ -173,12 +188,12 @@ namespace UniGLTF
}
var f = new JsonFormatter();
- GltfSerializer.Serialize(f, GLTF);
+ GltfSerializer.Serialize(f, Gltf);
var json = f.ToString().ParseAsJson().ToString(" ");
json = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(json);
- return (json, GLTF.buffers[0]);
+ return (json, Gltf.buffers[0]);
}
#endregion
}
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs
index 123d5ff50..ff35786c3 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs
@@ -92,7 +92,7 @@ namespace UniGLTF
public static GltfData CreateFromExportForTest(ExportingGltfData data)
{
- return CreateFromGltfDataForTest(data.GLTF, data.BinBytes);
+ return CreateFromGltfDataForTest(data.Gltf, data.BinBytes);
}
public static GltfData CreateFromGltfDataForTest(glTF gltf, ArraySegment bytes)
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs
index 205b630ed..8276891ba 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/BlendShapeExporter.cs
@@ -74,8 +74,8 @@ namespace UniGLTF
{
// position
var positionAccessorIndex = data.ExtendBufferAndGetAccessorIndex(positions, glBufferTarget.ARRAY_BUFFER);
- 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();
+ 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();
// normal
var normalAccessorIndex = -1;
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportUtil.cs
index 2013a1467..e796b4fb2 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportUtil.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportUtil.cs
@@ -115,7 +115,7 @@ namespace UniGLTF
var indicesAccessorIndex = data.ExtendBufferAndGetAccessorIndex(indices.Select(x => (uint)m_vertexIndexMap[x]).ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER);
var positions = m_positions.ToArray();
var positionAccessorIndex = data.ExtendBufferAndGetAccessorIndex(positions, glBufferTarget.ARRAY_BUFFER);
- AccessorsBounds.UpdatePositionAccessorsBounds(data.GLTF.accessors[positionAccessorIndex], positions);
+ AccessorsBounds.UpdatePositionAccessorsBounds(data.Gltf.accessors[positionAccessorIndex], positions);
var normals = m_normals.ToArray();
var normalAccessorIndex = data.ExtendBufferAndGetAccessorIndex(normals, glBufferTarget.ARRAY_BUFFER);
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs
index 00af168eb..e1885be1f 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs
@@ -32,7 +32,7 @@ namespace UniGLTF
var materials = unityMesh.Materials;
var positions = mesh.vertices.Select(axisInverter.InvertVector3).ToArray();
var positionAccessorIndex = data.ExtendBufferAndGetAccessorIndex(positions, glBufferTarget.ARRAY_BUFFER);
- AccessorsBounds.UpdatePositionAccessorsBounds(data.GLTF.accessors[positionAccessorIndex], positions);
+ AccessorsBounds.UpdatePositionAccessorsBounds(data.Gltf.accessors[positionAccessorIndex], positions);
var normalAccessorIndex = data.ExtendBufferAndGetAccessorIndex(mesh.normals.Select(y => axisInverter.InvertVector3(y.normalized)).ToArray(), glBufferTarget.ARRAY_BUFFER);
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs
index 48902834c..04d933e4c 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs
@@ -6,6 +6,25 @@ using System.Runtime.InteropServices;
namespace UniGLTF
{
+ ///
+ /// NativeArrayManager を Dispose する責務を負わない使用者は、こっちを使う。
+ ///
+ public interface INativeArrayManager
+ {
+ NativeArray CreateNativeArray(int size)
+ where T : struct;
+
+ NativeArray CreateNativeArray(ArraySegment data)
+ where T : struct;
+
+ NativeArray CreateNativeArray(T[] data)
+ where T : struct;
+
+ NativeArray Convert(NativeArray src, Func convert)
+ where T : struct
+ where U : struct;
+ }
+
///
/// 特定のコンテキスト(GltfDataなど)に関連する、NativeArrayの作成を代行し、
/// まとめてDisposeできるようにする。
@@ -15,7 +34,7 @@ namespace UniGLTF
/// また、Sparse や base64 encoding など単純なバイト列のスライスで済まない場合も同様である。
///
///
- public class NativeArrayManager : IDisposable
+ public class NativeArrayManager : INativeArrayManager, IDisposable
{
List m_disposables = new List();
@@ -28,6 +47,10 @@ namespace UniGLTF
m_disposables.Clear();
}
+ public NativeArrayManager()
+ {
+
+ }
///
/// NativeArrayを新規作成し、Dispose管理する。
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/Runtime/UniGLTF/IO/SpanLike.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs.meta
deleted file mode 100644
index ec2ce3377..000000000
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs.meta
+++ /dev/null
@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 8ef774aa861dc6f46a94330c7169bf04
-MonoImporter:
- externalObjects: {}
- serializedVersion: 2
- defaultReferences: []
- executionOrder: 0
- icon: {instanceID: 0}
- userData:
- assetBundleName:
- assetBundleVariant:
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs
index 603a94c2c..055821d81 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs
@@ -30,8 +30,8 @@ namespace UniGLTF
var viewIndex = data.ExtendBufferAndGetViewIndex(bytesWithMime.bytes);
// add image
- var imageIndex = data.GLTF.images.Count;
- data.GLTF.images.Add(new glTFImage
+ var imageIndex = data.Gltf.images.Count;
+ data.Gltf.images.Add(new glTFImage
{
name = TextureImportName.RemoveSuffix(texture.name),
bufferView = viewIndex,
@@ -39,13 +39,13 @@ namespace UniGLTF
});
// add sampler
- var samplerIndex = data.GLTF.samplers.Count;
+ var samplerIndex = data.Gltf.samplers.Count;
var sampler = TextureSamplerUtil.Export(texture);
- data.GLTF.samplers.Add(sampler);
+ data.Gltf.samplers.Add(sampler);
// add texture
- var textureIndex = data.GLTF.textures.Count;
- data.GLTF.textures.Add(new glTFTexture
+ var textureIndex = data.Gltf.textures.Count;
+ data.Gltf.textures.Add(new glTFTexture
{
sampler = samplerIndex,
source = imageIndex,
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
index e9014d5d1..28b97b39c 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
@@ -12,7 +12,7 @@ namespace UniGLTF
{
protected ExportingGltfData _data;
- protected glTF _gltf => _data.GLTF;
+ protected glTF _gltf => _data.Gltf;
public GameObject Copy
{
diff --git a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs
index 349db71fd..a72c42b24 100644
--- a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs
+++ b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs
@@ -134,7 +134,7 @@ namespace UniGLTF
exporter.Prepare(root);
exporter.Export(new EditorTextureSerializer());
}
- var gltf = data.GLTF;
+ var gltf = data.Gltf;
Assert.AreEqual(1, gltf.images.Count);
var exportedImage = gltf.images[0];
Assert.AreEqual("image/png", exportedImage.mimeType);
diff --git a/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs
index 22034d331..0894b3b37 100644
--- a/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs
+++ b/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs
@@ -11,7 +11,7 @@ namespace UniGLTF
public void TextureNameUniqueness()
{
var data = new ExportingGltfData();
- var gltf = data.GLTF;
+ var gltf = data.Gltf;
gltf.asset.version = "2.0";
gltf.textures.Add(new glTFTexture
{
diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs
index 5af60274e..5a8c0d48a 100644
--- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs
+++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs
@@ -139,7 +139,7 @@ namespace UniGLTF
: MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings)
;
- using (var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes))
+ using (var parsed = GltfData.CreateFromGltfDataForTest(data.Gltf, data.BinBytes))
{
{
@@ -190,7 +190,7 @@ namespace UniGLTF
: MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings)
;
- using (var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes))
+ using (var parsed = GltfData.CreateFromGltfDataForTest(data.Gltf, data.BinBytes))
{
{
var indices = parsed.GetIndices(gltfMesh.primitives[0].indices);
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/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs
index dd39a0fcf..f2bccab68 100644
--- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs
+++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs
@@ -109,9 +109,9 @@ namespace UniGLTF
exporter.Export(new EditorTextureSerializer());
// remove empty buffer
- data.GLTF.buffers.Clear();
+ data.Gltf.buffers.Clear();
- json = data.GLTF.ToJson();
+ json = data.Gltf.ToJson();
}
// parse
@@ -141,7 +141,7 @@ namespace UniGLTF
data.ExtendBufferAndGetView(bytes, glBufferTarget.NONE);
}
- Assert.AreEqual(values.Count, data.GLTF.buffers[0].byteLength);
+ Assert.AreEqual(values.Count, data.Gltf.buffers[0].byteLength);
Assert.True(Enumerable.SequenceEqual(values, data.BinBytes.ToArray()));
}
@@ -299,7 +299,7 @@ namespace UniGLTF
exporter.Export(new EditorTextureSerializer());
}
- var expected = data.GLTF.ToJson().ParseAsJson();
+ var expected = data.Gltf.ToJson().ParseAsJson();
expected.AddKey(Utf8String.From("meshes"));
expected.AddValue(default(ArraySegment), ValueNodeType.Array);
expected["meshes"].AddValue(default(ArraySegment), ValueNodeType.Object);
@@ -333,7 +333,7 @@ namespace UniGLTF
primitive["targets"][1].AddKey(Utf8String.From("TANGENT"));
primitive["targets"][1].AddValue(Utf8String.From("0").Bytes, ValueNodeType.Integer);
- data.GLTF.meshes.Add(new glTFMesh("test")
+ data.Gltf.meshes.Add(new glTFMesh("test")
{
primitives = new List
{
@@ -361,7 +361,7 @@ namespace UniGLTF
}
}
});
- var actual = data.GLTF.ToJson().ParseAsJson();
+ var actual = data.Gltf.ToJson().ParseAsJson();
Assert.AreEqual(expected, actual);
}
@@ -528,7 +528,7 @@ namespace UniGLTF
// export
var data = new ExportingGltfData();
- var gltf = data.GLTF;
+ var gltf = data.Gltf;
var json = default(string);
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
@@ -606,7 +606,7 @@ namespace UniGLTF
// export
var data = new ExportingGltfData();
- var gltf = data.GLTF;
+ var gltf = data.Gltf;
string json;
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
@@ -669,7 +669,7 @@ namespace UniGLTF
// export
var data = new ExportingGltfData();
- var gltf = data.GLTF;
+ var gltf = data.Gltf;
string json;
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
diff --git a/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs b/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs
index 95b081ba5..3b57264ba 100644
--- a/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs
+++ b/Assets/VRM/Tests/SampleTests/VRMImportExportTests.cs
@@ -169,7 +169,7 @@ namespace VRM.Samples
// TODO: Check contents in JSON
/*var exportJson = */
- JsonParser.Parse(vrm.GLTF.ToJson());
+ JsonParser.Parse(vrm.Gltf.ToJson());
// TODO: Check contents in JSON
/*var newExportedJson = */
diff --git a/Assets/VRM10/Editor/Vrm10ExportDialog.cs b/Assets/VRM10/Editor/Vrm10ExportDialog.cs
index 696966175..b2518cd9c 100644
--- a/Assets/VRM10/Editor/Vrm10ExportDialog.cs
+++ b/Assets/VRM10/Editor/Vrm10ExportDialog.cs
@@ -266,29 +266,32 @@ namespace UniVRM10
try
{
- var converter = new UniVRM10.ModelExporter();
- var model = converter.Export(root);
-
- // 右手系に変換
- m_logLabel += $"convert to right handed coordinate...\n";
- model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);
-
- // export vrm-1.0
- var exporter = new UniVRM10.Vrm10Exporter(new EditorTextureSerializer(), new GltfExportSettings());
- var option = new VrmLib.ExportArgs();
- exporter.Export(root, model, converter, option, Vrm ? Vrm.Meta : m_tmpObject.Meta);
-
- var exportedBytes = exporter.Storage.ToBytes();
-
- m_logLabel += $"write to {path}...\n";
- File.WriteAllBytes(path, exportedBytes);
- Debug.Log("exportedBytes: " + exportedBytes.Length);
-
- var assetPath = UniGLTF.UnityPath.FromFullpath(path);
- if (assetPath.IsUnderAssetsFolder)
+ using (var arrayManager = new NativeArrayManager())
{
- // asset folder 内。import を発動
- assetPath.ImportAsset();
+ var converter = new UniVRM10.ModelExporter();
+ var model = converter.Export(arrayManager, root);
+
+ // 右手系に変換
+ m_logLabel += $"convert to right handed coordinate...\n";
+ model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);
+
+ // export vrm-1.0
+ var exporter = new UniVRM10.Vrm10Exporter(new EditorTextureSerializer(), new GltfExportSettings());
+ var option = new VrmLib.ExportArgs();
+ exporter.Export(root, model, converter, option, Vrm ? Vrm.Meta : m_tmpObject.Meta);
+
+ var exportedBytes = exporter.Storage.ToGlbBytes();
+
+ m_logLabel += $"write to {path}...\n";
+ File.WriteAllBytes(path, exportedBytes);
+ Debug.Log("exportedBytes: " + exportedBytes.Length);
+
+ var assetPath = UniGLTF.UnityPath.FromFullpath(path);
+ if (assetPath.IsUnderAssetsFolder)
+ {
+ // asset folder 内。import を発動
+ assetPath.ImportAsset();
+ }
}
}
catch (Exception ex)
diff --git a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs
index 940f06551..c3ae9509c 100644
--- a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs
+++ b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Numerics;
using UniGLTF;
+using Unity.Collections;
namespace UniVRM10
{
@@ -30,7 +31,7 @@ namespace UniVRM10
}
public static int AddViewTo(this VrmLib.BufferAccessor self,
- Vrm10ExportData storage, int bufferIndex,
+ ExportingGltfData data, int bufferIndex,
int offset = 0, int count = 0)
{
var stride = self.Stride;
@@ -38,8 +39,8 @@ namespace UniVRM10
{
count = self.Count;
}
- var slice = self.Bytes.Slice(offset * stride, count * stride);
- return storage.AppendToBuffer(slice);
+ var slice = self.Bytes.GetSubArray(offset * stride, count * stride);
+ return data.AppendToBuffer(slice);
}
static glTFAccessor CreateGltfAccessor(this VrmLib.BufferAccessor self,
@@ -60,11 +61,11 @@ namespace UniVRM10
}
public static int AddAccessorTo(this VrmLib.BufferAccessor self,
- Vrm10ExportData storage, int viewIndex,
- Action, glTFAccessor> minMax = null,
+ ExportingGltfData data, int viewIndex,
+ Action, glTFAccessor> minMax = null,
int offset = 0, int count = 0)
{
- var gltf = storage.GLTF;
+ var gltf = data.Gltf;
var accessorIndex = gltf.accessors.Count;
var accessor = self.CreateGltfAccessor(viewIndex, count, offset * self.Stride);
if (minMax != null)
@@ -76,10 +77,10 @@ namespace UniVRM10
}
public static int AddAccessorTo(this VrmLib.BufferAccessor self,
- Vrm10ExportData storage, int bufferIndex,
+ ExportingGltfData data, int bufferIndex,
// GltfBufferTargetType targetType,
bool useSparse,
- Action, glTFAccessor> minMax = null,
+ Action, glTFAccessor> minMax = null,
int offset = 0, int count = 0)
{
if (self.ComponentType == VrmLib.AccessorValueType.FLOAT
@@ -104,53 +105,55 @@ namespace UniVRM10
&& sparseValuesWithIndex.Count * 16 < values.Length * 12)
{
// use sparse
- var sparseIndexBin = new ArraySegment(new byte[sparseValuesWithIndex.Count * 4]);
- var sparseIndexSpan = SpanLike.Wrap(sparseIndexBin);
- var sparseValueBin = new ArraySegment(new byte[sparseValuesWithIndex.Count * 12]);
- var sparseValueSpan = SpanLike.Wrap(sparseValueBin);
-
- for (int i = 0; i < sparseValuesWithIndex.Count; ++i)
+ using (var sparseIndexBin = new NativeArray(sparseValuesWithIndex.Count * 4, Allocator.Persistent))
+ using (var sparseValueBin = new NativeArray(sparseValuesWithIndex.Count * 12, Allocator.Persistent))
{
- var (index, value) = sparseValuesWithIndex[i];
- sparseIndexSpan[i] = index;
- sparseValueSpan[i] = value;
- }
+ var sparseIndexSpan = sparseIndexBin.Reinterpret(1);
+ var sparseValueSpan = sparseValueBin.Reinterpret(1);
- var sparseIndexView = storage.AppendToBuffer(sparseIndexBin);
- var sparseValueView = storage.AppendToBuffer(sparseValueBin);
-
- var accessorIndex = storage.GLTF.accessors.Count;
- var accessor = new glTFAccessor
- {
- componentType = (glComponentType)self.ComponentType,
- type = self.AccessorType.ToString(),
- count = self.Count,
- byteOffset = -1,
- sparse = new glTFSparse
+ for (int i = 0; i < sparseValuesWithIndex.Count; ++i)
{
- count = sparseValuesWithIndex.Count,
- indices = new glTFSparseIndices
- {
- componentType = (glComponentType)VrmLib.AccessorValueType.UNSIGNED_INT,
- bufferView = sparseIndexView,
- },
- values = new glTFSparseValues
- {
- bufferView = sparseValueView,
- },
+ var (index, value) = sparseValuesWithIndex[i];
+ sparseIndexSpan[i] = index;
+ sparseValueSpan[i] = value;
}
- };
- if (minMax != null)
- {
- minMax(sparseValueBin, accessor);
+
+ var sparseIndexView = data.AppendToBuffer(sparseIndexBin);
+ var sparseValueView = data.AppendToBuffer(sparseValueBin);
+
+ var accessorIndex = data.Gltf.accessors.Count;
+ var accessor = new glTFAccessor
+ {
+ componentType = (glComponentType)self.ComponentType,
+ type = self.AccessorType.ToString(),
+ count = self.Count,
+ byteOffset = -1,
+ sparse = new glTFSparse
+ {
+ count = sparseValuesWithIndex.Count,
+ indices = new glTFSparseIndices
+ {
+ componentType = (glComponentType)VrmLib.AccessorValueType.UNSIGNED_INT,
+ bufferView = sparseIndexView,
+ },
+ values = new glTFSparseValues
+ {
+ bufferView = sparseValueView,
+ },
+ }
+ };
+ if (minMax != null)
+ {
+ minMax(sparseValueBin, accessor);
+ }
+ data.Gltf.accessors.Add(accessor);
+ return accessorIndex;
}
- storage.GLTF.accessors.Add(accessor);
- return accessorIndex;
}
}
- var viewIndex = self.AddViewTo(storage, bufferIndex, offset, count);
- return self.AddAccessorTo(storage, viewIndex, minMax, 0, count);
+ var viewIndex = self.AddViewTo(data, bufferIndex, offset, count);
+ return self.AddAccessorTo(data, viewIndex, minMax, 0, count);
}
}
}
diff --git a/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs b/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs
index 7fdf0c4fe..aa86ff17b 100644
--- a/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs
+++ b/Assets/VRM10/Runtime/IO/Model/MeshImporter.cs
@@ -23,12 +23,12 @@ namespace UniVRM10
Profiler.BeginSample("MeshImporter.LoadSharedMesh");
var mesh = new Mesh();
- var positions = src.VertexBuffer.Positions.AsNativeArray(Allocator.TempJob);
- var normals = src.VertexBuffer.Normals?.AsNativeArray(Allocator.TempJob) ?? default;
- var texCoords = src.VertexBuffer.TexCoords?.AsNativeArray(Allocator.TempJob) ?? default;
- var colors = src.VertexBuffer.Colors?.AsNativeArray(Allocator.TempJob) ?? default;
- var weights = src.VertexBuffer.Weights?.AsNativeArray(Allocator.TempJob) ?? default;
- var joints = src.VertexBuffer.Joints?.AsNativeArray(Allocator.TempJob) ?? default;
+ var positions = src.VertexBuffer.Positions.Bytes.Reinterpret(1);
+ var normals = src.VertexBuffer.Normals?.Bytes.Reinterpret(1) ?? default;
+ var texCoords = src.VertexBuffer.TexCoords?.Bytes.Reinterpret(1) ?? default;
+ var colors = src.VertexBuffer.Colors?.Bytes.Reinterpret(1) ?? default;
+ var weights = src.VertexBuffer.Weights?.Bytes.Reinterpret(1) ?? default;
+ var joints = src.VertexBuffer.Joints?.Bytes.Reinterpret(1) ?? default;
using (var vertices = new NativeArray(positions.Length, Allocator.TempJob))
{
@@ -55,14 +55,6 @@ namespace UniVRM10
// Jobを完了
jobHandle.Complete();
- // 入力のNativeArrayを開放
- positions.Dispose();
- if (normals.IsCreated) normals.Dispose();
- if (texCoords.IsCreated) texCoords.Dispose();
- if (colors.IsCreated) colors.Dispose();
- if (weights.IsCreated) weights.Dispose();
- if (joints.IsCreated) joints.Dispose();
-
// 頂点を更新
MeshVertex.SetVertexBufferParamsToMesh(mesh, vertices.Length);
mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length);
@@ -74,16 +66,14 @@ namespace UniVRM10
switch (src.IndexBuffer.ComponentType)
{
case AccessorValueType.UNSIGNED_SHORT:
- var shortIndices = src.IndexBuffer.AsNativeArray(Allocator.Temp);
+ var shortIndices = src.IndexBuffer.Bytes.Reinterpret(1);
mesh.SetIndexBufferParams(shortIndices.Length, IndexFormat.UInt16);
mesh.SetIndexBufferData(shortIndices, 0, 0, shortIndices.Length);
- shortIndices.Dispose();
break;
case AccessorValueType.UNSIGNED_INT:
- var intIndices = src.IndexBuffer.AsNativeArray(Allocator.Temp);
+ var intIndices = src.IndexBuffer.Bytes.Reinterpret(1);
mesh.SetIndexBufferParams(intIndices.Length, IndexFormat.UInt32);
mesh.SetIndexBufferData(intIndices, 0, 0, intIndices.Length);
- intIndices.Dispose();
break;
default:
throw new NotImplementedException();
diff --git a/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs b/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs
index f27badeea..bda8578c2 100644
--- a/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs
+++ b/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs
@@ -126,8 +126,7 @@ namespace UniVRM10
case AccessorValueType.SHORT:
{
// unsigned short -> unsigned short
- var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob);
- disposables.Add(source);
+ var source = mesh.IndexBuffer.Bytes.Reinterpret(1);
jobHandle = new CopyIndicesJobs.Ushort2Ushort(
(ushort)vertexOffset,
new NativeSlice(source),
@@ -138,8 +137,7 @@ namespace UniVRM10
case AccessorValueType.UNSIGNED_INT:
{
// unsigned int -> unsigned short
- var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob);
- disposables.Add(source);
+ var source = mesh.IndexBuffer.Bytes.Reinterpret(1);
jobHandle = new CopyIndicesJobs.Uint2Ushort(
(ushort)vertexOffset,
source,
@@ -173,8 +171,7 @@ namespace UniVRM10
case AccessorValueType.SHORT:
{
// unsigned short -> unsigned int
- var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob);
- disposables.Add(source);
+ var source = mesh.IndexBuffer.Bytes.Reinterpret(1);
jobHandle = new CopyIndicesJobs.Ushort2Uint(
(uint)vertexOffset,
source,
@@ -185,8 +182,7 @@ namespace UniVRM10
case AccessorValueType.UNSIGNED_INT:
{
// unsigned int -> unsigned int
- var source = mesh.IndexBuffer.AsNativeArray(Allocator.TempJob);
- disposables.Add(source);
+ var source = mesh.IndexBuffer.Bytes.Reinterpret(1);
jobHandle = new CopyIndicesJobs.UInt2UInt(
(uint)vertexOffset,
source,
@@ -237,20 +233,15 @@ namespace UniVRM10
foreach (var mesh in meshGroup.Meshes)
{
- var positions = mesh.VertexBuffer.Positions.AsNativeArray(Allocator.TempJob);
- var normals = mesh.VertexBuffer.Normals.AsNativeArray(Allocator.TempJob);
- var texCoords = mesh.VertexBuffer.TexCoords.AsNativeArray(Allocator.TempJob);
+ var positions = mesh.VertexBuffer.Positions.Bytes.Reinterpret(1);
+ var normals = mesh.VertexBuffer.Normals.Bytes.Reinterpret(1);
+ var texCoords = mesh.VertexBuffer.TexCoords.Bytes.Reinterpret(1);
var weights = meshGroup.Skin != null
- ? mesh.VertexBuffer.Weights.AsNativeArray(Allocator.TempJob)
+ ? mesh.VertexBuffer.Weights.Bytes.Reinterpret(1)
: default;
var joints = meshGroup.Skin != null
- ? mesh.VertexBuffer.Joints.AsNativeArray(Allocator.TempJob)
+ ? mesh.VertexBuffer.Joints.Bytes.Reinterpret(1)
: default;
- if (positions.IsCreated) disposables.Add(positions);
- if (normals.IsCreated) disposables.Add(normals);
- if (texCoords.IsCreated) disposables.Add(texCoords);
- if (weights.IsCreated) disposables.Add(weights);
- if (joints.IsCreated) disposables.Add(joints);
interleaveVertexJob = new InterleaveMeshVerticesJob(
new NativeSlice(vertices, indexOffset, mesh.VertexBuffer.Count),
diff --git a/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs b/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs
index 1ce60e8e9..7a7ef16f7 100644
--- a/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs
+++ b/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Numerics;
using UniGLTF;
+using Unity.Collections;
using VrmLib;
namespace UniVRM10
@@ -12,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(Vrm10ExportData storage, 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(storage, 0, option.sparse, null, 0, count);
- }
- else
- {
- return x.AddAccessorTo(storage, 0, option.sparse, null, offset, count);
- }
- }
- else
- {
- return x.AddAccessorTo(storage, 0, option.sparse, null, offset, count);
- }
- }
-
///
/// https://github.com/vrm-c/UniVRM/issues/800
///
@@ -62,14 +20,14 @@ namespace UniVRM10
///
///
///
- ///
+ ///
///
///
static IEnumerable ExportMeshDivided(this VrmLib.Mesh mesh, List