如何在 RealityKit 中将与真实世界对象的碰撞应用到 3D 对象 (.usdz)? [英] How to apply collision with real-world objects to a 3D object (.usdz) in RealityKit?
本文介绍了如何在 RealityKit 中将与真实世界对象的碰撞应用到 3D 对象 (.usdz)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用在项目中导入的 .usdz 文件在真实世界的对象(如墙壁、沙发和...)之间应用碰撞.我曾尝试使用 PhysicsBodyComponent
和 CollisionComponent
但没有任何结果.这是导入 3D 对象的代码:
I'm trying to apply collision between real-world objects like wall, sofa and ... with a .usdz file imported in the project. I have tried using PhysicsBodyComponent
and CollisionComponent
but without any results. Here is the code for importing the 3D object:
let entityName = objects.objectName
guard let entity = try? Entity.load(named: entityName, in: .main) else {
return
}
// Creating parent ModelEntity
let parentEntity = ModelEntity()
parentEntity.addChild(entity)
// Anchoring the entity and adding it to the scene
// Add entity on horizontal planes if classified as `floor`.
// Minimum bounds is 10x10cm
let anchor = AnchorEntity(.plane(.horizontal, classification: .floor, minimumBounds: [0.1, 0.1]))
anchor.name = entityName
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
parentEntity.generateCollisionShapes(recursive: true)
// Installing gestures for the parentEntity
self.arView.installGestures([.translation, .rotation], for: parentEntity)
推荐答案
虚拟对象不会与基于碰撞的物理对象(墙壁、沙发、平原等)交互.
Virtual objects do not interact with physical objects(wall, sofa, plains etc..) on a collision base.
我可以为您提供两种可能的解决方案:
I can offer you 2 possible solutions:
- 基于虚拟的边界框 - 您可以通过跟踪添加的平面/网格并为每个平面/网格添加边界框来向这些元素添加虚拟容器.这样你就可以互动了
- 对运动矢量进行光线投射 - 您可以将运动矢量光线投射到平原(或模型的中心,以防它不移动).在这种情况下,您将收到相关平面/网格.
- Virtual based bounding boxes - You can add virtual containers to such elements by tracking the added planes/meshes and add bounding boxes for each. That way, you can interact
- Raycast on the movement vector - You can raycast the movement vector on to plains(or the center of the model in case it does not move). In such a case, you will receive back the relevant plane/mesh.
这篇关于如何在 RealityKit 中将与真实世界对象的碰撞应用到 3D 对象 (.usdz)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文