Search code examples
androidandroid-layoutsceneview

Add TextView on SceneView


I try to render 3D model using SceneView and I need to display text on SceneView. The problem that I am facing when viewing the 3D model is that it covers the text.

Enter image description here

Enter image description here

XML code

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <io.github.sceneview.SceneView
        android:id="@+id/sceneView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/topView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This view is on top of the SceneView!"
        android:textSize="24sp"
        android:textColor="#FFFFFF"
        android:background="#88000000"
        android:elevation="10dp"/>

</FrameLayout>

Java code

ModelRenderable.builder()
    .setSource(
        activity,
        Uri.parse(file.toString())
    )
    .setIsFilamentGltf(true)
    .build(lifecycle)
    .thenAccept { modelRenderable ->
    }
    .exceptionally { throwable ->
        val toast = Toast.makeText(
            activity,
            "Unable to load model ".plus(name),
            Toast.LENGTH_LONG
        )
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()
        null
    }

    val modelNode = ModelNode(
        position = Position(z = 0.0f),
        rotation = Rotation(x = 0.0f),
    )

    sceneView.addChild(modelNode)

    lifecycleScope.launchWhenCreated {
        modelNode.loadModel(
            context = activity,
            lifecycle = lifecycle,
            glbFileLocation = "https://sceneview.github.io/assets/models/MaterialSuite.glb",
            scaleToUnits = 1.1f,
            autoAnimate = false,
            centerOrigin = Position(x = 0.0f, y = 0.0f, z = 0f)
        )
    }

I try to use setZOrderMediaOverlay:

sceneView.setZOrderMediaOverlay(true)

But it does not make any effect. I want to keep the 3D model in SceneView under the text when zooming.

How can I achieve this?


Solution

  • I found out the cause of the problem.

    I've been using:

    sceneView.setBackgroundDrawable(resources.getDrawable(R.drawable.bg_white))
    

    That caused the problem.

    To add a background to sceneView:

    sceneView.backgroundColor = Color(25f, 25f, 25f, 1f)
    sceneView.setZOrderOnTop(false)