-
Notifications
You must be signed in to change notification settings - Fork 0
/
HumanChecker.cs
185 lines (159 loc) · 7.21 KB
/
HumanChecker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.XR.WSA;
public class AABB
{
public Vector2 Min { get; set; }
public Vector2 Max { get; set; }
public Vector2 Center => (Min + Max) / 2;
private Vector2 ScreenSize => new Vector2(Screen.width, Screen.height);
public string Tag { get; set; }
public override string ToString()
{
Vector2 normalizedMin = Min / ScreenSize;
Vector2 normalizedMax = Max / ScreenSize;
normalizedMin = new Vector2(Mathf.Clamp01(normalizedMin.x), Mathf.Clamp01(normalizedMin.y));
normalizedMax = new Vector2(Mathf.Clamp01(normalizedMax.x), Mathf.Clamp01(normalizedMax.y));
var normalizedCenter = (normalizedMax + normalizedMin) / 2;
var boxSize = (normalizedMax - normalizedMin);
return $"0 {normalizedCenter.x} {normalizedCenter.y} {boxSize.x} {boxSize.y}";
}
}
public static class AABBExtentions
{
public static AABB GetAABB(this SkinnedMeshRenderer renderer)
{
Vector3[] pts = new Vector3[8];
Bounds b = renderer.bounds;
Camera cam = Camera.main;
pts[0] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[1] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[2] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[3] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
pts[4] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[5] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[6] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[7] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
//Get them in GUI space
for (int i = 0; i < pts.Length; i++) pts[i].y = Screen.height - pts[i].y;
//Calculate the min and max positions
Vector3 min = pts[0];
Vector3 max = pts[0];
for (int i = 1; i < pts.Length; i++)
{
min = Vector3.Min(min, pts[i]);
max = Vector3.Max(max, pts[i]);
}
return new AABB { Min=min, Max=max, Tag = renderer.tag };
}
}
public class HumanChecker : MonoBehaviour
{
SkinnedMeshRenderer[] mRenderer;
SkinnedMeshRenderer[] wRenderer;
GameObject[] men;
GameObject[] women;
public float margin = 0;
public Rect boundingBox = new Rect();
Bounds b = new Bounds();
private Vector3[] pts = new Vector3[8];
float time = 5.0f;
private void Start()
{
StartCoroutine(ExecuteAfterSeconds(2));
}
IEnumerator ExecuteAfterSeconds(int seconds)
{
yield return new WaitForSeconds(seconds);
men = GameObject.FindGameObjectsWithTag("0");
women = GameObject.FindGameObjectsWithTag("1");
mRenderer = new SkinnedMeshRenderer[men.Length];
wRenderer = new SkinnedMeshRenderer[women.Length];
GameObject.FindGameObjectsWithTag("0")
.Select(go=> go.GetComponent<SkinnedMeshRenderer>())
.Where(r => IsVisible(r))
.Select(r => r.GetAABB())
.ToArray();
GameObject.FindGameObjectsWithTag("1")
.Select(go => go.GetComponent<SkinnedMeshRenderer>())
.Where(r => IsVisible(r))
.Select(r => r.GetAABB())
.ToArray();
}
void Update()
{
if(time >= 0)
{
time -= Time.deltaTime;
}
else
{
OutputVisibleRenderers(men);
OutputVisibleRenderers(women);
}
}
void OutputVisibleRenderers(GameObject[] humans)
{
foreach (var human in humans)
{
SkinnedMeshRenderer renderer = human.GetComponent<SkinnedMeshRenderer>();
b = renderer.bounds;
// output only the visible renderers' name
if (IsVisible(renderer))
{
boundingBox.height = renderer.bounds.size.y;
boundingBox.width = renderer.bounds.size.x;
boundingBox.center = renderer.bounds.center;
Camera cam = Camera.main;
//The object is behind us
if (cam.WorldToScreenPoint(b.center).z < 0) continue;
//All 8 vertices of the bounds
pts[0] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[1] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[2] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[3] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
pts[4] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[5] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[6] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[7] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
//Get them in GUI space
for (int i = 0; i < pts.Length; i++) pts[i].y = Screen.height - pts[i].y;
//Calculate the min and max positions
Vector3 min = pts[0];
Vector3 max = pts[0];
for (int i = 1; i < pts.Length; i++)
{
min = Vector3.Min(min, pts[i]);
max = Vector3.Max(max, pts[i]);
}
//Construct a rect of the min and max positions and apply some margin
boundingBox = Rect.MinMaxRect(min.x, min.y, max.x, max.y);
boundingBox.xMin -= margin;
boundingBox.xMax += margin;
boundingBox.yMin -= margin;
boundingBox.yMax += margin;
}
}
}
private void OnGUI()
{
Vector2 min = Camera.main.WorldToScreenPoint(b.min);
Vector2 max = Camera.main.WorldToScreenPoint(b.max);
GUI.Box(Rect.MinMaxRect(min.x,min.y,max.x,max.y), "");
}
private bool IsVisible(Renderer renderer)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
if (GeometryUtility.TestPlanesAABB(planes, renderer.bounds))
{
// Debug.Log(renderer.bounds.center);
// Debug.Log(renderer.bounds.size);
return true;
}
else
return false;
}
}