ios 场景视图平移/移动带有手势的视点相机

jmp7cifd  于 2023-08-08  发布在  iOS
关注(0)|答案(1)|浏览(111)

因此,我需要在Mac催化剂应用程序中使用平移手势移动sceneview的视点。对于iPhone和iPad,默认为两个手指拖动。
我试过

let point = gesture.location(in: self)
    let v1 = self.projectPoint(SCNVector3Zero)
    let vector = self.unprojectPoint(SCNVector3Make(Float(point.x), Float(point.y), v1.z))
    self.pointOfView?.position = SCNVector3(vector.x, vector.y, self.pointOfView!.position.z)

字符串
如果我将此应用于SceneKit根节点,它可以工作,但我只需要移动视点而不是根节点的位置
请建议

vxbzzdmp

vxbzzdmp1#

谁还不明白
这是解决方案

var previousLocation = SCNVector3(x: 0, y: 0, z: 0)
@objc func panned(gesture:UIPanGestureRecognizer) {
    
    
    let view = self.sceneView1!
    let translation = gesture.translation(in: view)

    let location = gesture.location(in: view)
    let secLocation = CGPoint(x: location.x + translation.x, y: location.y + translation.y)

    let P1 = view.unprojectPoint(SCNVector3(x: Float(location.x), y: Float(location.y), z: 0.0))
    let P2 = view.unprojectPoint(SCNVector3(x: Float(location.x), y: Float(location.y), z: 1.0))

       let Q1 = view.unprojectPoint(SCNVector3(x: Float(secLocation.x), y: Float(secLocation.y), z: 0.0))
       let Q2 = view.unprojectPoint(SCNVector3(x: Float(secLocation.x), y: Float(secLocation.y), z: 1.0))

       let t1 = -P1.z / (P2.z - P1.z)
       let t2 = -Q1.z / (Q2.z - Q1.z)

       let x1 = P1.x + t1 * (P2.x - P1.x)
       let y1 = P1.y + t1 * (P2.y - P1.y)

       let P0 = SCNVector3Make(x1, y1,0)

       let x2 = Q1.x + t2 * (Q2.x - Q1.x)
       let y2 = Q1.y + t2 * (Q2.y - Q1.y)

       let Q0 = SCNVector3Make(x2, y2, 0)

       var diffR = Q0 - P0
    
    diffR = SCNVector3Make(diffR.x * -1, diffR.y * -1, diffR.z * -1)
   //    diffR *= -1

    let cameraNode = view.pointOfView

       switch gesture.state {
       case .began:
           previousLocation = cameraNode!.position
           break;
       case .changed:
        cameraNode?.position = SCNVector3Make(previousLocation.x + diffR.x, previousLocation.y + diffR.y, previousLocation.z + diffR.z)
           break;
       default:
           break;
       }

}

字符串

支持旋转摄像头的更好版本

let translation = gesture.translation(in: view)
     let screenCenter = CGPoint(x: view.bounds.width * 0.5, y: view.bounds.height * 0.5)
     
     // Convert the screen center to a 3D point in the scene
     let center3D = view.unprojectPoint(SCNVector3(Float(screenCenter.x), Float(screenCenter.y), 0.0))
     
     // Convert the translation vector from 2D screen coordinates to 3D scene coordinates
     let translation3D = view.unprojectPoint(SCNVector3(Float(screenCenter.x + translation.x), Float(screenCenter.y + translation.y), 0.0))
     
     // Calculate the difference between the translated and original 3D points
     let diff3D =  center3D - translation3D
     
     // Use diff3D to calculate the updated camera position
     let cameraNode = view.pointOfView
     switch gesture.state {
     case .began:
         previousLocation = cameraNode!.position
     case .changed:
         cameraNode?.position = SCNVector3Make(
             previousLocation.x + diff3D.x,
             previousLocation.y + diff3D.y,
             previousLocation.z + diff3D.z
         )
     default:
         break
     }


的数据

相关问题