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:
parent
eb64e39a12
commit
fc2790879f
3175
Assets/Scenes/LoadingAndAnchoring-Courses.unity
Normal file
3175
Assets/Scenes/LoadingAndAnchoring-Courses.unity
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/Scenes/LoadingAndAnchoring-Courses.unity.meta
Normal file
7
Assets/Scenes/LoadingAndAnchoring-Courses.unity.meta
Normal file
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: abd4cb1ebafa649ceb66bfc0997721a8
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
86
Assets/Scripts/ImageTrackingObjectPlacer.cs
Normal file
86
Assets/Scripts/ImageTrackingObjectPlacer.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/ImageTrackingObjectPlacer.cs.meta
Normal file
11
Assets/Scripts/ImageTrackingObjectPlacer.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23b32f19b85b44ce1932172e609a1961
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user