将 CustomEntity 添加到 RealityKit 中的平面锚点

发布于 2025-01-21 00:19:13 字数 1105 浏览 0 评论 0原文

我实现了一个简单的自定义实体,如下所示:

class Box: Entity, HasModel, HasAnchoring {
        
    required init(color: UIColor) {            
        super.init()                    
        self.components[ModelComponent.self] = ModelComponent(mesh: MeshResource.generateBox(size: 0.3), 
                                                         materials: [SimpleMaterial(color: color, isMetallic: true)])
    }
    
    convenience init(color: UIColor, position: SIMD3<Float>) {
        self.init(color: color)
        self.position = position 
    }
    
    required init() {
        fatalError("init() has not been implemented")
    }
}

ContentView.swift

我尝试将 Box 实体添加到由水平面定义的锚点。

func makeUIView(context: Context) -> ARView {
        
    let arView = ARView(frame: .zero)            
    let anchorEntity = AnchorEntity(plane: .horizontal)
        
    let box = Box(color: .red)
    anchorEntity.addChild(box)
    arView.scene.anchors.append(anchorEntity)
    return arView
}

但是,长方体实体似乎从未停留在水平面上。它似乎总是漂浮在顶部。这是为什么?

I have implemented a simple custom entity as shown below:

class Box: Entity, HasModel, HasAnchoring {
        
    required init(color: UIColor) {            
        super.init()                    
        self.components[ModelComponent.self] = ModelComponent(mesh: MeshResource.generateBox(size: 0.3), 
                                                         materials: [SimpleMaterial(color: color, isMetallic: true)])
    }
    
    convenience init(color: UIColor, position: SIMD3<Float>) {
        self.init(color: color)
        self.position = position 
    }
    
    required init() {
        fatalError("init() has not been implemented")
    }
}

ContentView.swift

I try to add the Box entity to the anchor, defined by the horizontal plane.

func makeUIView(context: Context) -> ARView {
        
    let arView = ARView(frame: .zero)            
    let anchorEntity = AnchorEntity(plane: .horizontal)
        
    let box = Box(color: .red)
    anchorEntity.addChild(box)
    arView.scene.anchors.append(anchorEntity)
    return arView
}

But, the Box entity never appears to be resting on the horizontal plane. It always seems to be floating on the top. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓味的萝莉 2025-01-28 00:19:13

为了防止[0、0、0]锚定世界,您无需符合hasanchoring协议:

class Box: Entity, HasModel {
    // content...
}

因此,您的aNDERNENTITY(plane:.horizo​​ntal)是现在活跃。

To prevent world anchoring at [0, 0, 0], you don't need to conform to the HasAnchoring protocol:

class Box: Entity, HasModel {
    // content...
}

So, your AnchorEntity(plane: .horizontal) are now active.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文