Skip to content

Commit e1b5ae8

Browse files
authored
Merge pull request #47 from jadh4v/fix-picking-multiple-objects
Fix picking/hover to return closest object
2 parents 6ffe697 + d9b1901 commit e1b5ae8

File tree

1 file changed

+72
-12
lines changed

1 file changed

+72
-12
lines changed

src/core/View.js

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,10 @@ export default class View extends Component {
214214
return;
215215
}
216216
const tolerance = this.getPointerSizeTolerance();
217-
const selection = this.pick(
218-
Math.floor(x - tolerance),
219-
Math.floor(y - tolerance),
220-
Math.ceil(x + tolerance),
221-
Math.ceil(y + tolerance),
222-
false
217+
const selection = this.pickClosest(
218+
Math.floor(x),
219+
Math.floor(y),
220+
tolerance
223221
);
224222

225223
// Share the selection with the rest of the world
@@ -238,12 +236,10 @@ export default class View extends Component {
238236
}
239237

240238
const tolerance = this.getPointerSizeTolerance();
241-
const selection = this.pick(
242-
Math.floor(x - tolerance),
243-
Math.floor(y - tolerance),
244-
Math.ceil(x + tolerance),
245-
Math.ceil(y + tolerance),
246-
false
239+
const selection = this.pickClosest(
240+
Math.floor(x),
241+
Math.floor(y),
242+
tolerance
247243
);
248244

249245
// Guard against trigger of empty selection
@@ -525,6 +521,70 @@ export default class View extends Component {
525521
this.renderWindow.render();
526522
}
527523

524+
pickClosest(xp, yp, tolerance) {
525+
const x1 = Math.floor(xp - tolerance);
526+
const y1 = Math.floor(yp - tolerance);
527+
const x2 = Math.ceil(xp + tolerance);
528+
const y2 = Math.ceil(yp + tolerance);
529+
530+
this.selector.setArea(x1, y1, x2, y2);
531+
this.previousSelectedData = null;
532+
533+
if (this.selector.captureBuffers()) {
534+
const pos = [xp, yp];
535+
const outSelectedPosition = [0, 0];
536+
const info = this.selector.getPixelInformation(
537+
pos,
538+
tolerance,
539+
outSelectedPosition
540+
);
541+
542+
if (info == null || info.prop == null) return [];
543+
544+
const startPoint = this.openglRenderWindow.displayToWorld(
545+
Math.round((x1 + x2) / 2),
546+
Math.round((y1 + y2) / 2),
547+
0,
548+
this.renderer
549+
);
550+
551+
const endPoint = this.openglRenderWindow.displayToWorld(
552+
Math.round((x1 + x2) / 2),
553+
Math.round((y1 + y2) / 2),
554+
1,
555+
this.renderer
556+
);
557+
558+
const ray = [Array.from(startPoint), Array.from(endPoint)];
559+
560+
const worldPosition = Array.from(
561+
this.openglRenderWindow.displayToWorld(
562+
info.displayPosition[0],
563+
info.displayPosition[1],
564+
info.zValue,
565+
this.renderer
566+
)
567+
);
568+
569+
const displayPosition = [
570+
info.displayPosition[0],
571+
info.displayPosition[1],
572+
info.zValue,
573+
];
574+
575+
const selection = [];
576+
selection[0] = {
577+
worldPosition,
578+
displayPosition,
579+
compositeID: info.compositeID,
580+
...info.prop.get('representationId'),
581+
ray,
582+
};
583+
return selection;
584+
}
585+
return [];
586+
}
587+
528588
pick(x1, y1, x2, y2, useFrustrum = false) {
529589
this.selector.setArea(x1, y1, x2, y2);
530590
this.previousSelectedData = null;

0 commit comments

Comments
 (0)