通过 Swift 设置 ARKit 方向

Setting ARKit Orientation via Swift

我正在使用 OpenGL 开发 ARKit 应用程序,因此直接使用 ARKit 而不是使用 SceneKit。

默认情况下,ARKit 设置为横向,但我无法找到任何文档或示例来旋转为纵向。 SceneKit 示例适用于纵向,但 Metal 示例仅适用于横向。

是否可以更改 ARKit 跟踪方向?

目前 iOS 11 Beta,只有 plane detection 的水平表面跟踪。

By default, plane detection is off. If you enable horizontal plane detection, the session adds ARPlaneAnchor objects and notifies your ARSessionDelegate , ARSCNViewDelegate , or ARSKViewDelegate object whenever its analysis of captured video images detects an area that appears to be a flat surface.

如果您需要垂直跟踪,请使用 hitTest(_:types:) 功能自行完成。它允许您检查现实世界中的表面或物体。

Hit testing searches for real-world objects or surfaces detected through the AR session's processing of the camera image. A 2D point in the image coordinates can refer to any point along a 3D line that starts at the device camera and extends in a direction determined by the device orientation and camera projection. This method searches along that line, returning all objects that intersect it in order of distance from the camera.

我能够通过将相机矩阵乘以基于设备方向旋转的四元数来解决应用程序逻辑中的这个问题。

对不起objective-c。

找到并重写这个

uniforms->viewMatrix = [frame.camera viewMatrixForOrientation:UIInterfaceOrientationLandscapeRight];
uniforms->projectionMatrix = [frame.camera projectionMatrixForOrientation:UIInterfaceOrientationLandscapeRight viewportSize:_viewportSize zNear:0.001 zFar:1000];

uniforms->viewMatrix = [frame.camera viewMatrixForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
uniforms->projectionMatrix = [frame.camera projectionMatrixForOrientation:[[UIApplication sharedApplication] statusBarOrientation] viewportSize:_viewportSize zNear:0.001 zFar:1000];

CGAffineTransform displayToCameraTransform = CGAffineTransformInvert([frame displayTransformForOrientation:UIInterfaceOrientationLandscapeRight viewportSize:_viewportSize]);

CGAffineTransform displayToCameraTransform = CGAffineTransformInvert([frame displayTransformForOrientation:[[UIApplication sharedApplication] statusBarOrientation] viewportSize:_viewportSize]);