简体   繁体   English

替换iOS10中的ARKit

[英]Replacement for ARKit in iOS10

I want to include ARKit in an app designed for iOS10+ where I replace ARKit with SceneKit if the iOS version is <11. 我想将ARKit包含在专为iOS10 +设计的应用程序中,如果iOS版本低于11,我将用SceneKit替换ARKit。

Unfortunately it seems like there is no way to currently do this? 不幸的是,似乎目前尚无办法?

Depending on how you've set your expectations, it is possible to use SceneKit as a "fallback" from ARKit -- specifically, from ARSCNView . 根据您如何设置您的期望,也可以使用SceneKit从ARKit一个“回退” -具体而言,从ARSCNView

What you can easily do is create a 3D content experience that appears "in the real world" via AR when running on an ARKit capable device, and in an entirely virtual setting (that is, rendered in 3D without a camera feed as background) when running without ARKit. 您可以轻松地创建一个3D内容体验,当在支持ARKit的设备上运行时,它可以通过AR在“真实世界”中出现,并且在完全虚拟的设置(即以3D渲染,而没有相机作为背景)时,在没有ARKit的情况下运行。

Note: ARKit support isn't just an iOS 11 thing -- you also need an A9 device. 注意: ARKit支持不仅限于iOS 11,还需要A9设备。 So you might think about a fallback experience for older hardware on iOS 11, not just for older iOS versions. 因此,您可能会想到iOS 11上较旧的硬件的回退体验,而不仅仅是iOS较旧的版本。

To do that, you'll need to be able to swap out your view class at runtime. 为此,您需要能够在运行时换出您的视图类。 That means not creating ARSCNView in a storyboard, but initializing one and adding it to your view controller's root view in code. 这意味着不是在情节ARSCNView中创建ARSCNView ,而是对其进行初始化并将其添加到代码中视图控制器的根视图中。

override func viewDidLoad() {
    super.viewDidLoad()
    let sceneView = ARSCNView(frame: view.bounds)
    sceneView.scene = // ... set up your SceneKit scene ...
    view.addSubview(sceneView)
}

Once you're doing something like that, you can wrap the critical part in a conditional that uses ARSCNView when available and SCNView otherwise. 一旦你在做类似的东西,你可以在有条件的包裹关键部分使用ARSCNView时可用, SCNView否则。 Actually, you might want to set up a handy function for that... 实际上,您可能想要为此设置方便的功能...

var canUseARKit: Bool {
    if #available(iOS 11.0, *) {
        return ARWorldTrackingSessionConfiguration.isSupported
    } else {
        return false
    }
}

Then you can conditionalize your view setup: 然后,您可以条件化您的视图设置:

override func viewDidLoad() {
    super.viewDidLoad()
    let sceneView: SCNView
    if canUseARKit {
        sceneView = ARSCNView(frame: view.bounds)
    } else {
        sceneView = SCNView(frame: view.bounds)
    }
    sceneView.scene = // ... set up your SceneKit scene ...
    view.addSubview(sceneView)
}

Of course, there's still more to do after that: 当然,此后还有更多工作要做:

  • When you're using ARKit, you get camera control "for free"; 使用ARKit时,您可以“免费”获得摄像机控制; when you're using SceneKit on its own you'll have to think about where to place the camera and how to let the user control it. 单独使用SceneKit时,您必须考虑将相机放置在何处以及如何让用户控制它。 (Check the WWDC17 SceneKit session for some tips on new iOS 11 camera controls.) (有关新的iOS 11相机控件,请查看WWDC17 SceneKit会话以获取一些提示。)
  • When you're using SceneKit on its own, your 3D content defines "the world" that the user interacts with -- that is, you place cameras and such in relation to your content. 当您单独使用SceneKit时,您的3D内容将定义用户与之交互的“世界”-即,您将相机等与您的内容相关联。 When using ARKit, you have to think about where and how to place your content relative to the real world. 使用ARKit时,您必须考虑相对于现实世界放置内容的位置和方式。

But aside from such concerns, working with SceneKit content within ARSCNView is no different from working with SceneKit content in SCNView , so for the rest of your app/game you can share a lot of code and assets between AR and non-AR user experiences. 但是,除了这些顾虑,与内SceneKit内容合作ARSCNView无异于在SceneKit内容的工作不同SCNView ,所以对你的应用程序/游戏的其他部分可以共享大量的AR和非AR用户体验之间的代码和资源。

You can conditionally choose to include features using the following syntax: 您可以使用以下语法有条件地选择包括功能:

if #available(iOS 11.0, *) {
    // Use ARKit
} else {
   // Use SceneKit
}

Like any framework, ARKit exists in the system version(s) where it exists, and in no other system versions. 像任何框架一样,ARKit存在于存在它的系统版本中,并且没有其他系统版本中。 ARKit exists in iOS 11, not in iOS 10 or before. ARKit存在于iOS 11中,而不存在于iOS 10或更低版本中。 An iOS 10 user can never use ARKit (without upgrading to iOS 11). iOS 10用户永远无法使用ARKit(无需升级到iOS 11)。

(And even then, ARKit will be usable only on a subset of devices running iOS 11, as its features are highly hardware-dependent.) (即使那样,ARKit也只能在运行iOS 11的一部分设备上使用,因为它的功能高度依赖于硬件。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM