1

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

Kashif Mehmood
  • 323
  • 2
  • 15
  • Did you try moving sceneView initiation to onCreateView? Also since you see black screen only it means that you probably have an issue with sceneView - I'd change the color of sceneView in xml and comment the rest of the code - make sure it's working well before adding nodes and models – Kamil Kacprzak Dec 03 '20 at 12:59
  • yes it changes color i have tried initiating sceneview in on create view before but but it did not work same issue – Kashif Mehmood Dec 03 '20 at 13:47
  • If it works with colour than I don't think the problem is with sceneView. – Kamil Kacprzak Dec 03 '20 at 13:55
  • what can be the problem then i am completely new to all this – Kashif Mehmood Dec 03 '20 at 13:57
  • 1. Try to work with different model see if the problem exist. You can also see your model in outside model viewer - is there a plane that could cover the model from bad angle? is colors on model different enough to be visible on black sceneView ? What's the size of model - maybe it is actually visible, it's just not in camera view 2. Try different positions of camera and node (make sure you set the node in front of camera) 3. You can also try to comment model for now and just set camera and a light so that black sceneView actually doesn't seem black. – Kamil Kacprzak Dec 03 '20 at 14:01

1 Answers1

0

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

Kashif Mehmood
  • 323
  • 2
  • 15