user3742929
user3742929

Reputation: 400

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.

Upvotes: 2

Views: 2746

Answers (2)

user3742929
user3742929

Reputation: 400

if (useStaticArrow) {
            // Rotation at the registerListeners is measured, so that the arrow points to north in our coordinate system

            camera.getPose().compose(Pose.makeTranslation(0.37f, -0.17f, -1f))
                    .extractTranslation().toMatrix(cameraAnchorMatrix, 0);

            // Rotate the arrow in the needed direction to the next target
            Matrix.rotateM(cameraAnchorMatrix, 0, neededStartRotation + directionChange, 0f, 1f, 0f);

            virtualObject.updateModelMatrix(cameraAnchorMatrix, scaleFactor / 12f);
            virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, arrowColor);
} 

(Complete Code snippet for Renjith)

Upvotes: 1

Hermes
Hermes

Reputation: 2898

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()
    }
}

Upvotes: 2

Related Questions