设备移动时 SCNNode 在 ARSCNView 中移动

问题描述

我在 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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...