forked from lobrien/ARKit_CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
248 lines (201 loc) · 7.39 KB
/
Program.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using UIKit;
using SceneKit;
using ARKit;
using Foundation;
using System;
using CoreGraphics;
using System.Linq;
using OpenTK;
using Xamarin.Essentials;
using ARKit_Csharp;
namespace ARKitDemo
{
public class ARDelegate : ARSCNViewDelegate
{
public event EventHandler<ImageDetection> ImageDetected;
public override void DidAddNode(ISCNSceneRenderer renderer, SCNNode node, ARAnchor anchor)
{
if (anchor != null)
{
if (anchor is ARPlaneAnchor)
PlaceAnchorNode(node, anchor as ARPlaneAnchor);
else if (anchor is ARImageAnchor)
PlaceMarkerCube(node, anchor as ARImageAnchor);
}
}
private void PlaceMarkerCube(SCNNode node, ARImageAnchor aRImageAnchor)
{
ImageDetected?.Invoke(this, new ImageDetection
{
Anchor = aRImageAnchor,
});
//var plane = SCNPlane.Create(aRImageAnchor.ReferenceImage.PhysicalSize.Width, aRImageAnchor.ReferenceImage.PhysicalSize.Height);
//var planeNode = SCNNode.FromGeometry(plane);
//planeNode.Position = new SCNVector3(aRImageAnchor.Transform.Column3.X, aRImageAnchor.Transform.Column3.Y,aRImageAnchor.Transform.Column3.Z);
//planeNode.Transform = SCNMatrix4.CreateRotationX((float)(Math.PI / 2.0));
//node.AddChildNode(planeNode);
////Mark the anchor with a small red box
//var box = new SCNBox { Height = 0.5f, Width = 0.5f, Length =0.5f };
//box.FirstMaterial.Diffuse.ContentColor = UIColor.Green;
//var anchorNode = new SCNNode { Position = new SCNVector3(0, 0, 0), Geometry = box };
//planeNode.AddChildNode(anchorNode);
}
void PlaceAnchorNode(SCNNode node, ARPlaneAnchor anchor)
{
var plane = SCNPlane.Create(anchor.Extent.X, anchor.Extent.Z);
plane.FirstMaterial.Diffuse.Contents = UIColor.LightGray;
var planeNode = SCNNode.FromGeometry(plane);
//Locate the plane at the position of the anchor
planeNode.Position = new SCNVector3(anchor.Extent.X, 0.0f, anchor.Extent.Z);
//Rotate it to lie flat
planeNode.Transform = SCNMatrix4.CreateRotationX((float) (Math.PI / 2.0));
node.AddChildNode(planeNode);
//Mark the anchor with a small red box
var box = new SCNBox { Height = 0.18f, Width = 0.18f, Length = 0.18f };
box.FirstMaterial.Diffuse.ContentColor = UIColor.Red;
var anchorNode = new SCNNode { Position = new SCNVector3(0, 0, 0), Geometry = box };
planeNode.AddChildNode(anchorNode);
}
public override void DidUpdateNode(ISCNSceneRenderer renderer, SCNNode node, ARAnchor anchor)
{
if (anchor is ARPlaneAnchor)
{
var planeAnchor = anchor as ARPlaneAnchor;
//BUG: Extent.Z should be at least a few dozen centimeters
System.Console.WriteLine($"The (updated) extent of the anchor is [{planeAnchor.Extent.X}, {planeAnchor.Extent.Y}, {planeAnchor.Extent.Z}]");
}
}
}
public class ARKitController : UIViewController
{
ARSCNView scnView;
public ARKitController() : base()
{
}
public override bool ShouldAutorotate() => true;
public override void ViewDidLoad()
{
base.ViewDidLoad();
var arDel = new ARDelegate();
arDel.ImageDetected += ArDel_ImageDetected;
scnView = new ARSCNView()
{
Frame = this.View.Frame,
Delegate = arDel,
DebugOptions = ARSCNDebugOptions.ShowFeaturePoints | ARSCNDebugOptions.ShowWorldOrigin,
UserInteractionEnabled = true
};
this.View.AddSubview(scnView);
}
private void ArDel_ImageDetected(object sender, ImageDetection e)
{
Vibration.Vibrate();
var v = new SCNVector3(
e.Anchor.Transform.Column3.X,
e.Anchor.Transform.Column3.Y,
e.Anchor.Transform.Column3.Z);
PlaceCube(v);
scnView.Session.RemoveAnchor(e.Anchor);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var images = GetDetectionImages();
// Configure ARKit
var config = new ARWorldTrackingConfiguration();
config.PlaneDetection = ARPlaneDetection.Horizontal;
config.DetectionImages = images;
// This method is called subsequent to `ViewDidLoad` so we know `scnView` is instantiated
scnView.Session.Run(config, ARSessionRunOptions.RemoveExistingAnchors);
}
/// <summary>
/// Images that the app will detect
/// </summary>
/// <returns></returns>
private NSSet<ARReferenceImage> GetDetectionImages()
{
var i = UIImage.FromBundle("marker_image").CGImage;
var r = new ARReferenceImage(i, ImageIO.CGImagePropertyOrientation.Up, 0.07f);
var set = new NSSet<ARReferenceImage>(r);
return set;
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
var touch = touches.AnyObject as UITouch;
if (touch != null)
{
var loc = touch.LocationInView(scnView);
var worldPos = WorldPositionFromHitTest(loc);
if (worldPos.Item1.HasValue)
{
PlaceCube(worldPos.Item1.Value);
}
}
}
private SCNVector3 PositionFromTransform(NMatrix4 xform)
{
return new SCNVector3(xform.M14, xform.M24, xform.M34);
}
Tuple<SCNVector3?, ARAnchor> WorldPositionFromHitTest (CGPoint pt)
{
//Hit test against existing anchors
var hits = scnView.HitTest(pt, ARHitTestResultType.ExistingPlaneUsingExtent);
if (hits != null && hits.Length > 0)
{
var anchors = hits.Where(r => r.Anchor is ARPlaneAnchor);
if (anchors.Count() > 0)
{
var first = anchors.First();
var pos = PositionFromTransform(first.WorldTransform);
return new Tuple<SCNVector3?, ARAnchor>(pos, (ARPlaneAnchor)first.Anchor);
}
}
return new Tuple<SCNVector3?, ARAnchor>(null, null);
}
private SCNMaterial[] LoadMaterials()
{
Func<string, SCNMaterial> LoadMaterial = fname =>
{
var mat = new SCNMaterial();
mat.Diffuse.Contents = UIImage.FromFile(fname);
//mat.Diffuse.ContentColor = UIColor.Green;
mat.LocksAmbientWithDiffuse = true;
mat.Transparency = 0.6f;
mat.TransparencyMode = SCNTransparencyMode.SingleLayer;
return mat;
};
var a = LoadMaterial("msft_logo.png");
var b = LoadMaterial("xamagon.png");
var c = LoadMaterial("fsharp.png"); // This demo was originally in F# :-)
return new[] { a, b, a, b, c, c };
}
SCNNode PlaceCube(SCNVector3 pos)
{
var box = new SCNBox { Width = 0.25f, Height = 0.25f, Length = 0.25f };
var cubeNode = new SCNNode { Position = pos, Geometry = box };
cubeNode.Geometry.Materials = LoadMaterials();
scnView.Scene.RootNode.AddChildNode(cubeNode);
return cubeNode;
}
}
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = new ARKitController();
window.MakeKeyAndVisible ();
return true;
}
}
public class Application
{
static void Main (string [] args)
{
UIApplication.Main (args, null, "AppDelegate");
}
}
}