我正在尝试在场景视图上渲染 3d 模型,但无论我做什么都会出现黑屏。我想要做的是加载一个像 ARFragment 一样的 3d 模型,但具有更多类似场景视图的功能。
这是我的布局的代码
<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 也尝试了这个答案Black screen after loading SceneView, using Scenform, into a fragment 但似乎没有任何效果