Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 565
Oleksii Masnyi Avatar asked Nov 01 '18 14:11

Oleksii Masnyi


People also ask

What is the sceneform plugin for Android Studio?

Once installed, the Sceneform plugin lets you import, view, and build 3D assets in the Sceneform SDK for AR apps in Android Studio. It requires Android Studio versions 3.1 and above. To install the plugin: In Android Studio open the Plugins settings:

What is sceneform?

Sceneform is a 3D framework that makes it easy for you to build ARCore apps for Android without OpenGL. Sceneform is a 3D framework that makes it easy for you to build ARCore apps for Android without OpenGL.

Where can I find the previous version of sceneform SDK?

This site ( developers.google.com/sceneform) serves as the documentation archive for the previous version, Sceneform SDK for Android 1.15.0. Do not use version 1.17.0 of the Sceneform Maven artifacts.

Can I use sceneform without Arcore or camera?

This sample uses Sceneform and ARCore. To use Sceneform without ARCore, follow the steps below while ignoring the ARCore dependency and CAMERA permission requirements. Use SceneView in your app's layout as described in Build the Scene. This sample app is written as an AR Required app.


1 Answers

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.

enter image description here

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

enter image description here

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

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
}

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

camera.projectionMatrix = Matrix(newMatrix)
like image 105
Andy Jazz Avatar answered Sep 20 '22 01:09

Andy Jazz