-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathARPlaneAnchor.cs
177 lines (136 loc) · 4.73 KB
/
ARPlaneAnchor.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Runtime.InteropServices;
namespace UnityEngine.XR.iOS
{
public struct UnityARPlaneGeometry
{
public int vertexCount;
public IntPtr vertices;
public int textureCoordinateCount;
public IntPtr textureCoordinates;
public int triangleCount;
public IntPtr triangleIndices;
public int boundaryVertexCount;
public IntPtr boundaryVertices;
}
public struct UnityARAnchorData
{
public IntPtr ptrIdentifier;
/**
The transformation matrix that defines the anchor's rotation, translation and scale in world coordinates.
*/
public UnityARMatrix4x4 transform;
/**
The alignment of the plane.
*/
public ARPlaneAnchorAlignment alignment;
/**
The center of the plane in the anchor’s coordinate space.
*/
public Vector4 center;
/**
The extent of the plane in the anchor’s coordinate space.
*/
public Vector4 extent;
/**
The geometry that describes more accurately the surface found.
*/
public UnityARPlaneGeometry planeGeometry;
}
#if !UNITY_EDITOR
public class ARPlaneGeometry
{
private UnityARPlaneGeometry uPlaneGeometry;
public ARPlaneGeometry (UnityARPlaneGeometry upg)
{
uPlaneGeometry = upg;
}
public int vertexCount { get { return uPlaneGeometry.vertexCount; } }
public int triangleCount { get { return uPlaneGeometry.triangleCount; } }
public int textureCoordinateCount { get { return uPlaneGeometry.textureCoordinateCount; } }
public int boundaryVertexCount { get { return uPlaneGeometry.boundaryVertexCount; } }
public Vector3 [] vertices { get { return MarshalVertices(uPlaneGeometry.vertices,vertexCount); } }
public Vector3 [] boundaryVertices { get { return MarshalVertices(uPlaneGeometry.boundaryVertices,boundaryVertexCount); } }
public Vector2 [] textureCoordinates { get { return MarshalTexCoords(uPlaneGeometry.textureCoordinates, textureCoordinateCount); } }
public int [] triangleIndices { get { return MarshalIndices(uPlaneGeometry.triangleIndices, triangleCount); } }
Vector3 [] MarshalVertices(IntPtr ptrFloatArray, int vertCount)
{
int numFloats = vertCount * 4;
float [] workVerts = new float[numFloats];
Marshal.Copy (ptrFloatArray, workVerts, 0, (int)(numFloats));
Vector3[] verts = new Vector3[vertCount];
for (int count = 0; count < numFloats; count++)
{
verts [count / 4].x = workVerts[count++];
verts [count / 4].y = workVerts[count++];
verts [count / 4].z = -workVerts[count++];
}
return verts;
}
int [] MarshalIndices(IntPtr ptrIndices, int triCount)
{
int numIndices = triCount * 3;
short [] workIndices = new short[numIndices]; //since ARKit returns Int16
Marshal.Copy (ptrIndices, workIndices, 0, numIndices);
int[] triIndices = new int[numIndices];
for (int count = 0; count < numIndices; count+=3) {
//reverse winding order
triIndices [count] = workIndices [count];
triIndices [count + 1] = workIndices [count + 2];
triIndices [count + 2] = workIndices [count + 1];
}
return triIndices;
}
Vector2 [] MarshalTexCoords(IntPtr ptrTexCoords, int texCoordCount)
{
int numFloats = texCoordCount * 2;
float [] workTexCoords = new float[numFloats];
Marshal.Copy (ptrTexCoords, workTexCoords, 0, (int)(numFloats));
Vector2[] texCoords = new Vector2[texCoordCount];
for (int count = 0; count < numFloats; count++)
{
texCoords [count / 2].x = workTexCoords[count++];
texCoords [count / 2].y = workTexCoords[count];
}
return texCoords;
}
}
public class ARPlaneAnchor
{
private UnityARAnchorData planeAnchorData;
public string identifierStr { get; }
public string identifier { get { return identifierStr; } }
public ARPlaneAnchor (UnityARAnchorData ufad)
{
planeAnchorData = ufad;
identifierStr = Marshal.PtrToStringAuto(planeAnchorData.ptrIdentifier);
}
public Matrix4x4 transform {
get {
Matrix4x4 matrix = new Matrix4x4 ();
matrix.SetColumn (0, planeAnchorData.transform.column0);
matrix.SetColumn (1, planeAnchorData.transform.column1);
matrix.SetColumn (2, planeAnchorData.transform.column2);
matrix.SetColumn (3, planeAnchorData.transform.column3);
return matrix;
}
}
public ARPlaneAnchorAlignment alignment {
get {
return planeAnchorData.alignment;
}
}
public Vector3 extent {
get {
return new Vector3 (planeAnchorData.extent.x, planeAnchorData.extent.y, planeAnchorData.extent.z);
}
}
public Vector3 center {
get {
return new Vector3 (planeAnchorData.center.x, planeAnchorData.center.y, planeAnchorData.center.z);
}
}
public ARPlaneGeometry planeGeometry { get { return new ARPlaneGeometry (planeAnchorData.planeGeometry); } }
}
#endif
}