当我使用当前ARFrame的变换将对象放在相机前面时,它似乎使用横向作为设备的方向。因此,如果我以纵向方式握住设备并放置一个物体,它将显示为倾斜90度。
我希望相对于设备的肖像放置对象的方向。有可能改变这个吗?或者我必须旋转锚?怎么会这样做?
以下是我创建锚点的方法:
@IBAction func add(_ sender: Any) {
guard let currentFrame = self.sceneView.session.currentFrame else { return }
var translation = matrix_identity_float4x4
translation.columns.3.z = -1
let transform = currentFrame.camera.transform
let anchorTransform = matrix_multiply(transform, translation)
var anchor = ARAnchor(transform: anchorTransform)
self.sceneView.session.add(anchor: anchor)
}
以下是我如何创建渲染节点。请注意,我在一个父节点中有两个子节点,因为我希望能够将这两个节点作为一个节点移动。
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let childOne = SCNNode()
childOne.geometry = SCNBox(width: 0.5, height: 0.1, length: 0.3, chamferRadius: 0)
childOne.geometry?.firstMaterial?.diffuse.contents = UIColor.green
let childTwo = SCNNode()
childTwo.geometry = SCNBox(width: 0.2, height: 0.5, length: 0.1, chamferRadius: 0)
childTwo.position = SCNVector3(0, 0.25, 0)
childTwo.geometry?.firstMaterial?.diffuse.contents = UIColor.red
let node = SCNNode()
node.addChildNode(nodeOne)
node.addChildNode(nodeTwo)
return node
}
更新
通过创建一个旋转矩阵来解决它,将Z轴旋转90度,然后将其与平移矩阵相乘。
@IBAction func add(_ sender: Any) {
guard let currentFrame = self.sceneView.session.currentFrame else { return }
var translation = matrix_identity_float4x4
translation.columns.3.z = -1
let transform = currentFrame.camera.transform
let rotation = matrix_float4x4(SCNMatrix4MakeRotation(Float.pi/2, 0, 0, 1))
let anchorTransform = matrix_multiply(transform, matrix_multiply(translation, rotation))
var anchor = ARAnchor(transform: anchorTransform)
self.sceneView.session.add(anchor: anchor)
}