1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-22 15:18:25 -05:00

Added plane anchoring logic

This commit is contained in:
Noah 2024-04-18 05:48:36 -04:00
parent eb64e39a12
commit fc2790879f
4 changed files with 3279 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: abd4cb1ebafa649ceb66bfc0997721a8
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,86 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
namespace MAVRIC.GEEKCup
{
public class ImageTrackingObjectPlacer : MonoBehaviour
{
[SerializeField] private ARTrackedImageManager trackedImageManager;
[SerializeField] private ARRaycastManager raycastManager;
[SerializeField] private ARPlaneManager planeManager;
[SerializeField] private ARAnchorManager anchorManager;
[SerializeField] private GameObject objectToPlace;
void OnEnable()
{
trackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
CheckForAnchor();
ManagerCheck();
}
void OnDisable()
{
trackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
}
void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (var trackedImage in eventArgs.added)
{
AttachObjectToAnchor(trackedImage);
}
foreach (var trackedImage in eventArgs.updated)
{
AttachObjectToAnchor(trackedImage);
}
}
private void AttachObjectToAnchor(ARTrackedImage trackedImage)
{
Vector3 imagePosition = trackedImage.transform.position;
List<ARRaycastHit> hits = new List<ARRaycastHit>();
if (raycastManager.Raycast(imagePosition, hits, TrackableType.PlaneWithinPolygon))
{
var hit = hits[0];
if (hit.trackable is ARPlane plane)
{
anchorManager.anchorPrefab = objectToPlace;
anchorManager.AttachAnchor(plane, hit.pose);
}
}
}
private void CheckForAnchor()
{
if (objectToPlace.GetComponent<ARAnchor>() == null)
{
objectToPlace.AddComponent<ARAnchor>();
}
}
private void ManagerCheck()
{
if (trackedImageManager == null)
{
Debug.LogError("Failed to find ARTrackedImageManager...", this.gameObject);
}
if (raycastManager == null)
{
Debug.LogError("Failed to find ARRaycastManager...", this.gameObject);
}
if (planeManager == null)
{
Debug.LogError("Failed to find ARPlaneManager...", this.gameObject);
}
if (anchorManager == null)
{
Debug.LogError("Failed to find ARAnchorManager...", this.gameObject);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 23b32f19b85b44ce1932172e609a1961
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: