How can I set the background color of SceneView to transparent?

569 Views Asked by At

I am currently developing an Android app in Kotlin and I am using SceneView (https://github.com/SceneView/sceneview-android), since Sceneform was deprecated, to import a 3D Model to my scene. However, I am not being able to set SceneView's background color to transparent (or change it to other color besides black and white).

I have an activity that has two fragments, one of them fits to the whole size of the screen, the other is the SceneView fragment that occupies part of it. Screen

Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.NavigationActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentCamera"
        android:name="com.example.easier.view.CameraFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentSceneView"
        android:name="com.example.easier.view.SceneViewFragment"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.20" />

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.SceneViewFragment">

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

</FrameLayout>

In my fragment, I changed the background color to white with mSceneView.backgroundColor = colorOf(255f, 255f, 255f, 1f), but when I put the alpha = 0f, the whole fragment stays black.

I have also tried adding android:background="@android:color/transparent" to @+id/sceneView, but the whole fragment stays black.

Also, if I change mSceneView.backgroundColor to any other color, p.e., mSceneView.backgroundColor = colorOf(132f, 63f, 18f, 1f), it remains white.

SceneView Fragment:

class SceneViewFragment : Fragment() {

    private lateinit var mSceneView: SceneView

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        mViewModel = ViewModelProvider(this)[NavigationViewModel::class.java]
        return inflater.inflate(R.layout.fragment_scene_view, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mSceneView = view.findViewById(R.id.sceneView)

        val modelNode = ModelNode()
        lifecycleScope.launchWhenCreated {
            modelNode.loadModel(
                context = requireContext(),
                lifecycle = lifecycle,
                glbFileLocation = "low_poly_arrow/scene.gltf",
                autoAnimate = true,
                autoScale = true
            )
        }

        modelNode.scale = Scale(2.5f)
        modelNode.centerModel()
        mSceneView.addChild(modelNode)
        mSceneView.backgroundColor = colorOf(255f, 255f, 255f, 1f)
    }
}

There is still too little information about sceneview-android, I would appreciate some help!

0

There are 0 best solutions below