wavingaround.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /// <summary>
  2. /// ZWN-XHJ
  3. /// 20211018
  4. /// </summary>
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. public enum Wave_State
  11. {
  12. None,
  13. Wave_Left,//左挥手
  14. Wave_Right,//右挥手
  15. Wave_Up,//上挥手
  16. Wave_Down//上挥手
  17. }
  18. public class wavingaround : MonoBehaviour
  19. {
  20. public float space_time = 0.5f;//状态改变间隔时间
  21. public float space_nose = 1.3f;//肩的距离大于肩到鼻子高度的比例,小于该比例为侧身
  22. public float space_hand = 1.3f;//肘部位置小于手部的比例
  23. public float space_handX = 1f;//手部幅度大于肩宽一半的比例
  24. public bool Wave_Flag = false;//整体是否检测
  25. Dictionary<int, Vector3> zwn_Single_Vec = new Dictionary<int, Vector3>();//关于位置数据的存储
  26. Vector3 shoulder_Right, shoulder_Left;//右手//右肘//右肩//左手//左肘//左肩
  27. Vector3 hand_Right, hand_Elbow_Right, hand_Left, hand_Elbow_Left;//右手//右肘//右肩//左手//左肘//左肩
  28. public float width;//宽
  29. int hand_Count = 25, buff_sz = 3; //存储帧数// avg size
  30. public List<int> list_Shoulder_Y, list_Shoulder_X, list_ShoulderZ_X;//肩部XY记录
  31. //关于左侧属性
  32. #region
  33. bool flag_time_Left = false;
  34. float state_time_Left = 0;
  35. Wave_State cur_WaveState_Left = Wave_State.None;
  36. Wave_State last_WaveState_Left = Wave_State.None;
  37. public Wave_State Cur_WaveState_Left
  38. {
  39. get { return cur_WaveState_Left; }//返回属性的值
  40. set { cur_WaveState_Left = value; }//通过Value这个变量给属性赋值
  41. }
  42. Wave_State cur_WaveState_LeftUD = Wave_State.None;
  43. Wave_State last_WaveState_LeftUD = Wave_State.None;
  44. public Wave_State Cur_WaveState_LeftUD
  45. {
  46. get { return cur_WaveState_LeftUD; }//返回属性的值
  47. set { cur_WaveState_LeftUD = value; }//通过Value这个变量给属性赋值
  48. }
  49. public bool Wave_Flag_Left = false;//是否计算
  50. public List<float> list_Hand_X_Left, list_Hand_Y_Left;//左右手X Y列表
  51. public List<float> list_Elbow_X_Left, list_Elbow_Y_Left;//左右肩X Y列表
  52. int None_Num_Left, Wave_Num_Left;//挥手
  53. string last_Left;
  54. public Text show_Left, show_Left_Num;
  55. #endregion
  56. //关于右侧属性
  57. #region
  58. bool flag_time_Right = false;
  59. float state_time_Right = 0;
  60. Wave_State cur_WaveState_Right = Wave_State.None;
  61. Wave_State last_WaveState_Right = Wave_State.None;
  62. public Wave_State Cur_WaveState_Right
  63. {
  64. get { return cur_WaveState_Right; }//返回属性的值
  65. set { cur_WaveState_Right = value; }//通过Value这个变量给属性赋值
  66. }
  67. Wave_State cur_WaveState_RightUD = Wave_State.None;
  68. Wave_State last_WaveState_RightUD = Wave_State.None;
  69. public Wave_State Cur_WaveState_RightUD
  70. {
  71. get { return cur_WaveState_RightUD; }//返回属性的值
  72. set { cur_WaveState_RightUD = value; }//通过Value这个变量给属性赋值
  73. }
  74. public bool Wave_Flag_Right = false;//是否计算
  75. public List<float> list_Hand_X_Right, list_Hand_Y_Right;//左右手X Y列表
  76. public List<float> list_Elbow_X_Right, list_Elbow_Y_Right;//左右肩X Y列表
  77. int None_Num_Right, Wave_Num_Right;//挥手
  78. string last_Right;
  79. public Text show_Right, show_Right_Num;
  80. #endregion
  81. void Awake()
  82. {
  83. if (Application.platform == RuntimePlatform.WindowsEditor
  84. || Application.platform == RuntimePlatform.WindowsPlayer)
  85. {
  86. hand_Count = 25;
  87. buff_sz = 3;
  88. }
  89. if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
  90. {
  91. hand_Count = 6;
  92. buff_sz = 1;
  93. }
  94. space_handX = 0.5f;//手部幅度大于肩宽一半的比例
  95. }
  96. bool reInit = false;
  97. /// <summary>
  98. /// 初始化打拳
  99. /// </summary>
  100. public void Init_Wave()
  101. {
  102. cur_WaveState_Left = Wave_State.None;
  103. last_WaveState_Left = cur_WaveState_Left;
  104. cur_WaveState_LeftUD = Wave_State.None;
  105. last_WaveState_LeftUD = cur_WaveState_LeftUD;
  106. flag_time_Left = true;
  107. state_time_Left = 0;
  108. cur_WaveState_Right = Wave_State.None;
  109. last_WaveState_Right = cur_WaveState_Right;
  110. cur_WaveState_RightUD = Wave_State.None;
  111. last_WaveState_RightUD = cur_WaveState_RightUD;
  112. flag_time_Right = true;
  113. state_time_Right = 0;
  114. if (reInit)
  115. {
  116. if (list_Hand_X_Left.Count > 0)
  117. {
  118. list_Hand_X_Left.Clear();
  119. list_Elbow_X_Left.Clear();
  120. list_Hand_Y_Left.Clear();
  121. list_Elbow_Y_Left.Clear();
  122. }
  123. if (list_Hand_X_Right.Count > 0)
  124. {
  125. list_Hand_X_Right.Clear();
  126. list_Elbow_X_Right.Clear();
  127. list_Hand_Y_Right.Clear();
  128. list_Elbow_Y_Right.Clear();
  129. }
  130. }
  131. Wave_Flag = true;
  132. Wave_Flag_Left = true;
  133. Wave_Flag_Right = true;
  134. if (!reInit)
  135. {
  136. reInit = true;
  137. }
  138. }
  139. /// <summary>
  140. /// 结束打拳
  141. /// </summary>
  142. public void End_Wave()
  143. {
  144. if (reInit)
  145. {
  146. if (list_Hand_X_Left.Count > 0)
  147. {
  148. list_Hand_X_Left.Clear();
  149. list_Elbow_X_Left.Clear();
  150. list_Hand_Y_Left.Clear();
  151. list_Elbow_Y_Left.Clear();
  152. }
  153. if (list_Hand_X_Right.Count > 0)
  154. {
  155. list_Hand_X_Right.Clear();
  156. list_Elbow_X_Right.Clear();
  157. list_Hand_Y_Right.Clear();
  158. list_Elbow_Y_Right.Clear();
  159. }
  160. }
  161. cur_WaveState_Left = Wave_State.None;
  162. last_WaveState_Left = cur_WaveState_Left;
  163. cur_WaveState_LeftUD = Wave_State.None;
  164. last_WaveState_LeftUD = cur_WaveState_LeftUD;
  165. flag_time_Left = true;
  166. state_time_Left = 0;
  167. cur_WaveState_Right = Wave_State.None;
  168. last_WaveState_Right = cur_WaveState_Right;
  169. cur_WaveState_RightUD = Wave_State.None;
  170. last_WaveState_RightUD = cur_WaveState_RightUD;
  171. flag_time_Right = true;
  172. state_time_Right = 0;
  173. Wave_Flag = false;
  174. Wave_Flag_Left = false;
  175. Wave_Flag_Right = false;
  176. }
  177. float time_delay = 0;
  178. void Update()
  179. {
  180. time_delay += Time.deltaTime;
  181. //print("time_delay: " + time_delay);
  182. if (time_delay >= 0.5f)
  183. {
  184. Resources.UnloadUnusedAssets();//卸载未占用的asset资源
  185. System.GC.Collect();//回收内存
  186. time_delay = 0;
  187. }
  188. if (Wave_Flag)
  189. {
  190. ZWN_Single_Vec();
  191. List_Run();
  192. if (Wave_Flag_Left)
  193. {
  194. List_Wave_Left();
  195. }
  196. if (Wave_Flag_Right)
  197. {
  198. List_Wave_Right();
  199. }
  200. }
  201. }
  202. /// <summary>
  203. /// 单人点
  204. /// </summary>
  205. void ZWN_Single_Vec()
  206. {
  207. /*0:鼻子1:左眼2:右眼3:左耳4:右耳
  208. 5:左肩6:右肩7:左肘8:右肘9:左腕
  209. 10:右腕11:左臀12:右臀13:左膝
  210. 14:右膝15:左脚踝16:右脚踝 */
  211. for (int i = 0; i < 17; i++)
  212. {
  213. if (zwn_Single_Vec.ContainsKey(i))
  214. {
  215. zwn_Single_Vec[i] = common_data.single.zwn_single_vec[i];
  216. }
  217. else
  218. {
  219. zwn_Single_Vec.Add(i, common_data.single.zwn_single_vec[i]);
  220. }
  221. }
  222. hand_Right = zwn_Single_Vec[9];
  223. hand_Elbow_Right = zwn_Single_Vec[7];
  224. shoulder_Right = zwn_Single_Vec[5];
  225. hand_Left = zwn_Single_Vec[10];
  226. hand_Elbow_Left = zwn_Single_Vec[8];
  227. shoulder_Left = zwn_Single_Vec[6];
  228. //if (width != Helper.Width)
  229. // width = Helper.Width;
  230. }
  231. #region 肩部相关信息
  232. void List_Run()
  233. {
  234. if (
  235. shoulder_Right.x != -1 && shoulder_Right.y != -1 &&
  236. shoulder_Left.x != -1 && shoulder_Left.y != -1)
  237. {
  238. list_Shoulder_Y.Add((int)((shoulder_Right.y + shoulder_Left.y) / 2));
  239. list_Shoulder_X.Add((int)(Mathf.Abs(shoulder_Right.x - shoulder_Left.x) / 2));
  240. list_ShoulderZ_X.Add((int)(Mathf.Abs(shoulder_Right.x + shoulder_Left.x) / 2));
  241. }
  242. if (list_Shoulder_Y.Count > hand_Count)
  243. {
  244. list_Shoulder_Y.Remove(list_Shoulder_Y[0]);
  245. list_Shoulder_X.Remove(list_Shoulder_X[0]);
  246. list_ShoulderZ_X.Add(list_ShoulderZ_X[0]);
  247. List_Run_Y();
  248. List_Run_X();
  249. List_RunZ_X();
  250. ratioTmpY = (max_RunY - min_RunY) / width;//肩部Y占比例
  251. ratioTmpX = ave_List_RunX / width;//肩部X占比例
  252. ratioTmpZ_X = (max_RunY - min_RunY) / width;//肩部Y占比例
  253. }
  254. }
  255. float cur_DirZ_X = 0, cur_Dir_StartZ_X = 0, cur_Dir_EndZ_X = 0, max_RunZ_X = -0xffff, min_RunZ_X = 0xffff, ratioTmpZ_X;
  256. /// <summary>
  257. /// 求X位移差、位移比例、滑动方向
  258. /// </summary>
  259. void List_RunZ_X()
  260. {
  261. cur_DirZ_X = 0;
  262. cur_Dir_StartZ_X = 0;
  263. cur_Dir_EndZ_X = 0;
  264. max_RunZ_X = -0xffff;
  265. min_RunZ_X = 0xffff;
  266. for (int i = 0; i < list_ShoulderZ_X.Count; i++)
  267. {
  268. if (i == 0)
  269. {
  270. max_RunZ_X = list_ShoulderZ_X[i];
  271. min_RunZ_X = list_ShoulderZ_X[i];
  272. }
  273. if (i < buff_sz)
  274. {
  275. cur_Dir_StartZ_X += list_ShoulderZ_X[i];
  276. }
  277. if (i >= list_ShoulderZ_X.Count - buff_sz)
  278. {
  279. cur_Dir_EndZ_X += list_ShoulderZ_X[i];
  280. }
  281. if (list_ShoulderZ_X[i] > max_RunZ_X)
  282. max_RunZ_X = list_ShoulderZ_X[i];
  283. if (list_ShoulderZ_X[i] < min_RunZ_X)
  284. min_RunZ_X = list_ShoulderZ_X[i];
  285. }
  286. cur_DirZ_X = (cur_Dir_StartZ_X / buff_sz) - (cur_Dir_EndZ_X / buff_sz);
  287. }
  288. float cur_DirY = 0, cur_Dir_StartY = 0, cur_Dir_EndY = 0, max_RunY = -0xffff, min_RunY = 0xffff, ratioTmpY;
  289. /// <summary>
  290. /// 求X位移差、位移比例、滑动方向
  291. /// </summary>
  292. void List_Run_Y()
  293. {
  294. cur_DirY = 0;
  295. cur_Dir_StartY = 0;
  296. cur_Dir_EndY = 0;
  297. max_RunY = -0xffff;
  298. min_RunY = 0xffff;
  299. for (int i = 0; i < list_Shoulder_Y.Count; i++)
  300. {
  301. if (i == 0)
  302. {
  303. max_RunY = list_Shoulder_Y[i];
  304. min_RunY = list_Shoulder_Y[i];
  305. }
  306. if (i < buff_sz)
  307. {
  308. cur_Dir_StartY += list_Shoulder_Y[i];
  309. }
  310. if (i >= list_Shoulder_Y.Count - buff_sz)
  311. {
  312. cur_Dir_EndY += list_Shoulder_Y[i];
  313. }
  314. if (list_Shoulder_Y[i] > max_RunY)
  315. max_RunY = list_Shoulder_Y[i];
  316. if (list_Shoulder_Y[i] < min_RunY)
  317. min_RunY = list_Shoulder_Y[i];
  318. }
  319. cur_DirY = (cur_Dir_StartY / buff_sz) - (cur_Dir_EndY / buff_sz);
  320. }
  321. float cur_DirX = 0, cur_Dir_StartX = 0, cur_Dir_EndX = 0, max_Shoulderx = -0xffff, min_Shoulderx = 0xffff;
  322. float ave_List_RunX, ratioTmpX;
  323. /// <summary>
  324. /// 求X位移差、位移比例、滑动方向
  325. /// </summary>
  326. void List_Run_X()
  327. {
  328. cur_DirX = 0;
  329. cur_Dir_StartX = 0;
  330. cur_Dir_EndX = 0;
  331. max_Shoulderx = -0xffff;
  332. min_Shoulderx = 0xffff;
  333. ave_List_RunX = 0;
  334. for (int i = 0; i < list_Shoulder_X.Count; i++)
  335. {
  336. if (i == 0)
  337. {
  338. max_Shoulderx = list_Shoulder_X[i];
  339. min_Shoulderx = list_Shoulder_X[i];
  340. }
  341. if (i < buff_sz)
  342. {
  343. cur_Dir_StartX += list_Shoulder_X[i];
  344. }
  345. if (i >= list_Shoulder_X.Count - buff_sz)
  346. {
  347. cur_Dir_EndX += list_Shoulder_X[i];
  348. }
  349. if (list_Shoulder_X[i] > max_RunY)
  350. max_Shoulderx = list_Shoulder_X[i];
  351. if (list_Shoulder_X[i] < min_RunY)
  352. min_Shoulderx = list_Shoulder_X[i];
  353. ave_List_RunX += list_Shoulder_X[i];
  354. }
  355. ave_List_RunX = ave_List_RunX / list_Shoulder_X.Count;
  356. cur_DirX = (cur_Dir_StartX / buff_sz) - (cur_Dir_EndX / buff_sz);
  357. }
  358. #endregion
  359. //右侧信息
  360. #region
  361. /// <summary>
  362. /// 右侧状态判断
  363. /// </summary>
  364. void List_Wave_Right()
  365. {
  366. if (hand_Right.x != -1 && hand_Right.y != -1 &&
  367. hand_Elbow_Right.x != -1 && hand_Elbow_Right.y != -1)
  368. {
  369. list_Hand_X_Right.Add(hand_Right.x);
  370. list_Elbow_X_Right.Add(hand_Elbow_Right.x);
  371. list_Hand_Y_Right.Add(hand_Right.y);
  372. list_Elbow_Y_Right.Add(hand_Elbow_Right.y);
  373. }
  374. if (!flag_time_Right)
  375. {
  376. flag_time_Right = true;
  377. state_time_Right += Time.deltaTime;
  378. if (state_time_Right >= space_time)
  379. {
  380. flag_time_Right = true;
  381. state_time_Right = 0;
  382. }
  383. }
  384. if (list_Hand_X_Right.Count > hand_Count)
  385. {
  386. list_Hand_X_Right.Remove(list_Hand_X_Right[0]);
  387. list_Elbow_X_Right.Remove(list_Elbow_X_Right[0]);
  388. list_Hand_Y_Right.Remove(list_Hand_Y_Right[0]);
  389. list_Elbow_Y_Right.Remove(list_Elbow_Y_Right[0]);
  390. List_Wave_Right_X();
  391. List_Wave_Right_Y();
  392. List_Wave_Right_ElbowY();
  393. ratioTmpX_Right = (max_x_Right - min_x_Right) / width;//右手X最大差
  394. ratioTmpY_Right = (max_y_Right - min_y_Right) / width;//右手Y最大差
  395. last_ElbowY_Right = list_Elbow_Y_Right[list_Elbow_Y_Right.Count - 1];
  396. last_Y_Right = list_Hand_Y_Right[list_Hand_Y_Right.Count - 1];
  397. speed_list_X_Right =
  398. Mathf.Abs(list_Hand_X_Right[list_Hand_X_Right.Count - 1] -
  399. list_Hand_X_Right[list_Hand_X_Right.Count - 2]) / width;
  400. speed_list_Y_Right =
  401. Mathf.Abs(list_Hand_Y_Right[list_Hand_Y_Right.Count - 1] -
  402. list_Hand_Y_Right[list_Hand_Y_Right.Count - 2]) / width;
  403. //print((speed_list_X_Right == 0) + " " +(speed_list_Y_Right == 0));
  404. if (speed_list_X_Right == 0 || speed_list_Y_Right == 0)
  405. {
  406. State_None_Right();
  407. State_None_RightUD();
  408. None_Right();
  409. }
  410. else
  411. {
  412. //print((ratioTmpX > 0) + " " +
  413. //(ratioTmpX_Right - ratioTmpZ_X > ratioTmpX * space_handX) + " " +
  414. // (ratioTmpY_Right > ratioTmpX * space_handX));
  415. last_Right = "";
  416. if (ratioTmpX > 0)
  417. {
  418. if ((ratioTmpX_Right - ratioTmpZ_X > ratioTmpX * space_handX) ||
  419. (ratioTmpY_Right > ratioTmpX * space_handX))
  420. {
  421. if (ratioTmpX_Right - ratioTmpZ_X > ratioTmpX * space_handX)
  422. //&& last_Y_Right < last_ElbowY_Right)
  423. {
  424. if (cur_Dir_X_Right > 0)
  425. {
  426. State_Wave_Right(2);
  427. }
  428. else if (cur_Dir_X_Right < 0)
  429. {
  430. State_Wave_Right(1);
  431. }
  432. }
  433. else
  434. {
  435. State_None_Right();
  436. None_Right();
  437. }
  438. if (ratioTmpY_Right > ratioTmpX * space_handX)
  439. {
  440. if (cur_Dir_Y_Right > 0)
  441. {
  442. State_Wave_RightUD(4);
  443. }
  444. else if (cur_Dir_Y_Right < 0)
  445. {
  446. State_Wave_RightUD(3);
  447. }
  448. }
  449. else
  450. {
  451. State_None_RightUD();
  452. None_Right();
  453. }
  454. }
  455. else
  456. {
  457. /* State_None_Right();
  458. State_None_RightUD();
  459. None_Right();*/
  460. }
  461. }
  462. else
  463. {
  464. State_None_Right();
  465. State_None_RightUD();
  466. None_Right();
  467. }
  468. }
  469. if (show_Right != null)
  470. {
  471. show_Right.text = last_Right;
  472. }
  473. if (show_Right_Num != null)
  474. {
  475. show_Right_Num.text = None_Num_Right +
  476. "\n挥手" + Wave_Num_Right;
  477. }
  478. }
  479. }
  480. float ratioTmpX_Right;
  481. float cur_Dir_X_Right = 0;
  482. float cur_Dir_Start_RightX = 0, cur_Dir_End_RightX = 0;
  483. float max_x_Right, min_x_Right;
  484. float speed_list_X_Right;
  485. /// <summary>
  486. /// 求X位移差、位移比例、滑动方向
  487. /// </summary>
  488. void List_Wave_Right_X()
  489. {
  490. cur_Dir_X_Right = 0;
  491. cur_Dir_Start_RightX = 0;
  492. cur_Dir_End_RightX = 0;
  493. max_x_Right = -0xffff;
  494. min_x_Right = 0xffff;
  495. for (int i = 0; i < list_Hand_X_Right.Count; i++)
  496. {
  497. if (i == 0)
  498. {
  499. max_x_Right = list_Hand_X_Right[i];
  500. min_x_Right = list_Hand_X_Right[i];
  501. }
  502. if (i < buff_sz)
  503. {
  504. cur_Dir_Start_RightX += list_Hand_X_Right[i];
  505. }
  506. if (i >= list_Hand_X_Right.Count - buff_sz)
  507. {
  508. cur_Dir_End_RightX += list_Hand_X_Right[i];
  509. }
  510. if (list_Hand_X_Right[i] > max_x_Right)
  511. max_x_Right = list_Hand_X_Right[i];
  512. if (list_Hand_X_Right[i] < min_x_Right)
  513. min_x_Right = list_Hand_X_Right[i];
  514. }
  515. cur_Dir_X_Right = (cur_Dir_Start_RightX / buff_sz) - (cur_Dir_End_RightX / buff_sz);
  516. }
  517. float cur_Dir_Y_Right = 0;
  518. float cur_Dir_Start_RightY = 0, cur_Dir_End_RightY = 0;
  519. float max_y_Right, min_y_Right;
  520. float ratioTmpY_Right;
  521. float last_Y_Right, last_ElbowY_Right;
  522. float speed_list_Y_Right;
  523. /// <summary>
  524. /// 求Y位移差与位移比例
  525. /// </summary>
  526. void List_Wave_Right_Y()
  527. {
  528. cur_Dir_Y_Right = 0;
  529. cur_Dir_Start_RightY = 0;
  530. cur_Dir_End_RightY = 0;
  531. max_y_Right = -0xffff;
  532. min_y_Right = 0xffff;
  533. for (int i = 0; i < list_Hand_Y_Right.Count; i++)
  534. {
  535. if (i == 0)
  536. {
  537. max_y_Right = list_Hand_Y_Right[i];
  538. min_y_Right = list_Hand_Y_Right[i];
  539. }
  540. if (i < buff_sz)
  541. {
  542. cur_Dir_Start_RightY += list_Hand_Y_Right[i];
  543. }
  544. if (i >= list_Hand_Y_Right.Count - buff_sz)
  545. {
  546. cur_Dir_End_RightY += list_Hand_Y_Right[i];
  547. }
  548. if (list_Hand_Y_Right[i] > max_y_Right) max_y_Right = list_Hand_Y_Right[i];
  549. if (list_Hand_Y_Right[i] < min_y_Right) min_y_Right = list_Hand_Y_Right[i];
  550. }
  551. cur_Dir_Y_Right = ((cur_Dir_Start_RightY / buff_sz) - (cur_Dir_End_RightY / buff_sz)) / 2;
  552. }
  553. float cur_Dir_ElbowY_Right = 0;
  554. float cur_Dir_StartElbow_RightY = 0;
  555. float max_Elbowy_Right, min_Elbowy_Right;
  556. void List_Wave_Right_ElbowY()
  557. {
  558. cur_Dir_ElbowY_Right = 0;
  559. cur_Dir_StartElbow_RightY = 0;
  560. max_Elbowy_Right = 0;
  561. min_Elbowy_Right = 0;
  562. for (int i = 0; i < list_Elbow_Y_Right.Count; i++)
  563. {
  564. if (i == 0)
  565. {
  566. max_Elbowy_Right = list_Elbow_Y_Right[i];
  567. min_Elbowy_Right = list_Elbow_Y_Right[i];
  568. }
  569. if (list_Elbow_Y_Right[i] > max_Elbowy_Right)
  570. max_Elbowy_Right = list_Elbow_Y_Right[i];
  571. if (list_Elbow_Y_Right[i] < min_Elbowy_Right)
  572. min_Elbowy_Right = list_Elbow_Y_Right[i];
  573. if (i < buff_sz)
  574. {
  575. // cur_Dir_StartElbow_RightY += list_Elbow_Y_Right[i];
  576. }
  577. cur_Dir_StartElbow_RightY += list_Elbow_Y_Right[i];
  578. if (i >= list_Hand_Y_Right.Count - buff_sz)
  579. {
  580. // cur_Dir_EndElbow_RightY += list_Elbow_Y_Right[i];
  581. }
  582. }
  583. // cur_Dir_ElbowY_Right = ((cur_Dir_StartElbow_RightY / buff_sz) + (cur_Dir_EndElbow_RightY / buff_sz)) / 2;
  584. cur_Dir_ElbowY_Right = cur_Dir_StartElbow_RightY / (list_Hand_Y_Right.Count - buff_sz * 2);
  585. }
  586. /// <summary>
  587. /// 上一个状态提示
  588. /// </summary>
  589. void last_State_Right()
  590. {
  591. //last_Right = "上一个左右状态:";
  592. switch (last_WaveState_Right)
  593. {
  594. case Wave_State.None:
  595. //last_Right += "空\n";
  596. break;
  597. case Wave_State.Wave_Left:
  598. // last_Right += "向左挥手\n";
  599. break;
  600. case Wave_State.Wave_Right:
  601. // last_Right += "向右挥手\n";
  602. break;
  603. }
  604. }
  605. /// <summary>
  606. /// 上一个状态更新
  607. /// </summary>
  608. void End_State_Right()
  609. {
  610. last_WaveState_Right = cur_WaveState_Right;
  611. }
  612. /// <summary>
  613. /// 空状态
  614. /// </summary>
  615. void State_None_Right()
  616. {
  617. if (cur_WaveState_Right != Wave_State.None)
  618. {
  619. None_Num_Right += 1;
  620. last_State_Right();
  621. cur_WaveState_Right = Wave_State.None;
  622. End_State_Right();
  623. last_Right = last_Right + "\n当前左右状态:空\n";
  624. }
  625. }
  626. /// <summary>
  627. /// 右手状态
  628. /// </summary>
  629. void State_Wave_Right(int state)
  630. {
  631. //print("right: " + state);
  632. if (last_WaveState_Right == Wave_State.None)// && flag_time_Right) // || last_WaveState_Left == Wave_State.Stay))
  633. {
  634. Wave_Num_Right += 1;
  635. last_State_Right();
  636. switch (state)
  637. {
  638. case 1:
  639. if (cur_WaveState_Right != Wave_State.Wave_Right)
  640. {
  641. cur_WaveState_Right = Wave_State.Wave_Right;
  642. }
  643. last_Right = last_Right + "\n当前左右状态:向右挥手\n";
  644. break;
  645. case 2:
  646. if (cur_WaveState_Right != Wave_State.Wave_Left)
  647. {
  648. cur_WaveState_Right = Wave_State.Wave_Left;
  649. }
  650. last_Right = last_Right + "\n当前左右状态:向左挥手\n";
  651. break;
  652. }
  653. End_State_Right();
  654. flag_time_Right = false;
  655. state_time_Right = 0;
  656. }
  657. }
  658. /// <summary>
  659. /// 上一个状态提示
  660. /// </summary>
  661. void last_State_RightUD()
  662. {
  663. //last_Right = "上一个上下状态:";
  664. switch (last_WaveState_RightUD)
  665. {
  666. case Wave_State.None:
  667. //last_Right += "空\n";
  668. break;
  669. case Wave_State.Wave_Up:
  670. //last_Right += "向上挥手\n";
  671. break;
  672. case Wave_State.Wave_Down:
  673. //last_Right += "向下挥手\n";
  674. break;
  675. }
  676. }
  677. /// <summary>
  678. /// 上一个状态更新
  679. /// </summary>
  680. void End_State_RightUD()
  681. {
  682. last_WaveState_RightUD = cur_WaveState_RightUD;
  683. }
  684. /// <summary>
  685. /// 空状态
  686. /// </summary>
  687. void State_None_RightUD()
  688. {
  689. if (cur_WaveState_RightUD != Wave_State.None)
  690. {
  691. None_Num_Right += 1;
  692. last_State_RightUD();
  693. cur_WaveState_RightUD = Wave_State.None;
  694. End_State_RightUD();
  695. last_Right = last_Right + "\n当前上下状态:空\n";
  696. }
  697. }
  698. /// <summary>
  699. /// 右手状态
  700. /// </summary>
  701. void State_Wave_RightUD(int state)
  702. {
  703. //print("right: " + state);
  704. if (last_WaveState_RightUD == Wave_State.None)// && flag_time_Right) // || last_WaveState_Left == Wave_State.Stay))
  705. {
  706. Wave_Num_Right += 1;
  707. last_State_RightUD();
  708. switch (state)
  709. {
  710. case 3:
  711. if (cur_WaveState_RightUD != Wave_State.Wave_Up)
  712. {
  713. cur_WaveState_RightUD = Wave_State.Wave_Up;
  714. }
  715. last_Right = last_Right + "\n当前上下状态:向上挥手\n";
  716. break;
  717. case 4:
  718. if (cur_WaveState_RightUD != Wave_State.Wave_Down)
  719. {
  720. cur_WaveState_RightUD = Wave_State.Wave_Down;
  721. }
  722. last_Right = last_Right + "\n当前上下状态:向下挥手\n";
  723. break;
  724. }
  725. End_State_RightUD();
  726. flag_time_Right = false;
  727. state_time_Right = 0;
  728. }
  729. }
  730. void None_Right()
  731. {
  732. /* if (list_Hand_X_Right.Count > 0)
  733. {
  734. list_Elbow_X_Right.Clear();
  735. list_Hand_Y_Right.Clear();
  736. list_Elbow_Y_Right.Clear();
  737. list_Hand_X_Right.Clear();
  738. }*/
  739. }
  740. #endregion
  741. //左侧信息
  742. #region
  743. /// <summary>
  744. /// 右侧状态判断
  745. /// </summary>
  746. void List_Wave_Left()
  747. {
  748. if (hand_Left.x != -1 && hand_Left.y != -1 &&
  749. hand_Elbow_Left.x != -1 && hand_Elbow_Left.y != -1)
  750. {
  751. list_Hand_X_Left.Add(hand_Left.x);
  752. list_Elbow_X_Left.Add(hand_Elbow_Left.x);
  753. list_Hand_Y_Left.Add(hand_Left.y);
  754. list_Elbow_Y_Left.Add(hand_Elbow_Left.y);
  755. }
  756. if (!flag_time_Left)
  757. {
  758. flag_time_Left = true;
  759. state_time_Left += Time.deltaTime;
  760. if (state_time_Left >= space_time)
  761. {
  762. flag_time_Left = true;
  763. state_time_Left = 0;
  764. }
  765. }
  766. if (list_Hand_X_Left.Count > hand_Count)
  767. {
  768. list_Hand_X_Left.Remove(list_Hand_X_Left[0]);
  769. list_Elbow_X_Left.Remove(list_Elbow_X_Left[0]);
  770. list_Hand_Y_Left.Remove(list_Hand_Y_Left[0]);
  771. list_Elbow_Y_Left.Remove(list_Elbow_Y_Left[0]);
  772. List_Wave_Left_X();
  773. List_Wave_Left_Y();
  774. List_Wave_Left_ElbowY();
  775. ratioTmpX_Left = (max_x_Left - min_x_Left) / width;//右手左右最大差
  776. ratioTmpY_Left = (max_y_Left - min_y_Left) / width;
  777. last_ElbowY_Left = list_Elbow_Y_Left[list_Elbow_Y_Left.Count - 1];
  778. last_Y_Left = list_Hand_Y_Left[list_Hand_Y_Left.Count - 1];
  779. speed_list_X_Left =
  780. Mathf.Abs(list_Hand_X_Left[list_Hand_X_Left.Count - 1] -
  781. list_Hand_X_Left[list_Hand_X_Left.Count - 2]) / width;
  782. speed_list_Y_Left =
  783. Mathf.Abs(list_Hand_Y_Left[list_Hand_Y_Left.Count - 1] -
  784. list_Hand_Y_Left[list_Hand_Y_Left.Count - 2]) / width;
  785. if (speed_list_X_Left == 0 || speed_list_Y_Left == 0)
  786. {
  787. State_None_Left();
  788. State_None_LeftUD();
  789. None_Left();
  790. }
  791. else
  792. {
  793. last_Left = "";
  794. if (ratioTmpX > 0)
  795. {
  796. if ((ratioTmpX_Left - ratioTmpZ_X > ratioTmpX * space_handX)
  797. || (ratioTmpY_Left > ratioTmpX * space_handX))
  798. {
  799. if (ratioTmpX_Left - ratioTmpZ_X > ratioTmpX * space_handX)
  800. //&& last_Y_Left < last_ElbowY_Left)
  801. {
  802. if (cur_Dir_X_Left > 0)
  803. {
  804. State_Wave_Left(2);
  805. }
  806. else if (cur_Dir_X_Left < 0)
  807. {
  808. State_Wave_Left(1);
  809. }
  810. }
  811. else
  812. {
  813. State_None_Left();
  814. None_Left();
  815. }
  816. if (ratioTmpY_Left > ratioTmpX * space_handX)
  817. {
  818. if (cur_Dir_Y_Left > 0)
  819. {
  820. State_Wave_LeftUD(4);
  821. }
  822. else if (cur_Dir_Y_Left < 0)
  823. {
  824. State_Wave_LeftUD(3);
  825. }
  826. }
  827. else
  828. {
  829. State_None_LeftUD();
  830. None_Left();
  831. }
  832. }
  833. else
  834. {
  835. /* State_None_Left();
  836. State_None_LeftUD();
  837. None_Left();*/
  838. }
  839. }
  840. else
  841. {
  842. State_None_Left();
  843. State_None_LeftUD();
  844. None_Left();
  845. }
  846. }
  847. if (show_Left != null)
  848. {
  849. show_Left.text = last_Left;
  850. }
  851. if (show_Left_Num != null)
  852. {
  853. show_Left_Num.text = None_Num_Left +
  854. "\n挥手" + Wave_Num_Left;
  855. }
  856. }
  857. }
  858. float ratioTmpX_Left;
  859. float cur_Dir_X_Left = 0;
  860. float cur_Dir_Start_LeftX = 0, cur_Dir_End_LeftX = 0;
  861. float max_x_Left, min_x_Left;
  862. float speed_list_X_Left;
  863. /// <summary>
  864. /// 求X位移差、位移比例、滑动方向
  865. /// </summary>
  866. void List_Wave_Left_X()
  867. {
  868. cur_Dir_X_Left = 0;
  869. cur_Dir_Start_LeftX = 0;
  870. cur_Dir_End_LeftX = 0;
  871. max_x_Left = -0xffff;
  872. min_x_Left = 0xffff;
  873. for (int i = 0; i < list_Hand_X_Left.Count; i++)
  874. {
  875. if (i == 0)
  876. {
  877. max_x_Left = list_Hand_X_Left[i];
  878. min_x_Left = list_Hand_X_Left[i];
  879. }
  880. if (i < buff_sz)
  881. {
  882. cur_Dir_Start_LeftX += list_Hand_X_Left[i];
  883. }
  884. if (i >= list_Hand_X_Left.Count - buff_sz)
  885. {
  886. cur_Dir_End_LeftX += list_Hand_X_Left[i];
  887. }
  888. if (list_Hand_X_Left[i] > max_x_Left)
  889. max_x_Left = list_Hand_X_Left[i];
  890. if (list_Hand_X_Left[i] < min_x_Left)
  891. min_x_Left = list_Hand_X_Left[i];
  892. }
  893. cur_Dir_X_Left = (cur_Dir_Start_LeftX / buff_sz) - (cur_Dir_End_LeftX / buff_sz);
  894. }
  895. float cur_Dir_Y_Left = 0;
  896. float cur_Dir_Start_LeftY = 0, cur_Dir_End_LeftY = 0;
  897. float max_y_Left, min_y_Left;
  898. float ratioTmpY_Left;
  899. float last_Y_Left, last_ElbowY_Left;
  900. float speed_list_Y_Left;
  901. /// <summary>
  902. /// 求Y位移差与位移比例
  903. /// </summary>
  904. void List_Wave_Left_Y()
  905. {
  906. cur_Dir_Y_Left = 0;
  907. cur_Dir_Start_LeftY = 0;
  908. cur_Dir_End_LeftY = 0;
  909. max_y_Left = -0xffff;
  910. min_y_Left = 0xffff;
  911. for (int i = 0; i < list_Hand_Y_Left.Count; i++)
  912. {
  913. if (i == 0)
  914. {
  915. max_y_Left = list_Hand_Y_Left[i];
  916. min_y_Left = list_Hand_Y_Left[i];
  917. }
  918. if (i < buff_sz)
  919. {
  920. cur_Dir_Start_LeftY += list_Hand_Y_Left[i];
  921. }
  922. if (i >= list_Hand_Y_Left.Count - buff_sz)
  923. {
  924. cur_Dir_End_LeftY += list_Hand_Y_Left[i];
  925. }
  926. if (list_Hand_Y_Left[i] > max_y_Left) max_y_Left = list_Hand_Y_Left[i];
  927. if (list_Hand_Y_Left[i] < min_y_Left) min_y_Left = list_Hand_Y_Left[i];
  928. }
  929. cur_Dir_Y_Left = (cur_Dir_Start_LeftY / buff_sz) - (cur_Dir_End_LeftY / buff_sz);
  930. }
  931. float cur_Dir_ElbowY_Left = 0;
  932. float cur_Dir_StartElbow_LeftY = 0;
  933. float max_Elbowy_Left, min_Elbowy_Left;
  934. void List_Wave_Left_ElbowY()
  935. {
  936. cur_Dir_ElbowY_Left = 0;
  937. cur_Dir_StartElbow_LeftY = 0;
  938. max_Elbowy_Left = 0;
  939. min_Elbowy_Left = 0;
  940. for (int i = 0; i < list_Elbow_Y_Left.Count; i++)
  941. {
  942. if (i == 0)
  943. {
  944. max_Elbowy_Left = list_Elbow_Y_Left[i];
  945. min_Elbowy_Left = list_Elbow_Y_Left[i];
  946. }
  947. if (list_Elbow_Y_Left[i] > max_Elbowy_Left)
  948. max_Elbowy_Left = list_Elbow_Y_Left[i];
  949. if (list_Elbow_Y_Left[i] < min_Elbowy_Left)
  950. min_Elbowy_Left = list_Elbow_Y_Left[i];
  951. if (i < buff_sz)
  952. {
  953. // cur_Dir_StartElbow_LeftY += list_Elbow_Y_Left[i];
  954. }
  955. cur_Dir_StartElbow_LeftY += list_Elbow_Y_Left[i];
  956. if (i >= list_Hand_Y_Left.Count - buff_sz)
  957. {
  958. // cur_Dir_EndElbow_LeftY += list_Elbow_Y_Left[i];
  959. }
  960. }
  961. cur_Dir_ElbowY_Left = cur_Dir_StartElbow_LeftY / (list_Hand_Y_Left.Count - buff_sz * 2);
  962. }
  963. /// <summary>
  964. /// 上一个状态提示
  965. /// </summary>
  966. void last_State_Left()
  967. {
  968. // last_Left += "上一个左右状态:";
  969. switch (last_WaveState_Left)
  970. {
  971. case Wave_State.None:
  972. // last_Left += "空\n";
  973. break;
  974. case Wave_State.Wave_Left:
  975. //last_Left += "向左挥手\n";
  976. break;
  977. case Wave_State.Wave_Right:
  978. // last_Left += "向右挥手\n";
  979. break;
  980. }
  981. }
  982. /// <summary>
  983. /// 上一个状态更新
  984. /// </summary>
  985. void End_State_Left()
  986. {
  987. last_WaveState_Left = cur_WaveState_Left;
  988. }
  989. /// <summary>
  990. /// 空状态
  991. /// </summary>
  992. void State_None_Left()
  993. {
  994. if (cur_WaveState_Left != Wave_State.None)
  995. {
  996. None_Num_Left += 1;
  997. last_State_Left();
  998. cur_WaveState_Left = Wave_State.None;
  999. End_State_Left();
  1000. last_Left = last_Left + "\n当前左右状态:空\n";
  1001. }
  1002. }
  1003. /// <summary>
  1004. /// 左手状态
  1005. /// </summary>
  1006. void State_Wave_Left(int state)
  1007. {
  1008. //print("left: " + state);
  1009. if (last_WaveState_Left == Wave_State.None)// && flag_time_Left) // || last_WaveState_Left == Wave_State.Stay))
  1010. {
  1011. Wave_Num_Left += 1;
  1012. last_State_Left();
  1013. switch (state)
  1014. {
  1015. case 1:
  1016. if (cur_WaveState_Left != Wave_State.Wave_Right)
  1017. {
  1018. cur_WaveState_Left = Wave_State.Wave_Right;
  1019. }
  1020. last_Left = last_Left + "\n当前左右状态:向右挥手\n";
  1021. break;
  1022. case 2:
  1023. if (cur_WaveState_Left != Wave_State.Wave_Left)
  1024. {
  1025. cur_WaveState_Left = Wave_State.Wave_Left;
  1026. }
  1027. last_Left = last_Left + "\n当前左右状态:向左挥手\n";
  1028. break;
  1029. }
  1030. End_State_Left();
  1031. flag_time_Left = false;
  1032. state_time_Left = 0;
  1033. }
  1034. }
  1035. /// <summary>
  1036. /// 上一个状态提示
  1037. /// </summary>
  1038. void last_State_LeftUD()
  1039. {
  1040. // last_Left += "上一个上下状态:";
  1041. switch (last_WaveState_LeftUD)
  1042. {
  1043. case Wave_State.None:
  1044. // last_Left += "空\n";
  1045. break;
  1046. case Wave_State.Wave_Up:
  1047. // last_Left += "向上挥手\n";
  1048. break;
  1049. case Wave_State.Wave_Down:
  1050. // last_Left += "向下挥手\n";
  1051. break;
  1052. }
  1053. }
  1054. /// <summary>
  1055. /// 上一个状态更新
  1056. /// </summary>
  1057. void End_State_LeftUD()
  1058. {
  1059. last_WaveState_LeftUD = cur_WaveState_LeftUD;
  1060. }
  1061. /// <summary>
  1062. /// 空状态
  1063. /// </summary>
  1064. void State_None_LeftUD()
  1065. {
  1066. if (cur_WaveState_LeftUD != Wave_State.None)
  1067. {
  1068. None_Num_Left += 1;
  1069. last_State_LeftUD();
  1070. cur_WaveState_LeftUD = Wave_State.None;
  1071. End_State_LeftUD();
  1072. last_Left = last_Left + "\n当前上下状态:空\n";
  1073. }
  1074. }
  1075. /// <summary>
  1076. /// 左手状态
  1077. /// </summary>
  1078. void State_Wave_LeftUD(int state)
  1079. {
  1080. //print("left: " + state);
  1081. if (last_WaveState_LeftUD == Wave_State.None)//&& flag_time_Left) // || last_WaveState_Left == Wave_State.Stay))
  1082. {
  1083. Wave_Num_Left += 1;
  1084. last_State_LeftUD();
  1085. switch (state)
  1086. {
  1087. case 3:
  1088. if (cur_WaveState_LeftUD != Wave_State.Wave_Up)
  1089. {
  1090. cur_WaveState_LeftUD = Wave_State.Wave_Up;
  1091. }
  1092. last_Left = last_Left + "\n当前上下状态:向上挥手\n";
  1093. break;
  1094. case 4:
  1095. if (cur_WaveState_LeftUD != Wave_State.Wave_Down)
  1096. {
  1097. cur_WaveState_LeftUD = Wave_State.Wave_Down;
  1098. }
  1099. last_Left = last_Left + "\n当前上下状态:向下挥手\n";
  1100. break;
  1101. }
  1102. End_State_LeftUD();
  1103. flag_time_Left = false;
  1104. state_time_Left = 0;
  1105. }
  1106. }
  1107. void None_Left()
  1108. {
  1109. /*if (list_Hand_X_Left.Count > 0)
  1110. {
  1111. list_Elbow_X_Left.Clear();
  1112. list_Hand_Y_Left.Clear();
  1113. list_Elbow_Y_Left.Clear();
  1114. list_Hand_X_Left.Clear();
  1115. }*/
  1116. }
  1117. #endregion
  1118. }