VisualStudio: 8.8.4, Xamarin.iOS: 14.6.
Test Device: iPhone 12 Pro.
I am developing an App with AR features. But I can get nothing when hittest or raycast in ARSCNView.
Following is the key code, the values of 'hitTest' and 'rayCastResult' in function 'PerformHitTest' are always empty collection.
RunSession and PauseSession functions are invoked in another partial class file. 'SessionDidAddAnchors', 'SessionDidUpdateAnchor' and 'SessionDidRemoveAnchor' functions work well.
Someone knows what I am missing?
using System;
using ARKit;
using CoreFoundation;
using CoreGraphics;
using Foundation;
using SceneKit;
using UIKit;
namespace ARTestApplication
{
public partial class ViewController : IARSessionDelegate, IARSCNViewDelegate
{
private void SetupSceneView()
{
_session.Delegate = this;
_sceneView.Session = _session;
_sceneView.Delegate = this;
_sceneView.PreferredFramesPerSecond = 60;
}
private void RunSession()
{
var detection = UIDevice.CurrentDevice.CheckSystemVersion(11, 0) ?
ARPlaneDetection.Horizontal | ARPlaneDetection.Vertical :
ARPlaneDetection.Horizontal;
var configuration = new ARWorldTrackingConfiguration
{
PlaneDetection = detection
};
_session.Run(configuration, ARSessionRunOptions.RemoveExistingAnchors | ARSessionRunOptions.ResetTracking);
}
private void PauseSession()
{
_session.Pause();
}
private void PerformHitTest()
{
var screenCenter = new CGPoint(_sceneView.Bounds.GetMidX(), _sceneView.Bounds.GetMidY());
Console.WriteLine($"screenCenter: {screenCenter}");
var hitTest = _sceneView.HitTest(screenCenter, ARHitTestResultType.ExistingPlane);
Console.WriteLine($"hitTest: {hitTest}");
var query = _sceneView.CreateRaycastQuery(screenCenter, ARRaycastTarget.EstimatedPlane, ARRaycastTargetAlignment.Any);
if (null != query)
{
var rayCastResult = _sceneView.Session.Raycast(query);
Console.WriteLine($"rayCastResult: {rayCastResult}");
}
}
#region IARSessionDelegate
[Export("session:didAddAnchors:")]
public void SessionDidAddAnchors(ARSession session, ARAnchor[] anchors)
{
Console.WriteLine("SessionDidAddAnchors");
}
[Export("session:didUpdateAnchors:")]
public void SessionDidUpdateAnchor(ARSession session, ARAnchor[] anchors)
{
Console.WriteLine("SessionDidUpdateAnchor");
}
[Export("session:didRemoveAnchors:")]
public void SessionDidRemoveAnchor(ARSession session, ARAnchor[] anchors)
{
Console.WriteLine("SessionDidRemoveAnchor");
}
#endregion
#region IARSCNViewDelegate
[Export("renderer:didAddNode:forAnchor:")]
public void DidAddNode(ISCNSceneRenderer renderer, SceneKit.SCNNode node, ARAnchor anchor)
{
}
[Export("renderer:didUpdateNode:forAnchor:")]
public void DidUpdateNode(ISCNSceneRenderer renderer, SceneKit.SCNNode node, ARAnchor anchor)
{
}
[Export("renderer:didRemoveNode:forAnchor:")]
public void DidRemoveNode(ISCNSceneRenderer renderer, SceneKit.SCNNode node, ARAnchor anchor)
{
}
[Export("renderer:updateAtTime:")]
public void RendererUpdateAtTime(SCNSceneRenderer renderer, double updateAtTime)
{
Console.WriteLine("RendererUpdateAtTime");
DispatchQueue.MainQueue.DispatchAsync(() =>
{
PerformHitTest();
});
}
#endregion
}
}