SceneForm sceneview always black in fragment

我怕爱的太早我们不能终老 提交于 2021-01-05 11:28:12

问题


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

来源:https://stackoverflow.com/questions/65125461/sceneform-sceneview-always-black-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!