-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECAObject.cs
More file actions
443 lines (390 loc) · 15.8 KB
/
Copy pathECAObject.cs
File metadata and controls
443 lines (390 loc) · 15.8 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.Collections;
using ECARules4All_DLL.Utils;
using UnityEngine;
// ReSharper disable InconsistentNaming
namespace ECARules4All_DLL
{
/// <summary>
/// <b>ECAObject</b> is the base class for all virtual objects that can be used in the automations.
/// All the other classes in this package inherit from this class or one of its subclasses.
/// It supports properties such as position, rotation, scale, visibility, and activity, and provides methods for moving, rotating, scaling, and controlling visibility.
/// </summary>
[DisallowMultipleComponent]
[RequireComponent(typeof(ECATracker))]
[ECARules4All("object")]
public class ECAObject : MonoBehaviour
{
/// <summary>
/// <b> GameCollider </b> is the collider of the object.
/// </summary>
private Collider[] gameCollider;
/// <summary>
/// <b>GameRender</b> is the renderer of the object.
/// </summary>
private Renderer[] gameRenderer;
/// <summary>
/// <b>isBusyMoving</b> is a boolean that indicates if the object is moving.
/// </summary>
private bool isBusyMoving = false;
private float deltaTimeIsRendered = 0;
/// <summary>
/// <b>description</b> describes in a few words what the object is and its role.
/// </summary>
[ECARelevance(true)]
[StateVariable("description", ECARules4AllType.Text)]
public string description
{
get => _description;
set
{
_description = value;
ECAScript.NotifyUpdate(this, nameof(description), _description);
}
}
[SerializeField] private string _description;
/// <summary>
/// <b>p</b> represents the position of the virtual object in the 3D space. It's a vector with three components: x, y, and z.
/// </summary>
[ECARelevance(true)]
[StateVariable("position", ECARules4AllType.Position)]
public Position p
{
get => _p;
set
{
_p = value;
ECAScript.NotifyUpdate(this, nameof(p), _p);
}
}
private Position _p;
/// <summary>
/// <b>r</b> represents the rotation of the object in the 3D space. It's a vector with three components: x, y, and z (euler angles).
/// </summary>
[StateVariable("rotation", ECARules4AllType.Rotation)]
public Rotation r
{
get => _r;
set
{
_r = value;
ECAScript.NotifyUpdate(this, nameof(r), _r);
}
}
private Rotation _r;
/// <summary>
/// <b>r</b> represents the scale of the object in the 3D space.
/// </summary>
[StateVariable("scale", ECARules4AllType.Scale)]
public Scale s
{
get => _s;
set
{
_s = value;
ECAScript.NotifyUpdate(this, nameof(s), _s);
}
}
private Scale _s;
private Vector3 _originalPosition;
private Quaternion _originalQuaternion;
private Vector3 _originalScale;
/// <summary>
/// <b>visible</b> indicates whether the object is visible. The allowed values are either 'yes' or 'no'.
/// If invisible, the object is not rendered but remains interactive for collisions.
/// </summary>
[ECARelevance(true)]
[StateVariable("visible", ECARules4AllType.Boolean)]
public ECABoolean isVisible // = new ECABoolean(ECABoolean.BoolType.YES);
{
get => _isVisible;
set
{
_isVisible = value;
ECAScript.NotifyUpdate(this, nameof(isVisible), isVisible.ToString());
}
}
[SerializeField] private ECABoolean _isVisible = new ECABoolean(ECABoolean.BoolType.YES);
/// <summary>
/// <b>active</b> indicates whether the object is active. The allowed values are either 'yes' or 'no'.
/// When inactive, the object is not rendered and does not interact with other objects.
/// </summary>
[StateVariable("active", ECARules4AllType.Boolean)]
public ECABoolean isActive
{
get => _isActive;
set
{
_isActive = value;
ECAScript.NotifyUpdate(this, nameof(isActive), isActive.ToString());
}
}
[SerializeField] private ECABoolean _isActive = new ECABoolean(ECABoolean.BoolType.YES);
/// <summary>
/// <b>isInsideCamera</b> indicates whether the object is currently within the camera's field of view. This property is automatically updated at runtime.
/// </summary>
[ECADeque("DEQUE_FRAMED_OBJECTS")]
[StateVariable("isInsideCamera", ECARules4AllType.Boolean)]
public ECABoolean isInsideCamera
{
get => _isInsideCamera;
set
{
_isInsideCamera = value;
ECAScript.NotifyUpdate(this, nameof(isInsideCamera), isInsideCamera.ToString());
}
}
private ECABoolean _isInsideCamera = new ECABoolean(ECABoolean.BoolType.NO);
private Canvas canvas;
private Camera xrCamera;
private void Awake()
{
gameCollider = this.gameObject.GetComponents<Collider>();
gameRenderer = this.gameObject.GetComponents<Renderer>();
canvas = this.GetComponent<Canvas>();
if (gameRenderer.Length == 0)
gameRenderer = this.gameObject.GetComponentsInChildren<Renderer>();
if (gameRenderer.Length == 0)
{
Debug.LogWarning(
$"{gameObject.name} has not renderer component. This is important for insideCamera property.");
}
if (gameCollider.Length == 0)
gameCollider = this.gameObject.GetComponentsInChildren<Collider>();
p = new Position(transform.position);
r = new Rotation(transform.localRotation);
s = new Scale(transform.localScale);
// I don't want to know what should be done if there are multiple cameras in the scene
xrCamera = Camera.main;
if (xrCamera == null)
{
throw new Exception(
"No camera found in the scene. This is important for the isInsideCamera property!!");
}
}
private void Start()
{
_originalPosition = gameObject.transform.position;
_originalQuaternion = gameObject.transform.localRotation;
_originalScale = gameObject.transform.localScale;
UpdateVisibility();
}
/// <summary>
/// <b>Moves</b> (to) is a method that moves the object to a specified position in the 3D space.
/// </summary>
/// <param name="newPos">The target position to move to.</param>
[Action(typeof(ECAObject), "moves to", typeof(Position))]
public void Moves(Position newPos)
{
float speed = 1.0F;
Vector3 endMarker = new Vector3(newPos.x, newPos.y, newPos.z);
StartCoroutine(MoveObject(speed, endMarker));
}
/// <summary>
/// <b>Moves</b> (on) is a method that moves the object along a specified path, following sequential points.
/// </summary>
/// <param name="path">An array of positions the object will follow, one after another.</param>
[Action(typeof(ECAObject), "moves on", typeof(Path))]
public void Moves(Path path)
{
StartCoroutine(WaitForOrderedMovement(path));
}
private IEnumerator WaitForOrderedMovement(Path path)
{
foreach (Position pos in path.Points)
{
while (isBusyMoving)
{
yield return null;
}
Moves(pos);
}
}
/// <summary>
/// <b>Rotates</b> sets the object's rotation to a specified value in the 3D space.
/// </summary>
/// <param name="newRot">The target rotation expressed as a vector with three components: x, y, and z.</param>
[Action(typeof(ECAObject), "rotates around", typeof(Rotation))]
public void Rotates(Rotation newRot)
{
//r.Assign(newRot);
r = new Rotation(newRot);
transform.Rotate(r.x, r.y, r.z); // todo verify rotation
}
/// <summary>
/// <b>Looks</b> adjusts the object's rotation to face a specified target object.
/// </summary>
/// <param name="o">The target GameObject to look at.</param>
[Action(typeof(ECAObject), "looks at", typeof(GameObject))]
public void Looks(GameObject o)
{
ECAObject looked = o.GetComponent<ECAObject>();
Physics.Linecast(transform.position, o.transform.position, out var hit);
if (o.name == hit.collider.gameObject.name && looked.isActive)
{
transform.LookAt(looked.gameObject.transform);
//r.Assign(transform.rotation);
r = new Rotation(transform.rotation);
}
}
/// <summary>
/// <b>Scales</b> sets the object's scale to a specified value.
/// </summary>
/// <param name="newScale">The new scale value fo the object. The scale is a vector with three components: x, y, and z.</param>
[Action(typeof(ECAObject), "scales to", typeof(Scale))]
public void Scales(Scale newScale)
{
//r.Assign(newRot);
s = new Scale(newScale);
transform.localScale = new Vector3(s.x, s.y, s.z);
}
/// <summary>
/// Restores the object's original position, rotation, and scale to their initial values.
/// </summary>
[Action(typeof(ECAObject), "restores original settings")]
public void MovesOriginalPosition()
{
// float speed = 3.0F;
//StartCoroutine(MoveObject(speed, _originalPosition));
gameObject.transform.position = _originalPosition;
p = new Position(_originalPosition);
gameObject.transform.localRotation = _originalQuaternion;
r = new Rotation(_originalQuaternion);
gameObject.transform.localScale = _originalScale;
s = new Scale(_originalScale);
}
/// <summary>
/// <b>Shows</b> maakes the object visible if it is not already.
/// </summary>
[Action(typeof(ECAObject), "shows")]
[ECARelevance(true)]
public void Shows()
{
//isVisible.Assign(ECABoolean.BoolType.YES);
isVisible = new ECABoolean(ECABoolean.BoolType.YES);
UpdateVisibility();
}
/// <summary>
/// <b>Hides</b> makes the object invisible if it is not already.
/// </summary>
[ECARelevance(true)]
[Action(typeof(ECAObject), "hides")]
public void Hides()
{
//isVisible.Assign(ECABoolean.BoolType.NO);
isVisible = new ECABoolean(ECABoolean.BoolType.NO);
UpdateVisibility();
}
/// <summary>
/// <b>Activates</b> makes the object both interactable and visible.
/// </summary>
[Action(typeof(ECAObject), "activates")]
public void Activates()
{
//isActive.Assign(ECABoolean.BoolType.YES);
isActive = new ECABoolean(ECABoolean.BoolType.YES);
UpdateVisibility();
}
/// <summary>
/// <b>Deactivates</b> makes the object invisible and non-interactable.
/// </summary>
[Action(typeof(ECAObject), "deactivates")]
public void Deactivates()
{
//isActive.Assign(ECABoolean.BoolType.NO);
isActive = new ECABoolean(ECABoolean.BoolType.NO);
UpdateVisibility();
}
/// <summary>
/// <b>ShowsHides</b> changes the visibility state of the object based on a parameter. The parameter can be either 'yes' or 'no'.
/// </summary>
/// <param name="yesNo">The new visibility state.</param>
[ECARelevance(true)]
[Action(typeof(ECAObject), "changes", "visible", "to", typeof(YesNo))]
public void ShowsHides(ECABoolean yesNo)
{
isVisible = yesNo;
UpdateVisibility();
}
/// <summary>
/// <b>ActivatesDeactivates</b> changes the active state of the object based on a parameter. The parameter can be either 'yes' or 'no'.
/// </summary>
/// <param name="yesNo">The new active state.</param>
[Action(typeof(ECAObject), "changes", "active", "to", typeof(YesNo))]
public void ActivatesDeactivates(ECABoolean yesNo)
{
isActive = yesNo;
UpdateVisibility();
}
//TODO: should be private
public void UpdateVisibility()
{
foreach (Collider c in gameCollider)
{
c.enabled = isActive;
}
foreach (Renderer rend in gameRenderer)
{
if (!isActive || !isVisible)
{
rend.enabled = false;
}
else rend.enabled = true;
}
if (canvas != null)
{
if (!isActive || !isVisible)
{
canvas.enabled = false;
}
else canvas.enabled = true;
}
}
private IEnumerator MoveObject(float speed, Vector3 endMarker)
{
isBusyMoving = true;
//Vector3 startMarker = gameObject.transform.position;
Vector3 startMarker = gameObject.transform.position;
float startTime = Time.time;
float journeyLength = Vector3.Distance(startMarker, endMarker);
//while (gameObject.transform.position != endMarker)
while (gameObject.transform.position != endMarker)
{
float distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed equals current distance divided by total distance.
float fractionOfJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
gameObject.transform.position = Vector3.Lerp(startMarker, endMarker, fractionOfJourney);
//GetComponent<ECAObject>().p = new Position(gameObject.transform.position);
GetComponent<ECAObject>().p = new Position(gameObject.transform.position);
yield return null;
}
//GetComponent<ECAObject>().p = new Position(gameObject.transform.position);
GetComponent<ECAObject>().p = new Position(gameObject.transform.position);
isBusyMoving = false;
}
private void Update()
{
// check if object is within the camera's field of view.
// the object must be active
if (xrCamera && Time.time - deltaTimeIsRendered > 0.5f)
{
Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(xrCamera);
ECABoolean res = ECABoolean.NO;
foreach (Renderer render in gameRenderer)
{
if (GeometryUtility.TestPlanesAABB(frustumPlanes, render.bounds))
{
res = ECABoolean.YES;
break;
}
}
if (this.isInsideCamera != res)
{
this.isInsideCamera = res;
deltaTimeIsRendered = Time.time;
}
}
}
}
}