Search code examples
javaandroidaugmented-realityarcoreandroid-augmented-reality

Placing a static object in the corner of a screen with ArCore


I'm building an AR navigation app and I'm not really familiar with AR aspect of it. Is it possible to place an object in a static position for example in a corner of the screen?

I based my app on the sceneform example from Google: https://github.com/google-ar/sceneform-android-sdk/tree/master/samples/hellosceneform. I already imported the 3D-Model I want to use. Currently the app is looking for anchors on which you can place the object. I just want to place a navigation arrow in a corner of the screen, the arrow should still be able to rotate though.

I already searched for examples or similiar questions, but couldn't find any. Thank you for your help.


Solution

  • Make the 3D model a child of the camera node, by making it a child of the camera node you are forcing it's position to update relative to the camera and not an anchor. Use local rotation to rotate the model.

    [update]

    You can also use sceneform as part of the app's layout.xml. In the layout.xml for the view, add the SceneView as part of the view's layout.

    <com.google.ar.sceneform.SceneView
            android:id="@+id/sceneView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
    />
    

    then within your MainActivity.java

    import android.net.Uri
    import android.os.Bundle
    import android.support.v7.app.AppCompatActivity
    import android.widget.Toast
    import com.google.ar.sceneform.Node
    import com.google.ar.sceneform.Scene
    import com.google.ar.sceneform.math.Vector3
    import com.google.ar.sceneform.rendering.ModelRenderable
    import kotlinx.android.synthetic.main.act_main.*
    
    class MainActivity : AppCompatActivity() {
    
        private lateinit var scene: Scene
        private lateinit var node: Node
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.act_main)
    
            scene = sceneView.scene
            render(Uri.parse("coffee_cup.sfb"))
        }
    
        private fun render(uri: Uri) {
            ModelRenderable.builder()
                .setSource(this, uri)
                .build()
                .thenAccept {
                    addNode(it)
                }
                .exceptionally {
                    Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show()
                    return@exceptionally null
                }
    
        }
    
        private fun addNode(model: ModelRenderable?) {
            model?.let {
                node = Node().apply {
                    setParent(scene)
                    localPosition = Vector3(0f, -2f, -7f)
                    localScale = Vector3(3f, 3f, 3f)
                    renderable = it
                }
    
                scene.addChild(node)
            }
        }
    
        override fun onPause() {
            super.onPause()
            sceneView.pause()
        }
    
        override fun onResume() {
            super.onResume()
            sceneView.resume()
        }
    }