我有一个自定义的 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 ”。 (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)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)