将 CustomEntity 添加到 RealityKit 中的平面锚点
我实现了一个简单的自定义实体,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了防止[0、0、0]锚定世界,您无需符合
hasanchoring
协议:因此,您的
aNDERNENTITY(plane:.horizontal)
是现在活跃。To prevent world anchoring at [0, 0, 0], you don't need to conform to the
HasAnchoring
protocol:So, your
AnchorEntity(plane: .horizontal)
are now active.