Another alternative that I have used in a previous project it so just fire an event whenever a piece of UI is hovered over:
// Attached to UI Element
...
this.entity.element.on('mouseenter', function() {
this.app.fire('uiHover');
}, this);
this.entity.element.on('mouseleave', function() {
this.app.fire('uiUnHover');
}, this);
...
//Attached to Raycast Monitor
...
this.app.on('uiHover', this.uiBlock, this);
this.app.on('uiUnHover', this.uiUnBlock, this);
...
Raycast.prototype.uiBlock = function() {
this.blocked = true;
};
Raycast.prototype.uiUnBlock = function() {
this.blocked = false;
};
Raycast.prototype.doRaycast = function (screenX, screenY) {
var from = this.entity.getPosition();
var to = this.entity.camera.screenToWorld(screenX, screenY, this.entity.camera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result && !this.blocked) {
//Do stuff...
}
else {
}
};
This way we just don’t do raycasts at all unless we know the mouse is not over a piece of UI.
I hope this is helpful.
Quick edit: I just realized you were talking about CSS buttons. You could take the same approach with those by firing the events to the app when the mouse is over the buttons, but I realize the pseudo-code isn’t exactly what you’re looking for. Hopefully it may help others.