代码之家  ›  专栏  ›  技术社区  ›  blue

无法拖动ARKit SCNNode?

  •  4
  • blue  · 技术社区  · 7 年前

    好的,像其他人一样,我在ARKit/world空间中拖动/翻译SCNNode时遇到了麻烦。我看过 Dragging SCNNode in ARKit Using SceneKit 还有所有流行的问题,还有苹果的代码 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/

    我可以获取点击的对象并存储当前手指位置没有问题:

    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
    

    我使用此函数转换为SConvector3:

    func getARPos(hitFeature: ARHitTestResult)->SCNVector3
    {
    
        let hitTransform = SCNMatrix4.init(hitFeature.worldTransform)
        let hitPosition = SCNVector3Make(hitTransform.m41,
                                         hitTransform.m42,
                                         hitTransform.m43)
        return hitPosition
    }
    

    我尝试过:

    -按矢量平移 -平移手势(这会破坏其他功能) -命令。移动到

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

    1 回复  |  直到 7 年前
        1
  •  6
  •   BlackMirrorz    7 年前

    我同意@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