在 RealityKit 中启用手势 [英] Enabling gestures in RealityKit
问题描述
我有一个自定义的 usdz 文件(不是通过代码创建的,而是说一个真正的椅子!).我将其保存在 Entity
中.
I have a custom usdz file (not create through code, but let's say a real chair!). I save this in a Entity
.
一旦我有了它,这是我的代码:
Once I have it this is my code:
func updateUIView(_ uiView: ARView, context: Context) {
if let modelEntity = model.modelEntity {
print("\(model.modelName)")
let anchorEntity = AnchorEntity(plane: .horizontal)
anchorEntity.addChild(modelEntity.clone(recursive: true))
uiView.scene.addAnchor(anchorEntity)
// modelEntity.generateCollisionShapes(recursive: true)
// If we have multiple object, recursive true help to generate collision for all of them
uiView.installGestures(.rotation, for: modelEntity as! Entity & HasCollision)
uiView.debugOptions = .showPhysics
} else {
print("Unable to load modelEntity for \(model.modelName)")
}
}
这里的问题是参数类型‘实体’不符合预期的类型‘HasCollision’".所以我不能添加任何手势.
The problem here is that `"Argument type 'Entity' does not conform to expected type 'HasCollision'". So I cant add any gesture.
但是我找不到任何有用的资源来实现我的最终目标.有什么建议吗?
But I can't find any usefull resource to achieve my final goal. Is there any advice?
推荐答案
我遇到了一些其他情况:我需要从 .usdz
文件加载模型,它应该有一个动画.但我也需要有平移和旋转等手势.该研究将我引导至 thread 并提供了正确答案.我将在下面展示其中的代码,主要思想是将具有动画的加载实体嵌套在 ModelEntity 内部,然后根据加载模型的边界为该 ModelEntity 提供适当的 CollisionComponent".(c)
I got some other situation: I needed to load the model from .usdz
file and it should have an animation. But also I needed to have gestures like translation and rotation. The research directed me to the thread with the right answer. The code from it I'll show underneath, the main idea is "to nest the loaded entity which has the animations inside of a ModelEntity, and to then give that ModelEntity an appropriate CollisionComponent based on the loaded model's bounds." (c)
loadRequest = Entity.loadAsync(contentsOf: url).sink(receiveCompletion: { status in
print(status)
}) { entity in
// Scaling entity to a reasonable size
entity.setScale(SIMD3(repeating: 0.01), relativeTo: nil)
// Creating parent ModelEntity
let parentEntity = ModelEntity()
parentEntity.addChild(entity)
// Anchoring the entity and adding it to the scene
let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: .zero))
anchor.addChild(parentEntity)
self.arView.scene.addAnchor(anchor)
// Playing availableAnimations on repeat
entity.availableAnimations.forEach { entity.playAnimation($0.repeat()) }
// Add a collision component to the parentEntity with a rough shape and appropriate offset for the model that it contains
let entityBounds = entity.visualBounds(relativeTo: parentEntity)
parentEntity.collision = CollisionComponent(shapes: [ShapeResource.generateBox(size: entityBounds.extents).offsetBy(translation: entityBounds.center)])
// installing gestures for the parentEntity
self.arView.installGestures(for: parentEntity)
}
这篇关于在 RealityKit 中启用手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!