9 C
New York
Friday, October 18, 2024

ios – The way to show the identify of a selected half od mannequin when clicking on that half


In RealityKit, how can I show a popup with the identify of a selected a part of a mannequin when clicking on that half?

I’m growing an iOS AR software, and I’ve imported a USDZ mannequin of a tooth. I wish to implement a function the place, when a consumer faucets on a selected a part of the tooth mannequin, a small popup seems with some descriptive textual content about that half.

The primary problem I’m dealing with is figuring out which a part of the mannequin has been clicked. How can I obtain this? Might you please information me by way of the steps and the interfaces that I must name to implement this performance? Thanks very a lot on your assist!

Listed below are my code now:

import ARKit
import UIKit
import RealityKit

class ARViewController_plane: UIViewController {
    
    let arView = ARView()
    
    var modelEntity: ModelEntity?
    
    override func loadView() {
        tremendous.loadView()
        
        view = arView
        
        // create mannequin
        modelEntity = createModel()
        
        // 放置模型
        placeModel(mannequin: modelEntity!)
        
        // 设置手势
        installestures(on: modelEntity!)
        
        // 配置
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = .horizontal
        
        // 运行
        arView.session.run(configuration)
        
    }
    
    func createModel() -> ModelEntity {
        let modelEntity = attempt! ModelEntity.loadModel(named: "tsmile1.usdz")
        return modelEntity
    }
    
    func placeModel(mannequin:ModelEntity) {
        let planeAnchor = AnchorEntity(world: SIMD3(0, 0, 0))//(.aircraft(.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(goal: self, motion: #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!.rework

        // 创建新的四元数旋转(围绕模型的Z轴旋转)
        let newRotation = simd_quatf(angle: rotationAngle, axis: [0, 0, 1])

        // 将新的旋转与现有的旋转叠加
        currentTransform.rotation = simd_mul(currentTransform.rotation, newRotation)

        // 应用新的变换
        modelEntity!.rework = currentTransform
    }
}



Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles