当前位置:   article > 正文

【Unity】雷达+Unity +TUIO 介绍二_雷达发送tuio数据

雷达发送tuio数据

在上一遍 我们大概了解TUIO协议 和雷达 都是什么鬼,这篇主要是介绍一下 unity 与 tuio 的通信 

首先我们是在Unity 中引入了两个库 OSCsharp  TUIOsharp进行接收数据

具体库中信息可查看 https://github.com/valyard/TUIOsharp

  1. /*
  2. Simple TUIO v1.1 / OSC v1.1 network listener.
  3. Listen for TUIO or OSC network traffic and output it to the console. Useful for quickly checking/debugging data sent from TUIO server apps.
  4. Defaults to listening for TUIO on port 3333. Output radians/degrees using rads/degs options. Invert X/Y axis values in TUIO data using the invertx/y/xy options.
  5. Usage:
  6. > mono TUIOListener [port] [tuio|osc] [rads|degs] [invertx|inverty|invertxy]
  7. > mono TUIOListener -help
  8. Libraries:
  9. https://github.com/valyard/TUIOsharp (v1.1 development branch)
  10. https://github.com/valyard/OSCsharp
  11. Author:
  12. Greg Harding greg@flightless.co.nz
  13. www.flightless.co.nz
  14. Copyright 2015 Flightless Ltd.
  15. The MIT License (MIT)
  16. Copyright (c) 2015 Flightless Ltd
  17. Permission is hereby granted, free of charge, to any person obtaining a copy
  18. of this software and associated documentation files (the "Software"), to deal
  19. in the Software without restriction, including without limitation the rights
  20. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. copies of the Software, and to permit persons to whom the Software is
  22. furnished to do so, subject to the following conditions:
  23. The above copyright notice and this permission notice shall be included in all
  24. copies or substantial portions of the Software.
  25. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. SOFTWARE.
  32. */
  33. using System;
  34. using System.Threading;
  35. using System.Collections.Generic;
  36. using System.Text;
  37. using TUIOsharp;
  38. using TUIOsharp.DataProcessors;
  39. using TUIOsharp.Entities;
  40. using OSCsharp;
  41. using OSCsharp.Data;
  42. using OSCsharp.Net;
  43. using OSCsharp.Utils;
  44. using UnityEngine;
  45. namespace TUIOListener
  46. {
  47. class Program : MonoBehaviour
  48. {
  49. //public enum MessageType
  50. //{
  51. // TUIO,
  52. // OSC
  53. //};
  54. public static int port = 3333;
  55. //public static MessageType messageType = MessageType.TUIO;
  56. public static bool degs = false;
  57. public static bool invertX = false;
  58. public static bool invertY = false;
  59. private static TuioServer tuioServer;
  60. private int screenWidth;
  61. private int screenHeight;
  62. private void connect()
  63. {
  64. if (!Application.isPlaying) return;
  65. if (tuioServer != null) disconnect();
  66. tuioServer = new TuioServer(port);
  67. Debug.Log("TUIO Port" + port);
  68. tuioServer.Connect();
  69. Debug.Log("TUIO Connect");
  70. }
  71. private void OnEnable()
  72. {
  73. Debug.Log(string.Format("TUIO listening on port {0}... (Press escape to quit)", port));
  74. screenWidth = Screen.width;
  75. screenHeight = Screen.height;
  76. // tuio
  77. CursorProcessor cursorProcessor = new CursorProcessor();
  78. cursorProcessor.CursorAdded += OnCursorAdded;
  79. cursorProcessor.CursorUpdated += OnCursorUpdated;
  80. cursorProcessor.CursorRemoved += OnCursorRemoved;
  81. BlobProcessor blobProcessor = new BlobProcessor();
  82. blobProcessor.BlobAdded += OnBlobAdded;
  83. blobProcessor.BlobUpdated += OnBlobUpdated;
  84. blobProcessor.BlobRemoved += OnBlobRemoved;
  85. ObjectProcessor objectProcessor = new ObjectProcessor();
  86. objectProcessor.ObjectAdded += OnObjectAdded;
  87. objectProcessor.ObjectUpdated += OnObjectUpdated;
  88. objectProcessor.ObjectRemoved += OnObjectRemoved;
  89. // listen...
  90. connect();
  91. tuioServer.AddDataProcessor(cursorProcessor);
  92. tuioServer.AddDataProcessor(blobProcessor);
  93. tuioServer.AddDataProcessor(objectProcessor);
  94. Debug.Log("connect");
  95. }
  96. protected void OnDisable()
  97. {
  98. disconnect();
  99. }
  100. private void OnCursorAdded(object sender, TuioCursorEventArgs e)
  101. {
  102. var entity = e.Cursor;
  103. lock (tuioServer)
  104. {
  105. //var x = invertX ? (1 - entity.X) : entity.X;
  106. //var y = invertY ? (1 - entity.Y) : entity.Y;
  107. var x = entity.X * screenWidth;
  108. var y = (1 - entity.Y) * screenHeight;
  109. Debug.Log(string.Format("{0} Cursor Added {1}:{2},{3}", ((CursorProcessor)sender).FrameNumber, entity.Id, x, y));
  110. }
  111. Debug.Log("OnCursorAdded");
  112. }
  113. private void OnCursorUpdated(object sender, TuioCursorEventArgs e)
  114. {
  115. var entity = e.Cursor;
  116. lock (tuioServer)
  117. {
  118. //var x = invertX ? (1 - entity.X) : entity.X;
  119. //var y = invertY ? (1 - entity.Y) : entity.Y;
  120. var x = Mathf.Round(entity.X * screenWidth);
  121. var y = (1 - entity.Y) * screenHeight;
  122. Debug.Log(string.Format("{0} Cursor Moved {1}:{2},{3}", ((CursorProcessor)sender).FrameNumber, entity.Id, x, y));
  123. //MyTest.Instance.LimitGetPos(remapCoordinates(new Vector2(x, y)));
  124. //项目中测试方法
  125. //MyTest.Instance.LimitGetPos(new Vector2(x, y));
  126. }
  127. Debug.Log("OnCursorUpdated");
  128. }
  129. private void OnCursorRemoved(object sender, TuioCursorEventArgs e)
  130. {
  131. var entity = e.Cursor;
  132. lock (tuioServer)
  133. {
  134. Debug.Log(string.Format("{0} Cursor Removed {1}", ((CursorProcessor)sender).FrameNumber, entity.Id));
  135. }
  136. }
  137. private static void OnBlobAdded(object sender, TuioBlobEventArgs e)
  138. {
  139. var entity = e.Blob;
  140. lock (tuioServer)
  141. {
  142. var x = invertX ? (1 - entity.X) : entity.X;
  143. var y = invertY ? (1 - entity.Y) : entity.Y;
  144. var angle = degs ? (entity.Angle * (180f / Math.PI)) : entity.Angle;
  145. Debug.Log(string.Format("{0} Blob Added {1}:{2},{3} {4:F3}", ((BlobProcessor)sender).FrameNumber, entity.Id, x, y, angle));
  146. }
  147. Debug.Log("OnBlobAdded");
  148. }
  149. private static void OnBlobUpdated(object sender, TuioBlobEventArgs e)
  150. {
  151. var entity = e.Blob;
  152. lock (tuioServer)
  153. {
  154. var x = invertX ? (1 - entity.X) : entity.X;
  155. var y = invertY ? (1 - entity.Y) : entity.Y;
  156. var angle = degs ? (entity.Angle * (180f / Math.PI)) : entity.Angle;
  157. Debug.Log(string.Format("{0} Blob Moved {1}:{2},{3} {4:F3}", ((BlobProcessor)sender).FrameNumber, entity.Id, x, y, angle));
  158. }
  159. Debug.Log("OnBlobUpdated");
  160. }
  161. private static void OnBlobRemoved(object sender, TuioBlobEventArgs e)
  162. {
  163. var entity = e.Blob;
  164. lock (tuioServer)
  165. {
  166. Debug.Log(string.Format("{0} Blob Removed {1}", ((BlobProcessor)sender).FrameNumber, entity.Id));
  167. }
  168. Debug.Log("OnBlobRemoved");
  169. }
  170. private static void OnObjectAdded(object sender, TuioObjectEventArgs e)
  171. {
  172. var entity = e.Object;
  173. lock (tuioServer)
  174. {
  175. var x = invertX ? (1 - entity.X) : entity.X;
  176. var y = invertY ? (1 - entity.Y) : entity.Y;
  177. var angle = degs ? (entity.Angle * (180f / Math.PI)) : entity.Angle;
  178. Debug.Log(string.Format("{0} Object Added {1}/{2}:{3},{4} {5:F3}", ((ObjectProcessor)sender).FrameNumber, entity.ClassId, entity.Id, x, y, angle));
  179. }
  180. Debug.Log("OnObjectAdded");
  181. }
  182. private static void OnObjectUpdated(object sender, TuioObjectEventArgs e)
  183. {
  184. var entity = e.Object;
  185. lock (tuioServer)
  186. {
  187. var x = invertX ? (1 - entity.X) : entity.X;
  188. var y = invertY ? (1 - entity.Y) : entity.Y;
  189. var angle = degs ? (entity.Angle * (180f / Math.PI)) : entity.Angle;
  190. Debug.Log(string.Format("{0} Object Moved {1}/{2}:{3},{4} {5:F3}", ((ObjectProcessor)sender).FrameNumber, entity.ClassId, entity.Id, x, y, angle));
  191. }
  192. }
  193. private static void OnObjectRemoved(object sender, TuioObjectEventArgs e)
  194. {
  195. var entity = e.Object;
  196. lock (tuioServer)
  197. {
  198. Debug.Log(string.Format("{0} Object Removed {1}/{2}", ((ObjectProcessor)sender).FrameNumber, entity.ClassId, entity.Id));
  199. }
  200. }
  201. void disconnect()
  202. {
  203. if (tuioServer != null)
  204. {
  205. tuioServer.RemoveAllDataProcessors();
  206. tuioServer.Disconnect();
  207. tuioServer = null;
  208. }
  209. }
  210. }
  211. }

这样你就可以与雷达正常互动了,当然了 你光有雷达是不行的,你需要一个终端软件,这个你谷歌一下就可以搜出来,一般这个软件都是有厂家开发的 有一款是用Unity 做了一个客户端,里面融入了一个dll,如果想使用的话,基本上是一个加密狗 + 软件进行使用,而雷达厂商提供c的demo代码是纯c的,但是可以被认c语法的编程环境认,就从雷达厂商给的代码是c的都能看出来他们主要给单片机用和stm32用。

目前项目还在实践ing~~~~~~

下一篇将介绍tuio + 雷达 + Unity 的探坑之旅

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/114187
推荐阅读
相关标签
  

闽ICP备14008679号