RealityKit 作为构建 3D 非 AR 应用程序的框架
RealityKit as a framework to build 3D nonAR apps
RealityKit 有一个 .nonAR
相机选项,但缺少构建 3D 非 AR 应用程序的基本选项,例如相机控制 (.allowsCameraComtrol
)。
使用 RealityKit 替代 SceneKit 是否可行(SceneKit 在过去几年没有更新,可能很快就会被亵渎)?
Apple 一直在准备 SceneKit 的替代品,它绝对是一个 Reality 系列 – RealityKit、RealityFoundation、Reality Composer (iOS/macOS)、Reality Converter、QuickLook 等。使用 RealityKit 2.0,您可以创建两者增强现实和虚拟现实应用程序。作为制作 3D 场景原型的工具,您需要使用 Reality Composer。即使缺少像 .allowsCameraControl
这样重要的功能,您现在也可以使用 RealityKit 制作 VR 应用程序。
解决方法
尽管如此,请使用我的代码作为 起点 在 RealityKit 中创建您自己的 VR 相机控件:
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let buildings = try! Experience.loadScene()
let camera = PerspectiveCamera()
var current_X_Angle: Float = 0.0
var current_Y_Angle: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
arView.environment.background = .color(.systemCyan)
arView.scene.anchors.append(buildings)
self.camera.position.z = 1
buildings.children[0].addChild(self.camera)
self.gestureRecognizer()
}
func gestureRecognizer() {
for gestureRecognizer in [UIPanGestureRecognizer.self,
UIPinchGestureRecognizer.self] {
if gestureRecognizer == UIPinchGestureRecognizer.self {
let r = UIPinchGestureRecognizer(target: self,
action: #selector(allowCameraControl_01))
arView.addGestureRecognizer(r)
}
if gestureRecognizer == UIPanGestureRecognizer.self {
let r = UIPanGestureRecognizer(target: self,
action: #selector(allowCameraControl_02))
arView.addGestureRecognizer(r)
}
}
}
@objc func allowCameraControl_01(recognizer: UIPinchGestureRecognizer) {
switch recognizer.state {
case .changed, .ended:
self.camera.position.z *= 1 / Float(recognizer.scale)
recognizer.scale = 1.0
default: break
}
}
@objc func allowCameraControl_02(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .changed, .ended:
let translate = recognizer.translation(in: recognizer.view)
let angle_X = Float(translate.y / 300) * .pi / 180.0
let angle_Y = Float(translate.x / 100) * .pi / 180.0
self.current_X_Angle += angle_X
self.current_Y_Angle += angle_Y
camera.setOrientation(Transform(pitch: current_X_Angle,
yaw: current_Y_Angle,
roll: .zero).rotation,
relativeTo: buildings.anchor)
default: break
}
}
}
RealityKit 有一个 .nonAR
相机选项,但缺少构建 3D 非 AR 应用程序的基本选项,例如相机控制 (.allowsCameraComtrol
)。
使用 RealityKit 替代 SceneKit 是否可行(SceneKit 在过去几年没有更新,可能很快就会被亵渎)?
Apple 一直在准备 SceneKit 的替代品,它绝对是一个 Reality 系列 – RealityKit、RealityFoundation、Reality Composer (iOS/macOS)、Reality Converter、QuickLook 等。使用 RealityKit 2.0,您可以创建两者增强现实和虚拟现实应用程序。作为制作 3D 场景原型的工具,您需要使用 Reality Composer。即使缺少像 .allowsCameraControl
这样重要的功能,您现在也可以使用 RealityKit 制作 VR 应用程序。
解决方法
尽管如此,请使用我的代码作为 起点 在 RealityKit 中创建您自己的 VR 相机控件:
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let buildings = try! Experience.loadScene()
let camera = PerspectiveCamera()
var current_X_Angle: Float = 0.0
var current_Y_Angle: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
arView.environment.background = .color(.systemCyan)
arView.scene.anchors.append(buildings)
self.camera.position.z = 1
buildings.children[0].addChild(self.camera)
self.gestureRecognizer()
}
func gestureRecognizer() {
for gestureRecognizer in [UIPanGestureRecognizer.self,
UIPinchGestureRecognizer.self] {
if gestureRecognizer == UIPinchGestureRecognizer.self {
let r = UIPinchGestureRecognizer(target: self,
action: #selector(allowCameraControl_01))
arView.addGestureRecognizer(r)
}
if gestureRecognizer == UIPanGestureRecognizer.self {
let r = UIPanGestureRecognizer(target: self,
action: #selector(allowCameraControl_02))
arView.addGestureRecognizer(r)
}
}
}
@objc func allowCameraControl_01(recognizer: UIPinchGestureRecognizer) {
switch recognizer.state {
case .changed, .ended:
self.camera.position.z *= 1 / Float(recognizer.scale)
recognizer.scale = 1.0
default: break
}
}
@objc func allowCameraControl_02(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .changed, .ended:
let translate = recognizer.translation(in: recognizer.view)
let angle_X = Float(translate.y / 300) * .pi / 180.0
let angle_Y = Float(translate.x / 100) * .pi / 180.0
self.current_X_Angle += angle_X
self.current_Y_Angle += angle_Y
camera.setOrientation(Transform(pitch: current_X_Angle,
yaw: current_Y_Angle,
roll: .zero).rotation,
relativeTo: buildings.anchor)
default: break
}
}
}