Skip to content

Commit

Permalink
Allow passing custom scene hierarchy to intersect
Browse files Browse the repository at this point in the history
When switching between multiple scenes, add/remove can be very costly.
Also change the intersect handling to handle nested meshes.
  • Loading branch information
kschzt committed Jan 28, 2017
1 parent fa16a80 commit 48af8df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/ray-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class RayInput extends EventEmitter {
delete this.handlers[object.id]
}

update() {
update(meshes) {
let lookAt = new THREE.Vector3(0, 0, -1);
lookAt.applyQuaternion(this.camera.quaternion);

Expand Down Expand Up @@ -166,7 +166,7 @@ export default class RayInput extends EventEmitter {
default:
console.error('Unknown interaction mode.');
}
this.renderer.update();
this.renderer.update(meshes);
this.controller.update();
}

Expand Down
25 changes: 19 additions & 6 deletions src/ray-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,28 @@ export default class RayRenderer extends EventEmitter {
}
}

update() {
update(meshes) {
// Do the raycasting and issue various events as needed.
let intersects = this.raycaster.intersectObjects(meshes, true);
let intersectedMesh;

for (var i=0; i < intersects.length; i++) {
let obj = intersects[i].object;

// traverse the hierarchy backwards, to find a clickable mesh via a child
while (obj && !this.meshes[obj.id]) {
obj = obj.parent;
}

if (obj) {
intersectedMesh = obj.id;
break;
}
}

for (let id in this.meshes) {
let mesh = this.meshes[id];
let intersects = this.raycaster.intersectObject(mesh, true);
if (intersects.length > 1) {
console.warn('Unexpected: multiple meshes intersected.');
}
let isIntersected = (intersects.length > 0);
let isIntersected = (intersectedMesh === id);
let isSelected = this.selected[id];

// If it's newly selected, send rayover.
Expand Down

0 comments on commit 48af8df

Please sign in to comment.