问题描述
试图将我的RealityKit项目用作屏幕应用(VR)的基础,而不是从后置摄像头投影到真实世界(AR)。
任何人都知道如何使用.nonAR
摄像头选项异步加载RealityKit项目,因此它可以在应用中投影而不是利用后置摄像头吗?
我是否要在Swift代码或Reality Composer项目中创建位置信息?
解决方法
在这里,您可以借助RealityKit的.usdz
实例方法和Combine的.loadModelAsync()
类型来异步加载AnyCancellable
VR模型。
import UIKit
import RealityKit
import Combine
class VRViewController: UIViewController {
@IBOutlet var arView: ARView!
var anyCancellable: AnyCancellable? = nil
let anchorEntity = AnchorEntity(world: [0,-2])
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
arView.backgroundColor = .black
arView.cameraMode = .nonAR
anyCancellable = ModelEntity.loadModelAsync(named: "horse").sink(
receiveCompletion: { _ in
self.anyCancellable?.cancel()
},receiveValue: { [self] (object: Entity) in
if let model = object as? ModelEntity {
self.anchorEntity.addChild(model)
self.arView.scene.anchors.append(self.anchorEntity)
} else {
print("Can't load a model due to some issues")
}
}
)
}
}
但是,如果您想在3D环境中移动,请不要使用.nonAR
相机模式,
arView.environment.background = .color(.black)