SceneForm sceneview always black in fragment

1k views Asked by At

I am trying to render a 3d model on sceneview but i am getting black screen no matter what i do . what i am trying to do is to load a 3d model like in ARFragment but with more sceneview like features.

here is the code for my layout

<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="ui.ThreeDView">

    <com.google.ar.sceneform.SceneView
        android:id="@+id/SceneView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

and this is my fragment

class ThreeDView : Fragment(R.layout.sceneview_fragment) {
    lateinit var model: ModelRenderable
    lateinit var link: String
    lateinit var binding: SceneviewFragmentBinding
    lateinit var mScene: Scene
    lateinit var sceneView : SceneView
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding = SceneviewFragmentBinding.bind(view)
    val viewModel = (activity as MainActivity).activitySharedViewModel
    link = viewModel.modelUrl.value.toString()
    viewModel.modelUrl.observe(viewLifecycleOwner, {
        link = viewModel.modelUrl.value.toString()
    })
    sceneView = binding.SceneView



       Log.e("shown", "yes shown")
       createScene()

       Log.e("mot show", "not shown")


}


private fun createScene() {
    mScene = binding.SceneView.scene

    ModelRenderable.builder()
            .setSource(requireContext(), Uri.parse(link))
            .build()
            .thenAccept { renderable: ModelRenderable? ->
                if (renderable != null) {

                    addNodeToScene(renderable)
                }
            }
            .exceptionally { throwable: Throwable? ->
                Log.e("Sceneform", "failed to load model")
                null
            }
}

private fun addNodeToScene(model: ModelRenderable) {
    val cakeNode = Node()


    cakeNode.apply {
        renderable= model
        localPosition =Vector3(.3F, .3F, .3F)
        setParent(mScene)

    }
    with(mScene)
    {
        sunlight.let {
            Light.builder(Light.Type.SPOTLIGHT)
                    .setColor(Color(YELLOW))
                    .setShadowCastingEnabled(true)
                    .build()

        }
        addChild(cakeNode)
    }


    mScene.addChild(cakeNode)


            }

override fun onResume() {
    super.onResume()
    try {
        sceneView.resume()
    }
    catch (e : CameraNotAvailableException)
    {
        e.message
    }


}

override fun onPause() {
    super.onPause()
    sceneView.pause()
}
}

i have tried a lot of stuff like putting scenview in oncreate/oncreateview also tried this answer Black screen after loading SceneView, using Scenform, into a fragment but nothing seems like working

1

There are 1 answers

0
Kashif Mehmood On

the issue was with the background colour was not set. so, when I changed it to white it started working perfectly.

binding.SceneView.renderer!!.setClearColor(Color(LTGRAY))

Moreover, if you are using the navigation component make sure your navgraph is in fragment container view not in a fragment