【问题标题】:RealityKit for VR用于 VR 的 RealityKit
【发布时间】:2020-12-15 09:38:01
【问题描述】:
尝试使用我的 RealityKit 项目作为屏幕应用 (VR) 的基础,而不是通过后置摄像头投射到现实世界 (AR) 上。
有人知道如何使用.nonAR 摄像头选项异步加载 RealityKit 项目,因此它可以在应用程序中进行投影,而不是利用后置摄像头?
我是在 Swift 代码还是 Reality Composer 项目中创建位置信息?
【问题讨论】:
标签:
swift
virtual-reality
realitykit
reality-composer
【解决方案1】:
在 RealityKit 的 .loadModelAsync() 实例方法和 Combine 的 AnyCancellable 类型的帮助下,如何异步加载 .usdz VR 模型。
import UIKit
import RealityKit
import Combine
class VRViewController: UIViewController {
@IBOutlet var arView: ARView!
var anyCancellable: AnyCancellable? = nil
let anchorEntity = AnchorEntity(world: [0, 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)