设备移动时 SCNNode 在 ARSCNView 中移动

SCNNode moves in ARSCNView when device moves

我在 ARSCNView(sceneView) 中添加了一个 SCNNode(nodeObj) :

let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal]
self.sceneView.delegate = self
self.sceneView.session.run(config)

self.sceneView.autoenablesDefaultLighting = true
self.sceneView.automaticallyUpdatesLighting = true
let antennaScene = SCNScene(named: sceneName)

guard let antennaNode = antennaScene?.rootNode.childNode(withName: antennaNodeName,
                                                      recursively: true)
else { fatalError("no model found!") }

antennaNode.position = SCNVector3(x: 0.0, y: 0.0, z: -5.0)
antennaNode.scale = SCNVector3(0.002, 0.002, 0.002)
self.sceneView.scene.rootNode.addChildNode(antennaNode)

更新:

extension ARViewController: ARSCNViewDelegate {
func addPlane(node: SCNNode, anchor: ARPlaneAnchor) {
    let plane = Plane(anchor)
    planes[anchor] = plane
    plane.setPlaneVisibility(self.setPlaneVisibility)
    node.addChildNode(plane)
    NSLog("Added plane: \(plane)")
}

func updatePlane(anchor: ARPlaneAnchor) {
    if let plane = planes[anchor] {
        plane.update(anchor)
    }
}

func removePlane(anchor: ARPlaneAnchor) {
    NSLog("In removePlane :")
    if let plane = planes.removeValue(forKey: anchor) {
        plane.removeFromParentNode()
    }
}

 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    NSLog("In  didAdd:")
    if let planeAnchor = anchor as? ARPlaneAnchor {
            self.addPlane(node: node, anchor: planeAnchor)
            if !self.sceneRendered {
                self.sceneRendered = true
                
                self.selectedModel.scale = SCNVector3(0.001, 0.001, 0.001)
                self.selectedModel.position = SCNVector3Zero
                node.addChildNode(self.selectedModel)
            }
        }
    }
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
    NSLog("In  didUpdate:")
    DispatchQueue.main.async {
        
        if let planeAnchor = anchor as? ARPlaneAnchor {
            self.updatePlane(anchor: planeAnchor)
        }
    }
}
//MARK: Gesture Handling Methods

//This method enables multiple gestures recognizor settings
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
       shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer)
        -> Bool {
    return true
}
    
//Move Antenna across the screen
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    NSLog("In touchesMoved : ")
        NSLog("In  touchesMoved:")
        
        guard (self.selectedModel != nil) else {
            NSLog("In touches Moved : No Antenna added to the screen")
            return
        }
        //If touches is 2 then its pinch gesture for scaling. If touches is 1 then its pan gesture for antenna movement
        guard touches.count <= 1 else {
            NSLog("In touches Moved : returned")
            return
        }
        
        //1. Get The Current Touch Point
        guard let currentTouchPoint = touches.first?.location(in: self.sceneView),
            //2. Get The Existing ARPlaneAnchor
            let hitTest = self.sceneView.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
        /*!!AFTER THIS LINE EXECUTION MOVEMENT STARTS!!*/
        self.selectedModel.position = SCNVector3(SIMD3(x: newPosition.x, y: newPosition.y, z: newPosition.z))
    
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    NSLog("In touchesEnded :")
        
        self.setPlaneVisibility = false
        
        for anchor in planes {
            if let planeAnchor = anchor.key as? ARPlaneAnchor {
                removePlane(anchor: planeAnchor)
                
            }
        
        
    }
}

注意:当我在 touchesMoved 方法中设置位置时,节点开始随设备移动。

我也设置了它的位置。但是如果我移动 iPhone / iPad,节点也会移动。我需要将节点固定在一个位置,而不是随着设备的移动而移动。

任何帮助都会很棒。提前致谢!

可能有几个可能的问题。

第一个是Bad Tracking

第二个是手势执行错误。

第三个问题是名为 .allowsCameraControl 的实例 属性 的实现。当 运行 AR 应用程序将其设置为 false

//5。应用于节点 /!!此行执行后移动开始!!/ self.selectedModel.position = SCNVector3(SIMD3(x: newPosition.x, y: newPosition.y, z: newPosition.z))

我没有更改 self.selectedModel.position,而是将代码更新为 self.selectedModel.worldPosition