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

Each click event from multiple anchors #412

Closed iamcomgong closed 1 month ago

iamcomgong commented 4 months ago

hello! I'm using AR SceneView 2.0.2. I'm currently using several geo-anchors in a project, and I want when I click on each anchor, a Toast message containing the anchor's information will appear. I think I can use onSingleTab, but can you tell me the code?

` private fun addAnchorNode(anchor: Anchor) { Log.d("CameraFragmentAR", "addAnchorNode added") val anchorNode = AnchorNode(binding.sceneView.engine, anchor)

    viewLifecycleOwner.lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            binding.sceneView.modelLoader.loadModelInstance("https://sceneview.github.io/assets/models/DamagedHelmet.glb").let { modelInstance ->
                modelInstance?.let {
                    val modelNode = ModelNode(modelInstance = it).apply {
                        playAnimation(0)

                    }
                    anchorNode.addChildNode(modelNode)
                }
            }
        }
    }

    binding.sceneView.addChildNode(anchorNode)
    this.anchorNode = anchorNode
}

`

devR2id commented 4 months ago

Likely, you will need to configure the onSingleTap property along with another property called isTouchable, understanding that the onSingleTap property should be assigned to a function, which in your case would be to display the Toast.

onSingleTapConfirmed

But it's also possible that what you need to touch is the model and not the anchor, which would leave you with something like this

val modelNode = ModelNode(modelInstance = it).apply { playAnimation(0) isTouchable = true onSingleTapConfirmed = ::showToast() }