You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在visionOS的RealityView中用RealityKit添加PerspectiveCamera并禁用默认相机

解决RealityView中自定义PerspectiveCamera并禁用默认相机的问题

核心问题原因

你添加自定义相机后未生效,是因为Vision Pro默认会启用系统自带的用户跟踪透视相机,且自定义相机未被设置为活动渲染相机。

关键解决步骤

  • 设置自定义PerspectiveCamera的isActive属性为true,让RealityKit优先使用该相机渲染画面
  • 通过RealityViewConfiguration关闭用户头部跟踪,彻底禁用系统默认相机

完整修正代码

RealityView { content in
    let anchorEntity = AnchorEntity(world: [0, 0, 0])
    
    // 测试用场景实体
    let sphereMesh = MeshResource.generateSphere(radius: 1.5)
    let sphereMaterial = SimpleMaterial(color: .white, roughness: 0.5, isMetallic: true)
    let sphereEntity = ModelEntity(mesh: sphereMesh, materials: [sphereMaterial])
    sphereEntity.position = [0, 2, -5]
    anchorEntity.addChild(sphereEntity)
    
    // 自定义驾驶视角相机
    let camera = PerspectiveCamera()
    camera.position = [1, 4, 0]
    camera.isActive = true // 关键:将此相机设为活动渲染相机
    anchorEntity.addChild(camera)
    
    content.add(anchorEntity)
} configuration: { config in
    // 禁用系统默认的用户头部跟踪相机
    config.userTrackingMode = .none
}

驾驶视角动态控制扩展

如果要模拟驾驶时的视角跟随(比如车辆移动、转向),可以将相机挂载到车辆实体下,或通过代码实时更新相机的位置与朝向:

// 示例:模拟车辆向前行驶时的视角移动
func updateCamera(deltaTime: TimeInterval) {
    guard let camera = anchorEntity.children.first(where: { $0 is PerspectiveCamera }) as? PerspectiveCamera else { return }
    // 沿Z轴负方向移动,模拟向前行驶
    camera.position.z -= Float(deltaTime * 6)
}

内容的提问来源于stack exchange,提问作者Nathan Mak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 00:52:12