当用户在scnplane上点击时,我正在尝试3d对象参考图像。我已经开始触摸cncnplane添加新的3d对象。现在,我可以添加3d对象,但未引用参考图像。
我的问题是,当用户点击scnplane时,它显示要参考的3d对象,然后点击另一个scnplane来添加新的3d对象并删除较旧的3d对象?
这是我尝试的代码:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
DispatchQueue.main.async {
guard let imageAnchor = anchor as? ARImageAnchor,
let imageName = imageAnchor.referenceImage.name else { return }
let rotationAsRadian = CGFloat(GLKMathDegreesToRadians(360))
// Check To See The Detected Size Of Our Menu Card (Should By 18cm*27cm)
let menuCardWidth = imageAnchor.referenceImage.physicalSize.width
let menuCardHeight = imageAnchor.referenceImage.physicalSize.height
print(
"""
We Have Detected menu Card With Name \(imageName)
\(imageName)'s Width Is \(menuCardWidth)
\(imageName)'s Height Is \(menuCardHeight)
""")
let plane = SCNPlane(width: menuCardWidth, height: menuCardHeight)
plane.firstMaterial?.diffuse.contents = UIColor.black.withAlphaComponent(0.75)
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 4
planeNode.runAction(SCNAction.moveBy(x: 0, y: 0, z: 0, duration: 0.75))
/******************Break fast*******************/
//BREAKFAST, FALTBREADS, SANDWICHES, ALLDAY
let labelPlane = SCNPlane(width: 0.065, height: 0.015)
labelPlane.firstMaterial?.diffuse.contents = UIImage(named: "BREAKFAST")
let lableNode = SCNNode(geometry: labelPlane)
self.lableNode = lableNode
lableNode.name = "lableNode"
lableNode.eulerAngles.x = -.pi / 2
lableNode.runAction(SCNAction.moveBy(x: 0.2, y: 0, z: -0.125, duration: 0))
node.addChildNode(lableNode)
self.sceneView.scene.rootNode.addChildNode(node)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//1. Get The Current Touch Location & Perform An ARSCNHitTest To Check For Any Hit SCNNode's
guard let currentTouchLocation = touches.first?.location(in: self.sceneView),
let hitTestNode = self.sceneView.hitTest(currentTouchLocation, options: nil).first?.node else { return }
if let lableName = hitTestNode.name {
print("touch working")
if lableName == "lableNode"{
makeCakeOnNode(hitTestNode)
}
}
}
func makeCakeOnNode(_ node: SCNNode){
let overlayNode = self.getNode(withImageName: "imageName")
print("overlay::\(overlayNode)")
self.sceneView.scene.rootNode.addChildNode(overlayNode)
}
答案 0 :(得分:0)
创建新的scnplane并将3d对象添加为新的scnplane的子代
func makeCakeOnNode(_ node: SCNNode){
let planeGeometry = SCNPlane(width: 0.18 , height: 0.15)
planeGeometry.firstMaterial?.diffuse.contents = UIColor.black.withAlphaComponent(1)
planeNode0 = SCNNode(geometry: planeGeometry)
planeNode0?.runAction(SCNAction.moveBy(x: -0.2, y: -0.15, z: 0, duration: 0))
let overlayNode = self.getNode(withImageName: "menu")
print("overlay::\(overlayNode)")
if planeBool == true {
planeNode1?.isHidden = true
node.addChildNode(planeNode0!)
planeNode0?.addChildNode(overlayNode)
planeBool = false
} else {
print("plane removed")
planeNode0?.isHidden = true
planeBool = true
}
}
现在一切正常!