首页 文章

无法拖动ARKit SCNNode?

提问于
浏览
2

好的,就像其他人一样,我在ARKit / world空间中拖动/翻译SCNNode时遇到了麻烦 . 我看了Dragging SCNNode in ARKit Using SceneKit和所有流行的问题,以及来自Apple的代码https://github.com/gao0122/ARKit-Example-by-Apple/blob/master/ARKitExample/VirtualObject.swift

香港专业教育学院尽可能地尝试简化,并在正常的场景套件游戏中做了我想做的事情 - http://dayoftheindie.com/tutorials/3d-games-graphics/t-scenekit-3d-picking-dragging/

我可以得到tapped对象并存储当前手指pos没问题:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let touch = touches.first else { return }

        let results = gameView.hitTest(touch.location(in: gameView), types: [ARHitTestResult.ResultType.featurePoint])

        //TAP Test
        let hits = gameView.hitTest(touch.location(in: gameView), options: nil)
        currentTapPos = getARPos(hitFeature: hitFeature) //TAP POSITION

        if let tappedNode = hits.first?.node {

然而,我的问题是在更新中这样做 - 没有动画 . 无论我点击哪里,该对象都会出现,而且总体上不起作用 -

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let touch = touches.first else { return }

        let results = gameView.hitTest(touch.location(in: gameView), types: [ARHitTestResult.ResultType.featurePoint])
        guard let hitFeature = results.last else { return }

        testNode.position = currentTapPos

我用这个函数转换为SCNVector3:

func getARPos(hitFeature: ARHitTestResult)->SCNVector3
{

    let hitTransform = SCNMatrix4.init(hitFeature.worldTransform)
    let hitPosition = SCNVector3Make(hitTransform.m41,
                                     hitTransform.m42,
                                     hitTransform.m43)
    return hitPosition
}

我试过了:

-translate by vector -pan gesture(这搞砸了其他功能)-SCNAction.moveTo

我能在这做什么?怎么了?

1 回答

  • 2

    我同意@Rickster, Apple Code 提供了一个更强大的例子,但是,我有这个并且它似乎工作顺利(当你使用PlaneDetection时更是如此(因为特征点更加零星):

    我没有使用 touchesBegan 而只是简单地在 touchesMoved 中完成工作:

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        //1. Get The Current Touch Point
        guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
            //2. Get The Next Feature Point Etc
            let hitTest = augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }
    
        //3. Convert To World Coordinates
        let worldTransform = hitTest.worldTransform
    
        //4. Set The New Position
        let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)
    
        //5. Apply To The Node
        nodeToDrag.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)
    
    }
    

    我可能会以完全错误的方式解决这个问题(如果我是,我很乐意得到反馈) .

    无论如何,我希望它可能有所帮助......你可以在这里看到它的快速视频:Dragging SCNNode

相关问题