BytesBuffer.cs
4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace UniGLTF
{
public interface IBytesBuffer
{
string Uri { get; }
ArraySegment<Byte> GetBytes();
glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct;
}
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,";
public static Byte[] ReadEmbeded(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 = ReadEmbeded(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 glb chunk buffer read
/// </summary>
public class ArraySegmentByteBuffer : IBytesBuffer
{
ArraySegment<Byte> m_bytes;
public ArraySegmentByteBuffer(ArraySegment<Byte> bytes)
{
m_bytes = bytes;
}
public string Uri
{
get;
private set;
}
public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct
{
throw new NotImplementedException();
}
public ArraySegment<byte> GetBytes()
{
return m_bytes;
}
}
/// <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);
}
}
}