我有一个自定义的 usdz 文件(不是通过代码创建的,而是一把真正的椅子!)。我将其保存在 Entity
中。
一旦我有了它,这就是我的代码:
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)")
}
}
这里的问题是“参数类型‘Entity’不符合预期类型‘HasCollision’”。所以我不能添加任何手势。
但我找不到任何有用的资源来实现我的最终目标。有什么建议吗?
最佳答案
我遇到了一些其他情况:我需要从 .usdz
文件加载模型,它应该有一个动画。但我也需要有像平移和旋转这样的手势。研究将我引导至 thread与正确的答案。我将在下面展示它的代码,主要思想是“将具有动画的加载实体嵌套在 ModelEntity 中,然后根据加载模型的边界为该 ModelEntity 提供适当的 CollisionComponent。” (三)
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)
}
https://stackoverflow.com/questions/64182764/