更新时间:2021-12-18 22:14:57
我遇到了一些其他情况:我需要从 .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)
}