SceneView / sceneview-android

SceneView is a 3D and AR Android Composable and View with Google Filament and ARCore. This is a Sceneform replacement in Kotlin
Apache License 2.0
758 stars 151 forks source link

ViewNode is not visible in jetpack compose project #428

Closed obilesa closed 4 months ago

obilesa commented 4 months ago

Hi! I am encountering an issue where ViewNode is not visible in a project that uses Jetpack Compose: The code with compose:


class MainActivity : ComponentActivity() {
    private lateinit var sceneView: ARSceneView
    private lateinit var viewAttachmentManager: ViewAttachmentManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent{
            SceneviewBugTheme {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                ) {
                    AR()
                }
            }
        }
    }

    @Composable
    fun AR(){
        ARScene(
            onViewCreated = {
                sceneView = this
                viewAttachmentManager = ViewAttachmentManager(sceneView.context, sceneView)
             },
            modifier = Modifier.fillMaxSize()
        )

        Button(onClick = {
            val viewNode = ViewNode(sceneView.engine, sceneView.modelLoader, viewAttachmentManager)
            viewNode.loadView(sceneView.context, R.layout.test, onLoaded = { _, _ ->
                sceneView.addChildNode(viewNode)
                Log.i("ViewNode", "ViewNode loaded")
            }, onError = {
                Log.e("ViewNode", "Error loading view", it)
            })
        }, content = {
            Text(text = "Place Model")
        })
    }

    override fun onPause() {
        super.onPause()
        if(::viewAttachmentManager.isInitialized){
            viewAttachmentManager.onPause()
        }
    }

    override fun onResume() {
        super.onResume()
        if(::viewAttachmentManager.isInitialized){
            viewAttachmentManager.onResume()
        }
    }
}

Similar code using layout, this renders the ViewNode correctly:

class MainActivity : AppCompatActivity() {

    private lateinit var sceneView: ARSceneView
    private lateinit var placeButton: ExtendedFloatingActionButton
    private lateinit var viewAttachmentManager: ViewAttachmentManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        sceneView = findViewById<ARSceneView?>(R.id.sceneView)
        viewAttachmentManager = ViewAttachmentManager(sceneView.context, sceneView)

        placeButton = findViewById(R.id.place)

        placeButton.setOnClickListener {
            placeModel()
        }

    }

    private fun placeModel() {
        val viewNode = ViewNode(sceneView.engine, sceneView.modelLoader, viewAttachmentManager)
        viewNode.loadView(sceneView.context, R.layout.test, onLoaded = { _, _ ->
            sceneView.addChildNode(viewNode)
            Log.i("ViewNode", "ViewNode loaded")
        })

   }

    override fun onPause() {
        super.onPause()
        viewAttachmentManager.onPause()

    }
    override fun onResume() {
        super.onResume()
        viewAttachmentManager.onResume()
    }

}
Kliuchko commented 4 months ago

Check lifecycle calls. Is everything called correctly and in right place?

obilesa commented 4 months ago

@Kliuchko Thanks for the suggestion about checking lifecycle calls. Could you please provide some more guidance on the specific lifecycle calls I should examine, or what indicators to look for that might be causing the ViewNode visibility issue?

Agazor18 commented 4 months ago

try adding this after instantiating view Attachment Manager. viewAttachmentManager.onResume() I don't think it's a very correct solution but it has worked for me so far.

obilesa commented 4 months ago

Thanks, this fixed the problem for me