【问题标题】:RealityKit .nonAR installGestures is missing translation and rotation is y axis onlyRealityKit .nonAR installGestures 缺少平移和旋转仅是 y 轴
【发布时间】:2023-01-03 16:52:30
【问题描述】:

我正在尝试使用 RealityKit 对 3d Scanner App 进行逆向工程,但我很难获得一个适用于所有手势的基本模型。当我运行下面的代码时,我得到一个具有缩放和旋转(仅关于 y 轴)但没有平移交互的立方体。我试图找出如何绕任意轴旋转和平移,就像上面的 3d 扫描仪应用程序一样。我是 iOS 的新手,读过一篇文章应该使用 RealityKit,因为 Apple 不再真正支持 SceneKit,但我现在想知道 SceneKit 是否是可行的方法,因为 RealityKit 还很年轻。或者,如果有人知道 RealityKit ModelEntity 对象的扩展以提供更好的交互功能。

根据this 教程,我已经让我的应用程序使用 LiDAR 传感器进行扫描并将其作为 .usda 网格保存到磁盘,但是当我将网格作为 ModelEntity 加载并向其附加手势时,我不这样做获得任何互动。

下面的示例代码为盒子 ModelEntity 重新创建了有限的手势,我有一些注释行显示我将从磁盘加载我的 .usda 模型的位置,但是当它渲染时,它不会与手势交互。

任何帮助表示赞赏!

// ViewController.swift
import UIKit
import RealityKit

class ViewController: UIViewController {

    var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        arView = ARView(frame: view.frame, cameraMode: .nonAR, automaticallyConfigureSession: false)
        view.addSubview(arView)
        
        // create pointlight
        let pointLight = PointLight()
        pointLight.light.intensity = 10000
        // create light anchor
        let lightAnchor = AnchorEntity(world: [0, 0, 0])
        lightAnchor.addChild(pointLight)
        arView.scene.addAnchor(lightAnchor)
        
//      eventually want to load my model from disk and give it gestures.
//        guard let scanEntity = try? Entity.loadModel(contentsOf: urlOBJ) else {
//            print("couldn't load scan in this format")
//            return
//        }

        // entity to add gestures to
        let cubeMaterial = SimpleMaterial(color: .blue, isMetallic: true)
        let myEntity = ModelEntity(mesh: .generateBox(width: 0.1, height: 0.2, depth: 0.3, cornerRadius: 0.01, splitFaces: false), materials: [cubeMaterial])
        
        myEntity.generateCollisionShapes(recursive: false)
        
        let myAnchor = AnchorEntity(world: .zero)
        myAnchor.addChild(myEntity)
        
        // add collision and interaction
        let scanEntityBounds = myEntity.visualBounds(relativeTo: myAnchor)
        myEntity.collision = CollisionComponent(shapes: [.generateBox(size: scanEntityBounds.extents).offsetBy(translation: scanEntityBounds.center)])
        
        arView.installGestures(for: myEntity).forEach {
            gestureRecognizer in
            gestureRecognizer.addTarget(self, action: #selector(handleGesture(_:)))
        }
        arView.scene.addAnchor(myAnchor)
        
        // without this, get no gestures at all
        let camera = PerspectiveCamera()
        let cameraAnchor = AnchorEntity(world: [0, 0, 0.2])
        cameraAnchor.addChild(camera)
        arView.scene.addAnchor(cameraAnchor)
    }
    
    @objc private func handleGesture(_ recognizer: UIGestureRecognizer) {
        if recognizer is EntityTranslationGestureRecognizer {
            print("translation!")
        } else if recognizer is EntityScaleGestureRecognizer {
            print("scale!")
        } else if recognizer is EntityRotationGestureRecognizer {
            print("rotation!")
        }
    }

}


【问题讨论】:

    标签: swift augmented-reality scenekit arkit realitykit


    【解决方案1】:

    要扩展 ModelEntity 的 gesture interaction capabilities,请设置您自己的 2D 手势。 UIKit 中有 8 种屏幕手势,而在 SwiftUI 中,您有 5 种主要手势以及顺序、同步和独占变化。

    【讨论】:

      【解决方案2】:

      据我了解,这些手势适用于盒子,但不适用于您的 .usdz 文件/模型。如果是这种情况,那么问题是因为模型没有碰撞网格(HasCollsion)。如果您使用 reality composer 来编辑您的模型,您可以执行以下操作:

      1. 点击模型
      2. 在 Physics 下拉菜单下,单击 Participate
      3. 在碰撞形状下选择自动

        总的来说,确保模型有碰撞并且你在它有碰撞的代码中投射

        let myEntity = try? Entity.loadModel(named: "fileName") as! HasCollision
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-04
        • 2019-08-14
        • 2021-09-11
        • 1970-01-01
        • 2020-04-05
        • 2021-07-12
        • 1970-01-01
        相关资源
        最近更新 更多