DepthSourceView.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using UnityEngine;
  2. using System.Collections;
  3. using Windows.Kinect;
  4. public enum DepthViewMode
  5. {
  6. SeparateSourceReaders,
  7. MultiSourceReader,
  8. }
  9. public class DepthSourceView : MonoBehaviour
  10. {
  11. public DepthViewMode ViewMode = DepthViewMode.SeparateSourceReaders;
  12. public GameObject ColorSourceManager;
  13. public GameObject DepthSourceManager;
  14. public GameObject MultiSourceManager;
  15. private KinectSensor _Sensor;
  16. private CoordinateMapper _Mapper;
  17. private Mesh _Mesh;
  18. private Vector3[] _Vertices;
  19. private Vector2[] _UV;
  20. private int[] _Triangles;
  21. // Only works at 4 right now
  22. private const int _DownsampleSize = 4;
  23. private const double _DepthScale = 0.1f;
  24. private const int _Speed = 50;
  25. private MultiSourceManager _MultiManager;
  26. private ColorSourceManager _ColorManager;
  27. private DepthSourceManager _DepthManager;
  28. void Start()
  29. {
  30. _Sensor = KinectSensor.GetDefault();
  31. if (_Sensor != null)
  32. {
  33. _Mapper = _Sensor.CoordinateMapper;
  34. var frameDesc = _Sensor.DepthFrameSource.FrameDescription;
  35. // Downsample to lower resolution
  36. CreateMesh(frameDesc.Width / _DownsampleSize, frameDesc.Height / _DownsampleSize);
  37. if (!_Sensor.IsOpen)
  38. {
  39. _Sensor.Open();
  40. }
  41. }
  42. }
  43. void CreateMesh(int width, int height)
  44. {
  45. _Mesh = new Mesh();
  46. GetComponent<MeshFilter>().mesh = _Mesh;
  47. _Vertices = new Vector3[width * height];
  48. _UV = new Vector2[width * height];
  49. _Triangles = new int[6 * ((width - 1) * (height - 1))];
  50. int triangleIndex = 0;
  51. for (int y = 0; y < height; y++)
  52. {
  53. for (int x = 0; x < width; x++)
  54. {
  55. int index = (y * width) + x;
  56. _Vertices[index] = new Vector3(x, -y, 0);
  57. _UV[index] = new Vector2(((float)x / (float)width), ((float)y / (float)height));
  58. // Skip the last row/col
  59. if (x != (width - 1) && y != (height - 1))
  60. {
  61. int topLeft = index;
  62. int topRight = topLeft + 1;
  63. int bottomLeft = topLeft + width;
  64. int bottomRight = bottomLeft + 1;
  65. _Triangles[triangleIndex++] = topLeft;
  66. _Triangles[triangleIndex++] = topRight;
  67. _Triangles[triangleIndex++] = bottomLeft;
  68. _Triangles[triangleIndex++] = bottomLeft;
  69. _Triangles[triangleIndex++] = topRight;
  70. _Triangles[triangleIndex++] = bottomRight;
  71. }
  72. }
  73. }
  74. _Mesh.vertices = _Vertices;
  75. _Mesh.uv = _UV;
  76. _Mesh.triangles = _Triangles;
  77. _Mesh.RecalculateNormals();
  78. }
  79. void OnGUI()
  80. {
  81. GUI.BeginGroup(new Rect(0, 0, Screen.width, Screen.height));
  82. GUI.TextField(new Rect(Screen.width - 250 , 10, 250, 20), "DepthMode: " + ViewMode.ToString());
  83. GUI.EndGroup();
  84. }
  85. void Update()
  86. {
  87. if (_Sensor == null)
  88. {
  89. return;
  90. }
  91. if (Input.GetButtonDown("Fire1"))
  92. {
  93. if(ViewMode == DepthViewMode.MultiSourceReader)
  94. {
  95. ViewMode = DepthViewMode.SeparateSourceReaders;
  96. }
  97. else
  98. {
  99. ViewMode = DepthViewMode.MultiSourceReader;
  100. }
  101. }
  102. float yVal = Input.GetAxis("Horizontal");
  103. float xVal = -Input.GetAxis("Vertical");
  104. transform.Rotate(
  105. (xVal * Time.deltaTime * _Speed),
  106. (yVal * Time.deltaTime * _Speed),
  107. 0,
  108. Space.Self);
  109. if (ViewMode == DepthViewMode.SeparateSourceReaders)
  110. {
  111. if (ColorSourceManager == null)
  112. {
  113. return;
  114. }
  115. _ColorManager = ColorSourceManager.GetComponent<ColorSourceManager>();
  116. if (_ColorManager == null)
  117. {
  118. return;
  119. }
  120. if (DepthSourceManager == null)
  121. {
  122. return;
  123. }
  124. _DepthManager = DepthSourceManager.GetComponent<DepthSourceManager>();
  125. if (_DepthManager == null)
  126. {
  127. return;
  128. }
  129. gameObject.GetComponent<Renderer>().material.mainTexture = _ColorManager.GetColorTexture();
  130. RefreshData(_DepthManager.GetData(),
  131. _ColorManager.ColorWidth,
  132. _ColorManager.ColorHeight);
  133. }
  134. else
  135. {
  136. if (MultiSourceManager == null)
  137. {
  138. return;
  139. }
  140. _MultiManager = MultiSourceManager.GetComponent<MultiSourceManager>();
  141. if (_MultiManager == null)
  142. {
  143. return;
  144. }
  145. gameObject.GetComponent<Renderer>().material.mainTexture = _MultiManager.GetColorTexture();
  146. RefreshData(_MultiManager.GetDepthData(),
  147. _MultiManager.ColorWidth,
  148. _MultiManager.ColorHeight);
  149. }
  150. }
  151. private void RefreshData(ushort[] depthData, int colorWidth, int colorHeight)
  152. {
  153. var frameDesc = _Sensor.DepthFrameSource.FrameDescription;
  154. ColorSpacePoint[] colorSpace = new ColorSpacePoint[depthData.Length];
  155. _Mapper.MapDepthFrameToColorSpace(depthData, colorSpace);
  156. for (int y = 0; y < frameDesc.Height; y += _DownsampleSize)
  157. {
  158. for (int x = 0; x < frameDesc.Width; x += _DownsampleSize)
  159. {
  160. int indexX = x / _DownsampleSize;
  161. int indexY = y / _DownsampleSize;
  162. int smallIndex = (indexY * (frameDesc.Width / _DownsampleSize)) + indexX;
  163. double avg = GetAvg(depthData, x, y, frameDesc.Width, frameDesc.Height);
  164. avg = avg * _DepthScale;
  165. _Vertices[smallIndex].z = (float)avg;
  166. // Update UV mapping with CDRP
  167. var colorSpacePoint = colorSpace[(y * frameDesc.Width) + x];
  168. _UV[smallIndex] = new Vector2(colorSpacePoint.X / colorWidth, colorSpacePoint.Y / colorHeight);
  169. }
  170. }
  171. _Mesh.vertices = _Vertices;
  172. _Mesh.uv = _UV;
  173. _Mesh.triangles = _Triangles;
  174. _Mesh.RecalculateNormals();
  175. }
  176. private double GetAvg(ushort[] depthData, int x, int y, int width, int height)
  177. {
  178. double sum = 0.0;
  179. for (int y1 = y; y1 < y + 4; y1++)
  180. {
  181. for (int x1 = x; x1 < x + 4; x1++)
  182. {
  183. int fullIndex = (y1 * width) + x1;
  184. if (depthData[fullIndex] == 0)
  185. sum += 4500;
  186. else
  187. sum += depthData[fullIndex];
  188. }
  189. }
  190. return sum / 16;
  191. }
  192. void OnApplicationQuit()
  193. {
  194. if (_Mapper != null)
  195. {
  196. _Mapper = null;
  197. }
  198. if (_Sensor != null)
  199. {
  200. if (_Sensor.IsOpen)
  201. {
  202. _Sensor.Close();
  203. }
  204. _Sensor = null;
  205. }
  206. }
  207. }