OvrAvatarAssetMesh.cs
11.6 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
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
using System;
using Oculus.Avatar;
using UnityEngine;
using System.Runtime.InteropServices;
public class OvrAvatarAssetMesh : OvrAvatarAsset
{
public Mesh mesh;
private ovrAvatarSkinnedMeshPose skinnedBindPose;
public string[] jointNames;
public OvrAvatarAssetMesh(UInt64 _assetId, IntPtr asset, ovrAvatarAssetType meshType)
{
assetID = _assetId;
mesh = new Mesh();
mesh.name = "Procedural Geometry for asset " + _assetId;
SetSkinnedBindPose(asset, meshType);
long vertexCount = 0;
IntPtr vertexBuffer = IntPtr.Zero;
uint indexCount = 0;
IntPtr indexBuffer = IntPtr.Zero;
GetVertexAndIndexData(asset, meshType, out vertexCount, out vertexBuffer, out indexCount, out indexBuffer);
AvatarLogger.Log("OvrAvatarAssetMesh: " + _assetId + " " + meshType.ToString() + " VertexCount:" + vertexCount);
Vector3[] vertices = new Vector3[vertexCount];
Vector3[] normals = new Vector3[vertexCount];
Vector4[] tangents = new Vector4[vertexCount];
Vector2[] uv = new Vector2[vertexCount];
Color[] colors = new Color[vertexCount];
BoneWeight[] boneWeights = new BoneWeight[vertexCount];
long vertexBufferStart = vertexBuffer.ToInt64();
// We have different underlying vertex types to unpack, so switch on mesh type.
switch (meshType)
{
case ovrAvatarAssetType.Mesh:
{
long vertexSize = (long)Marshal.SizeOf(typeof(ovrAvatarMeshVertex));
for (long i = 0; i < vertexCount; i++)
{
long offset = vertexSize * i;
ovrAvatarMeshVertex vertex = (ovrAvatarMeshVertex)Marshal.PtrToStructure(new IntPtr(vertexBufferStart + offset), typeof(ovrAvatarMeshVertex));
vertices[i] = new Vector3(vertex.x, vertex.y, -vertex.z);
normals[i] = new Vector3(vertex.nx, vertex.ny, -vertex.nz);
tangents[i] = new Vector4(vertex.tx, vertex.ty, -vertex.tz, vertex.tw);
uv[i] = new Vector2(vertex.u, vertex.v);
colors[i] = new Color(0, 0, 0, 1);
boneWeights[i].boneIndex0 = vertex.blendIndices[0];
boneWeights[i].boneIndex1 = vertex.blendIndices[1];
boneWeights[i].boneIndex2 = vertex.blendIndices[2];
boneWeights[i].boneIndex3 = vertex.blendIndices[3];
boneWeights[i].weight0 = vertex.blendWeights[0];
boneWeights[i].weight1 = vertex.blendWeights[1];
boneWeights[i].weight2 = vertex.blendWeights[2];
boneWeights[i].weight3 = vertex.blendWeights[3];
}
}
break;
case ovrAvatarAssetType.CombinedMesh:
{
long vertexSize = (long)Marshal.SizeOf(typeof(ovrAvatarMeshVertexV2));
for (long i = 0; i < vertexCount; i++)
{
long offset = vertexSize * i;
ovrAvatarMeshVertexV2 vertex = (ovrAvatarMeshVertexV2)Marshal.PtrToStructure(new IntPtr(vertexBufferStart + offset), typeof(ovrAvatarMeshVertexV2));
vertices[i] = new Vector3(vertex.x, vertex.y, -vertex.z);
normals[i] = new Vector3(vertex.nx, vertex.ny, -vertex.nz);
tangents[i] = new Vector4(vertex.tx, vertex.ty, -vertex.tz, vertex.tw);
uv[i] = new Vector2(vertex.u, vertex.v);
colors[i] = new Color(vertex.r, vertex.g, vertex.b, vertex.a);
boneWeights[i].boneIndex0 = vertex.blendIndices[0];
boneWeights[i].boneIndex1 = vertex.blendIndices[1];
boneWeights[i].boneIndex2 = vertex.blendIndices[2];
boneWeights[i].boneIndex3 = vertex.blendIndices[3];
boneWeights[i].weight0 = vertex.blendWeights[0];
boneWeights[i].weight1 = vertex.blendWeights[1];
boneWeights[i].weight2 = vertex.blendWeights[2];
boneWeights[i].weight3 = vertex.blendWeights[3];
}
}
break;
default:
throw new Exception("Bad Mesh Asset Type");
}
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uv;
mesh.tangents = tangents;
mesh.boneWeights = boneWeights;
mesh.colors = colors;
LoadBlendShapes(asset, vertexCount);
LoadSubmeshes(asset, indexBuffer, indexCount);
UInt32 jointCount = skinnedBindPose.jointCount;
jointNames = new string[jointCount];
for (UInt32 i = 0; i < jointCount; i++)
{
jointNames[i] = Marshal.PtrToStringAnsi(skinnedBindPose.jointNames[i]);
}
}
private void LoadSubmeshes(IntPtr asset, IntPtr indexBufferPtr, ulong indexCount)
{
UInt32 subMeshCount = CAPI.ovrAvatarAsset_GetSubmeshCount(asset);
AvatarLogger.Log("LoadSubmeshes: " + subMeshCount);
Int16[] indices = new Int16[indexCount];
Marshal.Copy(indexBufferPtr, indices, 0, (int)indexCount);
mesh.subMeshCount = (int)subMeshCount;
uint accumedOffset = 0;
for (UInt32 index = 0; index < subMeshCount; index++)
{
var submeshIndexCount = CAPI.ovrAvatarAsset_GetSubmeshLastIndex(asset, index);
var currSpan = submeshIndexCount - accumedOffset;
Int32[] triangles = new Int32[currSpan];
int triangleOffset = 0;
for (ulong i = accumedOffset; i < submeshIndexCount; i += 3)
{
// NOTE: We are changing the order of each triangle to match unity expectations vs pipeline.
triangles[triangleOffset + 2] = (Int32)indices[i];
triangles[triangleOffset + 1] = (Int32)indices[i + 1];
triangles[triangleOffset] = (Int32)indices[i + 2];
triangleOffset += 3;
}
accumedOffset += currSpan;
mesh.SetIndices(triangles, MeshTopology.Triangles, (int)index);
}
}
private void LoadBlendShapes(IntPtr asset, long vertexCount)
{
UInt32 blendShapeCount = CAPI.ovrAvatarAsset_GetMeshBlendShapeCount(asset);
IntPtr blendShapeVerts = CAPI.ovrAvatarAsset_GetMeshBlendShapeVertices(asset);
AvatarLogger.Log("LoadBlendShapes: " + blendShapeCount);
if (blendShapeVerts != IntPtr.Zero)
{
long offset = 0;
long blendVertexSize = (long)Marshal.SizeOf(typeof(ovrAvatarBlendVertex));
long blendVertexBufferStart = blendShapeVerts.ToInt64();
for (UInt32 blendIndex = 0; blendIndex < blendShapeCount; blendIndex++)
{
Vector3[] blendVerts = new Vector3[vertexCount];
Vector3[] blendNormals = new Vector3[vertexCount];
Vector3[] blendTangents = new Vector3[vertexCount];
for (long i = 0; i < vertexCount; i++)
{
ovrAvatarBlendVertex vertex = (ovrAvatarBlendVertex)Marshal.PtrToStructure(new IntPtr(blendVertexBufferStart + offset), typeof(ovrAvatarBlendVertex));
blendVerts[i] = new Vector3(vertex.x, vertex.y, -vertex.z);
blendNormals[i] = new Vector3(vertex.nx, vertex.ny, -vertex.nz);
blendTangents[i] = new Vector4(vertex.tx, vertex.ty, -vertex.tz);
offset += blendVertexSize;
}
IntPtr namePtr = CAPI.ovrAvatarAsset_GetMeshBlendShapeName(asset, blendIndex);
string name = Marshal.PtrToStringAnsi(namePtr);
const float frameWeight = 100f;
mesh.AddBlendShapeFrame(name, frameWeight, blendVerts, blendNormals, blendTangents);
}
}
}
private void SetSkinnedBindPose(IntPtr asset, ovrAvatarAssetType meshType)
{
switch (meshType)
{
case ovrAvatarAssetType.Mesh:
skinnedBindPose = CAPI.ovrAvatarAsset_GetMeshData(asset).skinnedBindPose;
break;
case ovrAvatarAssetType.CombinedMesh:
skinnedBindPose = CAPI.ovrAvatarAsset_GetCombinedMeshData(asset).skinnedBindPose;
break;
default:
break;
}
}
private void GetVertexAndIndexData(
IntPtr asset,
ovrAvatarAssetType meshType,
out long vertexCount,
out IntPtr vertexBuffer,
out uint indexCount,
out IntPtr indexBuffer)
{
vertexCount = 0;
vertexBuffer = IntPtr.Zero;
indexCount = 0;
indexBuffer = IntPtr.Zero;
switch (meshType)
{
case ovrAvatarAssetType.Mesh:
vertexCount = CAPI.ovrAvatarAsset_GetMeshData(asset).vertexCount;
vertexBuffer = CAPI.ovrAvatarAsset_GetMeshData(asset).vertexBuffer;
indexCount = CAPI.ovrAvatarAsset_GetMeshData(asset).indexCount;
indexBuffer = CAPI.ovrAvatarAsset_GetMeshData(asset).indexBuffer;
break;
case ovrAvatarAssetType.CombinedMesh:
vertexCount = CAPI.ovrAvatarAsset_GetCombinedMeshData(asset).vertexCount;
vertexBuffer = CAPI.ovrAvatarAsset_GetCombinedMeshData(asset).vertexBuffer;
indexCount = CAPI.ovrAvatarAsset_GetCombinedMeshData(asset).indexCount;
indexBuffer = CAPI.ovrAvatarAsset_GetCombinedMeshData(asset).indexBuffer;
break;
default:
break;
}
}
public SkinnedMeshRenderer CreateSkinnedMeshRendererOnObject(GameObject target)
{
SkinnedMeshRenderer skinnedMeshRenderer = target.AddComponent<SkinnedMeshRenderer>();
skinnedMeshRenderer.sharedMesh = mesh;
mesh.name = "AvatarMesh_" + assetID;
UInt32 jointCount = skinnedBindPose.jointCount;
GameObject[] bones = new GameObject[jointCount];
Transform[] boneTransforms = new Transform[jointCount];
Matrix4x4[] bindPoses = new Matrix4x4[jointCount];
for (UInt32 i = 0; i < jointCount; i++)
{
bones[i] = new GameObject();
boneTransforms[i] = bones[i].transform;
bones[i].name = jointNames[i];
int parentIndex = skinnedBindPose.jointParents[i];
if (parentIndex == -1)
{
bones[i].transform.parent = skinnedMeshRenderer.transform;
skinnedMeshRenderer.rootBone = bones[i].transform;
}
else
{
bones[i].transform.parent = bones[parentIndex].transform;
}
// Set the position relative to the parent
Vector3 position = skinnedBindPose.jointTransform[i].position;
position.z = -position.z;
bones[i].transform.localPosition = position;
Quaternion orientation = skinnedBindPose.jointTransform[i].orientation;
orientation.x = -orientation.x;
orientation.y = -orientation.y;
bones[i].transform.localRotation = orientation;
bones[i].transform.localScale = skinnedBindPose.jointTransform[i].scale;
bindPoses[i] = bones[i].transform.worldToLocalMatrix * skinnedMeshRenderer.transform.localToWorldMatrix;
}
skinnedMeshRenderer.bones = boneTransforms;
mesh.bindposes = bindPoses;
return skinnedMeshRenderer;
}
}