1
ボタンをARSCNViewにプログラムで追加し、タップしたときに呼び出される関数を追加しました。この関数は3Dオブジェクトを作成して配置します。ボタンは最初に押されたときには完全に機能しますが、2回目以降はオブジェクトを配置しません。私のボタンが2回目に押されたときに3Dオブジェクトの配置が機能しない
はここに私のコードです:
プライベートVARボタン= 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)
}
ありがとうございました。私はそれを自分で考え出しました。 質問:毎回同じ位置に置かずにノードを回転させるにはどうすればよいですか? – JHarden13
@ Jharden13これは私の答えの最後の行に説明されていますが、どうか不明な点がある場合はお知らせください。 – diviaki
大丈夫です。 node.pivot = SCNMatrix4MakeRotation(-Float.pi/2、1、0、0)を設定するだけですか? – JHarden13