[VIEWER] Camera Select Bug Fixes (#791)

* Out of viewer click fixes

* Reset material color too

* Recursive visibility check

* Lint

* Semicolons
This commit is contained in:
David McAllister authored and GitHub committed 2022-10-16 18:58:58 -07:00
1 parent 8dedbe8787
commit 97cec622d8
1 file changed
+20 -10
@@ -25,6 +25,7 @@ export function get_scene_tree() {
const sceneTree = new SceneNode(scene);
const dispatch = useDispatch();
const BANNER_HEIGHT = 50;
// Main camera
const main_camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
@@ -118,6 +119,15 @@ export function get_scene_tree() {
keyMap[keyCode] = true;
}
function checkVisibility(camera) {
let curr = camera;
while (curr !== null) {
if (!curr.visible) return false;
curr = curr.parent;
}
return true;
}
window.addEventListener('keydown', onKeyDown, true);
window.addEventListener('keyup', onKeyUp, true);
@@ -241,15 +251,14 @@ export function get_scene_tree() {
sceneTree.metadata.renderer.getSize(size);
mouseVector.x = 2 * (e.clientX / size.x) - 1;
mouseVector.y = 1 - 2 * ((e.clientY - 50) / size.y);
mouseVector.y = 1 - 2 * ((e.clientY - BANNER_HEIGHT) / size.y);
// hacky out of bounds fix
if (mouseVector.x > 1 || mouseVector.x < -1) {
// some value that won't hit
mouseVector.x = -100;
}
if (mouseVector.y > 1 || mouseVector.y < -1) {
mouseVector.y = -100;
if (mouseVector.x > 1 || mouseVector.x < -1 || mouseVector.y > 1 || mouseVector.y < -1) {
if (selectedCam !== null) {
selectedCam.material.color = new THREE.Color(1, 1, 1);
selectedCam = null;
}
return;
}
raycaster.setFromCamera(mouseVector, sceneTree.metadata.camera);
@@ -259,8 +268,9 @@ export function get_scene_tree() {
selectedCam.material.color = new THREE.Color(1, 1, 1);
selectedCam = null;
}
if (intersections.length > 0) {
selectedCam = intersections[0].object;
const filtered_intersections = intersections.filter(isect => checkVisibility(isect.object));
if (filtered_intersections.length > 0) {
selectedCam = filtered_intersections[0].object;
selectedCam.material.color = new THREE.Color(0xfab300);
}
};