ios - Placement of 3D object not working the SECOND time my button is pressed -
i programmatically added button arscnview , added function that's called whenever it's tapped. function creates , place 3d object. button works first time it's pressed, second time onwards not place object.
here's code:
private var button = uibutton()
override func viewdidload() { super.viewdidload() // set 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 new scene our 3d object in print("hi") //this prints everytime button pressed, checked. let object = scnscene(named: "art.scnassets/manbody.obj") let node = scnnode() let nodearray = object!.rootnode.childnodes 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 camera node.simdtransform = simd_mul(currentframe.camera.transform, translation) // scnplanes vertically oriented in local coordinate space. // rotate match horizontal orientation of arplaneanchor. node.transform = scnmatrix4makerotation(-float.pi / 2, 1, 0, 0) sceneview.scene.rootnode.addchildnode(node) }
it work second time, check poly count. ;)
however, cannot see result of subsequent calls keep putting object @ same place.
you set new position here correctly:
node.simdtransform = simd_mul(currentframe.camera.transform, translation)
but overwrite transform on next line:
node.transform = scnmatrix4makerotation(-float.pi / 2, 1, 0, 0)
note node.simdtransform
, node.transform
different "views" of same thing.
change node's pivot apply rotation instead, or multiply transform instead of overwriting it.
Comments
Post a Comment