glTFExtensions.cs
6.77 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
namespace UniGLTF
{
[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
struct UShort4
{
public ushort x;
public ushort y;
public ushort z;
public ushort w;
public UShort4(ushort _x, ushort _y, ushort _z, ushort _w)
{
x = _x;
y = _y;
z = _z;
w = _w;
}
}
public static class glTFExtensions
{
struct ComponentVec
{
public glComponentType ComponentType;
public int ElementCount;
public ComponentVec(glComponentType componentType, int elementCount)
{
ComponentType = componentType;
ElementCount = elementCount;
}
}
static Dictionary<Type, ComponentVec> ComponentTypeMap = new Dictionary<Type, ComponentVec>
{
{ typeof(Vector2), new ComponentVec(glComponentType.FLOAT, 2) },
{ typeof(Vector3), new ComponentVec(glComponentType.FLOAT, 3) },
{ typeof(Vector4), new ComponentVec(glComponentType.FLOAT, 4) },
{ typeof(UShort4), new ComponentVec(glComponentType.UNSIGNED_SHORT, 4) },
{ typeof(Matrix4x4), new ComponentVec(glComponentType.FLOAT, 16) },
{ typeof(Color), new ComponentVec(glComponentType.FLOAT, 4) },
};
static glComponentType GetComponentType<T>()
{
var cv = default(ComponentVec);
if (ComponentTypeMap.TryGetValue(typeof(T), out cv))
{
return cv.ComponentType;
}
else if (typeof(T) == typeof(uint))
{
return glComponentType.UNSIGNED_INT;
}
else if (typeof(T) == typeof(float))
{
return glComponentType.FLOAT;
}
else
{
throw new NotImplementedException(typeof(T).Name);
}
}
static string GetAccessorType<T>()
{
var cv = default(ComponentVec);
if (ComponentTypeMap.TryGetValue(typeof(T), out cv))
{
switch (cv.ElementCount)
{
case 2: return "VEC2";
case 3: return "VEC3";
case 4: return "VEC4";
case 16: return "MAT4";
default: throw new Exception();
}
}
else
{
return "SCALAR";
}
}
static int GetAccessorElementCount<T>()
{
var cv = default(ComponentVec);
if (ComponentTypeMap.TryGetValue(typeof(T), out cv))
{
return cv.ElementCount;
}
else
{
return 1;
}
}
public static int ExtendBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex, T[] array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
return gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, new ArraySegment<T>(array), target);
}
public static int ExtendBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex,
ArraySegment<T> array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
if (array.Count == 0)
{
return -1;
}
var viewIndex = ExtendBufferAndGetViewIndex(gltf, bufferIndex, array, target);
// index buffer's byteStride is unnecessary
gltf.bufferViews[viewIndex].byteStride = 0;
var accessorIndex = gltf.accessors.Count;
gltf.accessors.Add(new glTFAccessor
{
bufferView = viewIndex,
byteOffset = 0,
componentType = GetComponentType<T>(),
type = GetAccessorType<T>(),
count = array.Count,
});
return accessorIndex;
}
public static int ExtendBufferAndGetViewIndex<T>(this glTF gltf, int bufferIndex,
T[] array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
return ExtendBufferAndGetViewIndex(gltf, bufferIndex, new ArraySegment<T>(array), target);
}
public static int ExtendBufferAndGetViewIndex<T>(this glTF gltf, int bufferIndex,
ArraySegment<T> array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
if (array.Count == 0)
{
return -1;
}
var view = gltf.buffers[bufferIndex].Append(array, target);
var viewIndex = gltf.bufferViews.Count;
gltf.bufferViews.Add(view);
return viewIndex;
}
public static int ExtendSparseBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex,
int accessorCount,
T[] sparseValues, int[] sparseIndices, int sparseViewIndex,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
return ExtendSparseBufferAndGetAccessorIndex(gltf, bufferIndex,
accessorCount,
new ArraySegment<T>(sparseValues), sparseIndices, sparseViewIndex,
target);
}
public static int ExtendSparseBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex,
int accessorCount,
ArraySegment<T> sparseValues, int[] sparseIndices, int sparseIndicesViewIndex,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
if (sparseValues.Count == 0)
{
return -1;
}
var sparseValuesViewIndex = ExtendBufferAndGetViewIndex(gltf, bufferIndex, sparseValues, target);
var accessorIndex = gltf.accessors.Count;
gltf.accessors.Add(new glTFAccessor
{
byteOffset = 0,
componentType = GetComponentType<T>(),
type = GetAccessorType<T>(),
count = accessorCount,
sparse = new glTFSparse
{
count=sparseIndices.Length,
indices = new glTFSparseIndices
{
bufferView = sparseIndicesViewIndex,
componentType = glComponentType.UNSIGNED_INT
},
values = new glTFSparseValues
{
bufferView = sparseValuesViewIndex,
}
}
});
return accessorIndex;
}
}
}