swift - 在 RealityKit 中启用手势

我有一个自定义的 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/

相关文章:

python-3.x - Pygame - 不存在 "Setup"文件,正在运行 "buildcon

python - 为什么 python 的 "gc.collect()"没有按预期工作?

haskell - 在 Haskell 的 do 上下文中应用构造函数

javascript - 使用 useEffect 加载脚本

java - 检查一个数组列表是否包含两个字符串

python - 重复值 n 次,n 在一个数组中

python - 如何使用 numba 在 GPU 上推广快速矩阵乘法

reactjs - 不能在 JSX 属性中使用 bool 值

python-3.x - Python Pandas : groupby one column, 只

c# - 无法将 TextMeshPro 文本获取到 Unity 中的变量槽