forked from Unity-Technologies/arfoundation-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnchorCreatorV2.cs
143 lines (117 loc) · 5.1 KB
/
AnchorCreatorV2.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
using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine.InputSystem;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARFoundation.Samples
{
[RequireComponent(typeof(ARAnchorManager))]
[RequireComponent(typeof(ARRaycastManager))]
public class AnchorCreatorV2 : MonoBehaviour
{
[SerializeField]
[Tooltip("The Input Action References to use. You can create this by right clicking in the Project Window " +
"and going to <b>XR</b> > AR Foundation > Input Action References.")]
InputActionReferences m_InputActionReferences;
[SerializeField]
[Tooltip("The type of trackable the raycast will hit.")]
TrackableType m_TrackableType = TrackableType.PlaneWithinPolygon;
static Ray s_RaycastRay;
static readonly List<ARRaycastHit> s_Hits = new();
readonly List<ARAnchor> m_Anchors = new();
ARRaycastManager m_RaycastManager;
ARAnchorManager m_AnchorManager;
[SerializeField]
GameObject m_Prefab;
public GameObject prefab
{
get => m_Prefab;
set => m_Prefab = value;
}
public void RemoveAllAnchors()
{
Logger.Log($"Removing all anchors ({m_Anchors.Count})");
foreach (var anchor in m_Anchors)
{
// It's possible for ARSession.Reset() to invalidate anchors outside of this,
// so the additional checks will skip deleted anchors/anchor GOs.
if (anchor != null && anchor.gameObject != null)
Destroy(anchor.gameObject);
}
m_Anchors.Clear();
}
void OnEnable()
{
if (m_InputActionReferences == null)
return;
if (m_InputActionReferences.rightTriggerPressed.action != null)
m_InputActionReferences.rightTriggerPressed.action.performed += RightTriggerPressed;
if (m_InputActionReferences.leftTriggerPressed.action != null)
m_InputActionReferences.leftTriggerPressed.action.performed += LeftTriggerPressed;
}
void OnDisable()
{
if (m_InputActionReferences == null)
return;
if (m_InputActionReferences.rightTriggerPressed.action != null)
m_InputActionReferences.rightTriggerPressed.action.performed -= RightTriggerPressed;
if (m_InputActionReferences.leftTriggerPressed.action != null)
m_InputActionReferences.leftTriggerPressed.action.performed -= LeftTriggerPressed;
}
void Awake()
{
m_RaycastManager = GetComponent<ARRaycastManager>();
m_AnchorManager = GetComponent<ARAnchorManager>();
if (m_AnchorManager.subsystem != null)
return;
enabled = false;
Debug.LogWarning($"No active XRAnchorSubsystem is available, so {typeof(AnchorCreator).FullName} will not be enabled.");
}
static void SetAnchorText(ARAnchor anchor, string text)
{
var canvasTextManager = anchor.GetComponent<CanvasTextManager>();
if (canvasTextManager)
{
canvasTextManager.text = text;
}
}
void RightTriggerPressed(InputAction.CallbackContext context)
{
RaycastFromHandPose(new Pose(
m_InputActionReferences.rightHandPosition.action.ReadValue<Vector3>(),
m_InputActionReferences.rightHandRotation.action.ReadValue<Quaternion>()));
}
void LeftTriggerPressed(InputAction.CallbackContext context)
{
RaycastFromHandPose(new Pose(
m_InputActionReferences.leftHandPosition.action.ReadValue<Vector3>(),
m_InputActionReferences.leftHandRotation.action.ReadValue<Quaternion>()));
}
void RaycastFromHandPose(Pose handPose)
{
s_RaycastRay = new Ray(handPose.position, handPose.forward);
if (m_RaycastManager.Raycast(s_RaycastRay, s_Hits, m_TrackableType))
{
CreateAnchor(s_Hits[0]);
}
}
void CreateAnchor(in ARRaycastHit hit)
{
ARAnchor anchor;
Logger.Log("Creating regular anchor.");
if (m_Prefab != null)
{
// Note: the anchor can be anywhere in the scene hierarchy
var anchorGameObject = Instantiate(m_Prefab, hit.pose.position, hit.pose.rotation);
// Make sure the new GameObject has an ARAnchor component
anchor = ComponentUtils.GetOrAddIf<ARAnchor>(anchorGameObject, true);
}
else
{
var anchorGameObject = new GameObject("Anchor");
anchorGameObject.transform.SetPositionAndRotation(hit.pose.position, hit.pose.rotation);
anchor = anchorGameObject.AddComponent<ARAnchor>();
}
SetAnchorText(anchor, $"Anchor (from {hit.hitType})");
}
}
}