如何以编程方式将对象添加到 RealityKit 场景?

问题描述 投票:0回答:1

我从 VisionOS 上的 RealityView 中的资源加载了一个场景。它显示正确。我想以编程方式添加其他对象,并且我正在尝试;

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

            var anchorEntity = AnchorEntity(world: [25, 0, -0.5])  
            let sphere = MeshResource.generateSphere(radius: 20.0)
            let material = SimpleMaterial(color: .red, roughness: 0.5, isMetallic: true)
            let modelEntity = ModelEntity(mesh: sphere, materials: [material])
            anchorEntity.addChild(modelEntity)
            content.add(modelEntity)
        }
    }
}

然而,球体从未出现。代码运行没有错误。

swiftui realitykit visionos
1个回答
0
投票

info.plist
中,将沉浸式空间应用程序会话角色打开(并更改
YourApp.swift
文件中的代码),您将立即看到半径为20米的巨大球体。另外,如果您不将其添加到场景中,为什么需要 25 米偏移的锚点?

content.add(anchorEntity)

默认情况下,RealityKit 不渲染 3D 对象的内表面,仅渲染外表面。如果没有锚点,您将看不到球体,因为它的直径为 40 米。

© www.soinside.com 2019 - 2024. All rights reserved.