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

如何在visionOS的RealityKit场景中通过代码添加对象?球体不显示

问题解决:visionOS RealityView中代码添加对象不显示

你代码里的核心问题是错误地将ModelEntity直接添加到RealityView的content中,而不是添加承载它的AnchorEntity。另外还有两个潜在问题需要调整:

  • 球体半径设为20过大,在visionOS的空间坐标系里这个尺寸会导致球体超出视野范围
  • 锚点的world坐标[25, 0, -0.5]偏移量太大,同样会让物体处于视野之外

修正后的代码如下:

var body: some View {
    RealityView { content in
        if let scene = try? await Entity(named: "Scene", in: realityKitContentBundle) {
            viewModel.rootEntity = scene
            content.add(scene)

            // 调整锚点位置到更合理的视野范围内
            let anchorEntity = AnchorEntity(world: [0, 0, -1.5])  
            // 缩小球体半径到合适尺寸
            let sphere = MeshResource.generateSphere(radius: 0.2)
            let material = SimpleMaterial(color: .red, roughness: 0.5, isMetallic: true)
            let modelEntity = ModelEntity(mesh: sphere, materials: [material])
            anchorEntity.addChild(modelEntity)
            // 添加锚点实体到content,而不是直接加modelEntity
            content.add(anchorEntity)
        }
    }
}

关键修正点:

  • 将content.add(modelEntity)改为content.add(anchorEntity):AnchorEntity负责在空间中定位子实体,只有添加锚点,ModelEntity才能被正确放置在世界空间里
  • 调整球体半径到0.2:这个尺寸在visionOS的1:1空间坐标系里是合理的(约20厘米直径)
  • 调整锚点坐标到[0,0,-1.5]:让球体处于用户前方1.5米的位置,刚好在视野中心附近

内容的提问来源于stack exchange,提问作者john elemans

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 05:42:20