Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Linq;
  5. using Oculus.Avatar;
  6. using System.Runtime.InteropServices;
  7. using System.Collections.Generic;
  8. using UnityEngine.Events;
  9.  
  10. #if UNITY_EDITOR
  11. using UnityEditor;
  12. #endif
  13.  
  14. [System.Serializable]
  15. public class AvatarLayer
  16. {
  17. public int layerIndex;
  18. }
  19.  
  20. #if UNITY_EDITOR
  21. [CustomPropertyDrawer(typeof(AvatarLayer))]
  22. public class AvatarLayerPropertyDrawer : PropertyDrawer
  23. {
  24. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  25. {
  26. EditorGUI.BeginProperty(position, GUIContent.none, property);
  27. SerializedProperty layerIndex = property.FindPropertyRelative("layerIndex");
  28. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  29. layerIndex.intValue = EditorGUI.LayerField(position, layerIndex.intValue);
  30. EditorGUI.EndProperty();
  31. }
  32. }
  33. #endif
  34.  
  35. [System.Serializable]
  36. public class PacketRecordSettings
  37. {
  38. internal bool RecordingFrames = false;
  39. public float UpdateRate = 1f / 30f; // 30 hz update of packets
  40. internal float AccumulatedTime;
  41. };
  42.  
  43. public class OvrAvatar : MonoBehaviour
  44. {
  45. public OvrAvatarMaterialManager DefaultBodyMaterialManager;
  46. public OvrAvatarMaterialManager DefaultHandMaterialManager;
  47.  
  48. public OvrAvatarDriver Driver;
  49. public OvrAvatarBase Base;
  50. public OvrAvatarBody Body;
  51. public OvrAvatarTouchController ControllerLeft;
  52. public OvrAvatarTouchController ControllerRight;
  53. public OvrAvatarHand HandLeft;
  54. public OvrAvatarHand HandRight;
  55. public bool RecordPackets;
  56. public bool UseSDKPackets = true;
  57.  
  58. public bool StartWithControllers;
  59. public AvatarLayer FirstPersonLayer;
  60. public AvatarLayer ThirdPersonLayer;
  61. public bool ShowFirstPerson = true;
  62. public bool ShowThirdPerson;
  63. public ovrAvatarCapabilities Capabilities = ovrAvatarCapabilities.All;
  64. public Shader SurfaceShader;
  65. public Shader SurfaceShaderSelfOccluding;
  66. public Shader SurfaceShaderPBS;
  67. public Shader SurfaceShaderPBSV2Single;
  68. public Shader SurfaceShaderPBSV2Combined;
  69. public Shader SurfaceShaderPBSV2Simple;
  70. public Shader SurfaceShaderPBSV2Loading;
  71.  
  72. int renderPartCount = 0;
  73. bool showLeftController;
  74. bool showRightController;
  75. List<float[]> voiceUpdates = new List<float[]>();
  76.  
  77. public string oculusUserID;
  78. internal UInt64 oculusUserIDInternal;
  79.  
  80. #if UNITY_ANDROID && UNITY_5_5_OR_NEWER && !UNITY_EDITOR
  81. bool CombineMeshes = true;
  82. #else
  83. bool CombineMeshes = false;
  84. #endif
  85.  
  86. #if UNITY_EDITOR && UNITY_ANDROID
  87. bool ForceMobileTextureFormat = true;
  88. #else
  89. bool ForceMobileTextureFormat = false;
  90.  
  91. #endif
  92. public bool CombineMesh = false;
  93. private bool WaitingForCombinedMesh = false;
  94.  
  95. public IntPtr sdkAvatar = IntPtr.Zero;
  96. private HashSet<UInt64> assetLoadingIds = new HashSet<UInt64>();
  97. private Dictionary<string, OvrAvatarComponent> trackedComponents =
  98. new Dictionary<string, OvrAvatarComponent>();
  99.  
  100. public UnityEvent AssetsDoneLoading = new UnityEvent();
  101. bool assetsFinishedLoading = false;
  102.  
  103. public Transform LeftHandCustomPose;
  104. public Transform RightHandCustomPose;
  105. Transform cachedLeftHandCustomPose;
  106. Transform[] cachedCustomLeftHandJoints;
  107. ovrAvatarTransform[] cachedLeftHandTransforms;
  108. Transform cachedRightHandCustomPose;
  109. Transform[] cachedCustomRightHandJoints;
  110. ovrAvatarTransform[] cachedRightHandTransforms;
  111.  
  112.  
  113. private Vector4 clothingAlphaOffset = new Vector4(0f, 0f, 0f, 1f);
  114. private UInt64 clothingAlphaTexture = 0;
  115.  
  116. public class PacketEventArgs : EventArgs
  117. {
  118. public readonly OvrAvatarPacket Packet;
  119. public PacketEventArgs(OvrAvatarPacket packet)
  120. {
  121. Packet = packet;
  122. }
  123. }
  124.  
  125. public PacketRecordSettings PacketSettings = new PacketRecordSettings();
  126.  
  127. OvrAvatarPacket CurrentUnityPacket;
  128.  
  129. public enum HandType
  130. {
  131. Right,
  132. Left,
  133.  
  134. Max
  135. };
  136.  
  137. public enum HandJoint
  138. {
  139. HandBase,
  140. IndexBase,
  141. IndexTip,
  142. ThumbBase,
  143. ThumbTip,
  144.  
  145. Max,
  146. }
  147.  
  148. private static string[,] HandJoints = new string[(int)HandType.Max, (int)HandJoint.Max]
  149. {
  150. {
  151. "hands:r_hand_world",
  152. "hands:r_hand_world/hands:b_r_hand/hands:b_r_index1",
  153. "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",
  154. "hands:r_hand_world/hands:b_r_hand/hands:b_r_thumb1/hands:b_r_thumb2",
  155. "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"
  156. },
  157. {
  158. "hands:l_hand_world",
  159. "hands:l_hand_world/hands:b_l_hand/hands:b_l_index1",
  160. "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",
  161. "hands:l_hand_world/hands:b_l_hand/hands:b_l_thumb1/hands:b_l_thumb2",
  162. "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"
  163. }
  164. };
  165.  
  166. #if UNITY_ANDROID
  167. internal ovrAvatarAssetLevelOfDetail LevelOfDetail = ovrAvatarAssetLevelOfDetail.Medium;
  168. #else
  169. internal ovrAvatarAssetLevelOfDetail LevelOfDetail = ovrAvatarAssetLevelOfDetail.Highest;
  170. #endif
  171.  
  172. private void Awake()
  173. {
  174. CombineMeshes = CombineMesh;
  175. }
  176.  
  177. void OnDestroy()
  178. {
  179. if (sdkAvatar != IntPtr.Zero)
  180. {
  181. CAPI.ovrAvatar_Destroy(sdkAvatar);
  182. }
  183. }
  184.  
  185. public EventHandler<PacketEventArgs> PacketRecorded;
  186.  
  187. public void AssetLoadedCallback(OvrAvatarAsset asset)
  188. {
  189. assetLoadingIds.Remove(asset.assetID);
  190. }
  191.  
  192. public void CombinedMeshLoadedCallback(IntPtr assetPtr)
  193. {
  194. if (!WaitingForCombinedMesh)
  195. {
  196. return;
  197. }
  198.  
  199. var meshIDs = CAPI.ovrAvatarAsset_GetCombinedMeshIDs(assetPtr);
  200. foreach (var id in meshIDs)
  201. {
  202. assetLoadingIds.Remove(id);
  203. }
  204.  
  205. CAPI.ovrAvatar_GetCombinedMeshAlphaData(sdkAvatar, ref clothingAlphaTexture, ref clothingAlphaOffset);
  206.  
  207. WaitingForCombinedMesh = false;
  208. }
  209.  
  210. private void AddAvatarComponent(GameObject componentObject, ovrAvatarComponent component)
  211. {
  212. OvrAvatarComponent ovrComponent = componentObject.AddComponent<OvrAvatarComponent>();
  213. trackedComponents.Add(component.name, ovrComponent);
  214.  
  215. if (ovrComponent.name == "body")
  216. {
  217. ovrComponent.ClothingAlphaOffset = clothingAlphaOffset;
  218. ovrComponent.ClothingAlphaTexture = clothingAlphaTexture;
  219. }
  220.  
  221. AddRenderParts(ovrComponent, component, componentObject.transform);
  222. }
  223.  
  224. private OvrAvatarSkinnedMeshRenderComponent AddSkinnedMeshRenderComponent(GameObject gameObject, ovrAvatarRenderPart_SkinnedMeshRender skinnedMeshRender)
  225. {
  226. OvrAvatarSkinnedMeshRenderComponent skinnedMeshRenderer = gameObject.AddComponent<OvrAvatarSkinnedMeshRenderComponent>();
  227. skinnedMeshRenderer.Initialize(skinnedMeshRender, SurfaceShader, SurfaceShaderSelfOccluding, ThirdPersonLayer.layerIndex, FirstPersonLayer.layerIndex, renderPartCount++);
  228. return skinnedMeshRenderer;
  229. }
  230.  
  231. private OvrAvatarSkinnedMeshRenderPBSComponent AddSkinnedMeshRenderPBSComponent(GameObject gameObject, ovrAvatarRenderPart_SkinnedMeshRenderPBS skinnedMeshRenderPBS)
  232. {
  233. OvrAvatarSkinnedMeshRenderPBSComponent skinnedMeshRenderer = gameObject.AddComponent<OvrAvatarSkinnedMeshRenderPBSComponent>();
  234. skinnedMeshRenderer.Initialize(skinnedMeshRenderPBS, SurfaceShaderPBS, ThirdPersonLayer.layerIndex, FirstPersonLayer.layerIndex, renderPartCount++);
  235. return skinnedMeshRenderer;
  236. }
  237.  
  238. private OvrAvatarSkinnedMeshPBSV2RenderComponent AddSkinnedMeshRenderPBSV2Component(
  239. IntPtr renderPart,
  240. GameObject gameObject,
  241. ovrAvatarRenderPart_SkinnedMeshRenderPBS_V2 skinnedMeshRenderPBSV2,
  242. OvrAvatarMaterialManager materialManager)
  243. {
  244. OvrAvatarSkinnedMeshPBSV2RenderComponent skinnedMeshRenderer = gameObject.AddComponent<OvrAvatarSkinnedMeshPBSV2RenderComponent>();
  245. skinnedMeshRenderer.Initialize(
  246. renderPart,
  247. skinnedMeshRenderPBSV2,
  248. materialManager,
  249. ThirdPersonLayer.layerIndex,
  250. FirstPersonLayer.layerIndex,
  251. renderPartCount++,
  252. gameObject.name.Contains("body") && CombineMeshes,
  253. LevelOfDetail);
  254.  
  255. return skinnedMeshRenderer;
  256. }
  257.  
  258. private OvrAvatarProjectorRenderComponent AddProjectorRenderComponent(GameObject gameObject, ovrAvatarRenderPart_ProjectorRender projectorRender)
  259. {
  260. ovrAvatarComponent component = CAPI.ovrAvatarComponent_Get(sdkAvatar, projectorRender.componentIndex);
  261. OvrAvatarComponent ovrComponent;
  262. if (trackedComponents.TryGetValue(component.name, out ovrComponent))
  263. {
  264. if (projectorRender.renderPartIndex < ovrComponent.RenderParts.Count)
  265. {
  266. OvrAvatarRenderComponent targetRenderPart = ovrComponent.RenderParts[(int)projectorRender.renderPartIndex];
  267. OvrAvatarProjectorRenderComponent projectorComponent = gameObject.AddComponent<OvrAvatarProjectorRenderComponent>();
  268. projectorComponent.InitializeProjectorRender(projectorRender, SurfaceShader, targetRenderPart);
  269. return projectorComponent;
  270. }
  271. }
  272. return null;
  273. }
  274.  
  275. static public IntPtr GetRenderPart(ovrAvatarComponent component, UInt32 renderPartIndex)
  276. {
  277. long offset = Marshal.SizeOf(typeof(IntPtr)) * renderPartIndex;
  278. IntPtr marshalPtr = new IntPtr(component.renderParts.ToInt64() + offset);
  279. return (IntPtr)Marshal.PtrToStructure(marshalPtr, typeof(IntPtr));
  280. }
  281.  
  282. private void UpdateAvatarComponent(ovrAvatarComponent component)
  283. {
  284. OvrAvatarComponent ovrComponent;
  285. if (!trackedComponents.TryGetValue(component.name, out ovrComponent))
  286. {
  287. throw new Exception(string.Format("trackedComponents didn't have {0}", component.name));
  288. }
  289.  
  290. ovrComponent.UpdateAvatar(component, this);
  291. }
  292.  
  293. private static string GetRenderPartName(ovrAvatarComponent component, uint renderPartIndex)
  294. {
  295. return component.name + "_renderPart_" + (int)renderPartIndex;
  296. }
  297.  
  298. internal static void ConvertTransform(ovrAvatarTransform transform, Transform target)
  299. {
  300. Vector3 position = transform.position;
  301. position.z = -position.z;
  302. Quaternion orientation = transform.orientation;
  303. orientation.x = -orientation.x;
  304. orientation.y = -orientation.y;
  305. target.localPosition = position;
  306. target.localRotation = orientation;
  307. target.localScale = transform.scale;
  308. }
  309.  
  310. public static ovrAvatarTransform CreateOvrAvatarTransform(Vector3 position, Quaternion orientation)
  311. {
  312. return new ovrAvatarTransform
  313. {
  314. position = new Vector3(position.x, position.y, -position.z),
  315. orientation = new Quaternion(-orientation.x, -orientation.y, orientation.z, orientation.w),
  316. scale = Vector3.one
  317. };
  318. }
  319.  
  320. private void RemoveAvatarComponent(string name)
  321. {
  322. OvrAvatarComponent componentObject;
  323. trackedComponents.TryGetValue(name, out componentObject);
  324. Destroy(componentObject.gameObject);
  325. trackedComponents.Remove(name);
  326. }
  327.  
  328. private void UpdateSDKAvatarUnityState()
  329. {
  330. //Iterate through all the render components
  331. UInt32 componentCount = CAPI.ovrAvatarComponent_Count(sdkAvatar);
  332. HashSet<string> componentsThisRun = new HashSet<string>();
  333. for (UInt32 i = 0; i < componentCount; i++)
  334. {
  335. IntPtr ptr = CAPI.ovrAvatarComponent_Get_Native(sdkAvatar, i);
  336. ovrAvatarComponent component = (ovrAvatarComponent)Marshal.PtrToStructure(ptr, typeof(ovrAvatarComponent));
  337. componentsThisRun.Add(component.name);
  338. if (!trackedComponents.ContainsKey(component.name))
  339. {
  340. GameObject componentObject = null;
  341. Type specificType = null;
  342. if ((Capabilities & ovrAvatarCapabilities.Base) != 0)
  343. {
  344. ovrAvatarBaseComponent? baseComponent = CAPI.ovrAvatarPose_GetBaseComponent(sdkAvatar);
  345. if (baseComponent.HasValue && ptr == baseComponent.Value.renderComponent)
  346. {
  347. specificType = typeof(OvrAvatarBase);
  348. if (Base != null)
  349. {
  350. componentObject = Base.gameObject;
  351. }
  352. }
  353. }
  354.  
  355. if (specificType == null && (Capabilities & ovrAvatarCapabilities.Body) != 0)
  356. {
  357. ovrAvatarBodyComponent? bodyComponent = CAPI.ovrAvatarPose_GetBodyComponent(sdkAvatar);
  358. if (bodyComponent.HasValue && ptr == bodyComponent.Value.renderComponent)
  359. {
  360. specificType = typeof(OvrAvatarBody);
  361. if (Body != null)
  362. {
  363. componentObject = Body.gameObject;
  364. }
  365. }
  366. }
  367.  
  368. if (specificType == null && (Capabilities & ovrAvatarCapabilities.Hands) != 0)
  369. {
  370. ovrAvatarControllerComponent? controllerComponent = CAPI.ovrAvatarPose_GetLeftControllerComponent(sdkAvatar);
  371. if (specificType == null && controllerComponent.HasValue && ptr == controllerComponent.Value.renderComponent)
  372. {
  373. specificType = typeof(OvrAvatarTouchController);
  374. if (ControllerLeft != null)
  375. {
  376. componentObject = ControllerLeft.gameObject;
  377. }
  378. }
  379.  
  380. controllerComponent = CAPI.ovrAvatarPose_GetRightControllerComponent(sdkAvatar);
  381. if (specificType == null && controllerComponent.HasValue && ptr == controllerComponent.Value.renderComponent)
  382. {
  383. specificType = typeof(OvrAvatarTouchController);
  384. if (ControllerRight != null)
  385. {
  386. componentObject = ControllerRight.gameObject;
  387. }
  388. }
  389.  
  390. ovrAvatarHandComponent? handComponent = CAPI.ovrAvatarPose_GetLeftHandComponent(sdkAvatar);
  391. if (specificType == null && handComponent.HasValue && ptr == handComponent.Value.renderComponent)
  392. {
  393. specificType = typeof(OvrAvatarHand);
  394. if (HandLeft != null)
  395. {
  396. componentObject = HandLeft.gameObject;
  397. }
  398. }
  399.  
  400. handComponent = CAPI.ovrAvatarPose_GetRightHandComponent(sdkAvatar);
  401. if (specificType == null && handComponent.HasValue && ptr == handComponent.Value.renderComponent)
  402. {
  403. specificType = typeof(OvrAvatarHand);
  404. if (HandRight != null)
  405. {
  406. componentObject = HandRight.gameObject;
  407. }
  408. }
  409. }
  410.  
  411. // If this is an unknown type, just create an object for the rendering
  412. if (componentObject == null && specificType == null)
  413. {
  414. componentObject = new GameObject();
  415. componentObject.name = component.name;
  416. componentObject.transform.SetParent(transform);
  417. }
  418. if (componentObject != null)
  419. {
  420. AddAvatarComponent(componentObject, component);
  421. }
  422. }
  423. UpdateAvatarComponent(component);
  424. }
  425. HashSet<string> deletableNames = new HashSet<string>(trackedComponents.Keys);
  426. deletableNames.ExceptWith(componentsThisRun);
  427. //deletableNames contains the name of all components which are tracked and were
  428. //not present in this run
  429. foreach (var name in deletableNames)
  430. {
  431. RemoveAvatarComponent(name);
  432. }
  433.  
  434. UpdateVoiceBehavior();
  435. }
  436.  
  437. void UpdateCustomPoses()
  438. {
  439. // Check to see if the pose roots changed
  440. if (UpdatePoseRoot(LeftHandCustomPose, ref cachedLeftHandCustomPose, ref cachedCustomLeftHandJoints, ref cachedLeftHandTransforms))
  441. {
  442. if (cachedLeftHandCustomPose == null && sdkAvatar != IntPtr.Zero)
  443. {
  444. CAPI.ovrAvatar_SetLeftHandGesture(sdkAvatar, ovrAvatarHandGesture.Default);
  445. }
  446. }
  447. if (UpdatePoseRoot(RightHandCustomPose, ref cachedRightHandCustomPose, ref cachedCustomRightHandJoints, ref cachedRightHandTransforms))
  448. {
  449. if (cachedRightHandCustomPose == null && sdkAvatar != IntPtr.Zero)
  450. {
  451. CAPI.ovrAvatar_SetRightHandGesture(sdkAvatar, ovrAvatarHandGesture.Default);
  452. }
  453. }
  454.  
  455. // Check to see if the custom gestures need to be updated
  456. if (sdkAvatar != IntPtr.Zero)
  457. {
  458. if (cachedLeftHandCustomPose != null && UpdateTransforms(cachedCustomLeftHandJoints, cachedLeftHandTransforms))
  459. {
  460. CAPI.ovrAvatar_SetLeftHandCustomGesture(sdkAvatar, (uint)cachedLeftHandTransforms.Length, cachedLeftHandTransforms);
  461. }
  462. if (cachedRightHandCustomPose != null && UpdateTransforms(cachedCustomRightHandJoints, cachedRightHandTransforms))
  463. {
  464. CAPI.ovrAvatar_SetRightHandCustomGesture(sdkAvatar, (uint)cachedRightHandTransforms.Length, cachedRightHandTransforms);
  465. }
  466. }
  467. }
  468.  
  469. static bool UpdatePoseRoot(Transform poseRoot, ref Transform cachedPoseRoot, ref Transform[] cachedPoseJoints, ref ovrAvatarTransform[] transforms)
  470. {
  471. if (poseRoot == cachedPoseRoot)
  472. {
  473. return false;
  474. }
  475.  
  476. if (!poseRoot)
  477. {
  478. cachedPoseRoot = null;
  479. cachedPoseJoints = null;
  480. transforms = null;
  481. }
  482. else
  483. {
  484. List<Transform> joints = new List<Transform>();
  485. OrderJoints(poseRoot, joints);
  486. cachedPoseRoot = poseRoot;
  487. cachedPoseJoints = joints.ToArray();
  488. transforms = new ovrAvatarTransform[joints.Count];
  489. }
  490. return true;
  491. }
  492.  
  493. static bool UpdateTransforms(Transform[] joints, ovrAvatarTransform[] transforms)
  494. {
  495. bool updated = false;
  496. for (int i = 0; i < joints.Length; ++i)
  497. {
  498. Transform joint = joints[i];
  499. ovrAvatarTransform transform = CreateOvrAvatarTransform(joint.localPosition, joint.localRotation);
  500. if (transform.position != transforms[i].position || transform.orientation != transforms[i].orientation)
  501. {
  502. transforms[i] = transform;
  503. updated = true;
  504. }
  505. }
  506. return updated;
  507. }
  508.  
  509.  
  510. private static void OrderJoints(Transform transform, List<Transform> joints)
  511. {
  512. joints.Add(transform);
  513. for (int i = 0; i < transform.childCount; ++i)
  514. {
  515. Transform child = transform.GetChild(i);
  516. OrderJoints(child, joints);
  517. }
  518. }
  519.  
  520. void AvatarSpecificationCallback(IntPtr avatarSpecification)
  521. {
  522. #if UNITY_ANDROID
  523. Capabilities &= ~ovrAvatarCapabilities.BodyTilt;
  524. #endif
  525. sdkAvatar = CAPI.ovrAvatar_Create(avatarSpecification, Capabilities);
  526. ShowLeftController(showLeftController);
  527. ShowRightController(showRightController);
  528.  
  529. //Fetch all the assets that this avatar uses.
  530. UInt32 assetCount = CAPI.ovrAvatar_GetReferencedAssetCount(sdkAvatar);
  531. for (UInt32 i = 0; i < assetCount; ++i)
  532. {
  533. UInt64 id = CAPI.ovrAvatar_GetReferencedAsset(sdkAvatar, i);
  534. if (OvrAvatarSDKManager.Instance.GetAsset(id) == null)
  535. {
  536. OvrAvatarSDKManager.Instance.BeginLoadingAsset(
  537. id,
  538. LevelOfDetail,
  539. AssetLoadedCallback);
  540.  
  541. assetLoadingIds.Add(id);
  542. }
  543. }
  544.  
  545. if (CombineMeshes)
  546. {
  547. OvrAvatarSDKManager.Instance.RegisterCombinedMeshCallback(
  548. sdkAvatar,
  549. CombinedMeshLoadedCallback);
  550. }
  551. }
  552. public bool forceMobileTexture = false;
  553. void Start()
  554. {
  555. #if !UNITY_ANDROID
  556. if (CombineMeshes)
  557. {
  558. CombineMeshes = false;
  559. AvatarLogger.Log("Combine Meshes Currently Only Supported On Android");
  560. }
  561. #endif
  562.  
  563. #if !UNITY_5_5_OR_NEWER
  564. if (CombineMeshes)
  565. {
  566. CombineMeshes = false;
  567. AvatarLogger.LogWarning("Unity Version too old to use Combined Mesh Shader, required 5.5.0+");
  568. }
  569. #endif
  570. ForceMobileTextureFormat = forceMobileTexture;
  571. CombineMeshes = CombineMesh;
  572. try
  573. {
  574. oculusUserIDInternal = UInt64.Parse(oculusUserID);
  575. }
  576. catch (Exception)
  577. {
  578. oculusUserIDInternal = 0;
  579.  
  580. AvatarLogger.LogWarning("Invalid Oculus User ID Format");
  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: " + ForceMobileTextureFormat);
  587. AvatarLogger.Log(AvatarLogger.Tab + "Oculus User ID: " + oculusUserIDInternal);
  588.  
  589. ShowLeftController(StartWithControllers);
  590. ShowRightController(StartWithControllers);
  591. OvrAvatarSDKManager.Instance.RequestAvatarSpecification(
  592. oculusUserIDInternal,
  593. this.AvatarSpecificationCallback,
  594. CombineMeshes,
  595. LevelOfDetail,
  596. ForceMobileTextureFormat);
  597.  
  598. WaitingForCombinedMesh = CombineMeshes;
  599. Driver.Mode = UseSDKPackets ? OvrAvatarDriver.PacketMode.SDK : OvrAvatarDriver.PacketMode.Unity;
  600. }
  601.  
  602. void Update()
  603. {
  604. if (sdkAvatar == IntPtr.Zero)
  605. {
  606. return;
  607. }
  608.  
  609. if (Driver != null)
  610. {
  611. Driver.UpdateTransforms(sdkAvatar);
  612.  
  613. foreach (float[] voiceUpdate in voiceUpdates)
  614. {
  615. CAPI.ovrAvatarPose_UpdateVoiceVisualization(sdkAvatar, voiceUpdate);
  616. }
  617.  
  618. voiceUpdates.Clear();
  619.  
  620. CAPI.ovrAvatarPose_Finalize(sdkAvatar, Time.deltaTime);
  621. }
  622.  
  623. if (RecordPackets)
  624. {
  625. RecordFrame();
  626. }
  627.  
  628. if (assetLoadingIds.Count == 0)
  629. {
  630. UpdateSDKAvatarUnityState();
  631. UpdateCustomPoses();
  632.  
  633. if (!assetsFinishedLoading)
  634. {
  635. AssetsDoneLoading.Invoke();
  636. assetsFinishedLoading = true;
  637. }
  638. }
  639. }
  640.  
  641. public static ovrAvatarHandInputState CreateInputState(ovrAvatarTransform transform, OvrAvatarDriver.ControllerPose pose)
  642. {
  643. ovrAvatarHandInputState inputState = new ovrAvatarHandInputState();
  644. inputState.transform = transform;
  645. inputState.buttonMask = pose.buttons;
  646. inputState.touchMask = pose.touches;
  647. inputState.joystickX = pose.joystickPosition.x;
  648. inputState.joystickY = pose.joystickPosition.y;
  649. inputState.indexTrigger = pose.indexTrigger;
  650. inputState.handTrigger = pose.handTrigger;
  651. inputState.isActive = pose.isActive;
  652. return inputState;
  653. }
  654.  
  655. public void ShowControllers(bool show)
  656. {
  657. ShowLeftController(show);
  658. ShowRightController(show);
  659. }
  660.  
  661. public void ShowLeftController(bool show)
  662. {
  663. if (sdkAvatar != IntPtr.Zero)
  664. {
  665. CAPI.ovrAvatar_SetLeftControllerVisibility(sdkAvatar, show);
  666. }
  667. showLeftController = show;
  668. }
  669.  
  670. public void ShowRightController(bool show)
  671. {
  672. if (sdkAvatar != IntPtr.Zero)
  673. {
  674. CAPI.ovrAvatar_SetRightControllerVisibility(sdkAvatar, show);
  675. }
  676. showRightController = show;
  677. }
  678.  
  679. public void UpdateVoiceVisualization(float[] voiceSamples)
  680. {
  681. voiceUpdates.Add(voiceSamples);
  682. }
  683.  
  684. void RecordFrame()
  685. {
  686. if (UseSDKPackets)
  687. {
  688. RecordSDKFrame();
  689. }
  690. else
  691. {
  692. RecordUnityFrame();
  693. }
  694. }
  695.  
  696. // Meant to be used mutually exclusively with RecordSDKFrame to give user more options to optimize or tweak packet data
  697. private void RecordUnityFrame()
  698. {
  699. var deltaSeconds = Time.deltaTime;
  700. var frame = Driver.GetCurrentPose();
  701. // If this is our first packet, store the pose as the initial frame
  702. if (CurrentUnityPacket == null)
  703. {
  704. CurrentUnityPacket = new OvrAvatarPacket(frame);
  705. deltaSeconds = 0;
  706. }
  707.  
  708. float recordedSeconds = 0;
  709. while (recordedSeconds < deltaSeconds)
  710. {
  711. float remainingSeconds = deltaSeconds - recordedSeconds;
  712. float remainingPacketSeconds = PacketSettings.UpdateRate - CurrentUnityPacket.Duration;
  713.  
  714. // If we're not going to fill the packet, just add the frame
  715. if (remainingSeconds < remainingPacketSeconds)
  716. {
  717. CurrentUnityPacket.AddFrame(frame, remainingSeconds);
  718. recordedSeconds += remainingSeconds;
  719. }
  720.  
  721. // If we're going to fill the packet, interpolate the pose, send the packet,
  722. // and open a new one
  723. else
  724. {
  725. // Interpolate between the packet's last frame and our target pose
  726. // to compute a pose at the end of the packet time.
  727. OvrAvatarDriver.PoseFrame a = CurrentUnityPacket.FinalFrame;
  728. OvrAvatarDriver.PoseFrame b = frame;
  729. float t = remainingPacketSeconds / remainingSeconds;
  730. OvrAvatarDriver.PoseFrame intermediatePose = OvrAvatarDriver.PoseFrame.Interpolate(a, b, t);
  731. CurrentUnityPacket.AddFrame(intermediatePose, remainingPacketSeconds);
  732. recordedSeconds += remainingPacketSeconds;
  733.  
  734. // Broadcast the recorded packet
  735. if (PacketRecorded != null)
  736. {
  737. PacketRecorded(this, new PacketEventArgs(CurrentUnityPacket));
  738. }
  739.  
  740. // Open a new packet
  741. CurrentUnityPacket = new OvrAvatarPacket(intermediatePose);
  742. }
  743. }
  744. }
  745.  
  746. private void RecordSDKFrame()
  747. {
  748. if (sdkAvatar == IntPtr.Zero)
  749. {
  750. return;
  751. }
  752.  
  753. if (!PacketSettings.RecordingFrames)
  754. {
  755. CAPI.ovrAvatarPacket_BeginRecording(sdkAvatar);
  756. PacketSettings.AccumulatedTime = 0.0f;
  757. PacketSettings.RecordingFrames = true;
  758. }
  759.  
  760. PacketSettings.AccumulatedTime += Time.deltaTime;
  761.  
  762. if (PacketSettings.AccumulatedTime >= PacketSettings.UpdateRate)
  763. {
  764. PacketSettings.AccumulatedTime = 0.0f;
  765. var packet = CAPI.ovrAvatarPacket_EndRecording(sdkAvatar);
  766. CAPI.ovrAvatarPacket_BeginRecording(sdkAvatar);
  767.  
  768. if (PacketRecorded != null)
  769. {
  770. PacketRecorded(this, new PacketEventArgs(new OvrAvatarPacket { ovrNativePacket = packet }));
  771. }
  772.  
  773. CAPI.ovrAvatarPacket_Free(packet);
  774. }
  775. }
  776.  
  777. private void AddRenderParts(
  778. OvrAvatarComponent ovrComponent,
  779. ovrAvatarComponent component,
  780. Transform parent)
  781. {
  782. for (UInt32 renderPartIndex = 0; renderPartIndex < component.renderPartCount; renderPartIndex++)
  783. {
  784. GameObject renderPartObject = new GameObject();
  785. renderPartObject.name = GetRenderPartName(component, renderPartIndex);
  786. renderPartObject.transform.SetParent(parent);
  787. IntPtr renderPart = GetRenderPart(component, renderPartIndex);
  788. ovrAvatarRenderPartType type = CAPI.ovrAvatarRenderPart_GetType(renderPart);
  789. OvrAvatarRenderComponent ovrRenderPart;
  790. switch (type)
  791. {
  792. case ovrAvatarRenderPartType.SkinnedMeshRender:
  793. ovrRenderPart = AddSkinnedMeshRenderComponent(renderPartObject, CAPI.ovrAvatarRenderPart_GetSkinnedMeshRender(renderPart));
  794. break;
  795. case ovrAvatarRenderPartType.SkinnedMeshRenderPBS:
  796. ovrRenderPart = AddSkinnedMeshRenderPBSComponent(renderPartObject, CAPI.ovrAvatarRenderPart_GetSkinnedMeshRenderPBS(renderPart));
  797. break;
  798. case ovrAvatarRenderPartType.ProjectorRender:
  799. ovrRenderPart = AddProjectorRenderComponent(renderPartObject, CAPI.ovrAvatarRenderPart_GetProjectorRender(renderPart));
  800. break;
  801. case ovrAvatarRenderPartType.SkinnedMeshRenderPBS_V2:
  802. {
  803. OvrAvatarMaterialManager materialManager = null;
  804.  
  805. if (ovrComponent.name == "body")
  806. {
  807. materialManager = DefaultBodyMaterialManager;
  808. }
  809. else if (ovrComponent.name.Contains("hand"))
  810. {
  811. materialManager = DefaultHandMaterialManager;
  812. }
  813.  
  814. ovrRenderPart = AddSkinnedMeshRenderPBSV2Component(
  815. renderPart,
  816. renderPartObject,
  817. CAPI.ovrAvatarRenderPart_GetSkinnedMeshRenderPBSV2(renderPart),
  818. materialManager);
  819. }
  820. break;
  821. default:
  822. throw new NotImplementedException(string.Format("Unsupported render part type: {0}", type.ToString()));
  823. }
  824.  
  825. ovrComponent.RenderParts.Add(ovrRenderPart);
  826. }
  827. }
  828.  
  829. public void RefreshBodyParts()
  830. {
  831. OvrAvatarComponent component;
  832. if (trackedComponents.TryGetValue("body", out component) && Body != null)
  833. {
  834. foreach (var part in component.RenderParts)
  835. {
  836. Destroy(part.gameObject);
  837. }
  838.  
  839. component.RenderParts.Clear();
  840.  
  841. ovrAvatarBodyComponent? sdkBodyComponent = CAPI.ovrAvatarPose_GetBodyComponent(sdkAvatar);
  842. if (sdkBodyComponent != null)
  843. {
  844. ovrAvatarComponent sdKComponent = (ovrAvatarComponent)Marshal.PtrToStructure(sdkBodyComponent.Value.renderComponent, typeof(ovrAvatarComponent));
  845. AddRenderParts(component, sdKComponent, Body.gameObject.transform);
  846. }
  847. else
  848. {
  849. throw new Exception("Destroyed the body component, but didn't find a new one in the SDK");
  850. }
  851. }
  852. }
  853.  
  854. public ovrAvatarBodyComponent? GetBodyComponent()
  855. {
  856. return CAPI.ovrAvatarPose_GetBodyComponent(sdkAvatar);
  857. }
  858.  
  859.  
  860. public Transform GetHandTransform(HandType hand, HandJoint joint)
  861. {
  862. if (hand >= HandType.Max || joint >= HandJoint.Max)
  863. {
  864. return null;
  865. }
  866.  
  867. var HandObject = hand == HandType.Left ? HandLeft : HandRight;
  868.  
  869. if (HandObject != null)
  870. {
  871. var AvatarComponent = HandObject.GetComponent<OvrAvatarComponent>();
  872. if (AvatarComponent != null && AvatarComponent.RenderParts.Count > 0)
  873. {
  874. var SkinnedMesh = AvatarComponent.RenderParts[0];
  875. return SkinnedMesh.transform.Find(HandJoints[(int)hand, (int)joint]);
  876. }
  877. }
  878.  
  879. return null;
  880. }
  881.  
  882. public void GetPointingDirection(HandType hand, ref Vector3 forward, ref Vector3 up)
  883. {
  884. Transform handBase = GetHandTransform(hand, HandJoint.HandBase);
  885.  
  886. if (handBase != null)
  887. {
  888. forward = handBase.forward;
  889. up = handBase.up;
  890. }
  891. }
  892.  
  893. public Transform GetMouthTransform()
  894. {
  895. OvrAvatarComponent component;
  896. if (trackedComponents.TryGetValue("voice", out component))
  897. {
  898. if (component.RenderParts.Count > 0)
  899. {
  900. return component.RenderParts[0].transform;
  901. }
  902. }
  903.  
  904. return null;
  905. }
  906.  
  907. static Vector3 MOUTH_POSITION_OFFSET = new Vector3(0, -0.018f, 0.1051f);
  908. static string VOICE_PROPERTY = "_Voice";
  909. static string MOUTH_POSITION_PROPERTY = "_MouthPosition";
  910. static string MOUTH_DIRECTION_PROPERTY = "_MouthDirection";
  911. static string MOUTH_SCALE_PROPERTY = "_MouthEffectScale";
  912.  
  913. static float MOUTH_SCALE_GLOBAL = 0.007f;
  914. static float MOUTH_MAX_GLOBAL = 0.007f;
  915. static string NECK_JONT = "root_JNT/body_JNT/chest_JNT/neckBase_JNT/neck_JNT";
  916.  
  917. public float VoiceAmplitude = 0f;
  918. public bool EnableMouthVertexAnimation = false;
  919.  
  920. private void UpdateVoiceBehavior()
  921. {
  922. if (!EnableMouthVertexAnimation)
  923. {
  924. return;
  925. }
  926.  
  927. OvrAvatarComponent component;
  928. if (trackedComponents.TryGetValue("body", out component))
  929. {
  930. VoiceAmplitude = Mathf.Clamp(VoiceAmplitude, 0f, 1f);
  931.  
  932. if (component.RenderParts.Count > 0)
  933. {
  934. var material = component.RenderParts[0].mesh.sharedMaterial;
  935. var neckJoint = component.RenderParts[0].mesh.transform.Find(NECK_JONT);
  936. var scaleDiff = neckJoint.TransformPoint(Vector3.up) - neckJoint.position;
  937.  
  938. material.SetFloat(MOUTH_SCALE_PROPERTY, scaleDiff.magnitude);
  939.  
  940. material.SetFloat(
  941. VOICE_PROPERTY,
  942. Mathf.Min(scaleDiff.magnitude * MOUTH_MAX_GLOBAL, scaleDiff.magnitude * VoiceAmplitude * MOUTH_SCALE_GLOBAL));
  943.  
  944. material.SetVector(
  945. MOUTH_POSITION_PROPERTY,
  946. neckJoint.TransformPoint(MOUTH_POSITION_OFFSET));
  947.  
  948. material.SetVector(MOUTH_DIRECTION_PROPERTY, neckJoint.up);
  949. }
  950. }
  951. }
  952. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement