/
AbcAPI.cs
460 lines (401 loc) · 17.3 KB
/
AbcAPI.cs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UTJ.Alembic
{
public partial class AbcAPI
{
public enum aiAspectRatioMode
{
CurrentResolution = 0,
DefaultResolution =1,
CameraAperture
};
public enum aiNormalsMode
{
ReadFromFile = 0,
ComputeIfMissing,
AlwaysCompute,
Ignore
}
public enum aiTangentsMode
{
None = 0,
Smooth,
Split
}
public enum aiTopologyVariance
{
Constant,
Homogeneous,
Heterogeneous
}
public enum aiPropertyType
{
Unknown,
// scalar types
Bool,
Int,
UInt,
Float,
Float2,
Float3,
Float4,
Float4x4,
// array types
BoolArray,
IntArray,
UIntArray,
FloatArray,
Float2Array,
Float3Array,
Float4Array,
Float4x4Array,
ScalarTypeBegin = Bool,
ScalarTypeEnd = Float4x4,
ArrayTypeBegin = BoolArray,
ArrayTypeEnd = Float4x4Array,
};
public delegate void aiNodeEnumerator(aiObject obj, IntPtr userData);
public delegate void aiConfigCallback(IntPtr _this, ref aiConfig config);
public delegate void aiSampleCallback(IntPtr _this, aiSample sample, Bool topologyChanged);
public struct aiConfig
{
public Bool swapHandedness;
public Bool swapFaceWinding;
public aiNormalsMode normalsMode;
public aiTangentsMode tangentsMode;
public Bool cacheTangentsSplits;
public float aspectRatio;
public Bool forceUpdate;
public Bool cacheSamples;
public Bool shareVertices;
public Bool treatVertexExtraDataAsStatics;
public Bool interpolateSamples;
public Bool turnQuadEdges;
public float vertexMotionScale;
public Bool use32BitsIndexBuffer;
public void SetDefaults()
{
swapHandedness = true;
swapFaceWinding = false;
normalsMode = aiNormalsMode.ComputeIfMissing;
tangentsMode = aiTangentsMode.None;
cacheTangentsSplits = true;
aspectRatio = -1.0f;
forceUpdate = false;
cacheSamples = false;
shareVertices = true;
treatVertexExtraDataAsStatics = false;
interpolateSamples = true;
turnQuadEdges = false;
vertexMotionScale = 1.0f;
use32BitsIndexBuffer = false;
}
}
public struct aiSampleSelector
{
public ulong requestedIndex;
public double requestedTime;
public int requestedTimeIndexType;
}
public struct aiFacesets
{
public int count;
public IntPtr faceCounts;
public IntPtr faceIndices;
}
public struct aiMeshSummary
{
public aiTopologyVariance topologyVariance;
public int peakVertexCount;
public int peakIndexCount;
public int peakTriangulatedIndexCount;
public int peakSubmeshCount;
}
public struct aiMeshSampleSummary
{
public int splitCount;
public Bool hasNormals;
public Bool hasUVs;
public Bool hasTangents;
public Bool hasVelocities;
}
public struct aiPolyMeshData
{
public IntPtr positions;
public IntPtr velocities;
public IntPtr interpolatedVelocitiesXY;
public IntPtr interpolatedVelocitiesZ;
public IntPtr normals;
public IntPtr uvs;
public IntPtr tangents;
public IntPtr indices;
public IntPtr normalIndices;
public IntPtr uvIndices;
public IntPtr faces;
public int positionCount;
public int normalCount;
public int uvCount;
public int indexCount;
public int normalIndexCount;
public int uvIndexCount;
public int faceCount;
public int triangulatedIndexCount;
public Vector3 center;
public Vector3 size;
}
public struct aiSubmeshSummary
{
public int index;
public int splitIndex;
public int splitSubmeshIndex;
public int facesetIndex;
public int triangleCount;
}
public struct aiSubmeshData
{
public IntPtr indices;
}
public struct aiXFormData
{
public Vector3 translation;
public Quaternion rotation;
public Vector3 scale;
public Bool inherits;
}
public struct aiCameraData
{
public float nearClippingPlane;
public float farClippingPlane;
public float fieldOfView; // in degree. vertical one
public float aspectRatio;
public float focusDistance; // in cm
public float focalLength; // in mm
public float aperture; // in cm. vertical one
}
public struct aiContext
{
public System.IntPtr ptr;
public static implicit operator bool(aiContext v) { return v.ptr != IntPtr.Zero; }
}
public struct aiObject
{
public System.IntPtr ptr;
public static implicit operator bool(aiObject v) { return v.ptr != IntPtr.Zero; }
}
public struct aiSchema
{
public System.IntPtr ptr;
public static implicit operator bool(aiSchema v) { return v.ptr != IntPtr.Zero; }
}
public struct aiProperty
{
public System.IntPtr ptr;
public static implicit operator bool(aiProperty v) { return v.ptr != IntPtr.Zero; }
}
public struct aiSample
{
public System.IntPtr ptr;
public static implicit operator bool(aiSample v) { return v.ptr != IntPtr.Zero; }
}
public struct aiPointsSummary
{
public Bool hasVelocity;
public Bool positionIsConstant;
public Bool idIsConstant;
public int peakCount;
public ulong minID;
public ulong maxID;
public Vector3 boundsCenter;
public Vector3 boundsExtents;
};
public struct aiPointsData
{
public IntPtr positions;
public IntPtr velocities;
public IntPtr ids;
public int count;
public Vector3 boundsCenter;
public Vector3 boundsExtents;
}
public struct aiPropertyData
{
public IntPtr data;
public int size;
aiPropertyType type;
}
[DllImport("abci")] public static extern aiSampleSelector aiTimeToSampleSelector(float time);
[DllImport("abci")] public static extern void aiCleanup();
[DllImport("abci")] public static extern void clearContextsWithPath(string path);
[DllImport("abci")] public static extern aiContext aiCreateContext(int uid);
[DllImport("abci")] public static extern void aiDestroyContext(aiContext ctx);
[DllImport("abci")] public static extern Bool aiLoad(aiContext ctx, string path);
[DllImport("abci")] public static extern void aiSetConfig(aiContext ctx, ref aiConfig conf);
[DllImport("abci")] public static extern float aiGetStartTime(aiContext ctx);
[DllImport("abci")] public static extern float aiGetEndTime(aiContext ctx);
[DllImport("abci")] public static extern int getFrameCount(aiContext ctx);
[DllImport("abci")] public static extern aiObject aiGetTopObject(aiContext ctx);
[DllImport("abci")] public static extern void aiDestroyObject(aiContext ctx, aiObject obj);
[DllImport("abci")] public static extern void aiUpdateSamples(aiContext ctx, float time);
[DllImport("abci")] public static extern void aiEnumerateChild(aiObject obj, aiNodeEnumerator e, IntPtr userData);
[DllImport("abci")] private static extern IntPtr aiGetNameS(aiObject obj);
public static string aiGetName(aiObject obj) { return Marshal.PtrToStringAnsi(aiGetNameS(obj)); }
[DllImport("abci")] public static extern void aiSchemaSetSampleCallback(aiSchema schema, aiSampleCallback cb, IntPtr arg);
[DllImport("abci")] public static extern void aiSchemaSetConfigCallback(aiSchema schema, aiConfigCallback cb, IntPtr arg);
[DllImport("abci")] public static extern aiSample aiSchemaUpdateSample(aiSchema schema, ref aiSampleSelector ss);
[DllImport("abci")] public static extern aiSchema aiGetXForm(aiObject obj);
[DllImport("abci")] public static extern void aiXFormGetData(aiSample sample, ref aiXFormData data);
[DllImport("abci")] public static extern aiSchema aiGetPolyMesh(aiObject obj);
[DllImport("abci")] public static extern void aiPolyMeshGetSummary(aiSchema schema, ref aiMeshSummary summary);
[DllImport("abci")] public static extern void aiPolyMeshGetSampleSummary(aiSample sample, ref aiMeshSampleSummary summary, Bool forceRefresh);
[DllImport("abci")] public static extern int aiPolyMeshGetVertexBufferLength(aiSample sample, int splitIndex);
[DllImport("abci")] public static extern void aiPolyMeshFillVertexBuffer(aiSample sample, int splitIndex, ref aiPolyMeshData data);
[DllImport("abci")] public static extern int aiPolyMeshPrepareSubmeshes(aiSample sample, ref aiFacesets facesets);
[DllImport("abci")] public static extern int aiPolyMeshGetSplitSubmeshCount(aiSample sample, int splitIndex);
[DllImport("abci")] public static extern Bool aiPolyMeshGetNextSubmesh(aiSample sample, ref aiSubmeshSummary smi);
[DllImport("abci")] public static extern void aiPolyMeshFillSubmeshIndices(aiSample sample, ref aiSubmeshSummary smi, ref aiSubmeshData data);
[DllImport("abci")] public static extern aiSchema aiGetCamera(aiObject obj);
[DllImport("abci")] public static extern void aiCameraGetData(aiSample sample, ref aiCameraData data);
[DllImport("abci")] public static extern aiSchema aiGetPoints(aiObject obj);
[DllImport("abci")] public static extern void aiPointsSetSort(aiSchema schema, Bool v);
[DllImport("abci")] public static extern void aiPointsSetSortBasePosition(aiSchema schema, Vector3 v);
[DllImport("abci")] public static extern void aiPointsGetSummary(aiSchema schema, ref aiPointsSummary summary);
[DllImport("abci")] public static extern void aiPointsCopyData(aiSample sample, ref aiPointsData data);
[DllImport("abci")] public static extern int aiSchemaGetNumProperties(aiSchema schema);
[DllImport("abci")] public static extern aiProperty aiSchemaGetPropertyByIndex(aiSchema schema, int i);
[DllImport("abci")] public static extern aiProperty aiSchemaGetPropertyByName(aiSchema schema, string name);
[DllImport("abci")] public static extern IntPtr aiPropertyGetNameS(aiProperty prop);
[DllImport("abci")] public static extern aiPropertyType aiPropertyGetType(aiProperty prop);
[DllImport("abci")] public static extern void aiPropertyGetData(aiProperty prop, aiPropertyData oData);
class ImportContext
{
public AlembicTreeNode alembicTreeNode;
public aiSampleSelector ss;
public bool createMissingNodes;
public List<aiObject> objectsToDelete;
}
public static float GetAspectRatio(aiAspectRatioMode mode)
{
if (mode == aiAspectRatioMode.CameraAperture)
{
return 0.0f;
}
else if (mode == aiAspectRatioMode.CurrentResolution)
{
return (float) Screen.width / (float) Screen.height;
}
else
{
#if UNITY_EDITOR
return (float) PlayerSettings.defaultScreenWidth / (float) PlayerSettings.defaultScreenHeight;
#else
// fallback on current resoltution
return (float) Screen.width / (float) Screen.height;
#endif
}
}
public static void UpdateAbcTree(aiContext ctx, AlembicTreeNode node, float time, bool createMissingNodes=true)
{
var ic = new ImportContext
{
alembicTreeNode = node,
ss = aiTimeToSampleSelector(time),
createMissingNodes = createMissingNodes,
objectsToDelete = new List<aiObject>()
};
GCHandle hdl = GCHandle.Alloc(ic);
aiObject top = aiGetTopObject(ctx);
if (top.ptr != (IntPtr)0)
{
aiEnumerateChild(top, ImportEnumerator, GCHandle.ToIntPtr(hdl));
foreach (aiObject obj in ic.objectsToDelete)
{
aiDestroyObject(ctx, obj);
}
}
}
static void ImportEnumerator(aiObject obj, IntPtr userData)
{
var ic = GCHandle.FromIntPtr(userData).Target as ImportContext;
AlembicTreeNode treeNode = ic.alembicTreeNode;
// Get child. create if needed and allowed.
string childName = aiGetName(obj);
// Find targetted child GameObj
AlembicTreeNode childTreeNode = null;
GameObject childGO = null;
var childTransf = treeNode.linkedGameObj==null ? null : treeNode.linkedGameObj.transform.Find(childName);
if (childTransf == null)
{
if (!ic.createMissingNodes)
{
ic.objectsToDelete.Add(obj);
return;
}
childGO = new GameObject {name = childName};
var trans = childGO.GetComponent<Transform>();
trans.parent = treeNode.linkedGameObj.transform;
trans.localPosition = Vector3.zero;
trans.localEulerAngles = Vector3.zero;
trans.localScale = Vector3.one;
}
else
childGO = childTransf.gameObject;
childTreeNode = new AlembicTreeNode() {linkedGameObj = childGO, streamDescriptor = treeNode.streamDescriptor };
treeNode.children.Add(childTreeNode);
// Update
AlembicElement elem = null;
aiSchema schema = default(aiSchema);
if (aiGetXForm(obj))
{
elem = childTreeNode.GetOrAddAlembicObj<AlembicXForm>();
schema = aiGetXForm(obj);
}
else if (aiGetPolyMesh(obj))
{
elem = childTreeNode.GetOrAddAlembicObj<AlembicMesh>();
schema = aiGetPolyMesh(obj);
}
else if (aiGetCamera(obj))
{
elem = childTreeNode.GetOrAddAlembicObj<AlembicCamera>();
schema = aiGetCamera(obj);
}
else if (aiGetPoints(obj))
{
elem = childTreeNode.GetOrAddAlembicObj<AlembicPoints>();
schema = aiGetPoints(obj);
}
if (elem != null)
{
elem.AbcSetup(obj, schema);
elem.AbcUpdateConfig();
aiSchemaUpdateSample(schema, ref ic.ss);
elem.AbcUpdate();
}
ic.alembicTreeNode = childTreeNode;
aiEnumerateChild(obj, ImportEnumerator, userData);
ic.alembicTreeNode = treeNode;
}
}
public class AbcUtils
{
#if UNITY_EDITOR
static MethodInfo s_GetBuiltinExtraResourcesMethod;
public static Material GetDefaultMaterial()
{
if (s_GetBuiltinExtraResourcesMethod == null)
{
BindingFlags bfs = BindingFlags.NonPublic | BindingFlags.Static;
s_GetBuiltinExtraResourcesMethod = typeof(EditorGUIUtility).GetMethod("GetBuiltinExtraResource", bfs);
}
return (Material)s_GetBuiltinExtraResourcesMethod.Invoke(null, new object[] { typeof(Material), "Default-Material.mat" });
}
#endif
}
}