Sceneview用手势平移/移动视点相机 [英] Sceneview Translate /move Point of view camera with gesture

查看:106
本文介绍了Sceneview用手势平移/移动视点相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我需要在Mac Catalyst App中使用平移手势来移动SceneView的视点.对于iPhone和iPad,默认情况下是两个手指拖动.

So I need to move Point of view of the sceneview with the pan gesture in Mac catalyst app. for iPhone and iPad there is default two finger drag.

我尝试过

    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根节点,它将起作用,但是我只需要移动视角而不是根节点的位置

if I apply this to SceneKit root node it is working , but I need to move point of view only not the position of the root node

请提出建议

推荐答案

谁仍然没有得到它

这是解决方法

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 + t1 * (Q2.x - Q1.x)
       let y2 = Q1.y + t1 * (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;
       }

}

这篇关于Sceneview用手势平移/移动视点相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆