Highlight hand when it intersects an entity

This commit is contained in:
David Rowe 2017-07-06 10:49:17 +12:00
parent 38e7ee3096
commit 72529cf90b

View file

@ -37,18 +37,37 @@
NULL_UUID = "{00000000-0000-0000-0000-000000000000}", NULL_UUID = "{00000000-0000-0000-0000-000000000000}",
HALF_TREE_SCALE = 16384; HALF_TREE_SCALE = 16384;
Highlights = function () { Highlights = function (hand) {
// Draws highlights on selected entities. // Draws highlights on selected entities.
var overlays = [], var handOverlay,
entityOverlays = [],
HIGHLIGHT_COLOR = { red: 240, green: 240, blue: 0 }, HIGHLIGHT_COLOR = { red: 240, green: 240, blue: 0 },
HIGHLIGHT_ALPHA = 0.8; HAND_HIGHLIGHT_ALPHA = 0.35,
ENTITY_HIGHLIGHT_ALPHA = 0.8,
HAND_HIGHLIGHT_DIMENSIONS = { x: 0.2, y: 0.2, z: 0.2 },
HAND_HIGHLIGHT_OFFSET = { x: 0.0, y: 0.11, z: 0.02 };
function maybeAddOverlay(index) { handOverlay = Overlays.addOverlay("sphere", {
if (index >= overlays.length) { dimensions: HAND_HIGHLIGHT_DIMENSIONS,
overlays.push(Overlays.addOverlay("cube", { parentID: AVATAR_SELF_ID,
parentJointIndex: MyAvatar.getJointIndex(hand === LEFT_HAND
? "_CONTROLLER_LEFTHAND"
: "_CONTROLLER_RIGHTHAND"),
localPosition: HAND_HIGHLIGHT_OFFSET,
color: HIGHLIGHT_COLOR, color: HIGHLIGHT_COLOR,
alpha: HIGHLIGHT_ALPHA, alpha: HAND_HIGHLIGHT_ALPHA,
solid: true,
drawInFront: true,
ignoreRayIntersection: true,
visible: false
});
function maybeAddEntityOverlay(index) {
if (index >= entityOverlays.length) {
entityOverlays.push(Overlays.addOverlay("cube", {
color: HIGHLIGHT_COLOR,
alpha: ENTITY_HIGHLIGHT_ALPHA,
solid: false, solid: false,
drawInFront: true, drawInFront: true,
ignoreRayIntersection: true, ignoreRayIntersection: true,
@ -57,8 +76,8 @@
} }
} }
function editOverlay(index, details) { function editEntityOverlay(index, details) {
Overlays.editOverlay(overlays[index], { Overlays.editOverlay(entityOverlays[index], {
position: details.position, position: details.position,
registrationPoint: details.registrationPoint, registrationPoint: details.registrationPoint,
rotation: details.rotation, rotation: details.rotation,
@ -67,20 +86,23 @@
}); });
} }
function display(selection) { function display(handSelected, selection) {
var i, var i,
length; length;
// Add/edit overlay. // Show/hide hand overlay.
Overlays.editOverlay(handOverlay, { visible: handSelected });
// Add/edit entity overlay.
for (i = 0, length = selection.length; i < length; i += 1) { for (i = 0, length = selection.length; i < length; i += 1) {
maybeAddOverlay(i); maybeAddEntityOverlay(i);
editOverlay(i, selection[i]); editEntityOverlay(i, selection[i]);
} }
// Delete extra overlays. // Delete extra entity overlays.
for (i = overlays.length - 1, length = selection.length; i >= length; i -= 1) { for (i = entityOverlays.length - 1, length = selection.length; i >= length; i -= 1) {
Overlays.deleteOverlay(overlays[i]); Overlays.deleteOverlay(entityOverlays[i]);
overlays.splice(i, 1); entityOverlays.splice(i, 1);
} }
} }
@ -88,14 +110,19 @@
var i, var i,
length; length;
for (i = 0, length = overlays.length; i < length; i += 1) { // Hide hand overlay.
Overlays.deleteOverlay(overlays[i]); Overlays.editOverlay(handOverlay, { visible: false });
// Delete entity overlays.
for (i = 0, length = entityOverlays.length; i < length; i += 1) {
Overlays.deleteOverlay(entityOverlays[i]);
} }
overlays = []; entityOverlays = [];
} }
function destroy() { function destroy() {
clear(); clear();
Overlays.deleteOverlay(handOverlay);
} }
if (!this instanceof Highlights) { if (!this instanceof Highlights) {
@ -337,7 +364,6 @@
isTriggerPressed = false, isTriggerPressed = false,
isHandHover = false,
isLaserOn = false, isLaserOn = false,
hoveredEntityID = null, hoveredEntityID = null,
@ -369,7 +395,7 @@
laser = new Laser(hand); laser = new Laser(hand);
selection = new Selection(); selection = new Selection();
highlights = new Highlights(); highlights = new Highlights(hand);
function startEditing(handPose) { function startEditing(handPose) {
var selectionPositionAndOrientation; var selectionPositionAndOrientation;
@ -405,13 +431,6 @@
isEditing = false; isEditing = false;
} }
function clearLaser() {
laser.clear();
selection.clear();
highlights.clear();
laseredEntityID = null;
}
function isEditableEntity(entityID) { function isEditableEntity(entityID) {
// Entity trees are moved as a group so check the root entity. // Entity trees are moved as a group so check the root entity.
var properties = Entities.getEntityProperties(entityID, EDITIBLE_ENTITY_QUERY_PROPERTYES); var properties = Entities.getEntityProperties(entityID, EDITIBLE_ENTITY_QUERY_PROPERTYES);
@ -477,13 +496,13 @@
} }
intersection = { intersection = {
intersects: entityID !== null, intersects: entityID !== null,
entityID: entityID entityID: entityID,
handSelected: true
}; };
isHandHover = intersection.intersects;
// Laser-entity intersection, if any. // Laser-entity intersection, if any.
wasLaserOn = isLaserOn; wasLaserOn = isLaserOn;
if (!isHandHover && isTriggerPressed) { if (!intersection.intersects && isTriggerPressed) {
handPosition = Vec3.sum(Vec3.multiplyQbyV(MyAvatar.orientation, handPose.translation), MyAvatar.position); handPosition = Vec3.sum(Vec3.multiplyQbyV(MyAvatar.orientation, handPose.translation), MyAvatar.position);
handOrientation = Quat.multiply(MyAvatar.orientation, handPose.rotation); handOrientation = Quat.multiply(MyAvatar.orientation, handPose.rotation);
deltaOrigin = Vec3.multiplyQbyV(handOrientation, GRAB_POINT_SPHERE_OFFSET); deltaOrigin = Vec3.multiplyQbyV(handOrientation, GRAB_POINT_SPHERE_OFFSET);
@ -538,7 +557,7 @@
if (wasEditing || intersection.entityID !== hoveredEntityID) { if (wasEditing || intersection.entityID !== hoveredEntityID) {
hoveredEntityID = intersection.entityID; hoveredEntityID = intersection.entityID;
selection.select(hoveredEntityID); selection.select(hoveredEntityID);
highlights.display(selection.selection()); highlights.display(intersection.handSelected, selection.selection());
} }
} else { } else {
// Unhover entities. // Unhover entities.