BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Avatar / Scripts / OvrAvatar.cs

OvrAvatar.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OvrAvatar.cs 44.8 KiB Download Raw
1using UnityEngine;
2using System.Collections;
3using System;
4using Oculus.Avatar;
5using System.Runtime.InteropServices;
6using System.Collections.Generic;
7#if UNITY_EDITOR
8using UnityEditor;
9#endif
10
11#if AVATAR_INTERNAL
12using UnityEngine.Events;
13#endif
14
15[System.Serializable]
16public class AvatarLayer
17{
18 public int layerIndex;
19}
20
21#if UNITY_EDITOR
22[CustomPropertyDrawer(typeof(AvatarLayer))]
23public class AvatarLayerPropertyDrawer : PropertyDrawer
24{
25 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
26 {
27 EditorGUI.BeginProperty(position, GUIContent.none, property);
28 SerializedProperty layerIndex = property.FindPropertyRelative("layerIndex");
29 position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
30 layerIndex.intValue = EditorGUI.LayerField(position, layerIndex.intValue);
31 EditorGUI.EndProperty();
32 }
33}
34#endif
35
36[System.Serializable]
37public class PacketRecordSettings
38{
39 internal bool RecordingFrames = false;
40 public float UpdateRate = 1f / 30f; // 30 hz update of packets
41 internal float AccumulatedTime;
42};
43
44public class OvrAvatar : MonoBehaviour
45{
46 [Header("Avatar")]
47 public IntPtr sdkAvatar = IntPtr.Zero;
48 public string oculusUserID;
49 public OvrAvatarDriver Driver;
50
51 [Header("Capabilities")]
52 public bool EnableBody = true;
53 public bool EnableHands = true;
54 public bool EnableBase = true;
55 public bool EnableExpressive = false;
56
57 [Header("Network")]
58 public bool RecordPackets;
59 public bool UseSDKPackets = true;
60 public PacketRecordSettings PacketSettings = new PacketRecordSettings();
61
62 [Header("Visibility")]
63 public bool StartWithControllers;
64 public AvatarLayer FirstPersonLayer;
65 public AvatarLayer ThirdPersonLayer;
66 public bool ShowFirstPerson = true;
67 public bool ShowThirdPerson;
68 internal ovrAvatarCapabilities Capabilities = ovrAvatarCapabilities.Body;
69
70 [Header("Performance")]
71#if UNITY_ANDROID
72 [Tooltip(
73 "LOD mesh complexity and texture resolution. Highest LOD recommended on PC and simple mobile apps." +
74 " Medium LOD recommended on mobile devices or for background characters on PC." +
75 " Lowest LOD recommended for background characters on mobile.")]
76 [SerializeField]
77 internal ovrAvatarAssetLevelOfDetail LevelOfDetail = ovrAvatarAssetLevelOfDetail.Medium;
78#else
79 [SerializeField]
80 internal ovrAvatarAssetLevelOfDetail LevelOfDetail = ovrAvatarAssetLevelOfDetail.Highest;
81#endif
82#if UNITY_ANDROID && UNITY_5_5_OR_NEWER && !UNITY_EDITOR
83 [Tooltip(
84 "Enable to use combined meshes to reduce draw calls. Currently only available on mobile devices. " +
85 "Will be forced to false on PC.")]
86 private bool CombineMeshes = true;
87#else
88 private bool CombineMeshes = false;
89#endif
90 [Tooltip(
91 "Enable to use transparent queue, disable to use geometry queue. Requires restart to take effect.")]
92 public bool UseTransparentRenderQueue = true;
93
94 [Header("Shaders")]
95 public Shader Monochrome_SurfaceShader;
96 public Shader Monochrome_SurfaceShader_SelfOccluding;
97 public Shader Monochrome_SurfaceShader_PBS;
98 public Shader Skinshaded_SurfaceShader_SingleComponent;
99 public Shader Skinshaded_VertFrag_SingleComponent;
100 public Shader Skinshaded_VertFrag_CombinedMesh;
101 public Shader Skinshaded_Expressive_SurfaceShader_SingleComponent;
102 public Shader Skinshaded_Expressive_VertFrag_SingleComponent;
103 public Shader Skinshaded_Expressive_VertFrag_CombinedMesh;
104 public Shader Loader_VertFrag_CombinedMesh;
105 public Shader EyeLens;
106 public Shader ControllerShader;
107
108 [Header("Other")]
109 public bool CanOwnMicrophone = true;
110 [Tooltip(
111 "Enable laughter detection and animation as part of OVRLipSync.")]
112 public bool EnableLaughter = true;
113 public GameObject MouthAnchor;
114 public Transform LeftHandCustomPose;
115 public Transform RightHandCustomPose;
116
117 // Avatar asset
118 private HashSet<UInt64> assetLoadingIds = new HashSet<UInt64>();
119 private bool assetsFinishedLoading = false;
120 private int renderPartCount = 0;
121
122 // Material manager
123 private OvrAvatarMaterialManager materialManager;
124 private bool waitingForCombinedMesh = false;
125
126 // Global expressive system initialization
127 private static bool doneExpressiveGlobalInit = false;
128
129 // Clothing offsets
130 private Vector4 clothingAlphaOffset = new Vector4(0f, 0f, 0f, 1f);
131 private UInt64 clothingAlphaTexture = 0;
132
133 // Lipsync
134 private OVRLipSyncMicInput micInput = null;
135 private OVRLipSyncContext lipsyncContext = null;
136 private OVRLipSync.Frame currentFrame = new OVRLipSync.Frame();
137 private float[] visemes = new float[VISEME_COUNT];
138 private AudioSource audioSource;
139 private ONSPAudioSource spatializedSource;
140 private List<float[]> voiceUpdates = new List<float[]>();
141 private static ovrAvatarVisemes RuntimeVisemes;
142
143 // Custom hand poses
144 private Transform cachedLeftHandCustomPose;
145 private Transform[] cachedCustomLeftHandJoints;
146 private ovrAvatarTransform[] cachedLeftHandTransforms;
147 private Transform cachedRightHandCustomPose;
148 private Transform[] cachedCustomRightHandJoints;
149 private ovrAvatarTransform[] cachedRightHandTransforms;
150 private bool showLeftController;
151 private bool showRightController;
152
153 // Consts
154#if UNITY_ANDROID
155 private const bool USE_MOBILE_TEXTURE_FORMAT = true;
156#else
157 private const bool USE_MOBILE_TEXTURE_FORMAT = false;
158#endif
159 private static readonly Vector3 MOUTH_HEAD_OFFSET = new Vector3(0, -0.085f, 0.09f);
160 private const string MOUTH_HELPER_NAME = "MouthAnchor";
161 // Initial 'silence' score, 14 viseme scores and 1 laughter score as last element
162 private const int VISEME_COUNT = 16;
163 // Lipsync animation speeds
164 private const float ACTION_UNIT_ONSET_SPEED = 30f;
165 private const float ACTION_UNIT_FALLOFF_SPEED = 20f;
166 private const float VISEME_LEVEL_MULTIPLIER = 1.5f;
167
168 // Internals
169 internal UInt64 oculusUserIDInternal;
170 internal OvrAvatarBase Base = null;
171 internal OvrAvatarTouchController ControllerLeft = null;
172 internal OvrAvatarTouchController ControllerRight = null;
173 internal OvrAvatarBody Body = null;
174 internal OvrAvatarHand HandLeft = null;
175 internal OvrAvatarHand HandRight = null;
176 internal ovrAvatarLookAndFeelVersion LookAndFeelVersion = ovrAvatarLookAndFeelVersion.Two;
177 internal ovrAvatarLookAndFeelVersion FallbackLookAndFeelVersion = ovrAvatarLookAndFeelVersion.One;
178#if AVATAR_INTERNAL
179 public AvatarControllerBlend BlendController;
180 public UnityEvent AssetsDoneLoading = new UnityEvent();
181#endif
182
183 // Avatar packets
184 public class PacketEventArgs : EventArgs
185 {
186 public readonly OvrAvatarPacket Packet;
187 public PacketEventArgs(OvrAvatarPacket packet)
188 {
189 Packet = packet;
190 }
191 }
192 private OvrAvatarPacket CurrentUnityPacket;
193 public EventHandler<PacketEventArgs> PacketRecorded;
194
195 public enum HandType
196 {
197 Right,
198 Left,
199
200 Max
201 };
202
203 public enum HandJoint
204 {
205 HandBase,
206 IndexBase,
207 IndexTip,
208 ThumbBase,
209 ThumbTip,
210
211 Max,
212 }
213
214 private static string[,] HandJoints = new string[(int)HandType.Max, (int)HandJoint.Max]
215 {
216 {
217 "hands:r_hand_world",
218 "hands:r_hand_world/hands:b_r_hand/hands:b_r_index1",
219 "hands:r_hand_world/hands:b_r_hand/hands:b_r_index1/hands:b_r_index2/hands:b_r_index3/hands:b_r_index_ignore",
220 "hands:r_hand_world/hands:b_r_hand/hands:b_r_thumb1/hands:b_r_thumb2",
221 "hands:r_hand_world/hands:b_r_hand/hands:b_r_thumb1/hands:b_r_thumb2/hands:b_r_thumb3/hands:b_r_thumb_ignore"
222 },
223 {
224 "hands:l_hand_world",
225 "hands:l_hand_world/hands:b_l_hand/hands:b_l_index1",
226 "hands:l_hand_world/hands:b_l_hand/hands:b_l_index1/hands:b_l_index2/hands:b_l_index3/hands:b_l_index_ignore",
227 "hands:l_hand_world/hands:b_l_hand/hands:b_l_thumb1/hands:b_l_thumb2",
228 "hands:l_hand_world/hands:b_l_hand/hands:b_l_thumb1/hands:b_l_thumb2/hands:b_l_thumb3/hands:b_l_thumb_ignore"
229 }
230 };
231
232 static OvrAvatar()
233 {
234 // This size has to match the 'MarshalAs' attribute in the ovrAvatarVisemes declaration.
235 RuntimeVisemes.visemeParams = new float[32];
236 RuntimeVisemes.visemeParamCount = VISEME_COUNT;
237 }
238
239 void OnDestroy()
240 {
241 if (sdkAvatar != IntPtr.Zero)
242 {
243 CAPI.ovrAvatar_Destroy(sdkAvatar);
244 }
245 }
246
247 public void AssetLoadedCallback(OvrAvatarAsset asset)
248 {
249 assetLoadingIds.Remove(asset.assetID);
250 }
251
252 public void CombinedMeshLoadedCallback(IntPtr assetPtr)
253 {
254 if (!waitingForCombinedMesh)
255 {
256 return;
257 }
258
259 var meshIDs = CAPI.ovrAvatarAsset_GetCombinedMeshIDs(assetPtr);
260 foreach (var id in meshIDs)
261 {
262 assetLoadingIds.Remove(id);
263 }
264
265 CAPI.ovrAvatar_GetCombinedMeshAlphaData(sdkAvatar, ref clothingAlphaTexture, ref clothingAlphaOffset);
266
267 waitingForCombinedMesh = false;
268 }
269
270 private OvrAvatarSkinnedMeshRenderComponent AddSkinnedMeshRenderComponent(GameObject gameObject, ovrAvatarRenderPart_SkinnedMeshRender skinnedMeshRender)
271 {
272 OvrAvatarSkinnedMeshRenderComponent skinnedMeshRenderer = gameObject.AddComponent<OvrAvatarSkinnedMeshRenderComponent>();
273 skinnedMeshRenderer.Initialize(skinnedMeshRender, Monochrome_SurfaceShader, Monochrome_SurfaceShader_SelfOccluding, ThirdPersonLayer.layerIndex, FirstPersonLayer.layerIndex);
274 return skinnedMeshRenderer;
275 }
276
277 private OvrAvatarSkinnedMeshRenderPBSComponent AddSkinnedMeshRenderPBSComponent(GameObject gameObject, ovrAvatarRenderPart_SkinnedMeshRenderPBS skinnedMeshRenderPBS)
278 {
279 OvrAvatarSkinnedMeshRenderPBSComponent skinnedMeshRenderer = gameObject.AddComponent<OvrAvatarSkinnedMeshRenderPBSComponent>();
280 skinnedMeshRenderer.Initialize(skinnedMeshRenderPBS, Monochrome_SurfaceShader_PBS, ThirdPersonLayer.layerIndex, FirstPersonLayer.layerIndex);
281 return skinnedMeshRenderer;
282 }
283
284 private OvrAvatarSkinnedMeshPBSV2RenderComponent AddSkinnedMeshRenderPBSV2Component(
285 IntPtr renderPart,
286 GameObject go,
287 ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2 skinnedMeshRenderPBSV2,
288 bool isBodyPartZero,
289 bool isControllerModel)
290 {
291 OvrAvatarSkinnedMeshPBSV2RenderComponent skinnedMeshRenderer = go.AddComponent<OvrAvatarSkinnedMeshPBSV2RenderComponent>();
292 skinnedMeshRenderer.Initialize(
293 renderPart,
294 skinnedMeshRenderPBSV2,
295 materialManager,
296 ThirdPersonLayer.layerIndex,
297 FirstPersonLayer.layerIndex,
298 isBodyPartZero && CombineMeshes,
299 LevelOfDetail,
300 isBodyPartZero && EnableExpressive,
301 this,
302 isControllerModel);
303
304 return skinnedMeshRenderer;
305 }
306
307 static public IntPtr GetRenderPart(ovrAvatarComponent component, UInt32 renderPartIndex)
308 {
309 return Marshal.ReadIntPtr(component.renderParts, Marshal.SizeOf(typeof(IntPtr)) * (int)renderPartIndex);
310 }
311
312 private static string GetRenderPartName(ovrAvatarComponent component, uint renderPartIndex)
313 {
314 return component.name + "_renderPart_" + (int)renderPartIndex;
315 }
316
317 internal static void ConvertTransform(float[] transform, ref ovrAvatarTransform target)
318 {
319 target.position.x = transform[0];
320 target.position.y = transform[1];
321 target.position.z = transform[2];
322
323 target.orientation.x = transform[3];
324 target.orientation.y = transform[4];
325 target.orientation.z = transform[5];
326 target.orientation.w = transform[6];
327
328 target.scale.x = transform[7];
329 target.scale.y = transform[8];
330 target.scale.z = transform[9];
331 }
332
333 internal static void ConvertTransform(ovrAvatarTransform transform, Transform target)
334 {
335 Vector3 position = transform.position;
336 position.z = -position.z;
337 Quaternion orientation = transform.orientation;
338 orientation.x = -orientation.x;
339 orientation.y = -orientation.y;
340 target.localPosition = position;
341 target.localRotation = orientation;
342 target.localScale = transform.scale;
343 }
344
345 public static ovrAvatarTransform CreateOvrAvatarTransform(Vector3 position, Quaternion orientation)
346 {
347 return new ovrAvatarTransform
348 {
349 position = new Vector3(position.x, position.y, -position.z),
350 orientation = new Quaternion(-orientation.x, -orientation.y, orientation.z, orientation.w),
351 scale = Vector3.one
352 };
353 }
354
355 private static ovrAvatarGazeTarget CreateOvrGazeTarget(uint targetId, Vector3 targetPosition, ovrAvatarGazeTargetType targetType)
356 {
357 return new ovrAvatarGazeTarget
358 {
359 id = targetId,
360 // Do coordinate system switch.
361 worldPosition = new Vector3(targetPosition.x, targetPosition.y, -targetPosition.z),
362 type = targetType
363 };
364 }
365
366 private void BuildRenderComponents()
367 {
368 ovrAvatarBaseComponent baseComponnet = new ovrAvatarBaseComponent();
369 ovrAvatarHandComponent leftHandComponnet = new ovrAvatarHandComponent();
370 ovrAvatarHandComponent rightHandComponnet = new ovrAvatarHandComponent();
371 ovrAvatarControllerComponent leftControllerComponent = new ovrAvatarControllerComponent();
372 ovrAvatarControllerComponent rightControllerComponent = new ovrAvatarControllerComponent();
373 ovrAvatarBodyComponent bodyComponent = new ovrAvatarBodyComponent();
374
375 ovrAvatarComponent dummyComponent = new ovrAvatarComponent();
376
377 const bool FetchName = true;
378
379 if (CAPI.ovrAvatarPose_GetLeftHandComponent(sdkAvatar, ref leftHandComponnet))
380 {
381 CAPI.ovrAvatarComponent_Get(leftHandComponnet.renderComponent, FetchName, ref dummyComponent);
382 AddAvatarComponent(ref HandLeft, dummyComponent);
383 HandLeft.isLeftHand = true;
384 }
385
386 if (CAPI.ovrAvatarPose_GetRightHandComponent(sdkAvatar, ref rightHandComponnet))
387 {
388 CAPI.ovrAvatarComponent_Get(rightHandComponnet.renderComponent, FetchName, ref dummyComponent);
389 AddAvatarComponent(ref HandRight, dummyComponent);
390 HandRight.isLeftHand = false;
391 }
392
393 if (CAPI.ovrAvatarPose_GetBodyComponent(sdkAvatar, ref bodyComponent))
394 {
395 CAPI.ovrAvatarComponent_Get(bodyComponent.renderComponent, FetchName, ref dummyComponent);
396 AddAvatarComponent(ref Body, dummyComponent);
397 }
398
399 if (CAPI.ovrAvatarPose_GetLeftControllerComponent(sdkAvatar, ref leftControllerComponent))
400 {
401 CAPI.ovrAvatarComponent_Get(leftControllerComponent.renderComponent, FetchName, ref dummyComponent);
402 AddAvatarComponent(ref ControllerLeft, dummyComponent);
403 ControllerLeft.isLeftHand = true;
404 }
405
406 if (CAPI.ovrAvatarPose_GetRightControllerComponent(sdkAvatar, ref rightControllerComponent))
407 {
408 CAPI.ovrAvatarComponent_Get(rightControllerComponent.renderComponent, FetchName, ref dummyComponent);
409 AddAvatarComponent(ref ControllerRight, dummyComponent);
410 ControllerRight.isLeftHand = false;
411 }
412
413 if (CAPI.ovrAvatarPose_GetBaseComponent(sdkAvatar, ref baseComponnet))
414 {
415 CAPI.ovrAvatarComponent_Get(baseComponnet.renderComponent, FetchName, ref dummyComponent);
416 AddAvatarComponent(ref Base, dummyComponent);
417 }
418 }
419
420 private void AddAvatarComponent<T>(ref T root, ovrAvatarComponent nativeComponent) where T : OvrAvatarComponent
421 {
422 GameObject componentObject = new GameObject();
423 componentObject.name = nativeComponent.name;
424 componentObject.transform.SetParent(transform);
425 root = componentObject.AddComponent<T>();
426 root.SetOvrAvatarOwner(this);
427 AddRenderParts(root, nativeComponent, componentObject.transform);
428 }
429
430 void UpdateCustomPoses()
431 {
432 // Check to see if the pose roots changed
433 if (UpdatePoseRoot(LeftHandCustomPose, ref cachedLeftHandCustomPose, ref cachedCustomLeftHandJoints, ref cachedLeftHandTransforms))
434 {
435 if (cachedLeftHandCustomPose == null && sdkAvatar != IntPtr.Zero)
436 {
437 CAPI.ovrAvatar_SetLeftHandGesture(sdkAvatar, ovrAvatarHandGesture.Default);
438 }
439 }
440 if (UpdatePoseRoot(RightHandCustomPose, ref cachedRightHandCustomPose, ref cachedCustomRightHandJoints, ref cachedRightHandTransforms))
441 {
442 if (cachedRightHandCustomPose == null && sdkAvatar != IntPtr.Zero)
443 {
444 CAPI.ovrAvatar_SetRightHandGesture(sdkAvatar, ovrAvatarHandGesture.Default);
445 }
446 }
447
448 // Check to see if the custom gestures need to be updated
449 if (sdkAvatar != IntPtr.Zero)
450 {
451 if (cachedLeftHandCustomPose != null && UpdateTransforms(cachedCustomLeftHandJoints, cachedLeftHandTransforms))
452 {
453 CAPI.ovrAvatar_SetLeftHandCustomGesture(sdkAvatar, (uint)cachedLeftHandTransforms.Length, cachedLeftHandTransforms);
454 }
455 if (cachedRightHandCustomPose != null && UpdateTransforms(cachedCustomRightHandJoints, cachedRightHandTransforms))
456 {
457 CAPI.ovrAvatar_SetRightHandCustomGesture(sdkAvatar, (uint)cachedRightHandTransforms.Length, cachedRightHandTransforms);
458 }
459 }
460 }
461
462 static bool UpdatePoseRoot(Transform poseRoot, ref Transform cachedPoseRoot, ref Transform[] cachedPoseJoints, ref ovrAvatarTransform[] transforms)
463 {
464 if (poseRoot == cachedPoseRoot)
465 {
466 return false;
467 }
468
469 if (!poseRoot)
470 {
471 cachedPoseRoot = null;
472 cachedPoseJoints = null;
473 transforms = null;
474 }
475 else
476 {
477 List<Transform> joints = new List<Transform>();
478 OrderJoints(poseRoot, joints);
479 cachedPoseRoot = poseRoot;
480 cachedPoseJoints = joints.ToArray();
481 transforms = new ovrAvatarTransform[joints.Count];
482 }
483 return true;
484 }
485
486 static bool UpdateTransforms(Transform[] joints, ovrAvatarTransform[] transforms)
487 {
488 bool updated = false;
489 for (int i = 0; i < joints.Length; ++i)
490 {
491 Transform joint = joints[i];
492 ovrAvatarTransform transform = CreateOvrAvatarTransform(joint.localPosition, joint.localRotation);
493 if (transform.position != transforms[i].position || transform.orientation != transforms[i].orientation)
494 {
495 transforms[i] = transform;
496 updated = true;
497 }
498 }
499 return updated;
500 }
501
502
503 private static void OrderJoints(Transform transform, List<Transform> joints)
504 {
505 joints.Add(transform);
506 for (int i = 0; i < transform.childCount; ++i)
507 {
508 Transform child = transform.GetChild(i);
509 OrderJoints(child, joints);
510 }
511 }
512
513 void AvatarSpecificationCallback(IntPtr avatarSpecification)
514 {
515 sdkAvatar = CAPI.ovrAvatar_Create(avatarSpecification, Capabilities);
516 ShowLeftController(showLeftController);
517 ShowRightController(showRightController);
518
519 //Fetch all the assets that this avatar uses.
520 UInt32 assetCount = CAPI.ovrAvatar_GetReferencedAssetCount(sdkAvatar);
521 for (UInt32 i = 0; i < assetCount; ++i)
522 {
523 UInt64 id = CAPI.ovrAvatar_GetReferencedAsset(sdkAvatar, i);
524 if (OvrAvatarSDKManager.Instance.GetAsset(id) == null)
525 {
526 OvrAvatarSDKManager.Instance.BeginLoadingAsset(
527 id,
528 LevelOfDetail,
529 AssetLoadedCallback);
530
531 assetLoadingIds.Add(id);
532 }
533 }
534
535 if (CombineMeshes)
536 {
537 OvrAvatarSDKManager.Instance.RegisterCombinedMeshCallback(
538 sdkAvatar,
539 CombinedMeshLoadedCallback);
540 }
541 }
542
543 void Start()
544 {
545 if (OvrAvatarSDKManager.Instance == null)
546 {
547 return;
548 }
549#if !UNITY_ANDROID
550 if (CombineMeshes)
551 {
552 CombineMeshes = false;
553 AvatarLogger.Log("Combined Meshes currently only supported on mobile");
554 }
555#endif
556#if !UNITY_5_5_OR_NEWER
557 if (CombineMeshes)
558 {
559 CombineMeshes = false;
560 AvatarLogger.LogWarning("Combined Meshes requires Unity 5.5.0+");
561 }
562#endif
563 materialManager = gameObject.AddComponent<OvrAvatarMaterialManager>();
564
565 try
566 {
567 oculusUserIDInternal = UInt64.Parse(oculusUserID);
568 }
569 catch (Exception)
570 {
571 oculusUserIDInternal = 0;
572 AvatarLogger.LogWarning("Invalid Oculus User ID Format");
573 }
574
575 // If no oculus ID is supplied then turn off combine meshes to prevent the texture arrays
576 // being populated by invalid textures.
577 if (oculusUserIDInternal == 0)
578 {
579 AvatarLogger.LogWarning("Oculus User ID set to 0. Provide actual user ID: " + gameObject.name);
580 CombineMeshes = false;
581 }
582
583 AvatarLogger.Log("Starting OvrAvatar " + gameObject.name);
584 AvatarLogger.Log(AvatarLogger.Tab + "LOD: " + LevelOfDetail.ToString());
585 AvatarLogger.Log(AvatarLogger.Tab + "Combine Meshes: " + CombineMeshes);
586 AvatarLogger.Log(AvatarLogger.Tab + "Force Mobile Textures: " + USE_MOBILE_TEXTURE_FORMAT);
587 AvatarLogger.Log(AvatarLogger.Tab + "Oculus User ID: " + oculusUserIDInternal);
588
589 Capabilities = 0;
590 if (EnableBody) Capabilities |= ovrAvatarCapabilities.Body;
591 if (EnableHands) Capabilities |= ovrAvatarCapabilities.Hands;
592 if (EnableBase && EnableBody) Capabilities |= ovrAvatarCapabilities.Base;
593 if (EnableExpressive) Capabilities |= ovrAvatarCapabilities.Expressive;
594
595 // Enable body tilt on 6dof devices
596 if(OVRPlugin.positionSupported)
597 {
598 Capabilities |= ovrAvatarCapabilities.BodyTilt;
599 }
600
601 ShowLeftController(StartWithControllers);
602 ShowRightController(StartWithControllers);
603
604 OvrAvatarSDKManager.AvatarSpecRequestParams avatarSpecRequest = new OvrAvatarSDKManager.AvatarSpecRequestParams(
605 oculusUserIDInternal,
606 this.AvatarSpecificationCallback,
607 CombineMeshes,
608 LevelOfDetail,
609 USE_MOBILE_TEXTURE_FORMAT,
610 LookAndFeelVersion,
611 FallbackLookAndFeelVersion,
612 EnableExpressive);
613
614 OvrAvatarSDKManager.Instance.RequestAvatarSpecification(avatarSpecRequest);
615 OvrAvatarSDKManager.Instance.AddLoadingAvatar(GetInstanceID());
616
617 waitingForCombinedMesh = CombineMeshes;
618 if (Driver != null)
619 {
620 Driver.Mode = UseSDKPackets ? OvrAvatarDriver.PacketMode.SDK : OvrAvatarDriver.PacketMode.Unity;
621 }
622 }
623
624 void Update()
625 {
626 if (!OvrAvatarSDKManager.Instance || sdkAvatar == IntPtr.Zero || materialManager == null)
627 {
628 return;
629 }
630
631 if (Driver != null)
632 {
633 Driver.UpdateTransforms(sdkAvatar);
634
635 foreach (float[] voiceUpdate in voiceUpdates)
636 {
637 CAPI.ovrAvatarPose_UpdateVoiceVisualization(sdkAvatar, voiceUpdate);
638 }
639
640 voiceUpdates.Clear();
641#if AVATAR_INTERNAL
642 if (BlendController != null)
643 {
644 BlendController.UpdateBlend(sdkAvatar);
645 }
646#endif
647 CAPI.ovrAvatarPose_Finalize(sdkAvatar, Time.deltaTime);
648 }
649
650 if (RecordPackets)
651 {
652 RecordFrame();
653 }
654
655 if (assetLoadingIds.Count == 0)
656 {
657 if (!assetsFinishedLoading)
658 {
659 BuildRenderComponents();
660#if AVATAR_INTERNAL
661 AssetsDoneLoading.Invoke();
662#endif
663 InitPostLoad();
664 assetsFinishedLoading = true;
665 OvrAvatarSDKManager.Instance.RemoveLoadingAvatar(GetInstanceID());
666 }
667
668 UpdateVoiceBehavior();
669 UpdateCustomPoses();
670 if (EnableExpressive)
671 {
672 UpdateExpressive();
673 }
674 }
675 }
676
677 public static ovrAvatarHandInputState CreateInputState(ovrAvatarTransform transform, OvrAvatarDriver.ControllerPose pose)
678 {
679 ovrAvatarHandInputState inputState = new ovrAvatarHandInputState();
680 inputState.transform = transform;
681 inputState.buttonMask = pose.buttons;
682 inputState.touchMask = pose.touches;
683 inputState.joystickX = pose.joystickPosition.x;
684 inputState.joystickY = pose.joystickPosition.y;
685 inputState.indexTrigger = pose.indexTrigger;
686 inputState.handTrigger = pose.handTrigger;
687 inputState.isActive = pose.isActive;
688 return inputState;
689 }
690
691 public void ShowControllers(bool show)
692 {
693 ShowLeftController(show);
694 ShowRightController(show);
695 }
696
697 public void ShowLeftController(bool show)
698 {
699 if (sdkAvatar != IntPtr.Zero)
700 {
701 CAPI.ovrAvatar_SetLeftControllerVisibility(sdkAvatar, show);
702 }
703 showLeftController = show;
704 }
705
706 public void ShowRightController(bool show)
707 {
708 if (sdkAvatar != IntPtr.Zero)
709 {
710 CAPI.ovrAvatar_SetRightControllerVisibility(sdkAvatar, show);
711 }
712 showRightController = show;
713 }
714
715 public void UpdateVoiceVisualization(float[] voiceSamples)
716 {
717 voiceUpdates.Add(voiceSamples);
718 }
719
720 void RecordFrame()
721 {
722 if(UseSDKPackets)
723 {
724 RecordSDKFrame();
725 }
726 else
727 {
728 RecordUnityFrame();
729 }
730 }
731
732 // Meant to be used mutually exclusively with RecordSDKFrame to give user more options to optimize or tweak packet data
733 private void RecordUnityFrame()
734 {
735 var deltaSeconds = Time.deltaTime;
736 var frame = Driver.GetCurrentPose();
737 // If this is our first packet, store the pose as the initial frame
738 if (CurrentUnityPacket == null)
739 {
740 CurrentUnityPacket = new OvrAvatarPacket(frame);
741 deltaSeconds = 0;
742 }
743
744 float recordedSeconds = 0;
745 while (recordedSeconds < deltaSeconds)
746 {
747 float remainingSeconds = deltaSeconds - recordedSeconds;
748 float remainingPacketSeconds = PacketSettings.UpdateRate - CurrentUnityPacket.Duration;
749
750 // If we're not going to fill the packet, just add the frame
751 if (remainingSeconds < remainingPacketSeconds)
752 {
753 CurrentUnityPacket.AddFrame(frame, remainingSeconds);
754 recordedSeconds += remainingSeconds;
755 }
756
757 // If we're going to fill the packet, interpolate the pose, send the packet,
758 // and open a new one
759 else
760 {
761 // Interpolate between the packet's last frame and our target pose
762 // to compute a pose at the end of the packet time.
763 OvrAvatarDriver.PoseFrame a = CurrentUnityPacket.FinalFrame;
764 OvrAvatarDriver.PoseFrame b = frame;
765 float t = remainingPacketSeconds / remainingSeconds;
766 OvrAvatarDriver.PoseFrame intermediatePose = OvrAvatarDriver.PoseFrame.Interpolate(a, b, t);
767 CurrentUnityPacket.AddFrame(intermediatePose, remainingPacketSeconds);
768 recordedSeconds += remainingPacketSeconds;
769
770 // Broadcast the recorded packet
771 if (PacketRecorded != null)
772 {
773 PacketRecorded(this, new PacketEventArgs(CurrentUnityPacket));
774 }
775
776 // Open a new packet
777 CurrentUnityPacket = new OvrAvatarPacket(intermediatePose);
778 }
779 }
780 }
781
782 private void RecordSDKFrame()
783 {
784 if (sdkAvatar == IntPtr.Zero)
785 {
786 return;
787 }
788
789 if (!PacketSettings.RecordingFrames)
790 {
791 CAPI.ovrAvatarPacket_BeginRecording(sdkAvatar);
792 PacketSettings.AccumulatedTime = 0.0f;
793 PacketSettings.RecordingFrames = true;
794 }
795
796 PacketSettings.AccumulatedTime += Time.deltaTime;
797
798 if (PacketSettings.AccumulatedTime >= PacketSettings.UpdateRate)
799 {
800 PacketSettings.AccumulatedTime = 0.0f;
801 var packet = CAPI.ovrAvatarPacket_EndRecording(sdkAvatar);
802 CAPI.ovrAvatarPacket_BeginRecording(sdkAvatar);
803
804 if (PacketRecorded != null)
805 {
806 PacketRecorded(this, new PacketEventArgs(new OvrAvatarPacket { ovrNativePacket = packet }));
807 }
808
809 CAPI.ovrAvatarPacket_Free(packet);
810 }
811 }
812
813 private void AddRenderParts(
814 OvrAvatarComponent ovrComponent,
815 ovrAvatarComponent component,
816 Transform parent)
817 {
818 bool isBody = ovrComponent.name == "body";
819 bool isLeftController = ovrComponent.name == "controller_left";
820 bool isReftController = ovrComponent.name == "controller_right";
821
822 for (UInt32 renderPartIndex = 0; renderPartIndex < component.renderPartCount; renderPartIndex++)
823 {
824 GameObject renderPartObject = new GameObject();
825 renderPartObject.name = GetRenderPartName(component, renderPartIndex);
826 renderPartObject.transform.SetParent(parent);
827 IntPtr renderPart = GetRenderPart(component, renderPartIndex);
828 ovrAvatarRenderPartType type = CAPI.ovrAvatarRenderPart_GetType(renderPart);
829 OvrAvatarRenderComponent ovrRenderPart = null;
830 switch (type)
831 {
832 case ovrAvatarRenderPartType.SkinnedMeshRender:
833 ovrRenderPart = AddSkinnedMeshRenderComponent(renderPartObject, CAPI.ovrAvatarRenderPart_GetSkinnedMeshRender(renderPart));
834 break;
835 case ovrAvatarRenderPartType.SkinnedMeshRenderPBS:
836 ovrRenderPart = AddSkinnedMeshRenderPBSComponent(renderPartObject, CAPI.ovrAvatarRenderPart_GetSkinnedMeshRenderPBS(renderPart));
837 break;
838 case ovrAvatarRenderPartType.SkinnedMeshRenderPBS_V2:
839 {
840 ovrRenderPart = AddSkinnedMeshRenderPBSV2Component(
841 renderPart,
842 renderPartObject,
843 CAPI.ovrAvatarRenderPart_GetSkinnedMeshRenderPBSV2(renderPart),
844 isBody && renderPartIndex == 0,
845 isLeftController || isReftController);
846 }
847 break;
848 default:
849 break;
850 }
851
852 if (ovrRenderPart != null)
853 {
854 ovrComponent.RenderParts.Add(ovrRenderPart);
855 }
856 }
857 }
858
859 public void RefreshBodyParts()
860 {
861 if (Body != null)
862 {
863 foreach (var part in Body.RenderParts)
864 {
865 Destroy(part.gameObject);
866 }
867
868 Body.RenderParts.Clear();
869
870 var nativeAvatarComponent = Body.GetNativeAvatarComponent();
871 if (nativeAvatarComponent.HasValue)
872 {
873 AddRenderParts(Body, nativeAvatarComponent.Value, Body.gameObject.transform);
874 }
875 }
876 }
877
878 public ovrAvatarBodyComponent? GetBodyComponent()
879 {
880 if (Body != null)
881 {
882 CAPI.ovrAvatarPose_GetBodyComponent(sdkAvatar, ref Body.component);
883 return Body.component;
884 }
885
886 return null;
887 }
888
889 public Transform GetHandTransform(HandType hand, HandJoint joint)
890 {
891 if (hand >= HandType.Max || joint >= HandJoint.Max)
892 {
893 return null;
894 }
895
896 var HandObject = hand == HandType.Left ? HandLeft : HandRight;
897
898 if (HandObject != null)
899 {
900 var AvatarComponent = HandObject.GetComponent<OvrAvatarComponent>();
901 if (AvatarComponent != null && AvatarComponent.RenderParts.Count > 0)
902 {
903 var SkinnedMesh = AvatarComponent.RenderParts[0];
904 return SkinnedMesh.transform.Find(HandJoints[(int)hand, (int)joint]);
905 }
906 }
907
908 return null;
909 }
910
911 public void GetPointingDirection(HandType hand, ref Vector3 forward, ref Vector3 up)
912 {
913 Transform handBase = GetHandTransform(hand, HandJoint.HandBase);
914
915 if (handBase != null)
916 {
917 forward = handBase.forward;
918 up = handBase.up;
919 }
920 }
921
922 static Vector3 MOUTH_POSITION_OFFSET = new Vector3(0, -0.018f, 0.1051f);
923 static string VOICE_PROPERTY = "_Voice";
924 static string MOUTH_POSITION_PROPERTY = "_MouthPosition";
925 static string MOUTH_DIRECTION_PROPERTY = "_MouthDirection";
926 static string MOUTH_SCALE_PROPERTY = "_MouthEffectScale";
927
928 static float MOUTH_SCALE_GLOBAL = 0.007f;
929 static float MOUTH_MAX_GLOBAL = 0.007f;
930 static string NECK_JONT = "root_JNT/body_JNT/chest_JNT/neckBase_JNT/neck_JNT";
931
932 public float VoiceAmplitude = 0f;
933 public bool EnableMouthVertexAnimation = false;
934
935 private void UpdateVoiceBehavior()
936 {
937 if (!EnableMouthVertexAnimation)
938 {
939 return;
940 }
941
942 if (Body != null)
943 {
944 OvrAvatarComponent component = Body.GetComponent<OvrAvatarComponent>();
945
946 VoiceAmplitude = Mathf.Clamp(VoiceAmplitude, 0f, 1f);
947
948 if (component.RenderParts.Count > 0)
949 {
950 var material = component.RenderParts[0].mesh.sharedMaterial;
951 var neckJoint = component.RenderParts[0].mesh.transform.Find(NECK_JONT);
952 var scaleDiff = neckJoint.TransformPoint(Vector3.up) - neckJoint.position;
953
954 material.SetFloat(MOUTH_SCALE_PROPERTY, scaleDiff.magnitude);
955
956 material.SetFloat(
957 VOICE_PROPERTY,
958 Mathf.Min(scaleDiff.magnitude * MOUTH_MAX_GLOBAL, scaleDiff.magnitude * VoiceAmplitude * MOUTH_SCALE_GLOBAL));
959
960 material.SetVector(
961 MOUTH_POSITION_PROPERTY,
962 neckJoint.TransformPoint(MOUTH_POSITION_OFFSET));
963
964 material.SetVector(MOUTH_DIRECTION_PROPERTY, neckJoint.up);
965 }
966 }
967 }
968
969 bool IsValidMic()
970 {
971 if (Microphone.devices.Length < 1)
972 {
973 return false;
974 }
975
976 string selectedDevice = Microphone.devices[0].ToString();
977
978 int minFreq;
979 int maxFreq;
980 Microphone.GetDeviceCaps(selectedDevice, out minFreq, out maxFreq);
981
982 if (maxFreq == 0)
983 {
984 maxFreq = 44100;
985 }
986
987 AudioClip clip = Microphone.Start(selectedDevice, true, 1, maxFreq);
988 if (clip == null)
989 {
990 return false;
991 }
992
993 Microphone.End(selectedDevice);
994 return true;
995 }
996
997 void InitPostLoad()
998 {
999 ExpressiveGlobalInit();
1000
1001 ConfigureHelpers();
1002
1003 if (GetComponent<OvrAvatarLocalDriver>() != null)
1004 {
1005 // Use mic.
1006 lipsyncContext.audioLoopback = false;
1007 if (CanOwnMicrophone && IsValidMic())
1008 {
1009 micInput = MouthAnchor.gameObject.AddComponent<OVRLipSyncMicInput>();
1010 micInput.enableMicSelectionGUI = false;
1011 micInput.MicFrequency = 44100;
1012 micInput.micControl = OVRLipSyncMicInput.micActivation.ConstantSpeak;
1013 }
1014
1015 // Set lipsync animation parameters in SDK
1016 CAPI.ovrAvatar_SetActionUnitOnsetSpeed(sdkAvatar, ACTION_UNIT_ONSET_SPEED);
1017 CAPI.ovrAvatar_SetActionUnitFalloffSpeed(sdkAvatar, ACTION_UNIT_FALLOFF_SPEED);
1018 CAPI.ovrAvatar_SetVisemeMultiplier(sdkAvatar, VISEME_LEVEL_MULTIPLIER);
1019 }
1020 }
1021
1022 static ovrAvatarLights ovrLights = new ovrAvatarLights();
1023 static void ExpressiveGlobalInit()
1024 {
1025 if (doneExpressiveGlobalInit)
1026 {
1027 return;
1028 }
1029
1030 doneExpressiveGlobalInit = true;
1031
1032 // This array size has to match the 'MarshalAs' attribute in the ovrAvatarLights declaration.
1033 const int MAXSIZE = 16;
1034 ovrLights.lights = new ovrAvatarLight[MAXSIZE];
1035
1036 InitializeLights();
1037 }
1038
1039 static void InitializeLights()
1040 {
1041 // Set light info. Lights are shared across all avatar instances.
1042 ovrLights.ambientIntensity = RenderSettings.ambientLight.grayscale * 0.5f;
1043
1044 Light[] sceneLights = FindObjectsOfType(typeof(Light)) as Light[];
1045 int i = 0;
1046 for (i = 0; i < sceneLights.Length && i < ovrLights.lights.Length; ++i)
1047 {
1048 Light sceneLight = sceneLights[i];
1049 if (sceneLight && sceneLight.enabled)
1050 {
1051 uint instanceID = (uint)sceneLight.transform.GetInstanceID();
1052 switch (sceneLight.type)
1053 {
1054 case LightType.Directional:
1055 {
1056 CreateLightDirectional(instanceID, sceneLight.transform.forward, sceneLight.intensity, ref ovrLights.lights[i]);
1057 break;
1058 }
1059 case LightType.Point:
1060 {
1061 CreateLightPoint(instanceID, sceneLight.transform.position, sceneLight.range, sceneLight.intensity, ref ovrLights.lights[i]);
1062 break;
1063 }
1064 case LightType.Spot:
1065 {
1066 CreateLightSpot(instanceID, sceneLight.transform.position, sceneLight.transform.forward, sceneLight.spotAngle, sceneLight.range, sceneLight.intensity, ref ovrLights.lights[i]);
1067 break;
1068 }
1069 }
1070 }
1071 }
1072
1073 ovrLights.lightCount = (uint)i;
1074
1075 CAPI.ovrAvatar_UpdateLights(ovrLights);
1076 }
1077
1078 static ovrAvatarLight CreateLightDirectional(uint id, Vector3 direction, float intensity, ref ovrAvatarLight light)
1079 {
1080 light.id = id;
1081 light.type = ovrAvatarLightType.Direction;
1082 light.worldDirection = new Vector3(direction.x, direction.y, -direction.z);
1083 light.intensity = intensity;
1084 return light;
1085 }
1086
1087 static ovrAvatarLight CreateLightPoint(uint id, Vector3 position, float range, float intensity, ref ovrAvatarLight light)
1088 {
1089 light.id = id;
1090 light.type = ovrAvatarLightType.Point;
1091 light.worldPosition = new Vector3(position.x, position.y, -position.z);
1092 light.range = range;
1093 light.intensity = intensity;
1094 return light;
1095 }
1096
1097 static ovrAvatarLight CreateLightSpot(uint id, Vector3 position, Vector3 direction, float spotAngleDeg, float range, float intensity, ref ovrAvatarLight light)
1098 {
1099 light.id = id;
1100 light.type = ovrAvatarLightType.Spot;
1101 light.worldPosition = new Vector3(position.x, position.y, -position.z);
1102 light.worldDirection = new Vector3(direction.x, direction.y, -direction.z);
1103 light.spotAngleDeg = spotAngleDeg;
1104 light.range = range;
1105 light.intensity = intensity;
1106 return light;
1107 }
1108
1109 void UpdateExpressive()
1110 {
1111 ovrAvatarTransform baseTransform = OvrAvatar.CreateOvrAvatarTransform(transform.position, transform.rotation);
1112 CAPI.ovrAvatar_UpdateWorldTransform(sdkAvatar, baseTransform);
1113
1114 UpdateFacewave();
1115 }
1116
1117 private void ConfigureHelpers()
1118 {
1119 Transform head =
1120 transform.Find("body/body_renderPart_0/root_JNT/body_JNT/chest_JNT/neckBase_JNT/neck_JNT/head_JNT");
1121 if (head == null)
1122 {
1123 AvatarLogger.LogError("Avatar helper config failed. Cannot find head transform. All helpers spawning on root avatar transform");
1124 head = transform;
1125 }
1126
1127 if (MouthAnchor == null)
1128 {
1129 MouthAnchor = CreateHelperObject(head, MOUTH_HEAD_OFFSET, MOUTH_HELPER_NAME);
1130 }
1131
1132 if (GetComponent<OvrAvatarLocalDriver>() != null)
1133 {
1134 if (audioSource == null)
1135 {
1136 audioSource = MouthAnchor.gameObject.AddComponent<AudioSource>();
1137 }
1138 spatializedSource = MouthAnchor.GetComponent<ONSPAudioSource>();
1139
1140 if (spatializedSource == null)
1141 {
1142 spatializedSource = MouthAnchor.gameObject.AddComponent<ONSPAudioSource>();
1143 }
1144
1145 spatializedSource.UseInvSqr = true;
1146 spatializedSource.EnableRfl = false;
1147 spatializedSource.EnableSpatialization = true;
1148 spatializedSource.Far = 100f;
1149 spatializedSource.Near = 0.1f;
1150
1151 // Add phoneme context to the mouth anchor
1152 lipsyncContext = MouthAnchor.GetComponent<OVRLipSyncContext>();
1153 if (lipsyncContext == null)
1154 {
1155 lipsyncContext = MouthAnchor.gameObject.AddComponent<OVRLipSyncContext>();
1156 }
1157
1158 lipsyncContext.provider = EnableLaughter
1159 ? OVRLipSync.ContextProviders.Enhanced_with_Laughter
1160 : OVRLipSync.ContextProviders.Enhanced;
1161
1162 // Ignore audio callback if microphone is owned by VoIP
1163 lipsyncContext.skipAudioSource = !CanOwnMicrophone;
1164
1165 StartCoroutine(WaitForMouthAudioSource());
1166 }
1167
1168 if (GetComponent<OvrAvatarRemoteDriver>() != null)
1169 {
1170 GazeTarget headTarget = head.gameObject.AddComponent<GazeTarget>();
1171 headTarget.Type = ovrAvatarGazeTargetType.AvatarHead;
1172 AvatarLogger.Log("Added head as gaze target");
1173
1174 Transform hand = transform.Find("hand_left");
1175 if (hand == null)
1176 {
1177 AvatarLogger.LogWarning("Gaze target helper config failed: Cannot find left hand transform");
1178 }
1179 else
1180 {
1181 GazeTarget handTarget = hand.gameObject.AddComponent<GazeTarget>();
1182 handTarget.Type = ovrAvatarGazeTargetType.AvatarHand;
1183 AvatarLogger.Log("Added left hand as gaze target");
1184 }
1185
1186 hand = transform.Find("hand_right");
1187 if (hand == null)
1188 {
1189 AvatarLogger.Log("Gaze target helper config failed: Cannot find right hand transform");
1190 }
1191 else
1192 {
1193 GazeTarget handTarget = hand.gameObject.AddComponent<GazeTarget>();
1194 handTarget.Type = ovrAvatarGazeTargetType.AvatarHand;
1195 AvatarLogger.Log("Added right hand as gaze target");
1196 }
1197 }
1198 }
1199
1200 private IEnumerator WaitForMouthAudioSource()
1201 {
1202 while (MouthAnchor.GetComponent<AudioSource>() == null)
1203 {
1204 yield return new WaitForSeconds(0.1f);
1205 }
1206 AudioSource AS = MouthAnchor.GetComponent<AudioSource>();
1207 AS.minDistance = 0.3f;
1208 AS.maxDistance = 4f;
1209 AS.rolloffMode = AudioRolloffMode.Logarithmic;
1210 AS.loop = true;
1211 AS.playOnAwake = true;
1212 AS.spatialBlend = 1.0f;
1213 AS.spatialize = true;
1214 AS.spatializePostEffects = true;
1215 }
1216
1217 public void DestroyHelperObjects()
1218 {
1219 if (MouthAnchor)
1220 {
1221 DestroyImmediate(MouthAnchor.gameObject);
1222 }
1223 }
1224
1225 public GameObject CreateHelperObject(Transform parent, Vector3 localPositionOffset, string helperName,
1226 string helperTag = "")
1227 {
1228 GameObject helper = new GameObject();
1229 helper.name = helperName;
1230 if (helperTag != "")
1231 {
1232 helper.tag = helperTag;
1233 }
1234 helper.transform.SetParent(parent);
1235 helper.transform.localRotation = Quaternion.identity;
1236 helper.transform.localPosition = localPositionOffset;
1237 return helper;
1238 }
1239
1240 public void UpdateVoiceData(short[] pcmData, int numChannels)
1241 {
1242 if (lipsyncContext != null && micInput == null)
1243 {
1244 lipsyncContext.ProcessAudioSamplesRaw(pcmData, numChannels);
1245 }
1246 }
1247 public void UpdateVoiceData(float[] pcmData, int numChannels)
1248 {
1249 if (lipsyncContext != null && micInput == null)
1250 {
1251 lipsyncContext.ProcessAudioSamplesRaw(pcmData, numChannels);
1252 }
1253 }
1254
1255
1256 private void UpdateFacewave()
1257 {
1258 if (lipsyncContext != null && (micInput != null || CanOwnMicrophone == false))
1259 {
1260 // Get the current viseme frame
1261 currentFrame = lipsyncContext.GetCurrentPhonemeFrame();
1262
1263 // Verify length (-1 for laughter)
1264 if (currentFrame.Visemes.Length != (VISEME_COUNT - 1))
1265 {
1266 Debug.LogError("Unexpected number of visemes " + currentFrame.Visemes);
1267 return;
1268 }
1269
1270 // Copy to viseme array
1271 currentFrame.Visemes.CopyTo(visemes, 0);
1272 // Copy laughter as final element
1273 visemes[VISEME_COUNT - 1] = EnableLaughter ? currentFrame.laughterScore : 0.0f;
1274
1275 // Send visemes to native implementation.
1276 for (int i = 0; i < VISEME_COUNT; i++)
1277 {
1278 RuntimeVisemes.visemeParams[i] = visemes[i];
1279 }
1280 CAPI.ovrAvatar_SetVisemes(sdkAvatar, RuntimeVisemes);
1281 }
1282 }
1283}