CaptureBehaviourBase.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Record
  10. {
  11. using System;
  12. using UnityEngine;
  13. using NRKernal;
  14. /// <summary> A capture behaviour base. </summary>
  15. public class CaptureBehaviourBase : MonoBehaviour, IFrameConsumer
  16. {
  17. /// <summary> The RGB camera rig. </summary>
  18. [SerializeField] Transform RGBCameraRig;
  19. /// <summary> The capture camera. </summary>
  20. public Camera CaptureCamera;
  21. private FrameCaptureContext m_FrameCaptureContext;
  22. /// <summary> Gets the context. </summary>
  23. /// <returns> The context. </returns>
  24. public FrameCaptureContext GetContext()
  25. {
  26. return m_FrameCaptureContext;
  27. }
  28. /// <summary> Initializes this object. </summary>
  29. /// <param name="context"> The context.</param>
  30. /// <param name="blendCamera"> The blend camera.</param>
  31. public virtual void Init(FrameCaptureContext context)
  32. {
  33. this.m_FrameCaptureContext = context;
  34. }
  35. public void SetCameraMask(int mask)
  36. {
  37. CaptureCamera.cullingMask = mask;
  38. }
  39. public void SetBackGroundColor(Color color)
  40. {
  41. this.CaptureCamera.backgroundColor = color; //new Color(color.r, color.g, color.b, 0);
  42. }
  43. /// <summary> Executes the 'frame' action. </summary>
  44. /// <param name="frame"> The frame.</param>
  45. public virtual void OnFrame(UniversalTextureFrame frame)
  46. {
  47. var mode = this.GetContext().GetBlender().BlendMode;
  48. switch (mode)
  49. {
  50. case BlendMode.RGBOnly:
  51. MoveToGod();
  52. break;
  53. case BlendMode.Blend:
  54. case BlendMode.VirtualOnly:
  55. case BlendMode.WidescreenBlend:
  56. // update camera pose
  57. UpdateHeadPoseByTimestamp(frame.timeStamp);
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. private void MoveToGod()
  64. {
  65. RGBCameraRig.transform.position = Vector3.one * 9999;
  66. }
  67. /// <summary> Updates the head pose by timestamp described by timestamp. </summary>
  68. /// <param name="timestamp"> The timestamp.</param>
  69. private void UpdateHeadPoseByTimestamp(UInt64 timestamp)
  70. {
  71. Pose head_pose = Pose.identity;
  72. var result = NRSessionManager.Instance.NRHMDPoseTracker.GetHeadPoseByTimeInUnityWorld(ref head_pose, timestamp);
  73. if (result)
  74. {
  75. // NRDebugger.Info("UpdateHeadPoseByTimestamp: timestamp={0}, pos={1}", timestamp, head_pose.ToString("F2"));
  76. RGBCameraRig.transform.position = head_pose.position;
  77. RGBCameraRig.transform.rotation = head_pose.rotation;
  78. }
  79. }
  80. }
  81. }