[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 sceneTree = new SceneNode(scene);
const dispatch = useDispatch(); const dispatch = useDispatch();
const BANNER_HEIGHT = 50;
// Main camera // Main camera
const main_camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000); const main_camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
@@ -118,6 +119,15 @@ export function get_scene_tree() {
keyMap[keyCode] = true; 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('keydown', onKeyDown, true);
window.addEventListener('keyup', onKeyUp, true); window.addEventListener('keyup', onKeyUp, true);
@@ -241,15 +251,14 @@ export function get_scene_tree() {
sceneTree.metadata.renderer.getSize(size); sceneTree.metadata.renderer.getSize(size);
mouseVector.x = 2 * (e.clientX / size.x) - 1; 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 || mouseVector.y > 1 || mouseVector.y < -1) {
if (mouseVector.x > 1 || mouseVector.x < -1) { if (selectedCam !== null) {
// some value that won't hit selectedCam.material.color = new THREE.Color(1, 1, 1);
mouseVector.x = -100; selectedCam = null;
} }
if (mouseVector.y > 1 || mouseVector.y < -1) { return;
mouseVector.y = -100;
} }
raycaster.setFromCamera(mouseVector, sceneTree.metadata.camera); 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.material.color = new THREE.Color(1, 1, 1);
selectedCam = null; selectedCam = null;
} }
if (intersections.length > 0) { const filtered_intersections = intersections.filter(isect => checkVisibility(isect.object));
selectedCam = intersections[0].object; if (filtered_intersections.length > 0) {
selectedCam = filtered_intersections[0].object;
selectedCam.material.color = new THREE.Color(0xfab300); selectedCam.material.color = new THREE.Color(0xfab300);
} }
}; };