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
756 stars 151 forks source link

Tap event is called multiple times and outside the node area #522

Open JeromeCHA opened 3 weeks ago

JeromeCHA commented 3 weeks ago

Currently, with the version 2.2.0, I am facing the issue where hitTest result is returned even if we tapped outside the node.

The behavior can be reproduced with the following simple code.

val engine = rememberEngine()
val modelLoader = rememberModelLoader(engine)
val testNode = SphereNode(
    engine = engine,
    radius = 1f,
).apply {
    position = Position(z = -5f)
}
Scene(
    modifier = Modifier.fillMaxSize(),
    engine = engine,
    modelLoader = modelLoader,
    childNodes = rememberNodes {
        add(testNode)
    },
    onTouchEvent = { _, result ->
        // check if the tapped node is our testNode
        if (result?.node == testNode) {
            Toast.makeText(this, "Tapped on the node", Toast.LENGTH_SHORT).show()
        }
        false
    }
)

or with the gesture listener

onGestureListener = rememberOnGestureListener(
    onSingleTapConfirmed = { _, node ->
        if (node == testNode) {
            Toast.makeText(this, "Tapped on the node", Toast.LENGTH_SHORT)
                .show()
        }
    }
),

Here is the result

tap_bug

Is there something I am doing wrong? or anything I can do to avoid this?

Edit : Still occurs in 2.2.1

RGregat commented 3 weeks ago

This is not a solution, but an idea why it happens. My guess is that the SphereNode is using a bounding box for the collision test.

SceneView_SphereNode_Issue

A quick rundown revealed, that a box must be used:

In the collision package there is a class called Sphere.java. Probably a remnant of SceneForm, but you can give it a try.

JeromeCHA commented 3 weeks ago

@RGregat Ohh I see, thank you for the explanation. I hope it can be fixed 🤔 For a SphereNode, I guess you can check if the tap is outside the sphere area, but for some models (using ModelNode for example) it will be tough to avoid that behavior

Shape Result
T