簡體   English   中英

使用觸摸在SceneKit中移動節點

[英]Moving a node in SceneKit using touch

我試圖通過觸摸移動SceneKit中的節點。 我在這里使用代碼: 使用觸摸在SceneKit中移動特定節點

我遇到的問題是,每次啟動panGesture時,我觸摸的對象都會到達場景的左下角以開始移動。 將它從該位置移開並釋放是可以的,只是在每次平移開始時,對象從左角重置的問題。 如果我縮小,它會從這個新縮放級別的角落重置。

我的代碼是:

func CGPointToSCNVector3(view: SCNView, depth: Float, point: CGPoint) -> SCNVector3 {
    let projectedOrigin = view.projectPoint(SCNVector3Make(0, 0, depth))
    let locationWithz   = SCNVector3Make(Float(point.x), Float(point.y), projectedOrigin.z)
    return view.unprojectPoint(locationWithz)
}
func dragObject(sender: UIPanGestureRecognizer){
    if(movingNow){
        let translation = sender.translationInView(sender.view!)
        var result : SCNVector3 = CGPointToSCNVector3(objectView, depth: tappedObjectNode.position.z, point: translation)
        tappedObjectNode.position = result
    }
    else{
        let hitResults = objectView.hitTest(sender.locationInView(objectView), options: nil)
        if hitResults.count > 0 {
            movingNow = true
        }
    }
    if(sender.state == UIGestureRecognizerState.Ended) {
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    let scnView = objectView
    scnView.backgroundColor = UIColor.whiteColor()
    scnView.autoenablesDefaultLighting = true
    scnView.allowsCameraControl = true

我暫時禁用allowCameraControl的panning函數,然后執行以下操作調用dragObject:

globalPanRecognizer = UIPanGestureRecognizer(target: self, 
    action:#selector(ViewController.dragObject(_:)))
objectView.addGestureRecognizer(globalPanRecognizer)

這些是CGPointToSCNVector3第一次調用中的值:

  • tappedObjectNode的初始值:SCNVector3(x:0.100000001,y:0.100000001,z:3.0)
  • projectedOrigin:SCNVector3(x:261.613159,y:285.530396,z:0.949569583) - 這個異常大
  • CGPointToSCNVector3返回的值:SCNVector3(x:1.03418088,y:1.9734658,z:4.64346933)

我玩過不同版本的CGPointToSCNVector3,但沒有運氣。 這種行為的原因是什么? 謝謝,

解決方案是將sender.translationInView(sender.view!)更改為sender.translationInView(sender.view!) sender.translationInView(self.view!)

Swift 4.1 / Xcode 9.3 / iOS 11.3

// node that you want the user to drag
var tappedObjectNode = SCNNode()

// the SCNView
@IBOutlet weak var sceneView: SCNView!

// the scene
let scene = SCNScene()

//helper
func CGPointToSCNVector3(view: SCNView, depth: Float, point: CGPoint) -> SCNVector3 {
    let projectedOrigin = view.projectPoint(SCNVector3Make(0, 0, depth))
    let locationWithz   = SCNVector3Make(Float(point.x), Float(point.y), projectedOrigin.z)
    return view.unprojectPoint(locationWithz)
}

// gesture handler
var movingNow = false
@objc func dragObject(sender: UIPanGestureRecognizer){
if(movingNow){
        let translation = sender.translation(in: sender.view!)
        var result : SCNVector3 = CGPointToSCNVector3(view: sceneView, depth: tappedObjectNode.position.z, point: translation)    
        tappedObjectNode.position = result
    } else {
        // view is the view containing the sceneView
        let hitResults = sceneView.hitTest(sender.location(in: view), options: nil)
        if hitResults.count > 0 {
            movingNow = true
        }
    }
}


// in viewDidLoad
sceneView.scene = scene

let panner = UIPanGestureRecognizer(target: self, action: #selector(dragObject(sender:)))
sceneView.addGestureRecognizer(panner)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM