swift - 设备移动时 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,节点也会移动。我需要将节点锚定在一个位置,并且不会随着设备的移动而移动。
任何帮助都会很棒。提前致谢!
解决方案
推荐阅读
- qt - Qt Widgets 在使用 spacer 或 addStretch 函数时消失
- ruby - Ruby I/O 编程关于一个除非循环
- java - 为什么我可以在没有 JSESSIONID 的情况下使用 StompCommand.CONNECT,但需要 X-XSRF-TOKEN?
- pentaho - Pentaho-如何查看报表的后端 sql?
- python - holoviz/holoviews/panel:在单独的窗格中“布局”的全息图的轴限制的默认设置
- azure-data-factory - Azure 数据工厂 - XML 源类型
- javascript - 如何按值排序和排列?例如前 4 比 2 比 16
- javascript - 基于第一个下拉菜单的多选动态选项
- amazon-web-services - AWS QuickSight 编程访问
- pandas - GroupBy 功能不适用