【发布时间】: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