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
824 stars 160 forks source link

Integration into Jetpack Compose #201

Closed MatHeartGaming closed 1 year ago

MatHeartGaming commented 1 year ago

Hi, I'm trying to use this library into a pure Jetpack Compose app and so far I've been facing some troubles. I only have a black screen. The code I'm using to initialize the ArScene class is the following:

`class MyActivity : AppCompatActivity() {
    private lateinit var arSceneView: ArSceneView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            arSceneView.planeRenderer.isEnabled = true
            arSceneView.planeRenderer.isVisible = true
            arSceneView.cameraFacingDirection = CameraConfig.FacingDirection.FRONT
            val session = Session(LocalContext.current)
            arSceneView = ArCoreView()
        }
    }

    override fun onResume() {
        super.onResume()
        arSceneView.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        arSceneView.onPause(this)
    }
}`

and then I try to call this View like this:

`@Composable
fun ArCoreView(): ArSceneView {
    val arSceneView = ArSceneView(LocalContext.current)
    Box(modifier = Modifier.fillMaxSize()){
        MyActivity()
    }
    return arSceneView
}
`

Any idea on how to render the camera? Thanks.

ThomasGorisse commented 1 year ago

Jetpack Compose has been added in v0.93 You can use it like this:

@Composable
fun ARScreen() {
    val nodes = remember { mutableStateListOf<ArNode>() }

    Box(modifier = Modifier.fillMaxSize()) {
        ARScene(
            modifier = Modifier.fillMaxSize(),
            nodes = nodes,
            planeRenderer = true,
            onCreate = { arSceneView ->
              // Apply your configuration
            },
            onSessionCreate = { session ->
              // Configure the ARCore session
            },
            onFrame = { arFrame ->
              // Retrieve ARCore frame update
            },
            onTap = { hitResult ->
              // User tapped in the AR view
            }
        )
    }
}