From 7a4c352b0dfed167023159fa0f83b1e9e0e90512 Mon Sep 17 00:00:00 2001 From: cbrown Date: Thu, 25 Apr 2024 13:31:00 -0400 Subject: [PATCH] Fixed QR Code Tracking --- .../Starter Assets/Scripts/ObjectSpawner.cs | 11 ++-- Assets/Scenes/Loading-Course.unity | 41 ++++++++---- Assets/Scripts/AR/TrackedImageChanged.cs | 66 +++++++++++++------ 3 files changed, 78 insertions(+), 40 deletions(-) diff --git a/Assets/Samples/XR Interaction Toolkit/2.5.2/Starter Assets/Scripts/ObjectSpawner.cs b/Assets/Samples/XR Interaction Toolkit/2.5.2/Starter Assets/Scripts/ObjectSpawner.cs index 666c9070..c1ab3c5e 100644 --- a/Assets/Samples/XR Interaction Toolkit/2.5.2/Starter Assets/Scripts/ObjectSpawner.cs +++ b/Assets/Samples/XR Interaction Toolkit/2.5.2/Starter Assets/Scripts/ObjectSpawner.cs @@ -251,6 +251,11 @@ namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets var objectIndex = isSpawnOptionRandomized ? Random.Range(0, m_ObjectPrefabs.Count) : m_SpawnOptionIndex; var newObject = Instantiate(m_ObjectPrefabs[objectIndex]); + // var facePosition = m_CameraToFace.transform.position; + // var forward = facePosition - spawnPoint; + // BurstMathUtility.ProjectOnPlane(forward, , out var projectedForward); + // newObject.transform.rotation = Quaternion.LookRotation(projectedForward, Vector3.up); + // if (m_SpawnAsChildren || space == Space.Self) { newObject.transform.parent = transform; @@ -263,12 +268,6 @@ namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets newObject.transform.rotation = spawnRotation; } - if (m_ApplyRandomAngleAtSpawn) - { - var randomRotation = Random.Range(-m_SpawnAngleRange, m_SpawnAngleRange); - newObject.transform.Rotate(Vector3.up, randomRotation); - } - objectSpawned?.Invoke(newObject); return true; } diff --git a/Assets/Scenes/Loading-Course.unity b/Assets/Scenes/Loading-Course.unity index 0037c646..28ee5fca 100644 --- a/Assets/Scenes/Loading-Course.unity +++ b/Assets/Scenes/Loading-Course.unity @@ -192,6 +192,11 @@ PrefabInstance: propertyPath: m_Name value: XR Origin (AR Rig) objectReference: {fileID: 0} + - target: {fileID: 2512387470528047737, guid: 48cb3fb68c91eb94999fd99957eb0cae, + type: 3} + propertyPath: m_DetectionMode + value: 1 + objectReference: {fileID: 0} - target: {fileID: 2512387470528047738, guid: 48cb3fb68c91eb94999fd99957eb0cae, type: 3} propertyPath: m_LocalEulerAnglesHint.x @@ -209,11 +214,15 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] - m_AddedGameObjects: [{targetCorrespondingSourceObject: {fileID: 2512387469002778308, - guid: 48cb3fb68c91eb94999fd99957eb0cae, type: 3}, insertIndex: -1, addedObject: { - fileID: 1103989921}}, {targetCorrespondingSourceObject: {fileID: 2512387470528047738, - guid: 48cb3fb68c91eb94999fd99957eb0cae, type: 3}, insertIndex: -1, addedObject: { - fileID: 573055497}}] + m_AddedGameObjects: + - targetCorrespondingSourceObject: {fileID: 2512387470528047738, guid: 48cb3fb68c91eb94999fd99957eb0cae, + type: 3} + insertIndex: -1 + addedObject: {fileID: 573055497} + - targetCorrespondingSourceObject: {fileID: 2512387469002778308, guid: 48cb3fb68c91eb94999fd99957eb0cae, + type: 3} + insertIndex: -1 + addedObject: {fileID: 1103989921} m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 48cb3fb68c91eb94999fd99957eb0cae, type: 3} --- !u!4 &331264286 stripped @@ -342,6 +351,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 2178f015d10c24dc7a75d7f4ad028733, type: 3} m_Name: m_EditorClassIdentifier: + planeManager: {fileID: 1037826357} trackedImageManager: {fileID: 1077107346} objectSpawner: {fileID: 436358033} trackedImageManagerEnabled: @@ -350,6 +360,7 @@ MonoBehaviour: _constant: {fileID: 0} _variable: {fileID: 11400000, guid: a4a6ccb6b3f244b0ca8da90699997ce5, type: 2} _variableInstancer: {fileID: 0} + isOneShot: 1 --- !u!1 &573055496 GameObject: m_ObjectHideFlags: 0 @@ -610,12 +621,6 @@ MonoBehaviour: m_LightCookieSize: {x: 1, y: 1} m_LightCookieOffset: {x: 0, y: 0} m_SoftShadowQuality: 0 ---- !u!4 &1103989921 stripped -Transform: - m_CorrespondingSourceObject: {fileID: 5662389616791750665, guid: abfb530320bd04a15b3bb76f4510e2f0, - type: 3} - m_PrefabInstance: {fileID: 2031524719} - m_PrefabAsset: {fileID: 0} --- !u!4 &1077107342 stripped Transform: m_CorrespondingSourceObject: {fileID: 2512387470528047738, guid: 48cb3fb68c91eb94999fd99957eb0cae, @@ -634,6 +639,12 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 4dea56a9eaadd1c439095de4f9a371cb, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!4 &1103989921 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 5662389616791750665, guid: abfb530320bd04a15b3bb76f4510e2f0, + type: 3} + m_PrefabInstance: {fileID: 2031524719} + m_PrefabAsset: {fileID: 0} --- !u!1 &1323550131 GameObject: m_ObjectHideFlags: 0 @@ -1466,9 +1477,11 @@ PrefabInstance: m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [{targetCorrespondingSourceObject: {fileID: 2515407931999965833, - guid: 3ccc2f7c888da2b4c9997752a4d80cc2, type: 3}, insertIndex: -1, addedObject: { - fileID: 1345703131}}] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 2515407931999965833, guid: 3ccc2f7c888da2b4c9997752a4d80cc2, + type: 3} + insertIndex: -1 + addedObject: {fileID: 1345703131} m_SourcePrefab: {fileID: 100100000, guid: 3ccc2f7c888da2b4c9997752a4d80cc2, type: 3} --- !u!1001 &9054095629342217002 PrefabInstance: diff --git a/Assets/Scripts/AR/TrackedImageChanged.cs b/Assets/Scripts/AR/TrackedImageChanged.cs index 2a3bb4d9..c421d431 100644 --- a/Assets/Scripts/AR/TrackedImageChanged.cs +++ b/Assets/Scripts/AR/TrackedImageChanged.cs @@ -2,6 +2,7 @@ using Unity.Logging; using UnityAtoms.BaseAtoms; using UnityEngine; using UnityEngine.XR.ARFoundation; +using UnityEngine.XR.ARSubsystems; namespace MAVRIC.GEEKCup { @@ -9,9 +10,11 @@ namespace MAVRIC.GEEKCup public class TrackedImageChanged : MonoBehaviour { + [SerializeField] private ARPlaneManager planeManager; [SerializeField] private ARTrackedImageManager trackedImageManager; [SerializeField] private ObjectSpawner objectSpawner; [SerializeField] private BoolReference trackedImageManagerEnabled; + [SerializeField] private bool isOneShot; private void Awake() { @@ -24,6 +27,15 @@ namespace MAVRIC.GEEKCup } } + if (planeManager == null) + { + if (!TryGetComponent(out planeManager)) + { + Log.Error("Failed to find ARPlaneManager...", this.gameObject); + enabled = false; + } + } + if (objectSpawner == null) { if (!TryGetComponent(out objectSpawner)) @@ -38,42 +50,40 @@ namespace MAVRIC.GEEKCup { trackedImageManager.trackedImagesChanged += OnTrackedImagesChanged; - objectSpawner.objectSpawned += UpdateObjectSpawned; + // objectSpawner.objectSpawned += UpdateObjectSpawned; } private void OnDisable() { trackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged; - objectSpawner.objectSpawned -= UpdateObjectSpawned; + // objectSpawner.objectSpawned -= UpdateObjectSpawned; } - private void UpdateObjectSpawned(GameObject obj) - { - if (obj == null) return; - - var position = obj.transform.localPosition; - - position.y = 0f; - obj.transform.localPosition = position; - } + // private void UpdateObjectSpawned(GameObject obj) + // { + // if (obj == null) return; + // + // var position = obj.transform.localPosition; + // + // position.y = 0f; + // obj.transform.localPosition = position; + // } private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs obj) { foreach (var trackedImage in obj.added) { - if (AttachTrackedImage(trackedImage)) - { - OneShot(); - } + if (!AttachTrackedImage(trackedImage)) continue; + + OneShot(isOneShot); } foreach (var trackedImage in obj.updated) { - if (AttachTrackedImage(trackedImage)) - { - OneShot(); - } + if (!AttachTrackedImage(trackedImage)) continue; + + OneShot(isOneShot); } } @@ -84,12 +94,28 @@ namespace MAVRIC.GEEKCup var position = trackedImage.transform.position; var rotation = trackedImage.transform.rotation; + + foreach (var plane in planeManager.trackables) + { + if (plane == null) continue; + + if (plane.alignment != PlaneAlignment.HorizontalUp) + { + Log.Error("Plane is not HorizontalUp...", this.gameObject); + continue; + } + + var planePosition = plane.transform.position; + position.y = planePosition.y; + } return objectSpawner.TrySpawnObject(position, rotation, Space.Self); } - private void OneShot() + private void OneShot(bool isOneShot) { + if (!isOneShot) return; + trackedImageManager.enabled = false; } }