如何实例化ARSCNView

时间:2017-11-20 21:52:59

标签: ios xamarin.ios arkit

我想使用ARKit来计算当前视频帧中的环境光量。但是,在我检索当前帧时创建ARSCNView对象后,它返回一个空值。

我做错了什么?

public class EyeAlignmentUICameraPreview : UIView, IAVCaptureVideoDataOutputSampleBufferDelegate
{

void Initialize()
{
      CaptureSession = new CaptureSession();
      PreviewLayer = new AVCaptureVideoPreviewLayer(CaptureSession)
      {
            Frame = Bounds,
            VideoGravity = AVLayerVideoGravity.ResizeAspectFill
      };
      var device = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInTelephotoCamera, AVMediaType.Video, AVCaptureDevicePosition.Back);

     ARSCNView SceneView = new ARSCNView();
     // frame is null after this line is executed
     var frame = SceneView.Session.CurrentFrame;
}
}

1 个答案:

答案 0 :(得分:1)

更新我的评论以回答更多详情。

ARFrame

  

作为AR会话的一部分捕获的视频图像和位置跟踪信息。

currentFrame

  

视频帧图像,以及相关的AR场景信息,最近由会话捕获。

根据这些Apple ARKit文档,当ARSession获取视频和关联的AR场景信息时,currentFrame将具有价值。 因此,我们必须首先运行会话。

要运行ARSession,我们需要一个会话配置:

  

运行会话需要会话配置:ARConfiguration类的实例或其子类ARWorldTrackingConfiguration。这些类确定ARKit如何跟踪设备相对于现实世界的位置和动作,从而影响您可以创建的AR体验类型。

因此,运行ARSession的代码片段如下:

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    //Create a session configuration
    var configuration = new ARWorldTrackingConfiguration
    {
        PlaneDetection = ARPlaneDetection.Horizontal,
        LightEstimationEnabled = true
    };

    // Run the view's session
    SceneView.Session.Run(configuration, ARSessionRunOptions.ResetTracking);
}
相关问题