Man*_*ero 3 scenekit swift virtual-reality realitykit
RealityKit 有相机选项.nonAR,但缺乏构建 3D 非 AR 应用程序的基本选项,例如相机控制 ( .allowsCameraComtrol)。
使用 RealityKit 替代 SceneKit 是否可行(过去几年没有更新,可能很快就会被亵渎)?
.allowsCameraControl相等的苹果一直在准备 SceneKit 的替代品,它绝对是 Reality 系列 \xe2\x80\x93 RealityKit、RealityFoundation、Reality Composer (iOS/macOS)、Reality Composer Pro(而不是visionOS)、Reality Converter、ARQuickLook、等。使用RealityKit 2.0,您可以创建AR和VR应用程序。作为制作 3D 场景原型的工具,您需要使用 Reality Composer。尽管缺少像 一样重要的功能.allowsCameraControl,您现在仍然可以使用 RealityKit 制作 VR 应用程序。
尽管如此,请使用我的代码作为起点,在 RealityKit 中为 VR 创建您自己的相机控件:
\nimport UIKit\nimport RealityKit\n\nclass ViewController: UIViewController {\n @IBOutlet var arView: ARView!\n let buildings = try! Experience.loadScene()\n let camera = PerspectiveCamera()\n var current_X_Angle: Float = 0.0\n var current_Y_Angle: Float = 0.0\n\n override func viewDidLoad() {\n super.viewDidLoad()\n arView.environment.background = .color(.systemCyan)\n arView.scene.anchors.append(buildings)\n self.camera.position.z = 1\n buildings.children[0].addChild(self.camera)\n self.gestureRecognizer()\n }\n func gestureRecognizer() {\n for gestureRecognizer in [UIPanGestureRecognizer.self,\n UIPinchGestureRecognizer.self] {\n if gestureRecognizer == UIPinchGestureRecognizer.self {\n let r = UIPinchGestureRecognizer(target: self,\n action: #selector(allowCameraControl_01))\n arView.addGestureRecognizer(r)\n }\n if gestureRecognizer == UIPanGestureRecognizer.self {\n let r = UIPanGestureRecognizer(target: self,\n action: #selector(allowCameraControl_02))\n arView.addGestureRecognizer(r)\n }\n }\n }\n @objc func allowCameraControl_01(recognizer: UIPinchGestureRecognizer) {\n switch recognizer.state {\n case .changed, .ended:\n self.camera.position.z *= 1 / Float(recognizer.scale)\n recognizer.scale = 1.0\n default: break\n }\n }\n @objc func allowCameraControl_02(recognizer: UIPanGestureRecognizer) {\n switch recognizer.state {\n case .changed, .ended:\n let translate = recognizer.translation(in: recognizer.view)\n let angle_X = Float(translate.y / 300) * .pi / 180.0\n let angle_Y = Float(translate.x / 100) * .pi / 180.0\n self.current_X_Angle += angle_X\n self.current_Y_Angle += angle_Y \n camera.setOrientation(Transform(pitch: current_X_Angle,\n yaw: current_Y_Angle,\n roll: .zero).rotation,\n relativeTo: buildings.anchor)\n default: break\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n在这里您可以找到SwiftUI 版本。
\n\n在 Xcode 15 的 VisionOS 模拟器中,右下角有Pan、Orbit和UI 按钮。Dolly
| 归档时间: |
|
| 查看次数: |
1736 次 |
| 最近记录: |