Better way to execute an action when the user clicks off a mesh

It would be really nice if we had an action that fires when a user clicks anything other than the mesh the action manager is attached to. This would support, e.g. an unselect action. The only thing I have up with is this, which seems clunky, and doesn’t really leverage actions well. I am aware that I could do the whole thing with pointer observables; however, those fire on every pointer event, including move and we can’t leverage the power of actions. Is there a better way that I’m missing? If not, this would be a great enhancement to have to actions.

// Register an action to track when the pointer enters and exits the 
        // mesh
        this.actionManager.registerAction(new BABYLON.ExecuteCodeAction(
            { trigger: BABYLON.ActionManager.OnPointerOutTrigger }, 
            (e) => {
                console.log("Pointer out " + this.id);
                this._pointerIn = false;
            }
        ));
        this.actionManager.registerAction(new BABYLON.ExecuteCodeAction(
            { trigger: BABYLON.ActionManager.OnPointerOverTrigger }, 
            (e) => {
                console.log("Pointer over " + this.id);
                this._pointerIn = true;
            }
        ));

		// Register a pointer observable to detect clicks on or off the 
		//  mesh.  Can't use an action for this as it won't fire when we click off of the mesh
		scene.onPointerObservable.add((e) => {
            if (e.type === BABYLON.PointerEventTypes.POINTERTAP &&
                e.event.button === 0) {
                if (this._pointerIn) {
                    console.log("mesh was picked")
                } else {
                    console.log("something else was picked")
                }
            }
		});

Is this what you want? Each box is pickable. Click somewhere else to unpick. https://playground.babylonjs.com/#RC2IAH#1

1 Like

Thanks for the response. This is using pointer events. I was looking for a way to use actions.

Yeah there is no trigger for that currently. :frowning: but we’d welcome a PR for it :slight_smile: