| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146 |
- /// <summary>
- /// ZWN-XHJ
- /// 20211018
- /// </summary>
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public enum Wave_State
- {
- None,
- Wave_Left,//左挥手
- Wave_Right,//右挥手
- Wave_Up,//上挥手
- Wave_Down//上挥手
- }
- public class wavingaround : MonoBehaviour
- {
- public float space_time = 0.5f;//状态改变间隔时间
- public float space_nose = 1.3f;//肩的距离大于肩到鼻子高度的比例,小于该比例为侧身
- public float space_hand = 1.3f;//肘部位置小于手部的比例
- public float space_handX = 1f;//手部幅度大于肩宽一半的比例
- public bool Wave_Flag = false;//整体是否检测
- Dictionary<int, Vector3> zwn_Single_Vec = new Dictionary<int, Vector3>();//关于位置数据的存储
- Vector3 shoulder_Right, shoulder_Left;//右手//右肘//右肩//左手//左肘//左肩
- Vector3 hand_Right, hand_Elbow_Right, hand_Left, hand_Elbow_Left;//右手//右肘//右肩//左手//左肘//左肩
- public float width;//宽
- int hand_Count = 25, buff_sz = 3; //存储帧数// avg size
- public List<int> list_Shoulder_Y, list_Shoulder_X, list_ShoulderZ_X;//肩部XY记录
- //关于左侧属性
- #region
- bool flag_time_Left = false;
- float state_time_Left = 0;
- Wave_State cur_WaveState_Left = Wave_State.None;
- Wave_State last_WaveState_Left = Wave_State.None;
- public Wave_State Cur_WaveState_Left
- {
- get { return cur_WaveState_Left; }//返回属性的值
- set { cur_WaveState_Left = value; }//通过Value这个变量给属性赋值
- }
- Wave_State cur_WaveState_LeftUD = Wave_State.None;
- Wave_State last_WaveState_LeftUD = Wave_State.None;
- public Wave_State Cur_WaveState_LeftUD
- {
- get { return cur_WaveState_LeftUD; }//返回属性的值
- set { cur_WaveState_LeftUD = value; }//通过Value这个变量给属性赋值
- }
- public bool Wave_Flag_Left = false;//是否计算
- public List<float> list_Hand_X_Left, list_Hand_Y_Left;//左右手X Y列表
- public List<float> list_Elbow_X_Left, list_Elbow_Y_Left;//左右肩X Y列表
- int None_Num_Left, Wave_Num_Left;//挥手
- string last_Left;
- public Text show_Left, show_Left_Num;
- #endregion
- //关于右侧属性
- #region
- bool flag_time_Right = false;
- float state_time_Right = 0;
- Wave_State cur_WaveState_Right = Wave_State.None;
- Wave_State last_WaveState_Right = Wave_State.None;
- public Wave_State Cur_WaveState_Right
- {
- get { return cur_WaveState_Right; }//返回属性的值
- set { cur_WaveState_Right = value; }//通过Value这个变量给属性赋值
- }
- Wave_State cur_WaveState_RightUD = Wave_State.None;
- Wave_State last_WaveState_RightUD = Wave_State.None;
- public Wave_State Cur_WaveState_RightUD
- {
- get { return cur_WaveState_RightUD; }//返回属性的值
- set { cur_WaveState_RightUD = value; }//通过Value这个变量给属性赋值
- }
- public bool Wave_Flag_Right = false;//是否计算
- public List<float> list_Hand_X_Right, list_Hand_Y_Right;//左右手X Y列表
- public List<float> list_Elbow_X_Right, list_Elbow_Y_Right;//左右肩X Y列表
- int None_Num_Right, Wave_Num_Right;//挥手
- string last_Right;
- public Text show_Right, show_Right_Num;
- #endregion
- void Awake()
- {
- if (Application.platform == RuntimePlatform.WindowsEditor
- || Application.platform == RuntimePlatform.WindowsPlayer)
- {
- hand_Count = 25;
- buff_sz = 3;
- }
- if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
- {
- hand_Count = 6;
- buff_sz = 1;
- }
- space_handX = 0.5f;//手部幅度大于肩宽一半的比例
- }
- bool reInit = false;
- /// <summary>
- /// 初始化打拳
- /// </summary>
- public void Init_Wave()
- {
- cur_WaveState_Left = Wave_State.None;
- last_WaveState_Left = cur_WaveState_Left;
- cur_WaveState_LeftUD = Wave_State.None;
- last_WaveState_LeftUD = cur_WaveState_LeftUD;
- flag_time_Left = true;
- state_time_Left = 0;
- cur_WaveState_Right = Wave_State.None;
- last_WaveState_Right = cur_WaveState_Right;
- cur_WaveState_RightUD = Wave_State.None;
- last_WaveState_RightUD = cur_WaveState_RightUD;
- flag_time_Right = true;
- state_time_Right = 0;
- if (reInit)
- {
- if (list_Hand_X_Left.Count > 0)
- {
- list_Hand_X_Left.Clear();
- list_Elbow_X_Left.Clear();
- list_Hand_Y_Left.Clear();
- list_Elbow_Y_Left.Clear();
- }
- if (list_Hand_X_Right.Count > 0)
- {
- list_Hand_X_Right.Clear();
- list_Elbow_X_Right.Clear();
- list_Hand_Y_Right.Clear();
- list_Elbow_Y_Right.Clear();
- }
- }
- Wave_Flag = true;
- Wave_Flag_Left = true;
- Wave_Flag_Right = true;
- if (!reInit)
- {
- reInit = true;
- }
- }
- /// <summary>
- /// 结束打拳
- /// </summary>
- public void End_Wave()
- {
- if (reInit)
- {
- if (list_Hand_X_Left.Count > 0)
- {
- list_Hand_X_Left.Clear();
- list_Elbow_X_Left.Clear();
- list_Hand_Y_Left.Clear();
- list_Elbow_Y_Left.Clear();
- }
- if (list_Hand_X_Right.Count > 0)
- {
- list_Hand_X_Right.Clear();
- list_Elbow_X_Right.Clear();
- list_Hand_Y_Right.Clear();
- list_Elbow_Y_Right.Clear();
- }
- }
- cur_WaveState_Left = Wave_State.None;
- last_WaveState_Left = cur_WaveState_Left;
- cur_WaveState_LeftUD = Wave_State.None;
- last_WaveState_LeftUD = cur_WaveState_LeftUD;
- flag_time_Left = true;
- state_time_Left = 0;
- cur_WaveState_Right = Wave_State.None;
- last_WaveState_Right = cur_WaveState_Right;
- cur_WaveState_RightUD = Wave_State.None;
- last_WaveState_RightUD = cur_WaveState_RightUD;
- flag_time_Right = true;
- state_time_Right = 0;
- Wave_Flag = false;
- Wave_Flag_Left = false;
- Wave_Flag_Right = false;
- }
- float time_delay = 0;
- void Update()
- {
- time_delay += Time.deltaTime;
- //print("time_delay: " + time_delay);
- if (time_delay >= 0.5f)
- {
- Resources.UnloadUnusedAssets();//卸载未占用的asset资源
- System.GC.Collect();//回收内存
- time_delay = 0;
- }
- if (Wave_Flag)
- {
- ZWN_Single_Vec();
- List_Run();
- if (Wave_Flag_Left)
- {
- List_Wave_Left();
- }
- if (Wave_Flag_Right)
- {
- List_Wave_Right();
- }
- }
- }
- /// <summary>
- /// 单人点
- /// </summary>
- void ZWN_Single_Vec()
- {
- /*0:鼻子1:左眼2:右眼3:左耳4:右耳
- 5:左肩6:右肩7:左肘8:右肘9:左腕
- 10:右腕11:左臀12:右臀13:左膝
- 14:右膝15:左脚踝16:右脚踝 */
- for (int i = 0; i < 17; i++)
- {
- if (zwn_Single_Vec.ContainsKey(i))
- {
- zwn_Single_Vec[i] = common_data.single.zwn_single_vec[i];
- }
- else
- {
- zwn_Single_Vec.Add(i, common_data.single.zwn_single_vec[i]);
- }
- }
- hand_Right = zwn_Single_Vec[9];
- hand_Elbow_Right = zwn_Single_Vec[7];
- shoulder_Right = zwn_Single_Vec[5];
- hand_Left = zwn_Single_Vec[10];
- hand_Elbow_Left = zwn_Single_Vec[8];
- shoulder_Left = zwn_Single_Vec[6];
- //if (width != Helper.Width)
- // width = Helper.Width;
- }
- #region 肩部相关信息
- void List_Run()
- {
- if (
- shoulder_Right.x != -1 && shoulder_Right.y != -1 &&
- shoulder_Left.x != -1 && shoulder_Left.y != -1)
- {
- list_Shoulder_Y.Add((int)((shoulder_Right.y + shoulder_Left.y) / 2));
- list_Shoulder_X.Add((int)(Mathf.Abs(shoulder_Right.x - shoulder_Left.x) / 2));
- list_ShoulderZ_X.Add((int)(Mathf.Abs(shoulder_Right.x + shoulder_Left.x) / 2));
- }
- if (list_Shoulder_Y.Count > hand_Count)
- {
- list_Shoulder_Y.Remove(list_Shoulder_Y[0]);
- list_Shoulder_X.Remove(list_Shoulder_X[0]);
- list_ShoulderZ_X.Add(list_ShoulderZ_X[0]);
- List_Run_Y();
- List_Run_X();
- List_RunZ_X();
- ratioTmpY = (max_RunY - min_RunY) / width;//肩部Y占比例
- ratioTmpX = ave_List_RunX / width;//肩部X占比例
- ratioTmpZ_X = (max_RunY - min_RunY) / width;//肩部Y占比例
- }
- }
- 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;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_RunZ_X()
- {
- cur_DirZ_X = 0;
- cur_Dir_StartZ_X = 0;
- cur_Dir_EndZ_X = 0;
- max_RunZ_X = -0xffff;
- min_RunZ_X = 0xffff;
- for (int i = 0; i < list_ShoulderZ_X.Count; i++)
- {
- if (i == 0)
- {
- max_RunZ_X = list_ShoulderZ_X[i];
- min_RunZ_X = list_ShoulderZ_X[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_StartZ_X += list_ShoulderZ_X[i];
- }
- if (i >= list_ShoulderZ_X.Count - buff_sz)
- {
- cur_Dir_EndZ_X += list_ShoulderZ_X[i];
- }
- if (list_ShoulderZ_X[i] > max_RunZ_X)
- max_RunZ_X = list_ShoulderZ_X[i];
- if (list_ShoulderZ_X[i] < min_RunZ_X)
- min_RunZ_X = list_ShoulderZ_X[i];
- }
- cur_DirZ_X = (cur_Dir_StartZ_X / buff_sz) - (cur_Dir_EndZ_X / buff_sz);
- }
- float cur_DirY = 0, cur_Dir_StartY = 0, cur_Dir_EndY = 0, max_RunY = -0xffff, min_RunY = 0xffff, ratioTmpY;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Run_Y()
- {
- cur_DirY = 0;
- cur_Dir_StartY = 0;
- cur_Dir_EndY = 0;
- max_RunY = -0xffff;
- min_RunY = 0xffff;
- for (int i = 0; i < list_Shoulder_Y.Count; i++)
- {
- if (i == 0)
- {
- max_RunY = list_Shoulder_Y[i];
- min_RunY = list_Shoulder_Y[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_StartY += list_Shoulder_Y[i];
- }
- if (i >= list_Shoulder_Y.Count - buff_sz)
- {
- cur_Dir_EndY += list_Shoulder_Y[i];
- }
- if (list_Shoulder_Y[i] > max_RunY)
- max_RunY = list_Shoulder_Y[i];
- if (list_Shoulder_Y[i] < min_RunY)
- min_RunY = list_Shoulder_Y[i];
- }
- cur_DirY = (cur_Dir_StartY / buff_sz) - (cur_Dir_EndY / buff_sz);
- }
- float cur_DirX = 0, cur_Dir_StartX = 0, cur_Dir_EndX = 0, max_Shoulderx = -0xffff, min_Shoulderx = 0xffff;
- float ave_List_RunX, ratioTmpX;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Run_X()
- {
- cur_DirX = 0;
- cur_Dir_StartX = 0;
- cur_Dir_EndX = 0;
- max_Shoulderx = -0xffff;
- min_Shoulderx = 0xffff;
- ave_List_RunX = 0;
- for (int i = 0; i < list_Shoulder_X.Count; i++)
- {
- if (i == 0)
- {
- max_Shoulderx = list_Shoulder_X[i];
- min_Shoulderx = list_Shoulder_X[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_StartX += list_Shoulder_X[i];
- }
- if (i >= list_Shoulder_X.Count - buff_sz)
- {
- cur_Dir_EndX += list_Shoulder_X[i];
- }
- if (list_Shoulder_X[i] > max_RunY)
- max_Shoulderx = list_Shoulder_X[i];
- if (list_Shoulder_X[i] < min_RunY)
- min_Shoulderx = list_Shoulder_X[i];
- ave_List_RunX += list_Shoulder_X[i];
- }
- ave_List_RunX = ave_List_RunX / list_Shoulder_X.Count;
- cur_DirX = (cur_Dir_StartX / buff_sz) - (cur_Dir_EndX / buff_sz);
- }
- #endregion
- //右侧信息
- #region
- /// <summary>
- /// 右侧状态判断
- /// </summary>
- void List_Wave_Right()
- {
- if (hand_Right.x != -1 && hand_Right.y != -1 &&
- hand_Elbow_Right.x != -1 && hand_Elbow_Right.y != -1)
- {
- list_Hand_X_Right.Add(hand_Right.x);
- list_Elbow_X_Right.Add(hand_Elbow_Right.x);
- list_Hand_Y_Right.Add(hand_Right.y);
- list_Elbow_Y_Right.Add(hand_Elbow_Right.y);
- }
- if (!flag_time_Right)
- {
- flag_time_Right = true;
- state_time_Right += Time.deltaTime;
- if (state_time_Right >= space_time)
- {
- flag_time_Right = true;
- state_time_Right = 0;
- }
- }
- if (list_Hand_X_Right.Count > hand_Count)
- {
- list_Hand_X_Right.Remove(list_Hand_X_Right[0]);
- list_Elbow_X_Right.Remove(list_Elbow_X_Right[0]);
- list_Hand_Y_Right.Remove(list_Hand_Y_Right[0]);
- list_Elbow_Y_Right.Remove(list_Elbow_Y_Right[0]);
- List_Wave_Right_X();
- List_Wave_Right_Y();
- List_Wave_Right_ElbowY();
- ratioTmpX_Right = (max_x_Right - min_x_Right) / width;//右手X最大差
- ratioTmpY_Right = (max_y_Right - min_y_Right) / width;//右手Y最大差
- last_ElbowY_Right = list_Elbow_Y_Right[list_Elbow_Y_Right.Count - 1];
- last_Y_Right = list_Hand_Y_Right[list_Hand_Y_Right.Count - 1];
- speed_list_X_Right =
- Mathf.Abs(list_Hand_X_Right[list_Hand_X_Right.Count - 1] -
- list_Hand_X_Right[list_Hand_X_Right.Count - 2]) / width;
- speed_list_Y_Right =
- Mathf.Abs(list_Hand_Y_Right[list_Hand_Y_Right.Count - 1] -
- list_Hand_Y_Right[list_Hand_Y_Right.Count - 2]) / width;
- //print((speed_list_X_Right == 0) + " " +(speed_list_Y_Right == 0));
- if (speed_list_X_Right == 0 || speed_list_Y_Right == 0)
- {
- State_None_Right();
- State_None_RightUD();
- None_Right();
- }
- else
- {
- //print((ratioTmpX > 0) + " " +
- //(ratioTmpX_Right - ratioTmpZ_X > ratioTmpX * space_handX) + " " +
- // (ratioTmpY_Right > ratioTmpX * space_handX));
- last_Right = "";
- if (ratioTmpX > 0)
- {
- if ((ratioTmpX_Right - ratioTmpZ_X > ratioTmpX * space_handX) ||
- (ratioTmpY_Right > ratioTmpX * space_handX))
- {
- if (ratioTmpX_Right - ratioTmpZ_X > ratioTmpX * space_handX)
- //&& last_Y_Right < last_ElbowY_Right)
- {
- if (cur_Dir_X_Right > 0)
- {
- State_Wave_Right(2);
- }
- else if (cur_Dir_X_Right < 0)
- {
- State_Wave_Right(1);
- }
- }
- else
- {
- State_None_Right();
- None_Right();
- }
- if (ratioTmpY_Right > ratioTmpX * space_handX)
- {
- if (cur_Dir_Y_Right > 0)
- {
- State_Wave_RightUD(4);
- }
- else if (cur_Dir_Y_Right < 0)
- {
- State_Wave_RightUD(3);
- }
- }
- else
- {
- State_None_RightUD();
- None_Right();
- }
- }
- else
- {
- /* State_None_Right();
- State_None_RightUD();
- None_Right();*/
- }
- }
- else
- {
- State_None_Right();
- State_None_RightUD();
- None_Right();
- }
- }
- if (show_Right != null)
- {
- show_Right.text = last_Right;
- }
- if (show_Right_Num != null)
- {
- show_Right_Num.text = None_Num_Right +
- "\n挥手" + Wave_Num_Right;
- }
- }
- }
- float ratioTmpX_Right;
- float cur_Dir_X_Right = 0;
- float cur_Dir_Start_RightX = 0, cur_Dir_End_RightX = 0;
- float max_x_Right, min_x_Right;
- float speed_list_X_Right;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Wave_Right_X()
- {
- cur_Dir_X_Right = 0;
- cur_Dir_Start_RightX = 0;
- cur_Dir_End_RightX = 0;
- max_x_Right = -0xffff;
- min_x_Right = 0xffff;
- for (int i = 0; i < list_Hand_X_Right.Count; i++)
- {
- if (i == 0)
- {
- max_x_Right = list_Hand_X_Right[i];
- min_x_Right = list_Hand_X_Right[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_Start_RightX += list_Hand_X_Right[i];
- }
- if (i >= list_Hand_X_Right.Count - buff_sz)
- {
- cur_Dir_End_RightX += list_Hand_X_Right[i];
- }
- if (list_Hand_X_Right[i] > max_x_Right)
- max_x_Right = list_Hand_X_Right[i];
- if (list_Hand_X_Right[i] < min_x_Right)
- min_x_Right = list_Hand_X_Right[i];
- }
- cur_Dir_X_Right = (cur_Dir_Start_RightX / buff_sz) - (cur_Dir_End_RightX / buff_sz);
- }
- float cur_Dir_Y_Right = 0;
- float cur_Dir_Start_RightY = 0, cur_Dir_End_RightY = 0;
- float max_y_Right, min_y_Right;
- float ratioTmpY_Right;
- float last_Y_Right, last_ElbowY_Right;
- float speed_list_Y_Right;
- /// <summary>
- /// 求Y位移差与位移比例
- /// </summary>
- void List_Wave_Right_Y()
- {
- cur_Dir_Y_Right = 0;
- cur_Dir_Start_RightY = 0;
- cur_Dir_End_RightY = 0;
- max_y_Right = -0xffff;
- min_y_Right = 0xffff;
- for (int i = 0; i < list_Hand_Y_Right.Count; i++)
- {
- if (i == 0)
- {
- max_y_Right = list_Hand_Y_Right[i];
- min_y_Right = list_Hand_Y_Right[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_Start_RightY += list_Hand_Y_Right[i];
- }
- if (i >= list_Hand_Y_Right.Count - buff_sz)
- {
- cur_Dir_End_RightY += list_Hand_Y_Right[i];
- }
- if (list_Hand_Y_Right[i] > max_y_Right) max_y_Right = list_Hand_Y_Right[i];
- if (list_Hand_Y_Right[i] < min_y_Right) min_y_Right = list_Hand_Y_Right[i];
- }
- cur_Dir_Y_Right = ((cur_Dir_Start_RightY / buff_sz) - (cur_Dir_End_RightY / buff_sz)) / 2;
- }
- float cur_Dir_ElbowY_Right = 0;
- float cur_Dir_StartElbow_RightY = 0;
- float max_Elbowy_Right, min_Elbowy_Right;
- void List_Wave_Right_ElbowY()
- {
- cur_Dir_ElbowY_Right = 0;
- cur_Dir_StartElbow_RightY = 0;
- max_Elbowy_Right = 0;
- min_Elbowy_Right = 0;
- for (int i = 0; i < list_Elbow_Y_Right.Count; i++)
- {
- if (i == 0)
- {
- max_Elbowy_Right = list_Elbow_Y_Right[i];
- min_Elbowy_Right = list_Elbow_Y_Right[i];
- }
- if (list_Elbow_Y_Right[i] > max_Elbowy_Right)
- max_Elbowy_Right = list_Elbow_Y_Right[i];
- if (list_Elbow_Y_Right[i] < min_Elbowy_Right)
- min_Elbowy_Right = list_Elbow_Y_Right[i];
- if (i < buff_sz)
- {
- // cur_Dir_StartElbow_RightY += list_Elbow_Y_Right[i];
- }
- cur_Dir_StartElbow_RightY += list_Elbow_Y_Right[i];
- if (i >= list_Hand_Y_Right.Count - buff_sz)
- {
- // cur_Dir_EndElbow_RightY += list_Elbow_Y_Right[i];
- }
- }
- // cur_Dir_ElbowY_Right = ((cur_Dir_StartElbow_RightY / buff_sz) + (cur_Dir_EndElbow_RightY / buff_sz)) / 2;
- cur_Dir_ElbowY_Right = cur_Dir_StartElbow_RightY / (list_Hand_Y_Right.Count - buff_sz * 2);
- }
- /// <summary>
- /// 上一个状态提示
- /// </summary>
- void last_State_Right()
- {
- //last_Right = "上一个左右状态:";
- switch (last_WaveState_Right)
- {
- case Wave_State.None:
- //last_Right += "空\n";
- break;
- case Wave_State.Wave_Left:
- // last_Right += "向左挥手\n";
- break;
- case Wave_State.Wave_Right:
- // last_Right += "向右挥手\n";
- break;
- }
- }
- /// <summary>
- /// 上一个状态更新
- /// </summary>
- void End_State_Right()
- {
- last_WaveState_Right = cur_WaveState_Right;
- }
- /// <summary>
- /// 空状态
- /// </summary>
- void State_None_Right()
- {
- if (cur_WaveState_Right != Wave_State.None)
- {
- None_Num_Right += 1;
- last_State_Right();
- cur_WaveState_Right = Wave_State.None;
- End_State_Right();
- last_Right = last_Right + "\n当前左右状态:空\n";
- }
- }
- /// <summary>
- /// 右手状态
- /// </summary>
- void State_Wave_Right(int state)
- {
- //print("right: " + state);
- if (last_WaveState_Right == Wave_State.None)// && flag_time_Right) // || last_WaveState_Left == Wave_State.Stay))
- {
- Wave_Num_Right += 1;
- last_State_Right();
- switch (state)
- {
- case 1:
- if (cur_WaveState_Right != Wave_State.Wave_Right)
- {
- cur_WaveState_Right = Wave_State.Wave_Right;
- }
- last_Right = last_Right + "\n当前左右状态:向右挥手\n";
- break;
- case 2:
- if (cur_WaveState_Right != Wave_State.Wave_Left)
- {
- cur_WaveState_Right = Wave_State.Wave_Left;
- }
- last_Right = last_Right + "\n当前左右状态:向左挥手\n";
- break;
- }
- End_State_Right();
- flag_time_Right = false;
- state_time_Right = 0;
- }
- }
- /// <summary>
- /// 上一个状态提示
- /// </summary>
- void last_State_RightUD()
- {
- //last_Right = "上一个上下状态:";
- switch (last_WaveState_RightUD)
- {
- case Wave_State.None:
- //last_Right += "空\n";
- break;
- case Wave_State.Wave_Up:
- //last_Right += "向上挥手\n";
- break;
- case Wave_State.Wave_Down:
- //last_Right += "向下挥手\n";
- break;
- }
- }
- /// <summary>
- /// 上一个状态更新
- /// </summary>
- void End_State_RightUD()
- {
- last_WaveState_RightUD = cur_WaveState_RightUD;
- }
- /// <summary>
- /// 空状态
- /// </summary>
- void State_None_RightUD()
- {
- if (cur_WaveState_RightUD != Wave_State.None)
- {
- None_Num_Right += 1;
- last_State_RightUD();
- cur_WaveState_RightUD = Wave_State.None;
- End_State_RightUD();
- last_Right = last_Right + "\n当前上下状态:空\n";
- }
- }
- /// <summary>
- /// 右手状态
- /// </summary>
- void State_Wave_RightUD(int state)
- {
- //print("right: " + state);
- if (last_WaveState_RightUD == Wave_State.None)// && flag_time_Right) // || last_WaveState_Left == Wave_State.Stay))
- {
- Wave_Num_Right += 1;
- last_State_RightUD();
- switch (state)
- {
- case 3:
- if (cur_WaveState_RightUD != Wave_State.Wave_Up)
- {
- cur_WaveState_RightUD = Wave_State.Wave_Up;
- }
- last_Right = last_Right + "\n当前上下状态:向上挥手\n";
- break;
- case 4:
- if (cur_WaveState_RightUD != Wave_State.Wave_Down)
- {
- cur_WaveState_RightUD = Wave_State.Wave_Down;
- }
- last_Right = last_Right + "\n当前上下状态:向下挥手\n";
- break;
- }
- End_State_RightUD();
- flag_time_Right = false;
- state_time_Right = 0;
- }
- }
- void None_Right()
- {
- /* if (list_Hand_X_Right.Count > 0)
- {
- list_Elbow_X_Right.Clear();
- list_Hand_Y_Right.Clear();
- list_Elbow_Y_Right.Clear();
- list_Hand_X_Right.Clear();
- }*/
- }
- #endregion
- //左侧信息
- #region
- /// <summary>
- /// 右侧状态判断
- /// </summary>
- void List_Wave_Left()
- {
- if (hand_Left.x != -1 && hand_Left.y != -1 &&
- hand_Elbow_Left.x != -1 && hand_Elbow_Left.y != -1)
- {
- list_Hand_X_Left.Add(hand_Left.x);
- list_Elbow_X_Left.Add(hand_Elbow_Left.x);
- list_Hand_Y_Left.Add(hand_Left.y);
- list_Elbow_Y_Left.Add(hand_Elbow_Left.y);
- }
- if (!flag_time_Left)
- {
- flag_time_Left = true;
- state_time_Left += Time.deltaTime;
- if (state_time_Left >= space_time)
- {
- flag_time_Left = true;
- state_time_Left = 0;
- }
- }
- if (list_Hand_X_Left.Count > hand_Count)
- {
- list_Hand_X_Left.Remove(list_Hand_X_Left[0]);
- list_Elbow_X_Left.Remove(list_Elbow_X_Left[0]);
- list_Hand_Y_Left.Remove(list_Hand_Y_Left[0]);
- list_Elbow_Y_Left.Remove(list_Elbow_Y_Left[0]);
- List_Wave_Left_X();
- List_Wave_Left_Y();
- List_Wave_Left_ElbowY();
- ratioTmpX_Left = (max_x_Left - min_x_Left) / width;//右手左右最大差
- ratioTmpY_Left = (max_y_Left - min_y_Left) / width;
- last_ElbowY_Left = list_Elbow_Y_Left[list_Elbow_Y_Left.Count - 1];
- last_Y_Left = list_Hand_Y_Left[list_Hand_Y_Left.Count - 1];
- speed_list_X_Left =
- Mathf.Abs(list_Hand_X_Left[list_Hand_X_Left.Count - 1] -
- list_Hand_X_Left[list_Hand_X_Left.Count - 2]) / width;
- speed_list_Y_Left =
- Mathf.Abs(list_Hand_Y_Left[list_Hand_Y_Left.Count - 1] -
- list_Hand_Y_Left[list_Hand_Y_Left.Count - 2]) / width;
- if (speed_list_X_Left == 0 || speed_list_Y_Left == 0)
- {
- State_None_Left();
- State_None_LeftUD();
- None_Left();
- }
- else
- {
- last_Left = "";
- if (ratioTmpX > 0)
- {
- if ((ratioTmpX_Left - ratioTmpZ_X > ratioTmpX * space_handX)
- || (ratioTmpY_Left > ratioTmpX * space_handX))
- {
- if (ratioTmpX_Left - ratioTmpZ_X > ratioTmpX * space_handX)
- //&& last_Y_Left < last_ElbowY_Left)
- {
- if (cur_Dir_X_Left > 0)
- {
- State_Wave_Left(2);
- }
- else if (cur_Dir_X_Left < 0)
- {
- State_Wave_Left(1);
- }
- }
- else
- {
- State_None_Left();
- None_Left();
- }
- if (ratioTmpY_Left > ratioTmpX * space_handX)
- {
- if (cur_Dir_Y_Left > 0)
- {
- State_Wave_LeftUD(4);
- }
- else if (cur_Dir_Y_Left < 0)
- {
- State_Wave_LeftUD(3);
- }
- }
- else
- {
- State_None_LeftUD();
- None_Left();
- }
- }
- else
- {
- /* State_None_Left();
- State_None_LeftUD();
- None_Left();*/
- }
- }
- else
- {
- State_None_Left();
- State_None_LeftUD();
- None_Left();
- }
- }
- if (show_Left != null)
- {
- show_Left.text = last_Left;
- }
- if (show_Left_Num != null)
- {
- show_Left_Num.text = None_Num_Left +
- "\n挥手" + Wave_Num_Left;
- }
- }
- }
- float ratioTmpX_Left;
- float cur_Dir_X_Left = 0;
- float cur_Dir_Start_LeftX = 0, cur_Dir_End_LeftX = 0;
- float max_x_Left, min_x_Left;
- float speed_list_X_Left;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Wave_Left_X()
- {
- cur_Dir_X_Left = 0;
- cur_Dir_Start_LeftX = 0;
- cur_Dir_End_LeftX = 0;
- max_x_Left = -0xffff;
- min_x_Left = 0xffff;
- for (int i = 0; i < list_Hand_X_Left.Count; i++)
- {
- if (i == 0)
- {
- max_x_Left = list_Hand_X_Left[i];
- min_x_Left = list_Hand_X_Left[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_Start_LeftX += list_Hand_X_Left[i];
- }
- if (i >= list_Hand_X_Left.Count - buff_sz)
- {
- cur_Dir_End_LeftX += list_Hand_X_Left[i];
- }
- if (list_Hand_X_Left[i] > max_x_Left)
- max_x_Left = list_Hand_X_Left[i];
- if (list_Hand_X_Left[i] < min_x_Left)
- min_x_Left = list_Hand_X_Left[i];
- }
- cur_Dir_X_Left = (cur_Dir_Start_LeftX / buff_sz) - (cur_Dir_End_LeftX / buff_sz);
- }
- float cur_Dir_Y_Left = 0;
- float cur_Dir_Start_LeftY = 0, cur_Dir_End_LeftY = 0;
- float max_y_Left, min_y_Left;
- float ratioTmpY_Left;
- float last_Y_Left, last_ElbowY_Left;
- float speed_list_Y_Left;
- /// <summary>
- /// 求Y位移差与位移比例
- /// </summary>
- void List_Wave_Left_Y()
- {
- cur_Dir_Y_Left = 0;
- cur_Dir_Start_LeftY = 0;
- cur_Dir_End_LeftY = 0;
- max_y_Left = -0xffff;
- min_y_Left = 0xffff;
- for (int i = 0; i < list_Hand_Y_Left.Count; i++)
- {
- if (i == 0)
- {
- max_y_Left = list_Hand_Y_Left[i];
- min_y_Left = list_Hand_Y_Left[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_Start_LeftY += list_Hand_Y_Left[i];
- }
- if (i >= list_Hand_Y_Left.Count - buff_sz)
- {
- cur_Dir_End_LeftY += list_Hand_Y_Left[i];
- }
- if (list_Hand_Y_Left[i] > max_y_Left) max_y_Left = list_Hand_Y_Left[i];
- if (list_Hand_Y_Left[i] < min_y_Left) min_y_Left = list_Hand_Y_Left[i];
- }
- cur_Dir_Y_Left = (cur_Dir_Start_LeftY / buff_sz) - (cur_Dir_End_LeftY / buff_sz);
- }
- float cur_Dir_ElbowY_Left = 0;
- float cur_Dir_StartElbow_LeftY = 0;
- float max_Elbowy_Left, min_Elbowy_Left;
- void List_Wave_Left_ElbowY()
- {
- cur_Dir_ElbowY_Left = 0;
- cur_Dir_StartElbow_LeftY = 0;
- max_Elbowy_Left = 0;
- min_Elbowy_Left = 0;
- for (int i = 0; i < list_Elbow_Y_Left.Count; i++)
- {
- if (i == 0)
- {
- max_Elbowy_Left = list_Elbow_Y_Left[i];
- min_Elbowy_Left = list_Elbow_Y_Left[i];
- }
- if (list_Elbow_Y_Left[i] > max_Elbowy_Left)
- max_Elbowy_Left = list_Elbow_Y_Left[i];
- if (list_Elbow_Y_Left[i] < min_Elbowy_Left)
- min_Elbowy_Left = list_Elbow_Y_Left[i];
- if (i < buff_sz)
- {
- // cur_Dir_StartElbow_LeftY += list_Elbow_Y_Left[i];
- }
- cur_Dir_StartElbow_LeftY += list_Elbow_Y_Left[i];
- if (i >= list_Hand_Y_Left.Count - buff_sz)
- {
- // cur_Dir_EndElbow_LeftY += list_Elbow_Y_Left[i];
- }
- }
- cur_Dir_ElbowY_Left = cur_Dir_StartElbow_LeftY / (list_Hand_Y_Left.Count - buff_sz * 2);
- }
- /// <summary>
- /// 上一个状态提示
- /// </summary>
- void last_State_Left()
- {
- // last_Left += "上一个左右状态:";
- switch (last_WaveState_Left)
- {
- case Wave_State.None:
- // last_Left += "空\n";
- break;
- case Wave_State.Wave_Left:
- //last_Left += "向左挥手\n";
- break;
- case Wave_State.Wave_Right:
- // last_Left += "向右挥手\n";
- break;
- }
- }
- /// <summary>
- /// 上一个状态更新
- /// </summary>
- void End_State_Left()
- {
- last_WaveState_Left = cur_WaveState_Left;
- }
- /// <summary>
- /// 空状态
- /// </summary>
- void State_None_Left()
- {
- if (cur_WaveState_Left != Wave_State.None)
- {
- None_Num_Left += 1;
- last_State_Left();
- cur_WaveState_Left = Wave_State.None;
- End_State_Left();
- last_Left = last_Left + "\n当前左右状态:空\n";
- }
- }
- /// <summary>
- /// 左手状态
- /// </summary>
- void State_Wave_Left(int state)
- {
- //print("left: " + state);
- if (last_WaveState_Left == Wave_State.None)// && flag_time_Left) // || last_WaveState_Left == Wave_State.Stay))
- {
- Wave_Num_Left += 1;
- last_State_Left();
- switch (state)
- {
- case 1:
- if (cur_WaveState_Left != Wave_State.Wave_Right)
- {
- cur_WaveState_Left = Wave_State.Wave_Right;
- }
- last_Left = last_Left + "\n当前左右状态:向右挥手\n";
- break;
- case 2:
- if (cur_WaveState_Left != Wave_State.Wave_Left)
- {
- cur_WaveState_Left = Wave_State.Wave_Left;
- }
- last_Left = last_Left + "\n当前左右状态:向左挥手\n";
- break;
- }
- End_State_Left();
- flag_time_Left = false;
- state_time_Left = 0;
- }
- }
- /// <summary>
- /// 上一个状态提示
- /// </summary>
- void last_State_LeftUD()
- {
- // last_Left += "上一个上下状态:";
- switch (last_WaveState_LeftUD)
- {
- case Wave_State.None:
- // last_Left += "空\n";
- break;
- case Wave_State.Wave_Up:
- // last_Left += "向上挥手\n";
- break;
- case Wave_State.Wave_Down:
- // last_Left += "向下挥手\n";
- break;
- }
- }
- /// <summary>
- /// 上一个状态更新
- /// </summary>
- void End_State_LeftUD()
- {
- last_WaveState_LeftUD = cur_WaveState_LeftUD;
- }
- /// <summary>
- /// 空状态
- /// </summary>
- void State_None_LeftUD()
- {
- if (cur_WaveState_LeftUD != Wave_State.None)
- {
- None_Num_Left += 1;
- last_State_LeftUD();
- cur_WaveState_LeftUD = Wave_State.None;
- End_State_LeftUD();
- last_Left = last_Left + "\n当前上下状态:空\n";
- }
- }
- /// <summary>
- /// 左手状态
- /// </summary>
- void State_Wave_LeftUD(int state)
- {
- //print("left: " + state);
- if (last_WaveState_LeftUD == Wave_State.None)//&& flag_time_Left) // || last_WaveState_Left == Wave_State.Stay))
- {
- Wave_Num_Left += 1;
- last_State_LeftUD();
- switch (state)
- {
- case 3:
- if (cur_WaveState_LeftUD != Wave_State.Wave_Up)
- {
- cur_WaveState_LeftUD = Wave_State.Wave_Up;
- }
- last_Left = last_Left + "\n当前上下状态:向上挥手\n";
- break;
- case 4:
- if (cur_WaveState_LeftUD != Wave_State.Wave_Down)
- {
- cur_WaveState_LeftUD = Wave_State.Wave_Down;
- }
- last_Left = last_Left + "\n当前上下状态:向下挥手\n";
- break;
- }
- End_State_LeftUD();
- flag_time_Left = false;
- state_time_Left = 0;
- }
- }
- void None_Left()
- {
- /*if (list_Hand_X_Left.Count > 0)
- {
- list_Elbow_X_Left.Clear();
- list_Hand_Y_Left.Clear();
- list_Elbow_Y_Left.Clear();
- list_Hand_X_Left.Clear();
- }*/
- }
- #endregion
- }
|