【发布时间】:2023-04-05 02:42:01
【问题描述】:
我以编程方式向我的 ARSCNView 添加了一个按钮,并添加了一个在点击时调用的函数。该函数创建并放置一个 3D 对象。该按钮第一次按下时可以正常工作,但第二次之后它不会放置我的对象。
这是我的代码:
private var button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
button.setTitle("Body", for: button.state)
button.frame = CGRect(x: 100, y: 25, width: 100, height: 25)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
view.addSubview(button)
}
@objc func buttonAction(sender: UIButton!) {
self.createPlaneNode()
}
func createPlaneNode() {
//Create a new scene with our 3D object in it
print("Hi")
//This prints everytime the button is pressed, I checked.
let object = SCNScene(named: "art.scnassets/manbody.obj")
let node = SCNNode()
let nodeArray = object!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode)
}
guard let currentFrame = sceneView.session.currentFrame else {
return
}
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.1 // Translate 10 cm in front of the camera
node.simdTransform = simd_mul(currentFrame.camera.transform, translation)
// SCNPlanes are vertically oriented in their local coordinate space.
// Rotate it to match the horizontal orientation of the ARPlaneAnchor.
node.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)
sceneView.scene.rootNode.addChildNode(node)
}
【问题讨论】: