mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-08-24 11:35:24 -05:00
use UniGLTF.IBytesBuffer
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
|
||||
namespace UniGLTF
|
||||
@@ -27,9 +26,11 @@ namespace UniGLTF
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ArraySegment<byte> GetBytes()
|
||||
public void ExtendCapacity(int capacity)
|
||||
{
|
||||
return m_bytes;
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ArraySegment<byte> Bytes => m_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ namespace UniGLTF
|
||||
public interface IBytesBuffer
|
||||
{
|
||||
string Uri { get; }
|
||||
ArraySegment<Byte> GetBytes();
|
||||
glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct;
|
||||
ArraySegment<Byte> Bytes { get; }
|
||||
glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target = glBufferTarget.NONE) where T : struct;
|
||||
void ExtendCapacity(int capacity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,13 +40,13 @@ namespace UniGLTF
|
||||
public glTFBufferView Append<T>(ArraySegment<T> segment, glBufferTarget target) where T : struct
|
||||
{
|
||||
var view = Storage.Extend(segment, target);
|
||||
byteLength = Storage.GetBytes().Count;
|
||||
byteLength = Storage.Bytes.Count;
|
||||
return view;
|
||||
}
|
||||
|
||||
public ArraySegment<Byte> GetBytes()
|
||||
{
|
||||
return Storage.GetBytes();
|
||||
return Storage.Bytes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
98
Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs
Normal file
98
Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
/// <summary>
|
||||
/// for exporter
|
||||
/// </summary>
|
||||
public class ArrayByteBuffer : IBytesBuffer
|
||||
{
|
||||
public string Uri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
Byte[] m_bytes;
|
||||
int m_used;
|
||||
|
||||
public ArrayByteBuffer(Byte[] bytes = null)
|
||||
{
|
||||
Uri = "";
|
||||
m_bytes = bytes;
|
||||
}
|
||||
|
||||
public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct
|
||||
{
|
||||
using (var pin = Pin.Create(array))
|
||||
{
|
||||
var elementSize = Marshal.SizeOf(typeof(T));
|
||||
var view = Extend(pin.Ptr, array.Count * elementSize, elementSize, target);
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarget target)
|
||||
{
|
||||
var tmp = m_bytes;
|
||||
// alignment
|
||||
var padding = m_used % stride == 0 ? 0 : stride - m_used % stride;
|
||||
|
||||
if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length)
|
||||
{
|
||||
// recreate buffer
|
||||
m_bytes = new Byte[m_used + padding + bytesLength];
|
||||
if (m_used > 0)
|
||||
{
|
||||
Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used);
|
||||
}
|
||||
}
|
||||
if (m_used + padding + bytesLength > m_bytes.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
Marshal.Copy(p, m_bytes, m_used + padding, bytesLength);
|
||||
var result = new glTFBufferView
|
||||
{
|
||||
buffer = 0,
|
||||
byteLength = bytesLength,
|
||||
byteOffset = m_used + padding,
|
||||
byteStride = stride,
|
||||
target = target,
|
||||
};
|
||||
m_used = m_used + padding + bytesLength;
|
||||
return result;
|
||||
}
|
||||
|
||||
public void ExtendCapacity(int capacity)
|
||||
{
|
||||
if (m_bytes != null && capacity < m_bytes.Length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newBuffer = new byte[capacity];
|
||||
if (m_bytes != null && m_used > 0)
|
||||
{
|
||||
Buffer.BlockCopy(m_bytes, 0, newBuffer, 0, m_used);
|
||||
}
|
||||
m_bytes = newBuffer;
|
||||
}
|
||||
|
||||
public ArraySegment<byte> Bytes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_bytes == null)
|
||||
{
|
||||
return new ArraySegment<byte>();
|
||||
}
|
||||
|
||||
return new ArraySegment<byte>(m_bytes, 0, m_used);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e9c9201942704b4f9dd050115073032
|
||||
guid: d095d4c4eb5e68f4bb10c21bb3604cdb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -1,158 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public static class IBytesBufferExtensions
|
||||
{
|
||||
public static glTFBufferView Extend<T>(this IBytesBuffer buffer, T[] array, glBufferTarget target) where T : struct
|
||||
{
|
||||
return buffer.Extend(new ArraySegment<T>(array), target);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// for buffer with uri read
|
||||
/// </summary>
|
||||
public class UriByteBuffer : IBytesBuffer
|
||||
{
|
||||
public string Uri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
Byte[] m_bytes;
|
||||
public ArraySegment<byte> GetBytes()
|
||||
{
|
||||
return new ArraySegment<byte>(m_bytes);
|
||||
}
|
||||
|
||||
public UriByteBuffer(string baseDir, string uri)
|
||||
{
|
||||
Uri = uri;
|
||||
m_bytes = ReadFromUri(baseDir, uri);
|
||||
}
|
||||
|
||||
const string DataPrefix = "data:application/octet-stream;base64,";
|
||||
|
||||
const string DataPrefix2 = "data:application/gltf-buffer;base64,";
|
||||
|
||||
const string DataPrefix3 = "data:image/jpeg;base64,";
|
||||
|
||||
[Obsolete("Use ReadEmbedded(uri)")]
|
||||
public static Byte[] ReadEmbeded(string uri)
|
||||
{
|
||||
return ReadEmbedded(uri);
|
||||
}
|
||||
|
||||
public static Byte[] ReadEmbedded(string uri)
|
||||
{
|
||||
var pos = uri.IndexOf(";base64,");
|
||||
if (pos < 0)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Convert.FromBase64String(uri.Substring(pos + 8));
|
||||
}
|
||||
}
|
||||
|
||||
Byte[] ReadFromUri(string baseDir, string uri)
|
||||
{
|
||||
var bytes = ReadEmbedded(uri);
|
||||
if (bytes != null)
|
||||
{
|
||||
return bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
// as local file path
|
||||
return File.ReadAllBytes(Path.Combine(baseDir, uri));
|
||||
}
|
||||
}
|
||||
|
||||
public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// for exporter
|
||||
/// </summary>
|
||||
public class ArrayByteBuffer : IBytesBuffer
|
||||
{
|
||||
public string Uri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
Byte[] m_bytes;
|
||||
int m_used;
|
||||
|
||||
public ArrayByteBuffer(Byte[] bytes = null)
|
||||
{
|
||||
Uri = "";
|
||||
m_bytes = bytes;
|
||||
}
|
||||
|
||||
public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct
|
||||
{
|
||||
using (var pin = Pin.Create(array))
|
||||
{
|
||||
var elementSize = Marshal.SizeOf(typeof(T));
|
||||
var view = Extend(pin.Ptr, array.Count * elementSize, elementSize, target);
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarget target)
|
||||
{
|
||||
var tmp = m_bytes;
|
||||
// alignment
|
||||
var padding = m_used % stride == 0 ? 0 : stride - m_used % stride;
|
||||
|
||||
if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length)
|
||||
{
|
||||
// recreate buffer
|
||||
m_bytes = new Byte[m_used + padding + bytesLength];
|
||||
if (m_used > 0)
|
||||
{
|
||||
Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used);
|
||||
}
|
||||
}
|
||||
if (m_used + padding + bytesLength > m_bytes.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
Marshal.Copy(p, m_bytes, m_used + padding, bytesLength);
|
||||
var result=new glTFBufferView
|
||||
{
|
||||
buffer = 0,
|
||||
byteLength = bytesLength,
|
||||
byteOffset = m_used + padding,
|
||||
byteStride = stride,
|
||||
target = target,
|
||||
};
|
||||
m_used = m_used + padding + bytesLength;
|
||||
return result;
|
||||
}
|
||||
|
||||
public ArraySegment<byte> GetBytes()
|
||||
{
|
||||
if (m_bytes == null)
|
||||
{
|
||||
return new ArraySegment<byte>();
|
||||
}
|
||||
|
||||
return new ArraySegment<byte>(m_bytes, 0, m_used);
|
||||
}
|
||||
}
|
||||
}
|
||||
75
Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs
Normal file
75
Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
/// <summary>
|
||||
/// for buffer with uri read
|
||||
/// </summary>
|
||||
public class UriByteBuffer : IBytesBuffer
|
||||
{
|
||||
public string Uri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
Byte[] m_bytes;
|
||||
public ArraySegment<byte> Bytes => new ArraySegment<byte>(m_bytes);
|
||||
|
||||
public UriByteBuffer(string baseDir, string uri)
|
||||
{
|
||||
Uri = uri;
|
||||
m_bytes = ReadFromUri(baseDir, uri);
|
||||
}
|
||||
|
||||
const string DataPrefix = "data:application/octet-stream;base64,";
|
||||
|
||||
const string DataPrefix2 = "data:application/gltf-buffer;base64,";
|
||||
|
||||
const string DataPrefix3 = "data:image/jpeg;base64,";
|
||||
|
||||
[Obsolete("Use ReadEmbedded(uri)")]
|
||||
public static Byte[] ReadEmbeded(string uri)
|
||||
{
|
||||
return ReadEmbedded(uri);
|
||||
}
|
||||
|
||||
public static Byte[] ReadEmbedded(string uri)
|
||||
{
|
||||
var pos = uri.IndexOf(";base64,");
|
||||
if (pos < 0)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Convert.FromBase64String(uri.Substring(pos + 8));
|
||||
}
|
||||
}
|
||||
|
||||
Byte[] ReadFromUri(string baseDir, string uri)
|
||||
{
|
||||
var bytes = ReadEmbedded(uri);
|
||||
if (bytes != null)
|
||||
{
|
||||
return bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
// as local file path
|
||||
return File.ReadAllBytes(Path.Combine(baseDir, uri));
|
||||
}
|
||||
}
|
||||
|
||||
public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void ExtendCapacity(int capacity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c700d6d6fbb38fa4d9662213c58d5725
|
||||
guid: 8b75aca0e4a7415418cd1c3d018483b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -1,89 +0,0 @@
|
||||
using System;
|
||||
using VrmLib;
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
/// <summary>
|
||||
/// for exporter
|
||||
/// </summary>
|
||||
public class ArrayByteBuffer10
|
||||
{
|
||||
public ArraySegment<byte> Bytes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_bytes == null)
|
||||
{
|
||||
return new ArraySegment<byte>();
|
||||
}
|
||||
|
||||
return new ArraySegment<byte>(m_bytes, 0, m_used);
|
||||
}
|
||||
}
|
||||
|
||||
Byte[] m_bytes;
|
||||
int m_used = 0;
|
||||
|
||||
public ArrayByteBuffer10(Byte[] bytes = null)
|
||||
{
|
||||
m_bytes = bytes ?? (new byte[] { });
|
||||
}
|
||||
|
||||
public ArrayByteBuffer10(Byte[] bytes, int used)
|
||||
{
|
||||
m_bytes = bytes ?? (new byte[] { });
|
||||
m_used = used;
|
||||
}
|
||||
|
||||
|
||||
public void ExtendCapacity(int byteLength)
|
||||
{
|
||||
var backup = m_bytes;
|
||||
m_bytes = new byte[backup.Length + byteLength];
|
||||
backup.CopyTo(m_bytes, backup.Length);
|
||||
}
|
||||
|
||||
public void Extend(ArraySegment<byte> array, out int offset, out int length)
|
||||
{
|
||||
var tmp = m_bytes;
|
||||
// alignment
|
||||
var padding = 0; // m_used % stride == 0 ? 0 : stride - m_used % stride;
|
||||
|
||||
if (m_bytes == null || m_used + padding + array.Count > m_bytes.Length)
|
||||
{
|
||||
// recreate buffer
|
||||
var newSize = Math.Max(m_used + padding + array.Count, m_bytes.Length * 2);
|
||||
m_bytes = new Byte[newSize];
|
||||
if (m_used > 0)
|
||||
{
|
||||
Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used);
|
||||
}
|
||||
}
|
||||
if (m_used + padding + array.Count > m_bytes.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
Buffer.BlockCopy(array.Array, array.Offset, m_bytes, m_used + padding, array.Count);
|
||||
|
||||
length = array.Count;
|
||||
offset = m_used + padding;
|
||||
|
||||
|
||||
// var result = new GltfBufferView
|
||||
// {
|
||||
// buffer = 0,
|
||||
// byteLength = array.Length,
|
||||
// byteOffset = m_used + padding,
|
||||
// target = target,
|
||||
// };
|
||||
// if (target == GltfBufferTargetType.ARRAY_BUFFER)
|
||||
// {
|
||||
// result.byteStride = stride;
|
||||
// }
|
||||
|
||||
m_used = m_used + padding + array.Count;
|
||||
// return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ namespace UniVRM10
|
||||
private set;
|
||||
}
|
||||
|
||||
public readonly List<ArrayByteBuffer10> Buffers;
|
||||
public readonly List<UniGLTF.IBytesBuffer> Buffers;
|
||||
|
||||
public UniGLTF.Extensions.VRMC_vrm.VRMC_vrm gltfVrm;
|
||||
|
||||
@@ -33,9 +33,9 @@ namespace UniVRM10
|
||||
{
|
||||
extensionsUsed = new List<string>(),
|
||||
};
|
||||
Buffers = new List<ArrayByteBuffer10>()
|
||||
Buffers = new List<UniGLTF.IBytesBuffer>()
|
||||
{
|
||||
new ArrayByteBuffer10()
|
||||
new UniGLTF.ArrayByteBuffer()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -61,10 +61,9 @@ namespace UniVRM10
|
||||
gltfVrmSpringBone = springBone;
|
||||
}
|
||||
|
||||
var array = bin.ToArray();
|
||||
Buffers = new List<ArrayByteBuffer10>()
|
||||
Buffers = new List<UniGLTF.IBytesBuffer>()
|
||||
{
|
||||
new ArrayByteBuffer10(array, bin.Count)
|
||||
new UniGLTF.ArraySegmentByteBuffer(bin)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -75,14 +74,9 @@ namespace UniVRM10
|
||||
|
||||
public int AppendToBuffer(int bufferIndex, ArraySegment<byte> segment)
|
||||
{
|
||||
Buffers[bufferIndex].Extend(segment, out int offset, out int length);
|
||||
var gltfBufferView = Buffers[bufferIndex].Extend(segment);
|
||||
var viewIndex = Gltf.bufferViews.Count;
|
||||
Gltf.bufferViews.Add(new UniGLTF.glTFBufferView
|
||||
{
|
||||
buffer = 0,
|
||||
byteOffset = offset,
|
||||
byteLength = length,
|
||||
});
|
||||
Gltf.bufferViews.Add(gltfBufferView);
|
||||
return viewIndex;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user