OVRSkeletonRenderer.cs
9.76 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
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
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
https://developer.oculus.com/licenses/oculussdk/
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express or implied. See the License for the specific language governing
permissions and limitations under the License.
************************************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DefaultExecutionOrder(-70)]
public class OVRSkeletonRenderer : MonoBehaviour
{
public interface IOVRSkeletonRendererDataProvider
{
SkeletonRendererData GetSkeletonRendererData();
}
public struct SkeletonRendererData
{
public float RootScale { get; set; }
public bool IsDataValid { get; set; }
public bool IsDataHighConfidence { get; set; }
public bool ShouldUseSystemGestureMaterial { get; set; }
}
public enum ConfidenceBehavior
{
None,
ToggleRenderer,
}
public enum SystemGestureBehavior
{
None,
SwapMaterial,
}
[SerializeField]
private IOVRSkeletonRendererDataProvider _dataProvider;
[SerializeField]
private ConfidenceBehavior _confidenceBehavior = ConfidenceBehavior.ToggleRenderer;
[SerializeField]
private SystemGestureBehavior _systemGestureBehavior = SystemGestureBehavior.SwapMaterial;
[SerializeField]
private bool _renderPhysicsCapsules = false;
[SerializeField]
private Material _skeletonMaterial;
private Material _skeletonDefaultMaterial;
[SerializeField]
private Material _capsuleMaterial;
private Material _capsuleDefaultMaterial;
[SerializeField]
private Material _systemGestureMaterial = null;
private Material _systemGestureDefaultMaterial;
private const float LINE_RENDERER_WIDTH = 0.005f;
private List<BoneVisualization> _boneVisualizations;
private List<CapsuleVisualization> _capsuleVisualizations;
private OVRSkeleton _ovrSkeleton;
private GameObject _skeletonGO;
private float _scale;
private static readonly Quaternion _capsuleRotationOffset = Quaternion.Euler(0, 0, 90);
public bool IsInitialized { get; private set; }
public bool IsDataValid { get; private set; }
public bool IsDataHighConfidence { get; private set; }
public bool ShouldUseSystemGestureMaterial { get; private set; }
private class BoneVisualization
{
private GameObject BoneGO;
private Transform BoneBegin;
private Transform BoneEnd;
private LineRenderer Line;
private Material RenderMaterial;
private Material SystemGestureMaterial;
public BoneVisualization(GameObject rootGO,
Material renderMat,
Material systemGestureMat,
float scale,
Transform begin,
Transform end)
{
RenderMaterial = renderMat;
SystemGestureMaterial = systemGestureMat;
BoneBegin = begin;
BoneEnd = end;
BoneGO = new GameObject(begin.name);
BoneGO.transform.SetParent(rootGO.transform, false);
Line = BoneGO.AddComponent<LineRenderer>();
Line.sharedMaterial = RenderMaterial;
Line.useWorldSpace = true;
Line.positionCount = 2;
Line.SetPosition(0, BoneBegin.position);
Line.SetPosition(1, BoneEnd.position);
Line.startWidth = LINE_RENDERER_WIDTH * scale;
Line.endWidth = LINE_RENDERER_WIDTH * scale;
}
public void Update(float scale,
bool shouldRender,
bool shouldUseSystemGestureMaterial,
ConfidenceBehavior confidenceBehavior,
SystemGestureBehavior systemGestureBehavior)
{
Line.SetPosition(0, BoneBegin.position);
Line.SetPosition(1, BoneEnd.position);
Line.startWidth = LINE_RENDERER_WIDTH * scale;
Line.endWidth = LINE_RENDERER_WIDTH * scale;
if (confidenceBehavior == ConfidenceBehavior.ToggleRenderer)
{
Line.enabled = shouldRender;
}
if (systemGestureBehavior == SystemGestureBehavior.SwapMaterial)
{
if (shouldUseSystemGestureMaterial && Line.sharedMaterial != SystemGestureMaterial)
{
Line.sharedMaterial = SystemGestureMaterial;
}
else if (!shouldUseSystemGestureMaterial && Line.sharedMaterial != RenderMaterial)
{
Line.sharedMaterial = RenderMaterial;
}
}
}
}
private class CapsuleVisualization
{
private GameObject CapsuleGO;
private OVRBoneCapsule BoneCapsule;
private Vector3 capsuleScale;
private MeshRenderer Renderer;
private Material RenderMaterial;
private Material SystemGestureMaterial;
public CapsuleVisualization(GameObject rootGO,
Material renderMat,
Material systemGestureMat,
float scale,
OVRBoneCapsule boneCapsule)
{
RenderMaterial = renderMat;
SystemGestureMaterial = systemGestureMat;
BoneCapsule = boneCapsule;
CapsuleGO = GameObject.CreatePrimitive(PrimitiveType.Capsule);
CapsuleCollider collider = CapsuleGO.GetComponent<CapsuleCollider>();
Destroy(collider);
Renderer = CapsuleGO.GetComponent<MeshRenderer>();
Renderer.sharedMaterial = RenderMaterial;
capsuleScale = Vector3.one;
capsuleScale.y = boneCapsule.CapsuleCollider.height / 2;
capsuleScale.x = boneCapsule.CapsuleCollider.radius * 2;
capsuleScale.z = boneCapsule.CapsuleCollider.radius * 2;
CapsuleGO.transform.localScale = capsuleScale * scale;
}
public void Update(float scale,
bool shouldRender,
bool shouldUseSystemGestureMaterial,
ConfidenceBehavior confidenceBehavior,
SystemGestureBehavior systemGestureBehavior)
{
if (confidenceBehavior == ConfidenceBehavior.ToggleRenderer)
{
if (CapsuleGO.activeSelf != shouldRender)
{
CapsuleGO.SetActive(shouldRender);
}
}
CapsuleGO.transform.rotation = BoneCapsule.CapsuleCollider.transform.rotation * _capsuleRotationOffset;
CapsuleGO.transform.position = BoneCapsule.CapsuleCollider.transform.TransformPoint(BoneCapsule.CapsuleCollider.center);
CapsuleGO.transform.localScale = capsuleScale * scale;
if (systemGestureBehavior == SystemGestureBehavior.SwapMaterial)
{
if (shouldUseSystemGestureMaterial && Renderer.sharedMaterial != SystemGestureMaterial)
{
Renderer.sharedMaterial = SystemGestureMaterial;
}
else if (!shouldUseSystemGestureMaterial && Renderer.sharedMaterial != RenderMaterial)
{
Renderer.sharedMaterial = RenderMaterial;
}
}
}
}
private void Awake()
{
if (_dataProvider == null)
{
_dataProvider = GetComponent<IOVRSkeletonRendererDataProvider>();
}
if (_ovrSkeleton == null)
{
_ovrSkeleton = GetComponent<OVRSkeleton>();
}
}
private void Start()
{
if (_ovrSkeleton == null)
{
this.enabled = false;
return;
}
if (ShouldInitialize())
{
Initialize();
}
}
private bool ShouldInitialize()
{
if (IsInitialized)
{
return false;
}
return _ovrSkeleton.IsInitialized;
}
private void Initialize()
{
_boneVisualizations = new List<BoneVisualization>();
_capsuleVisualizations = new List<CapsuleVisualization>();
_ovrSkeleton = GetComponent<OVRSkeleton>();
_skeletonGO = new GameObject("SkeletonRenderer");
_skeletonGO.transform.SetParent(transform, false);
if (_skeletonMaterial == null)
{
_skeletonDefaultMaterial = new Material(Shader.Find("Diffuse"));
_skeletonMaterial = _skeletonDefaultMaterial;
}
if (_capsuleMaterial == null)
{
_capsuleDefaultMaterial = new Material(Shader.Find("Diffuse"));
_capsuleMaterial = _capsuleDefaultMaterial;
}
if (_systemGestureMaterial == null)
{
_systemGestureDefaultMaterial = new Material(Shader.Find("Diffuse"));
_systemGestureDefaultMaterial.color = Color.blue;
_systemGestureMaterial = _systemGestureDefaultMaterial;
}
if (_ovrSkeleton.IsInitialized)
{
for (int i = 0; i < _ovrSkeleton.Bones.Count; i++)
{
var boneVis = new BoneVisualization(
_skeletonGO,
_skeletonMaterial,
_systemGestureMaterial,
_scale,
_ovrSkeleton.Bones[i].Transform,
_ovrSkeleton.Bones[i].Transform.parent);
_boneVisualizations.Add(boneVis);
}
if (_renderPhysicsCapsules && _ovrSkeleton.Capsules != null)
{
for (int i = 0; i < _ovrSkeleton.Capsules.Count; i++)
{
var capsuleVis = new CapsuleVisualization(
_skeletonGO,
_capsuleMaterial,
_systemGestureMaterial,
_scale,
_ovrSkeleton.Capsules[i]);
_capsuleVisualizations.Add(capsuleVis);
}
}
IsInitialized = true;
}
}
public void Update()
{
#if UNITY_EDITOR
if (ShouldInitialize())
{
Initialize();
}
#endif
IsDataValid = false;
IsDataHighConfidence = false;
ShouldUseSystemGestureMaterial = false;
if (IsInitialized)
{
bool shouldRender = false;
if (_dataProvider != null)
{
var data = _dataProvider.GetSkeletonRendererData();
IsDataValid = data.IsDataValid;
IsDataHighConfidence = data.IsDataHighConfidence;
ShouldUseSystemGestureMaterial = data.ShouldUseSystemGestureMaterial;
shouldRender = data.IsDataValid && data.IsDataHighConfidence;
if (data.IsDataValid)
{
_scale = data.RootScale;
}
}
for (int i = 0; i < _boneVisualizations.Count; i++)
{
_boneVisualizations[i].Update(_scale, shouldRender, ShouldUseSystemGestureMaterial, _confidenceBehavior, _systemGestureBehavior);
}
for (int i = 0; i < _capsuleVisualizations.Count; i++)
{
_capsuleVisualizations[i].Update(_scale, shouldRender, ShouldUseSystemGestureMaterial, _confidenceBehavior, _systemGestureBehavior);
}
}
}
private void OnDestroy()
{
if (_skeletonDefaultMaterial != null)
{
DestroyImmediate(_skeletonDefaultMaterial, false);
}
if (_capsuleDefaultMaterial != null)
{
DestroyImmediate(_capsuleDefaultMaterial, false);
}
if (_systemGestureDefaultMaterial != null)
{
DestroyImmediate(_systemGestureDefaultMaterial, false);
}
}
}