| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- /// <summary>
- /// ZWN-XHJ
- /// 20211019
- /// </summary>
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Analytics;
- public enum human_area_state
- {
- halfbody,//半身
- totalbody//全身
- }
- public class human_area : MonoBehaviour
- {
- public human_area_state human_area_state_cur;
- Vector3 middle_vec, head_vec, foot_vec;
- int area = 0, area_sum = 0;//当前是否在区域内计数
- [HideInInspector]
- public List<int> area_list;//中心的位置
- private float area_mid_maxX = 100,
- area_mid_minX = -100,
- area_mid_maxY = 505,
- area_mid_minY = -505,
- area_height = 844;//中心的范围
- private float this_height, this_height_left, this_height_right;
- int area_count = 15;
- private bool area_true = false;//当前是否在区域内
- float area_per = 0.7f;
- Vector3 head, right_ankle, left_ankle;
- Vector3 default_vec = new Vector3(-Screen.width, -Screen.height, 0);
- float area_value = 0.5f;
- float area_value_total = 0.5f;
- float area_value_half = 0.5f;
- void Awake()
- {
- if (human_area_state_cur == human_area_state.totalbody)
- {
- //area_height = 844;//中心的范围
- area_height = 1009;//中心的范围
- }
- else if (human_area_state_cur == human_area_state.halfbody)
- {
- //area_height = 714;//中心的范围
- area_height = 1009;//中心的范围
- }
- }
- void Start()
- {
- /* area_mid_maxX = 100;
- area_mid_minX = -100;
- area_mid_maxY = 505;
- area_mid_minY = -505;*/
- area_mid_maxX = 274;
- area_mid_minX = -274;
- area_mid_maxY = 505;
- area_mid_minY = -505;
- if (Application.platform == RuntimePlatform.WindowsEditor
- || Application.platform == RuntimePlatform.WindowsPlayer)
- {
- area_count = 15;
- }
- if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
- {
- area_count = 6;
- }
- area_value_total = PlayerPrefs.GetFloat("total");
- if (area_value_total == 0)
- {
- area_value_total = 0.5f;
- }
- area_value_half = PlayerPrefs.GetFloat("half");
- if (area_value_half == 0)
- {
- area_value_half = 0.5f;
- }
- if (human_area_state_cur == human_area_state.totalbody)
- {
- area_value = area_value_total;
- text_total.text = area_value.ToString();
- }
- else if (human_area_state_cur == human_area_state.halfbody)
- {
- area_value = area_value_half;
- text_half.text = area_value.ToString();
- }
- }
- /* //使用例子
- void Update()
- {
- print(human_area_true());
- }*/
- int area_num = -1;
- public Text text_total;
- public Text text_half;
- public void total_sum()
- {
- area_value_total += 0.1f;
- area_value = area_value_total;
- PlayerPrefs.SetFloat("total", area_value_total);
- text_total.text = area_value.ToString();
- }
- public void total_dec()
- {
- area_value_total -= 0.1f;
- area_value = area_value_total;
- PlayerPrefs.SetFloat("total", area_value_total);
- text_total.text = area_value.ToString();
- }
- public void half_sum()
- {
- area_value_half += 0.1f;
- area_value = area_value_half;
- PlayerPrefs.SetFloat("half", area_value_half);
- text_half.text = area_value.ToString();
- }
- public void half_dec()
- {
- area_value_half -= 0.1f;
- area_value = area_value_half;
- PlayerPrefs.SetFloat("half", area_value_half);
- text_half.text = area_value.ToString();
- }
- /*float area_value_total = 0.5f;
- float area_value_half = 0.5f;*/
- //public Text show;
- /// <summary>
- /// -1:不存在
- /// 0:在区域内
- /// 1:偏左
- /// 2:偏右
- /// 3:偏前
- /// 4:偏后
- /// 5:人数太多
- /// </summary>
- /// <returns></returns>
- public int human_area_true()
- {
- //print("common_data.single.detail_lists[3] == 1: " + (common_data.single.detail_lists[3] == 1));
- /* 0:鼻子1:左眼2:右眼3:左耳4:右耳5:左肩6:右肩7:左肘
- 8:右肘9:左腕10:右腕11:左臀12:右臀
- 13:左膝14:右膝15:左脚踝16:右脚踝*/
- //if (zwn_interface.single.PoseHelper.Zwn_Pose_Data_Roi)
- //{
- // if (zwn_interface.single.PoseHelper.zwn_pose_array_cur.count < 1)
- // {
- // area_num = -1;
- // }
- // else if (zwn_interface.single.PoseHelper.zwn_pose_array_cur.count > 1)
- // {
- // area_num = 5;
- // }
- // else
- // {
- // /*if (zwn_common_data.single.zwn_pose_android_cur.point.rect_xy.x < area_mid_minX)
- // {
- // //print("偏左");
- // area_num = 1;
- // }
- // else if (zwn_common_data.single.zwn_pose_android_cur.point.rect_xy.x > area_mid_maxX)
- // {
- // //print("偏右");
- // area_num = 2;
- // }
- // else
- // {
- // area_num = area_points();
- // }*/
- // if (zwn_common_data.single.zwn_pose_cur.point.rect_xy.x < area_mid_minX)
- // {
- // //print("偏左");
- // area_num = 1;
- // }
- // else if (zwn_common_data.single.zwn_pose_cur.point.rect_xy.x > area_mid_maxX)
- // {
- // //print("偏右");
- // area_num = 2;
- // }
- // else
- // {
- // area_num = area_points();
- // }
- // }
- // return area_num;
- //}
- return -1;
- }
- int area_points()
- {
- if ((ZwnAmlogicHelperData.isSomeone == true
- && human_area_state_cur == human_area_state.totalbody
- && zwn_common_data.single.zwn_transform_pose[15] != default_vec
- && zwn_common_data.single.zwn_transform_pose[16] != default_vec)
- || (ZwnAmlogicHelperData.isSomeone == true && human_area_state_cur == human_area_state.halfbody))//是否存在整个人
- {
- if (human_area_state_cur == human_area_state.totalbody)
- {
- middle_vec = (zwn_common_data.single.zwn_transform_pose[15]
- + zwn_common_data.single.zwn_transform_pose[16]
- + zwn_common_data.single.zwn_transform_pose[11]
- + zwn_common_data.single.zwn_transform_pose[12]
- + zwn_common_data.single.zwn_transform_pose[5]
- + zwn_common_data.single.zwn_transform_pose[6]
- ) / 6;
- foot_vec = (zwn_common_data.single.zwn_transform_pose[15]
- + zwn_common_data.single.zwn_transform_pose[16]
- ) / 2;
- head_vec = zwn_common_data.single.zwn_transform_pose[0];
- head = zwn_common_data.single.zwn_transform_pose[1];
- right_ankle = zwn_common_data.single.zwn_transform_pose[15];
- left_ankle = zwn_common_data.single.zwn_transform_pose[16];
- if (right_ankle.y != -1 && left_ankle.y != -1)
- {
- if (head.y > 0 && right_ankle.y > 0)
- {
- this_height_right = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle.y));
- }
- else
- {
- this_height_right = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(right_ankle.y));
- }
- if (head.y > 0 && left_ankle.y > 0)
- {
- this_height_left = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle.y));
- }
- else
- {
- this_height_left = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(left_ankle.y));
- }
- this_height = Mathf.Max(this_height_right, this_height_left);
- }
- }
- else if (human_area_state_cur == human_area_state.halfbody)
- {
- middle_vec = (zwn_common_data.single.zwn_transform_pose[11]
- + zwn_common_data.single.zwn_transform_pose[12]
- + zwn_common_data.single.zwn_transform_pose[5]
- + zwn_common_data.single.zwn_transform_pose[6]
- ) / 4;
- foot_vec = (zwn_common_data.single.zwn_transform_pose[11]
- + zwn_common_data.single.zwn_transform_pose[12]
- ) / 2;
- //head_vec = zwn_common_data.single.zwn_transform_pose[0];
- //head = zwn_common_data.single.zwn_transform_pose[1];
- head_vec = (zwn_common_data.single.zwn_transform_pose[5]
- + zwn_common_data.single.zwn_transform_pose[6]
- ) / 2;
- head = (zwn_common_data.single.zwn_transform_pose[5]
- + zwn_common_data.single.zwn_transform_pose[6]
- ) / 2;
- right_ankle = zwn_common_data.single.zwn_transform_pose[11];
- left_ankle = zwn_common_data.single.zwn_transform_pose[12];
- }
- if (right_ankle.y != -1 && left_ankle.y != -1)
- {
- if (head.y > 0 && right_ankle.y > 0)
- {
- this_height_right = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle.y));
- }
- else
- {
- this_height_right = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(right_ankle.y));
- }
- if (head.y > 0 && left_ankle.y > 0)
- {
- this_height_left = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle.y));
- }
- else
- {
- this_height_left = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(left_ankle.y));
- }
- this_height = Mathf.Max(this_height_right, this_height_left);
- }
- // print("this_height: " + this_height + " area_height * 0.75f: " + area_height * 0.75f);
- //show.text = "this_height: " + this_height + " area_height * 0.75f: " + area_height * 0.75f;
- if (
- middle_vec.x >= area_mid_minX
- && middle_vec.x <= area_mid_maxX
- && foot_vec.y >= area_mid_minY
- && foot_vec.y <= area_mid_maxY
- && head_vec.y >= area_mid_minY
- && head_vec.y <= area_mid_maxY
- && this_height >= area_height * area_value
- )
- {
- if (area != 1)
- {
- area = 1;
- }
- }
- else
- {
- if (area != 0)
- {
- area = 0;
- }
- }
- area_list.Add(area);
- if (area_sum != 0)
- {
- area_sum = 0;
- }
- if (area_list.Count >= area_count)
- {
- area_list.Remove(area_list[0]);
- for (int i = 0; i < area_list.Count; i++)
- {
- area_sum += area_list[i];
- }
- if (area_sum >= area_count * area_per)
- {
- if (!area_true)
- {
- area_true = true;
- }
- }
- else
- {
- area_true = false;
- if (middle_vec.x < area_mid_minX)
- {
- //print("偏左");
- area_num = 1;
- }
- else if (middle_vec.x > area_mid_maxX)
- {
- //print("偏右");
- area_num = 2;
- }
- else if (this_height < area_height * area_value)
- {
- //print("偏后");
- area_num = 4;
- }
- else
- {
- //print("偏前");
- area_num = 3;
- }
- }
- }
- if (!area_true)
- {
- //人不在区域内
- return area_num;
- }
- else
- {
- //print("人存在区域");
- return 0;
- }
- }
- else
- {
- area_list.Clear();
- //print("人不存在");
- return -1;
- }
- }
- }
|