making the arkit camera vertical rather than horizontal

北慕城南 提交于 2020-04-06 22:21:37

问题


I am trying to make an app where the user enters some text in a textfield and then the app displays this text in front of the ar camera to the user. I positioned the text correctly in front of the camera and I have changed the anchor of the text to be in the center of the text. but when I add the text into the scene the text is rotated 90 degrees around the z-axis. And I know why but I don't know how to solve it. The reason is that the camera of the arscene.session has a rotation of 0 for all x, y, z when the device is in landscape but since I want my app to be in portrait I rotate the device 90 degrees which rotates the camera as well and since the text has the same camera rotation, it's rotated as well. I tried correcting the rotation of the text by rotating it again around the z-axis but that doesn't solve the entire issue because when I change the direction of my phone, that affects the camera axis which will affect different axis of the text(not the same axis because I rotated the axis in the correction step). so I think the only way to solve the issue is to rotate the camera to be in consistent with the portrait mode from the beginning but I haven't found any way to set the rotation of the camera here is the code of adding the text:

private func createTextNode(text:String?)
{
    guard let text = text else {return}
    let arText = SCNText(string: text, extrusionDepth: 1)
    arText.font = UIFont(name: arText.font.fontName, size: 2)
    arText.firstMaterial?.diffuse.contents = selectedColor

    //making the node
    let node = SCNNode()
    node.geometry = arText
    center(node: node)

    guard let currentFrame = sceneView.session.currentFrame else {return}
    let camera = currentFrame.camera
    let cameraTransform = camera.transform
    var newTransform = matrix_identity_float4x4
    newTransform.columns.3.z = -0.2
    let modifiedTransform = matrix_multiply(cameraTransform, newTransform)
    node.transform = SCNMatrix4(modifiedTransform)
    node.scale = SCNVector3(0.02, 0.02, 0.02)
    self.sceneView.scene.rootNode.addChildNode(node)
    node.eulerAngles.x = 90.degrees
}

and that's how the output looks like.. output

any help will be appreciated


回答1:


You cannot use the matrix identity for any orientation, it has to be rotated depending on device orientation. I have a function in my apps that I call to update that before I perform the matrix multiplication :

var translation = matrix_identity_float4x4

func updateTranslationMatrix() {

    switch UIDevice.current.orientation{
    case .portrait, .portraitUpsideDown, .unknown, .faceDown, .faceUp:
        print("portrait ")
        translation.columns.0.x = -cos(.pi/2)
        translation.columns.0.y = sin(.pi/2)
        translation.columns.1.x = -sin(.pi/2)
        translation.columns.1.y = -cos(.pi/2)
    case .landscapeLeft :
        print("landscape left")
        translation.columns.0.x = 1
        translation.columns.0.y = 0
        translation.columns.1.x = 0
        translation.columns.1.y = 1
    case .landscapeRight :
        print("landscape right")
        translation.columns.0.x = cos(.pi)
        translation.columns.0.y = -sin(.pi)
        translation.columns.1.x = sin(.pi)
        translation.columns.1.y = cos(.pi)
    }
    translation.columns.3.z = -0.6 //60cm in front of the camera
}



回答2:


If you mean that you don't want the orientation of the device to change along with the rotation, then:

Go to Project > General > Deployment Info

Under device orientation, uncheck all boxes except 'Portrait'.

If this does not solve your problem and what you really want is to fix the euler angles of your text node, let me know, I'll be happy to help.



来源:https://stackoverflow.com/questions/51566409/making-the-arkit-camera-vertical-rather-than-horizontal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!