1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

click on raycaster

This commit is contained in:
Sander Moolin 2021-03-26 13:37:15 -04:00
parent 808d32b83a
commit c4679c6d1a
2 changed files with 17 additions and 2 deletions

View File

@ -1,7 +1,7 @@
## TODO ## TODO
- [X] Document raycaster component - [X] Document raycaster component
- [X] Document new events/props on Object3D - [X] Document new events/props on Object3D
- [ ] Click events on Raycaster - [X] Click events on Raycaster
- [X] Click events on Object3D - [X] Click events on Object3D
## Changelog ## Changelog
@ -17,6 +17,7 @@ See [here](https://troisjs-instancedcolors.netlify.app/) for an example ([source
:onPointerOver="callback that accepts an array of all new intersections, like onMouseEnter" :onPointerOver="callback that accepts an array of all new intersections, like onMouseEnter"
:onPointerLeave="callback that accepts an array of all newly-ended intersections, like onMouseLeave" :onPointerLeave="callback that accepts an array of all newly-ended intersections, like onMouseLeave"
:onPointerOver="callback that accepts array of all current intersections" :onPointerOver="callback that accepts array of all current intersections"
:onClick="callback that accepts array of currently intersected objects"
:onBeforeRender="callback that fires every frame - optional, accepts the created raycaster. setting this property assumes the user is implementing all raycaster functionality and nullifies all other props and built-in functionality." :onBeforeRender="callback that fires every frame - optional, accepts the created raycaster. setting this property assumes the user is implementing all raycaster functionality and nullifies all other props and built-in functionality."
:scene="THREE scene - optional, defaults to current scene" :scene="THREE scene - optional, defaults to current scene"

View File

@ -20,6 +20,10 @@ export default {
type: Function, type: Function,
default: null default: null
}, },
onClick: {
type: Function,
default: null
},
scene: { scene: {
type: Object, type: Object,
default: null default: null
@ -71,6 +75,9 @@ export default {
// add event listeners // add event listeners
window.addEventListener('mousemove', this.onMouseMove); window.addEventListener('mousemove', this.onMouseMove);
window.addEventListener('touchmove', this.onTouchMove); window.addEventListener('touchmove', this.onTouchMove);
if (this.onClick) {
window.addEventListener('click', this.clickHandler);
}
// TODO: touch // TODO: touch
}, },
methods: { methods: {
@ -106,7 +113,7 @@ export default {
// TODO: optimize // TODO: optimize
const expiredIntersects = this._intersects.filter(intersect => !newObjects.find(val => val.object === intersect.object && val.instanceId === intersect.instanceId)); const expiredIntersects = this._intersects.filter(intersect => !newObjects.find(val => val.object === intersect.object && val.instanceId === intersect.instanceId));
if (expiredIntersects.length) { if (expiredIntersects.length) {
this.onPointerLeave(expiredIntersects) this.onPointerLeave(expiredIntersects);
} }
} }
@ -138,6 +145,12 @@ export default {
this.pointer.x = ((evt.x - canvasLeft) / this.three.size.width) * 2 - 1; this.pointer.x = ((evt.x - canvasLeft) / this.three.size.width) * 2 - 1;
this.pointer.y = - ((evt.y - canvasTop) / this.three.size.height) * 2 + 1; this.pointer.y = - ((evt.y - canvasTop) / this.three.size.height) * 2 + 1;
}, },
clickHandler(evt) {
// if we have any intersects, fire onclick method
if (this._intersects && this._intersects.length) {
this.onClick(this._intersects);
}
}
}, },
render() { render() {
return this.$slots.default ? this.$slots.default() : []; return this.$slots.default ? this.$slots.default() : [];
@ -145,6 +158,7 @@ export default {
unmounted() { unmounted() {
window.removeEventListener('mousemove', this.onMouseMove); window.removeEventListener('mousemove', this.onMouseMove);
window.removeEventListener('touchstart', this.onTouchMove); window.removeEventListener('touchstart', this.onTouchMove);
window.removeEventListener('click', this.clickHandler);
// TODO: touch // TODO: touch
}, },