How to change camera type to Orthographic in Sceneform Android SDK?

故事扮演 提交于 2020-01-02 05:26:20

问题


I'd like to use an Orthographic camera when presenting a model in SceneView (without AR). Couldn't find a way to do so in API. Am I missing something or the feature is missing?


回答1:


As far as I know, there's no ORTHO method (cube frustum) for camera projection in ARCore / Sceneform at the moment. But you can make it yourself via 4x4 Matrix. So, all you need to do is to calculate left, right, top, bottom, near and far properties using the following principles.

Here is how your projection matrix 4x4 must look like:

Hope this helps.

Edit: working code where scaleFactor is a value around 1 and height/width are properties of the SceneView.

val newMatrix = buildOrthographicMatrix(1f / scaleFactor, 1f / scaleFactor * height / width, 30f, 0.01f)
camera.projectionMatrix = Matrix(newMatrix)

private fun buildOrthographicMatrix(right: Float, top: Float, far: Float, near: Float): FloatArray {
   val matrix = FloatArray(16)
   matrix[0] = 1 / right
   matrix[1] = 0f
   matrix[2] = 0f
   matrix[3] = 0f
   matrix[4] = 0f
   matrix[5] = 1 / top
   matrix[6] = 0f
   matrix[7] = 0f
   matrix[8] = 0f
   matrix[9] = 0f
   matrix[10] = -2 / (far - near)
   matrix[11] = 0f
   matrix[12] = 0f
   matrix[13] = 0f
   matrix[14] = -(far + near) / (far - near)
   matrix[15] = 1f
   return matrix
}


来源:https://stackoverflow.com/questions/53103712/how-to-change-camera-type-to-orthographic-in-sceneform-android-sdk

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