Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  1. using System;
  2. using UnityEngine;
  3. using Oculus.Avatar;
  4. public class GazeTarget : MonoBehaviour
  5. {
  6. public ovrAvatarGazeTargetType Type;
  7. private static ovrAvatarGazeTargets RuntimeTargetList;
  8. static GazeTarget()
  9. {
  10. // This size has to match the 'MarshalAs' attribute in the ovrAvatarGazeTargets declaration.
  11. RuntimeTargetList.targets = new ovrAvatarGazeTarget[128];
  12. RuntimeTargetList.targetCount = 1;
  13. }
  14. void Start()
  15. {
  16. UpdateGazeTarget();
  17. transform.hasChanged = false;
  18. }
  19. void Update()
  20. {
  21. if (transform.hasChanged)
  22. {
  23. transform.hasChanged = false;
  24. UpdateGazeTarget();
  25. }
  26. }
  27. void OnDestroy()
  28. {
  29. UInt32[] targetIds = new UInt32[1];
  30. targetIds[0] = (UInt32) transform.GetInstanceID();
  31. CAPI.ovrAvatar_RemoveGazeTargets(1, targetIds);
  32. }
  33. private void UpdateGazeTarget()
  34. {
  35. ovrAvatarGazeTarget target = CreateOvrGazeTarget((UInt32) transform.GetInstanceID(), transform.position, Type);
  36. RuntimeTargetList.targets[0] = target;
  37. CAPI.ovrAvatar_UpdateGazeTargets(RuntimeTargetList);
  38. }
  39. private ovrAvatarGazeTarget CreateOvrGazeTarget(UInt32 targetId, Vector3 targetPosition, ovrAvatarGazeTargetType targetType)
  40. {
  41. return new ovrAvatarGazeTarget
  42. {
  43. id = targetId,
  44. // Do coordinate system switch.
  45. worldPosition = new Vector3(targetPosition.x, targetPosition.y, -targetPosition.z),
  46. type = targetType
  47. };
  48. }
  49. }