ARCoreRaycastSubsystem.cs
4.43 KB
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
using System.Runtime.InteropServices;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
using Unity.Collections;
namespace UnityEngine.XR.ARCore
{
/// <summary>
/// ARCore implementation of the <c>XRRaycastSubsystem</c>. Do not create this directly. Use the <c>SubsystemManager</c> instead.
/// </summary>
[Preserve]
public sealed class ARCoreRaycastSubsystem : XRRaycastSubsystem
{
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Creates the ARCore-specific implementation which will service the `XRRaycastSubsystem`.
/// </summary>
/// <returns>A new instance of the `Provider` specific to ARCore.</returns>
protected override Provider CreateProvider() => new ARCoreProvider();
#endif
class ARCoreProvider : Provider
{
public override unsafe NativeArray<XRRaycastHit> Raycast(
XRRaycastHit defaultRaycastHit,
Ray ray,
TrackableType trackableTypeMask,
Allocator allocator)
{
void* hitBuffer;
int hitCount, elementSize;
UnityARCore_raycast_acquireHitResultsRay(
ray.origin,
ray.direction,
trackableTypeMask,
out hitBuffer,
out hitCount,
out elementSize);
try
{
return NativeCopyUtility.PtrToNativeArrayWithDefault<XRRaycastHit>(
defaultRaycastHit,
hitBuffer, elementSize,
hitCount, allocator);
}
finally
{
UnityARCore_raycast_releaseHitResults(hitBuffer);
}
}
public override unsafe NativeArray<XRRaycastHit> Raycast(
XRRaycastHit defaultRaycastHit,
Vector2 screenPoint,
TrackableType trackableTypeMask,
Allocator allocator)
{
void* hitBuffer;
int hitCount, elementSize;
UnityARCore_raycast_acquireHitResults(
screenPoint,
trackableTypeMask,
out hitBuffer,
out hitCount,
out elementSize);
try
{
return NativeCopyUtility.PtrToNativeArrayWithDefault<XRRaycastHit>(
defaultRaycastHit,
hitBuffer, elementSize,
hitCount, allocator);
}
finally
{
UnityARCore_raycast_releaseHitResults(hitBuffer);
}
}
[DllImport("UnityARCore")]
static unsafe extern void UnityARCore_raycast_acquireHitResults(
Vector2 screenPoint,
TrackableType filter,
out void* hitBuffer,
out int hitCount,
out int elementSize);
[DllImport("UnityARCore")]
static unsafe extern void UnityARCore_raycast_acquireHitResultsRay(
Vector3 rayOrigin,
Vector3 rayDirection,
TrackableType filter,
out void* hitBuffer,
out int hitCount,
out int elementSize);
[DllImport("UnityARCore")]
static unsafe extern void UnityARCore_raycast_releaseHitResults(
void* buffer);
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void RegisterDescriptor()
{
#if UNITY_ANDROID && !UNITY_EDITOR
XRRaycastSubsystemDescriptor.RegisterDescriptor(new XRRaycastSubsystemDescriptor.Cinfo
{
id = "ARCore-Raycast",
#if UNITY_2020_2_OR_NEWER
providerType = typeof(ARCoreRaycastSubsystem.ARCoreProvider),
subsystemTypeOverride = typeof(ARCoreRaycastSubsystem),
#else
subsystemImplementationType = typeof(ARCoreRaycastSubsystem),
#endif
supportsViewportBasedRaycast = true,
supportsWorldBasedRaycast = true,
supportedTrackableTypes =
(TrackableType.Planes & ~TrackableType.PlaneWithinInfinity) |
TrackableType.FeaturePoint
});
#endif
}
}
}