使Arkit相机垂直而不是水平 [英] making the arkit camera vertical rather than horizontal

查看:86
本文介绍了使Arkit相机垂直而不是水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序,其中用户在文本字段中输入一些文本,然后该应用程序在ar相机前面向用户显示此文本.我将文字正确地放置在相机的前面,并且将文字的锚点更改为文字的中心.但是当我将文本添加到场景中时,文本会绕z轴旋转90度.我知道为什么,但我不知道如何解决.原因是当设备处于横向时,arscene.session的摄像机对于所有x,y,z的旋转角度均为0,但是由于我希望我的应用为纵向,因此我将设备旋转90度,从而将摄像机旋转为很好,并且由于文本具有相同的摄像机旋转角度,因此它也被旋转了.我尝试通过再次绕着z轴旋转文本来纠正文本的旋转,但这不能解决整个问题,因为当我更改手机的方向时,这会影响相机轴,这会影响文本的不同轴(不一样的轴,因为我在校正步骤中旋转了轴).因此,我认为解决该问题的唯一方法是从一开始就将相机旋转为与人像模式保持一致,但是我没有找到任何方法来设置相机的旋转 这是添加文本的代码:

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

任何帮助将不胜感激

推荐答案

您不能将矩阵标识用于任何方向,必须根据设备方向对其进行旋转.在执行矩阵乘法之前,我的应用程序中有一个函数需要对其进行更新:

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
}

这篇关于使Arkit相机垂直而不是水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆