-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargetPlacement.cs
217 lines (195 loc) · 7.33 KB
/
TargetPlacement.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Unity.Robotics.PickAndPlace
{
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(BoxCollider))]
public class TargetPlacement : MonoBehaviour
{
const string k_NameExpectedTarget = "Target";
static readonly int k_ShaderColorId = Shader.PropertyToID("_Color");
// The threshold that the Target's speed must be under to be considered "placed" in the target area
const float k_MaximumSpeedForStopped = 0.01f;
[SerializeField]
[Tooltip("Target object expected by this placement area. Can be left blank if only one Target in scene")]
GameObject m_Target;
[SerializeField]
[Range(0, 255)]
[Tooltip("Alpha value for any color set during state changes.")]
int m_ColorAlpha = 100;
MeshRenderer m_TargetMeshRenderer;
float m_ColorAlpha01 => m_ColorAlpha / 255f;
MeshRenderer m_MeshRenderer;
BoxCollider m_BoxCollider;
PlacementState m_CurrentState;
PlacementState m_LastColoredState;
[SerializeField]
public ObjReciever reciever;
private int lifecounter = 0;
public PlacementState CurrentState
{
get => m_CurrentState;
private set
{
m_CurrentState = value;
UpdateStateColor();
}
}
public enum PlacementState
{
Outside,
InsideFloating,
InsidePlaced
}
// Start is called before the first frame update
// void Start()
// {
// // Check for mis-configurations and disable if something has changed without this script being updated
// // These are warnings because this script does not contain critical functionality
// if (m_Target == null)
// {
// m_Target = GameObject.Find(k_NameExpectedTarget);
// }
// if (m_Target == null)
// {
// Debug.LogWarning($"{nameof(TargetPlacement)} expects to find a GameObject named " +
// $"{k_NameExpectedTarget} to track, but did not. Can't track placement state.");
// enabled = false;
// return;
// }
// if (!TrySetComponentReferences())
// {
// enabled = false;
// return;
// }
// InitializeState();
// }
void Update() {
if (reciever.ids.Count != 0 & m_Target == null) {
int id = reciever.ids[reciever.ids.Count-1];
m_Target = GameObject.Find("cube"+id.ToString()+"(Clone)");
TrySetComponentReferences();
InitializeState();
}
if (m_Target != null) {
if (CurrentState != PlacementState.Outside)
{
CurrentState = IsTargetStoppedInsideBounds() ?
PlacementState.InsidePlaced : PlacementState.InsideFloating;
}
}
if (CurrentState == PlacementState.InsidePlaced) {
lifecounter += 1;
}
if (lifecounter >= 500) {
GameObject textConsole = GameObject.Find("TextConsole");
Text console = textConsole.GetComponentInChildren<Text>();
console.text = "Object was placed 5s ago. I'm destroying the object";
Destroy(m_Target);
CurrentState = PlacementState.Outside;
lifecounter = 0;
m_Target = null;
}
// Debug.Log(CurrentState);
}
bool TrySetComponentReferences()
{
m_TargetMeshRenderer = m_Target.GetComponent<MeshRenderer>();
if (m_TargetMeshRenderer == null)
{
Debug.LogWarning($"{nameof(TargetPlacement)} expects a {nameof(MeshRenderer)} to be attached " +
$"to {k_NameExpectedTarget}. Cannot check bounds without it, so cannot track placement state.");
return false;
}
// Assume these are here because they are RequiredComponent components
m_MeshRenderer = GetComponent<MeshRenderer>();
m_BoxCollider = GetComponent<BoxCollider>();
return true;
}
void OnValidate()
{
// Useful for visualizing state in editor, but doesn't wholly guarantee accurate coloring in EditMode
// Enter PlayMode to see color update correctly
if (m_Target != null)
{
if (TrySetComponentReferences())
{
InitializeState();
}
}
}
void InitializeState()
{
if (m_Target.GetComponent<BoxCollider>().bounds.Intersects(m_BoxCollider.bounds))
{
CurrentState = IsTargetStoppedInsideBounds() ?
PlacementState.InsidePlaced : PlacementState.InsideFloating;
}
else
{
CurrentState = PlacementState.Outside;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == m_Target.name)
{
CurrentState = PlacementState.InsideFloating;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.name == m_Target.name)
{
CurrentState = PlacementState.Outside;
}
}
bool IsTargetStoppedInsideBounds()
{
var targetIsStopped = m_Target.GetComponent<Rigidbody>().velocity.magnitude < k_MaximumSpeedForStopped;
var targetIsInBounds = m_BoxCollider.bounds.Contains(m_TargetMeshRenderer.bounds.center);
return targetIsStopped && targetIsInBounds;
}
// Update is called once per frame
// void Update()
// {
// if (CurrentState != PlacementState.Outside)
// {
// CurrentState = IsTargetStoppedInsideBounds() ?
// PlacementState.InsidePlaced : PlacementState.InsideFloating;
// }
// }
void UpdateStateColor()
{
if (m_CurrentState == m_LastColoredState)
{
return;
}
var mpb = new MaterialPropertyBlock();
Color stateColor;
switch (m_CurrentState)
{
case PlacementState.Outside:
stateColor = Color.red;
break;
case PlacementState.InsideFloating:
stateColor = Color.yellow;
break;
case PlacementState.InsidePlaced:
stateColor = Color.green;
break;
default:
Debug.LogError($"No state handling implemented for {m_CurrentState}");
stateColor = Color.magenta;
break;
}
stateColor.a = m_ColorAlpha01;
mpb.SetColor(k_ShaderColorId, stateColor);
m_MeshRenderer.SetPropertyBlock(mpb);
m_LastColoredState = m_CurrentState;
}
}
}