-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathObjectPlacementManager.cs
586 lines (497 loc) · 22.8 KB
/
ObjectPlacementManager.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.Collections.Generic;
using System.Linq;
using CrypticCabinet.SceneManagement;
using CrypticCabinet.UI;
using Cysharp.Threading.Tasks;
using Oculus.Interaction.Input;
using UnityEngine;
namespace CrypticCabinet.Utils
{
/// <summary>
/// The manager responsible for the object placements in the room during scene setup.
/// </summary>
public class ObjectPlacementManager : Meta.Utilities.Singleton<ObjectPlacementManager>
{
#if UNITY_EDITOR
private void Update()
{
if (gameObject.activeInHierarchy && Input.GetKeyDown(KeyCode.Alpha1))
{
ShufflePlacements();
}
}
#endif
public ObjectPlacementManager(SceneObject o)
{
}
public enum LoadableSceneObjects
{
ORRERY,
CABINET,
SAND_SHOOT,
WINDOW_PULLY,
LIGHT_PLATE,
BUCKET,
MINI_GENERATOR_SMALL,
MAIN_GENERATOR,
MAIN_GENERATOR_COIL,
GENERATOR_SCHEMATIC1,
DIRECTIONAL_APPARATUS,
MINI_GENERATOR_MEDIUM,
MINI_GENERATOR_LARGE,
ATLAS_STATUE,
LIGHT_BULB_UV_MACHINE,
UV_SIGN_CLUE,
UV_SIGN_TEXT1,
UV_SIGN_TEXT2,
UV_SIGN_TEXT3,
SAFE_PUZZLE,
GENERATOR_SCHEMATIC2
}
[Serializable]
public struct SceneObject
{
public string ObjectName;
public ObjectPlacementVisualiser DisplayPrefab;
public Vector3 MainPosition;
public Vector3 WallPosition;
public Quaternion WallRotation;
public bool SuccessfullyPlaced;
public Quaternion MainRotation;
[HideInInspector] public ObjectPlacementVisualiser SpawnedDisplayObject;
}
[SerializeField] private GameObject m_spawnUi;
[SerializeField] private List<SceneObject> m_againstWallObject = new();
[SerializeField] private List<SceneObject> m_onWallObject = new();
[SerializeField] private List<SceneObject> m_deskObject = new();
[SerializeField] private List<SceneObject> m_floorObject = new();
[SerializeField] private List<SceneObject> m_anyHorizontalObject = new();
public Action ConfirmObjectPlacementsCallback;
private List<GameObject> m_spawnedVisualiserObjects = new();
private SceneUnderstandingLocationPlacer m_sceneUnderstandingLocationPlacer;
private OVRCameraRigRef m_cameraRig;
private bool m_isInShuffleMode = false;
protected override void InternalAwake()
{
base.InternalAwake();
m_spawnUi.SetActive(false);
}
private async void Start()
{
m_spawnedVisualiserObjects = new List<GameObject>();
m_sceneUnderstandingLocationPlacer = SceneUnderstandingLocationPlacer.Instance;
if (m_sceneUnderstandingLocationPlacer == null)
{
Debug.LogError("Missing SceneUnderstandingLocationPlacer in scene unable to layout objects.");
}
await Init();
}
private async UniTask Init()
{
await FindFloorWallLocation();
await UniTask.Yield();
FindWallLocation();
await UniTask.Yield();
FindDeskLocation();
await UniTask.Yield();
FindFloorLocation();
await UniTask.Yield();
FindHorizontalLocation();
await UniTask.Yield();
RequestCollidersDisabled();
await UniTask.Yield();
await ShuffleIfNeeded();
await UniTask.Yield();
RandomlyPlaceFailedObjects();
await UniTask.Yield();
ShowVisualizers();
UISystem.Instance.HideAll();
m_spawnUi.SetActive(true);
}
private void RandomlyPlaceFailedObjects()
{
var ovrCameraRig = FindObjectOfType<OVRCameraRig>();
var userPos = ovrCameraRig.centerEyeAnchor.position;
foreach (var againstWallObject in m_againstWallObject.Where(o => !o.SuccessfullyPlaced).ToList())
{
m_sceneUnderstandingLocationPlacer.RequestTotallyRandomWallLocation(1, out var position, out var rotation);
position.y = 0;
PlaceObject(againstWallObject, position, rotation);
}
foreach (var wallObject in m_onWallObject.Where(o => !o.SuccessfullyPlaced).ToList())
{
m_sceneUnderstandingLocationPlacer.RequestTotallyRandomWallLocation(1, out var position, out var rotation);
PlaceObject(wallObject, position, rotation);
}
foreach (var deskObject in m_deskObject.Where(o => !o.SuccessfullyPlaced).ToList())
{
m_sceneUnderstandingLocationPlacer.RequestTotallyRandomDeskLocation(out var position);
PlaceObjectLookAtUser(deskObject, userPos, position);
}
foreach (var floorObject in m_floorObject.Where(o => !o.SuccessfullyPlaced).ToList())
{
m_sceneUnderstandingLocationPlacer.RequestTotallyRandomFloorLocation(out var position);
PlaceObjectLookAtUser(floorObject, userPos, position);
}
foreach (var sceneObject in m_anyHorizontalObject.Where(o => !o.SuccessfullyPlaced).ToList())
{
m_sceneUnderstandingLocationPlacer.RequestTotallyRandomFloorLocation(out var position);
PlaceObjectLookAtUser(sceneObject, userPos, position);
}
}
private static void PlaceObject(SceneObject sceneObject, Vector3 targetPosition, Quaternion targetRotation)
{
var placedObjectTransform = sceneObject.SpawnedDisplayObject.transform;
placedObjectTransform.position = targetPosition;
placedObjectTransform.rotation = targetRotation;
sceneObject.SpawnedDisplayObject.UpdateLocation();
}
private static void PlaceObjectLookAtUser(SceneObject sceneObject, Vector3 userPosition, Vector3 targetPosition)
{
var placedObjectTransform = sceneObject.SpawnedDisplayObject.transform;
placedObjectTransform.position = targetPosition;
userPosition.y = placedObjectTransform.position.y;
placedObjectTransform.LookAt(userPosition);
sceneObject.SpawnedDisplayObject.UpdateLocation();
}
private async UniTask ShuffleIfNeeded(int maxRetries = 3)
{
for (var i = 0; i < maxRetries; i++)
{
if (DetectFailedPlacements() > 0)
{
Debug.Log($"Shuffle iteration {i + 1}");
await ShufflePlacementsAsync();
}
else
{
return;
}
}
}
private int DetectFailedPlacements()
{
var againstWallObjectFailed = m_againstWallObject.Count(o => !o.SuccessfullyPlaced);
var onWallObjectFailed = m_onWallObject.Count(o => !o.SuccessfullyPlaced);
var deskObjectFailed = m_deskObject.Count(o => !o.SuccessfullyPlaced);
var floorObjectFailed = m_floorObject.Count(o => !o.SuccessfullyPlaced);
var anyHorizontalObjectFailed = m_anyHorizontalObject.Count(o => !o.SuccessfullyPlaced);
var totalFailed = againstWallObjectFailed + onWallObjectFailed + deskObjectFailed + floorObjectFailed +
anyHorizontalObjectFailed;
return totalFailed;
}
[ContextMenu("Confirm objects placement")]
public void ConfirmObjectPlacements()
{
CleanUp();
ConfirmObjectPlacementsCallback?.Invoke();
}
public void CleanUp()
{
foreach (var visualiserObject in m_spawnedVisualiserObjects)
{
Destroy(visualiserObject);
}
m_spawnedVisualiserObjects.Clear();
if (m_spawnUi != null)
{
m_spawnUi.SetActive(false);
}
}
[ContextMenu("Shuffle placements")]
public async void ShufflePlacements()
{
if (m_isInShuffleMode)
{
return;
}
await ShufflePlacementsAsync();
}
private async UniTask ShufflePlacementsAsync()
{
m_isInShuffleMode = true;
foreach (var visualiserObject in m_spawnedVisualiserObjects)
{
Destroy(visualiserObject);
}
m_spawnedVisualiserObjects.Clear();
await UniTask.Yield();
SceneUnderstandingReset();
await UniTask.Yield();
await FindFloorWallLocation();
await UniTask.Yield();
FindWallLocation();
await UniTask.Yield();
FindDeskLocation();
await UniTask.Yield();
FindFloorLocation();
await UniTask.Yield();
FindHorizontalLocation();
await UniTask.Yield();
RequestCollidersDisabled();
await UniTask.Yield();
RandomlyPlaceFailedObjects();
await UniTask.Yield();
ShowVisualizers();
m_isInShuffleMode = false;
}
public List<SceneObject> RequestObjects(LoadableSceneObjects objectType)
{
var returnObjects = new List<SceneObject>();
returnObjects.AddRange(m_againstWallObject.Where(o => o.DisplayPrefab.GetObjectType == objectType).ToList());
returnObjects.AddRange(m_onWallObject.Where(o => o.DisplayPrefab.GetObjectType == objectType).ToList());
returnObjects.AddRange(m_deskObject.Where(o => o.DisplayPrefab.GetObjectType == objectType).ToList());
returnObjects.AddRange(m_floorObject.Where(o => o.DisplayPrefab.GetObjectType == objectType).ToList());
returnObjects.AddRange(
m_anyHorizontalObject.Where(o => o.DisplayPrefab.GetObjectType == objectType).ToList());
return returnObjects;
}
public void UpdatePlacedObject(LoadableSceneObjects objectType, Vector3 mainPosition, Vector3 wallPosition,
Quaternion mainRotation, Quaternion wallRotation)
{
UpdateObject(ref m_againstWallObject, objectType, mainPosition, wallPosition, mainRotation, wallRotation);
UpdateObject(ref m_onWallObject, objectType, mainPosition, wallPosition, mainRotation, wallRotation);
UpdateObject(ref m_deskObject, objectType, mainPosition, wallPosition, mainRotation, wallRotation);
UpdateObject(ref m_floorObject, objectType, mainPosition, wallPosition, mainRotation, wallRotation);
UpdateObject(ref m_anyHorizontalObject, objectType, mainPosition, wallPosition, mainRotation, wallRotation);
}
private static void UpdateObject(ref List<SceneObject> objects, LoadableSceneObjects objectType, Vector3 mainPosition,
Vector3 wallPosition,
Quaternion mainRotation, Quaternion wallRotation)
{
for (var i = 0; i < objects.Count; i++)
{
var sceneObject = objects[i];
if (sceneObject.DisplayPrefab.GetObjectType == objectType)
{
sceneObject.WallPosition = wallPosition;
sceneObject.MainPosition = mainPosition;
sceneObject.WallRotation = wallRotation;
sceneObject.MainRotation = mainRotation;
objects[i] = sceneObject;
}
}
}
private void ShowVisualizers()
{
foreach (var visualizer in m_spawnedVisualiserObjects)
{
visualizer.SetActive(true);
}
}
private ObjectPlacementVisualiser InstantiateVisualizer(ref SceneObject sceneObject)
{
var objectPlacementVisualiser = Instantiate(sceneObject.DisplayPrefab);
GameObject go;
(go = objectPlacementVisualiser.gameObject).SetActive(false);
m_spawnedVisualiserObjects.Add(go);
sceneObject.SpawnedDisplayObject = objectPlacementVisualiser;
return objectPlacementVisualiser;
}
private async UniTask FindFloorWallLocation()
{
m_againstWallObject.Sort((l, r) => r.DisplayPrefab.GetWallObjectWidth.CompareTo(l.DisplayPrefab.GetWallObjectWidth));
for (var i = 0; i < m_againstWallObject.Count; i++)
{
var sceneObject = m_againstWallObject[i];
var successfullyPlaced = RequestRandomFloorWallLocation(ref sceneObject, 1.05f);
sceneObject.SuccessfullyPlaced = successfullyPlaced;
m_againstWallObject[i] = sceneObject;
if (sceneObject.DisplayPrefab == null)
{
Debug.LogError("Scene Object without object visualizer! Skip placement");
continue;
}
var objectPlacementVisualiser = InstantiateVisualizer(ref sceneObject);
objectPlacementVisualiser.Setup(
this, sceneObject.MainPosition, sceneObject.WallPosition, sceneObject.WallRotation);
m_againstWallObject[i] = sceneObject;
await UniTask.Yield();
}
}
private bool RequestRandomFloorWallLocation(ref SceneObject sceneObject, float sizeMultiplier)
{
return m_sceneUnderstandingLocationPlacer.RequestRandomFloorWallLocation(
sceneObject.DisplayPrefab.GetRadius * sizeMultiplier,
sceneObject.DisplayPrefab.GetWallObjectWidth * sizeMultiplier,
sceneObject.DisplayPrefab.GetWallObjectHeight,
out sceneObject.MainPosition,
out sceneObject.WallPosition,
out sceneObject.WallRotation,
sceneObject.DisplayPrefab.GetDistanceFromEdge);
}
private void FindWallLocation()
{
m_onWallObject.Sort((l, r) => r.DisplayPrefab.GetRadius.CompareTo(l.DisplayPrefab.GetRadius));
for (var i = 0; i < m_onWallObject.Count; i++)
{
var sceneObject = m_onWallObject[i];
var successfullyPlaced = m_sceneUnderstandingLocationPlacer.RequestRandomWallLocation(
sceneObject.DisplayPrefab.GetWallObjectHeight,
sceneObject.DisplayPrefab.GetWallObjectWidth,
sceneObject.DisplayPrefab.GetWallObjectVerticalSize,
sceneObject.DisplayPrefab.GetDistanceFromEdge,
ignoreSceneBlocked: false,
out sceneObject.MainPosition,
out sceneObject.WallRotation);
if (!successfullyPlaced)
{
// Try again but allow placing over scene blocked cells.
successfullyPlaced = m_sceneUnderstandingLocationPlacer.RequestRandomWallLocation(
sceneObject.DisplayPrefab.GetWallObjectHeight,
sceneObject.DisplayPrefab.GetWallObjectWidth,
sceneObject.DisplayPrefab.GetWallObjectVerticalSize,
sceneObject.DisplayPrefab.GetDistanceFromEdge,
ignoreSceneBlocked: true,
out sceneObject.MainPosition,
out sceneObject.WallRotation);
}
sceneObject.WallPosition = sceneObject.MainPosition;
sceneObject.SuccessfullyPlaced = successfullyPlaced;
m_onWallObject[i] = sceneObject;
if (sceneObject.DisplayPrefab == null)
{
Debug.LogError("Scene Object without object visualizer! Skip placement");
continue;
}
var objectPlacementVisualiser = InstantiateVisualizer(ref sceneObject);
objectPlacementVisualiser.Setup(
this, sceneObject.MainPosition, sceneObject.WallPosition, sceneObject.WallRotation);
m_onWallObject[i] = sceneObject;
}
}
private void FindDeskLocation()
{
m_deskObject.Sort((l, r) => r.DisplayPrefab.GetRadius.CompareTo(l.DisplayPrefab.GetRadius));
for (var i = 0; i < m_deskObject.Count; i++)
{
var sceneObject = m_deskObject[i];
var successfullyPlaced = RequestRandomDeskLocation(ref sceneObject, 1.05f);
if (!successfullyPlaced)
{
successfullyPlaced = RequestRandomDeskLocation(ref sceneObject, 0.5f);
}
if (!successfullyPlaced)
{
// failed to find any desk location looking for any horizontal location
successfullyPlaced = RequestRandomLocation(ref sceneObject, 1.05f);
}
sceneObject.SuccessfullyPlaced = successfullyPlaced;
sceneObject.WallPosition = sceneObject.MainPosition;
sceneObject.MainRotation = GetRotationTowardsUser(sceneObject.MainPosition, sceneObject.DisplayPrefab.GetFlipFaceDir);
m_deskObject[i] = sceneObject;
if (sceneObject.DisplayPrefab == null)
{
Debug.LogError("Scene Object without object visualizer! Skip placement");
continue;
}
var objectPlacementVisualiser = InstantiateVisualizer(ref sceneObject);
objectPlacementVisualiser.Setup(this, sceneObject.MainPosition);
m_deskObject[i] = sceneObject;
}
}
private bool RequestRandomDeskLocation(ref SceneObject sceneObject, float radiusMultiplier)
{
return m_sceneUnderstandingLocationPlacer.RequestRandomDeskLocation(GetUserPosition(),
sceneObject.DisplayPrefab.GetRadius * radiusMultiplier, out sceneObject.MainPosition);
}
private void FindFloorLocation()
{
m_floorObject.Sort((l, r) => r.DisplayPrefab.GetRadius.CompareTo(l.DisplayPrefab.GetRadius));
for (var i = 0; i < m_floorObject.Count; i++)
{
var sceneObject = m_floorObject[i];
var successfullyPlaced = RequestRandomFloorLocation(ref sceneObject, 1.05f);
if (!successfullyPlaced)
{
successfullyPlaced = RequestRandomFloorLocation(ref sceneObject, 0.5f);
}
if (!successfullyPlaced)
{
successfullyPlaced = RequestRandomFloorLocation(ref sceneObject, 0.01f);
}
sceneObject.WallPosition = sceneObject.MainPosition;
sceneObject.SuccessfullyPlaced = successfullyPlaced;
sceneObject.WallRotation = sceneObject.MainRotation;
m_floorObject[i] = sceneObject;
if (sceneObject.DisplayPrefab == null)
{
Debug.LogError("Scene Object without object visualizer! Skip placement");
continue;
}
var objectPlacementVisualiser = InstantiateVisualizer(ref sceneObject);
objectPlacementVisualiser.Setup(this, sceneObject.MainPosition, sceneObject.MainRotation);
m_floorObject[i] = sceneObject;
}
}
private bool RequestRandomFloorLocation(ref SceneObject sceneObject, float radiusMultiplier)
{
return m_sceneUnderstandingLocationPlacer.RequestRandomFloorLocation(GetUserPosition(),
sceneObject.DisplayPrefab.GetObjectDimensions * radiusMultiplier, out sceneObject.MainPosition, out sceneObject.MainRotation);
}
private void FindHorizontalLocation()
{
m_anyHorizontalObject.Sort((l, r) => r.DisplayPrefab.GetRadius.CompareTo(l.DisplayPrefab.GetRadius));
for (var i = 0; i < m_anyHorizontalObject.Count; i++)
{
var sceneObject = m_anyHorizontalObject[i];
var successfullyPlaced = RequestRandomLocation(ref sceneObject, 1.05f);
if (!successfullyPlaced)
{
successfullyPlaced = RequestRandomLocation(ref sceneObject, 0.5f);
}
if (!successfullyPlaced)
{
successfullyPlaced = RequestRandomLocation(ref sceneObject, 0.01f);
}
if (sceneObject.DisplayPrefab == null)
{
Debug.LogError("Scene Object without object visualizer! Skip placement");
continue;
}
sceneObject.WallPosition = sceneObject.MainPosition;
sceneObject.SuccessfullyPlaced = successfullyPlaced;
sceneObject.MainRotation = GetRotationTowardsUser(sceneObject.MainPosition, sceneObject.DisplayPrefab.GetFlipFaceDir);
m_anyHorizontalObject[i] = sceneObject;
var objectPlacementVisualiser = InstantiateVisualizer(ref sceneObject);
objectPlacementVisualiser.Setup(this, sceneObject.MainPosition);
m_anyHorizontalObject[i] = sceneObject;
}
}
private bool RequestRandomLocation(ref SceneObject sceneObject, float radiusMultiplier)
{
return m_sceneUnderstandingLocationPlacer.RequestRandomLocation(GetUserPosition(),
sceneObject.DisplayPrefab.GetRadius * radiusMultiplier, out sceneObject.MainPosition);
}
private void RequestCollidersDisabled()
{
m_sceneUnderstandingLocationPlacer.RequestCollidersDisabled();
}
private void SceneUnderstandingReset()
{
m_sceneUnderstandingLocationPlacer.ResetSceneUnderstanding();
}
private Quaternion GetRotationTowardsUser(Vector3 objectPosition, bool flipFaceDirection)
{
objectPosition.y = 0;
if (m_cameraRig == null)
{
m_cameraRig = FindObjectOfType<OVRCameraRigRef>();
}
var headsetPos = m_cameraRig.CameraRig.centerEyeAnchor.position;
headsetPos.y = 0;
var lookDir = flipFaceDirection ? objectPosition - headsetPos : headsetPos - objectPosition;
return Quaternion.LookRotation(lookDir.normalized, Vector3.up);
}
private Vector3 GetUserPosition()
{
if (m_cameraRig == null)
{
m_cameraRig = FindObjectOfType<OVRCameraRigRef>();
}
return m_cameraRig.CameraRig.centerEyeAnchor.position;
}
}
}