Как в RealityKit отображать всплывающее окно с названием определенной части модели при нажатии на эту часть?
Я разрабатываю AR-приложение для iOS, и у меня есть импортировали модель зуба USDZ. Я хочу реализовать функцию, при которой, когда пользователь нажимает на определенную часть модели зуба, появляется небольшое всплывающее окно с описательным текстом об этой части.
Основная проблема, с которой я столкнулся определяет, на какую часть модели был сделан щелчок. Как я могу этого добиться? Не могли бы вы рассказать мне, какие шаги и интерфейсы мне нужно вызвать для реализации этой функции? Большое спасибо за вашу помощь!
Вот мой код:
import ARKit
import UIKit
import RealityKit
class ARViewController_plane: UIViewController {
let arView = ARView()
var modelEntity: ModelEntity?
override func loadView() {
super.loadView()
view = arView
// create model
modelEntity = createModel()
// 放置模型
placeModel(model: modelEntity!)
// 设置手势
installestures(on: modelEntity!)
// 配置
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
// 运行
arView.session.run(configuration)
}
func createModel() -> ModelEntity {
let modelEntity = try! ModelEntity.loadModel(named: "tsmile1.usdz")
return modelEntity
}
func placeModel(model:ModelEntity) {
let planeAnchor = AnchorEntity(world: SIMD3(0, 0, 0))//(.plane(.horizontal, classification: .any, minimumBounds: SIMD2(0.02, 0.02)))
planeAnchor.addChild(modelEntity!)
arView.scene.addAnchor(planeAnchor)
}
func installestures(on object: ModelEntity) {
// scale
object.generateCollisionShapes(recursive: true)
arView.installGestures([.scale], for: object)
// my rotation
let rotationGesture = UIPanGestureRecognizer(target: self, action: #selector(handleRotationGesture(_:)))
arView.addGestureRecognizer(rotationGesture)
}
@objc func handleRotationGesture(_ gesture: UIPanGestureRecognizer) {
let translation = gesture.translation(in: arView)
// 控制旋转速度
let rotationAngle = Float(translation.x) * 0.001
// 获取当前模型的变换
var currentTransform = modelEntity!.transform
// 创建新的四元数旋转(围绕模型的Z轴旋转)
let newRotation = simd_quatf(angle: rotationAngle, axis: [0, 0, 1])
// 将新的旋转与现有的旋转叠加
currentTransform.rotation = simd_mul(currentTransform.rotation, newRotation)
// 应用新的变换
modelEntity!.transform = currentTransform
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -that-part