问题描述
我正在尝试在Sceneview上渲染3D模型,但是无论我做什么,我都会黑屏.我想做的是像在ARFragment中那样加载3d模型,但具有更多的诸如viewview之类的功能.
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.
这是我的布局代码
<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>
这是我的片段
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()
}
}
我尝试了很多操作,例如将scenview放在oncreate/oncreateview中,也尝试了此答案,但似乎没有任何作用
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
推荐答案
未设置背景色的问题.因此,当我将其更改为白色时,它开始正常工作.
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
这篇关于SceneForm SceneView始终在片段中呈黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!