CoordinateMapperManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using UnityEngine;
  2. using System.Collections;
  3. using Windows.Kinect;
  4. using System.Runtime.InteropServices;
  5. using System;
  6. public class CoordinateMapperManager : MonoBehaviour
  7. {
  8. private KinectSensor m_pKinectSensor;
  9. private CoordinateMapper m_pCoordinateMapper;
  10. private MultiSourceFrameReader m_pMultiSourceFrameReader;
  11. private DepthSpacePoint[] m_pDepthCoordinates;
  12. private byte[] pColorBuffer;
  13. private byte[] pBodyIndexBuffer;
  14. private ushort[] pDepthBuffer;
  15. const int cDepthWidth = 512;
  16. const int cDepthHeight = 424;
  17. const int cColorWidth = 1920;
  18. const int cColorHeight = 1080;
  19. long frameCount = 0;
  20. double elapsedCounter = 0.0;
  21. double fps = 0.0;
  22. Texture2D m_pColorRGBX;
  23. bool nullFrame = false;
  24. void Awake()
  25. {
  26. pColorBuffer = new byte[cColorWidth * cColorHeight * 4];
  27. pBodyIndexBuffer = new byte[cDepthWidth * cDepthHeight];
  28. pDepthBuffer = new ushort[cDepthWidth * cDepthHeight];
  29. m_pColorRGBX = new Texture2D (cColorWidth, cColorHeight, TextureFormat.RGBA32, false);
  30. m_pDepthCoordinates = new DepthSpacePoint[cColorWidth * cColorHeight];
  31. InitializeDefaultSensor ();
  32. }
  33. Rect fpsRect = new Rect(10, 10, 200, 30);
  34. Rect nullFrameRect = new Rect(10, 50, 200, 30);
  35. void OnGUI ()
  36. {
  37. GUI.Box (fpsRect, "FPS: " + fps.ToString("0.00"));
  38. if (nullFrame)
  39. {
  40. GUI.Box (nullFrameRect, "NULL MSFR Frame");
  41. }
  42. }
  43. public Texture2D GetColorTexture()
  44. {
  45. return m_pColorRGBX;
  46. }
  47. public byte[] GetBodyIndexBuffer()
  48. {
  49. return pBodyIndexBuffer;
  50. }
  51. public DepthSpacePoint[] GetDepthCoordinates()
  52. {
  53. return m_pDepthCoordinates;
  54. }
  55. void InitializeDefaultSensor()
  56. {
  57. m_pKinectSensor = KinectSensor.GetDefault();
  58. if (m_pKinectSensor != null)
  59. {
  60. // Initialize the Kinect and get coordinate mapper and the frame reader
  61. m_pCoordinateMapper = m_pKinectSensor.CoordinateMapper;
  62. m_pKinectSensor.Open();
  63. if (m_pKinectSensor.IsOpen)
  64. {
  65. m_pMultiSourceFrameReader = m_pKinectSensor.OpenMultiSourceFrameReader(
  66. FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.BodyIndex);
  67. }
  68. }
  69. if (m_pKinectSensor == null)
  70. {
  71. UnityEngine.Debug.LogError("No ready Kinect found!");
  72. }
  73. }
  74. void ProcessFrame()
  75. {
  76. var pDepthData = GCHandle.Alloc(pDepthBuffer, GCHandleType.Pinned);
  77. var pDepthCoordinatesData = GCHandle.Alloc(m_pDepthCoordinates, GCHandleType.Pinned);
  78. m_pCoordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
  79. pDepthData.AddrOfPinnedObject(),
  80. (uint)pDepthBuffer.Length * sizeof(ushort),
  81. pDepthCoordinatesData.AddrOfPinnedObject(),
  82. (uint)m_pDepthCoordinates.Length);
  83. pDepthCoordinatesData.Free();
  84. pDepthData.Free();
  85. m_pColorRGBX.LoadRawTextureData(pColorBuffer);
  86. m_pColorRGBX.Apply ();
  87. }
  88. void Update()
  89. {
  90. // Get FPS
  91. elapsedCounter+=Time.deltaTime;
  92. if(elapsedCounter > 1.0)
  93. {
  94. fps = frameCount / elapsedCounter;
  95. frameCount = 0;
  96. elapsedCounter = 0.0;
  97. }
  98. if (m_pMultiSourceFrameReader == null)
  99. {
  100. return;
  101. }
  102. var pMultiSourceFrame = m_pMultiSourceFrameReader.AcquireLatestFrame();
  103. if (pMultiSourceFrame != null)
  104. {
  105. frameCount++;
  106. nullFrame = false;
  107. using(var pDepthFrame = pMultiSourceFrame.DepthFrameReference.AcquireFrame())
  108. {
  109. using(var pColorFrame = pMultiSourceFrame.ColorFrameReference.AcquireFrame())
  110. {
  111. using(var pBodyIndexFrame = pMultiSourceFrame.BodyIndexFrameReference.AcquireFrame())
  112. {
  113. // Get Depth Frame Data.
  114. if (pDepthFrame != null)
  115. {
  116. var pDepthData = GCHandle.Alloc (pDepthBuffer, GCHandleType.Pinned);
  117. pDepthFrame.CopyFrameDataToIntPtr(pDepthData.AddrOfPinnedObject(), (uint)pDepthBuffer.Length * sizeof(ushort));
  118. pDepthData.Free();
  119. }
  120. // Get Color Frame Data
  121. if (pColorFrame != null)
  122. {
  123. var pColorData = GCHandle.Alloc (pColorBuffer, GCHandleType.Pinned);
  124. pColorFrame.CopyConvertedFrameDataToIntPtr(pColorData.AddrOfPinnedObject(), (uint)pColorBuffer.Length, ColorImageFormat.Rgba);
  125. pColorData.Free();
  126. }
  127. // Get BodyIndex Frame Data.
  128. if (pBodyIndexFrame != null)
  129. {
  130. var pBodyIndexData = GCHandle.Alloc (pBodyIndexBuffer, GCHandleType.Pinned);
  131. pBodyIndexFrame.CopyFrameDataToIntPtr(pBodyIndexData.AddrOfPinnedObject(), (uint)pBodyIndexBuffer.Length);
  132. pBodyIndexData.Free();
  133. }
  134. }
  135. }
  136. }
  137. ProcessFrame();
  138. }
  139. else
  140. {
  141. nullFrame = true;
  142. }
  143. }
  144. void OnApplicationQuit()
  145. {
  146. pDepthBuffer = null;
  147. pColorBuffer = null;
  148. pBodyIndexBuffer = null;
  149. if (m_pDepthCoordinates != null)
  150. {
  151. m_pDepthCoordinates = null;
  152. }
  153. if (m_pMultiSourceFrameReader != null)
  154. {
  155. m_pMultiSourceFrameReader.Dispose();
  156. m_pMultiSourceFrameReader = null;
  157. }
  158. if (m_pKinectSensor != null)
  159. {
  160. m_pKinectSensor.Close();
  161. m_pKinectSensor = null;
  162. }
  163. }
  164. }