我一直在Swift Playgrounds上试用ARKit。我已经写了入门代码,但是运行时什么也没发生。它不显示代码,而是显示弹出窗口,该弹出窗口显示代码中的问题。
我知道我正在使用的代码有效,因为我在运行较早版本的Swift Playgrounds的iPad上使用了相同的代码,并且该代码运行良好。 Swift Playgrounds 3或Swift 5似乎都存在问题。
这是有趣的部分。当我删除运行ARWorldTrackingConfiguration
初始化程序的代码行以及使视图控制器成为会话和场景的委托的代码时,该代码将正常运行。当我放回原处时,它再次发生相同的错误。我不知道怎么了。
我在iPad 6和第六代上运行Swift Playgrounds 3.0。游乐场使用ARKit,UIKit,SceneKit和PlaygroundSupport。
最后,这是一些代码。
// Code inside modules can be shared between pages and other source files.
import ARKit
import SceneKit
import UIKit
extension ARSCNView {
public func setup(){
antialiasingMode = .multisampling4X
automaticallyUpdatesLighting = false
preferredFramesPerSecond = 60
contentScaleFactor = 1.0
if let camera = pointOfView?.camera {
camera.wantsHDR = true
camera.wantsExposureAdaptation = true
camera.exposureOffset = -1
camera.minimumExposure = -1
camera.maximumExposure = 3
}
}
}
public class vc : UIViewController, ARSessionDelegate, ARSCNViewDelegate {
var arscn : ARSCNView!
var scene : SCNScene!
public override func loadView() {
arscn = ARSCNView(frame: CGRect(x: 0, y: 0, width: 768, height: 1024))
arscn.delegate = self
arscn.setup()
scene = SCNScene()
arscn.scene = scene
var config = ARWorldTrackingConfiguration()
config.planeDetection = .horizontal
arscn.session.delegate = self
self.view = arscn
arscn.session.run(configåå)
}
public func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
}
public func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
public func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
}
最后,请注意,我要在操场的主页面上显示实时取景,并将该课程放入共享代码中。
答案 0 :(得分:1)
使用UpperCamelCasing
作为类的名称,并在底部添加两串代码。
此代码适用于iPad Swift Playgrounds
和macOS Playground
:
import ARKit
import PlaygroundSupport
class LiveVC: UIViewController, ARSessionDelegate, ARSCNViewDelegate {
let scene = SCNScene()
var arscn = ARSCNView(frame: CGRect(x: 0,
y: 0,
width: 640,
height: 360))
override func viewDidLoad() {
super.viewDidLoad()
arscn.delegate = self
arscn.session.delegate = self
arscn.scene = scene
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal]
arscn.session.run(config)
}
func session(_ session: ARSession, didFailWithError error: Error) {}
func sessionWasInterrupted(_ session: ARSession) {}
func sessionInterruptionEnded(_ session: ARSession) {}
}
PlaygroundPage.current.liveView = LiveVC().arscn
PlaygroundPage.current.needsIndefiniteExecution = true
PS 。有关macOS上Playground的提示(尽管在使用ARKit模块时没有多大意义):
要在Playground 11.0中打开
Live View
,请使用以下快捷方式:
命令 + 选项 + 返回
答案 1 :(得分:0)
我想出了一种方法来完成这项工作。我要做的就是将视图控制器分配给一个变量,然后显示该变量。我不确定它为什么能工作,我只是知道能做到。
import ARKit
import SceneKit
import UIKit
import PlaygroundSupport
public class LiveVC: UIViewController, ARSessionDelegate, ARSCNViewDelegate {
let scene = SCNScene()
public var arscn = ARSCNView(frame: CGRect(x: 0,y: 0,width: 640,height: 360))
override public func viewDidLoad() {
super.viewDidLoad()
arscn.delegate = self
arscn.session.delegate = self
arscn.scene = scene
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal]
arscn.session.run(config)
view.addSubview(arscn)
}
public func session(_ session: ARSession, didFailWithError error: Error) {}
public func sessionWasInterrupted(_ session: ARSession) {}
public func sessionInterruptionEnded(_ session: ARSession) {}
}
var vc = LiveVC()
PlaygroundPage.current.liveView = vc
PlaygroundPage.current.needsIndefiniteExecution = true