Pick entity or overlay depending on distance

This commit is contained in:
Gabriel Calero 2018-06-27 12:23:43 -03:00
parent 48cdd6d64c
commit 1e09f98f6e

View file

@ -21,59 +21,59 @@ function printd(str) {
print("[clickWeb.js] " + str); print("[clickWeb.js] " + str);
} }
function intersectsWebOverlay(intersection) {
return intersection && intersection.intersects && intersection.overlayID &&
Overlays.getOverlayType(intersection.overlayID) == "web3d";
}
function findOverlayIDRayIntersection(pickRay) { function intersectsWebEntity(intersection) {
// Check 3D overlays and entities. Argument is an object with origin and direction. if (intersection && intersection.intersects && intersection.entityID) {
var rayIntersection = Overlays.findRayIntersection(pickRay); var properties = Entities.getEntityProperties(intersection.entityID, ["type", "sourceUrl"]);
if (rayIntersection && rayIntersection.intersects && rayIntersection.overlayID && return properties.type && properties.type == "Web" && properties.sourceUrl;
Overlays.getOverlayType(rayIntersection.overlayID) == "web3d") {
return rayIntersection.overlayID;
} }
return false; return false;
} }
function findRayIntersection(pickRay) {
function findEntityIDRayIntersection(pickRay) {
// Check 3D overlays and entities. Argument is an object with origin and direction. // Check 3D overlays and entities. Argument is an object with origin and direction.
var rayIntersection = Entities.findRayIntersection(pickRay, true); var overlayRayIntersection = Overlays.findRayIntersection(pickRay);
if (rayIntersection.entityID) { var entityRayIntersection = Entities.findRayIntersection(pickRay, true);
var properties = Entities.getEntityProperties(rayIntersection.entityID, ["type", "sourceUrl"]); var isOverlayInters = intersectsWebOverlay(overlayRayIntersection);
if (properties.type && properties.type == "Web" && properties.sourceUrl) { var isEntityInters = intersectsWebEntity(entityRayIntersection);
return rayIntersection.entityID; if (isOverlayInters &&
} (!isEntityInters ||
overlayRayIntersection.distance < entityRayIntersection.distance)) {
return { type: 'overlay', obj: overlayRayIntersection };
} else if (isEntityInters &&
(!isOverlayInters ||
entityRayIntersection.distance < overlayRayIntersection.distance)) {
return { type: 'entity', obj: entityRayIntersection };
} }
return false; return false;
} }
function touchBegin(event) { function touchBegin(event) {
var overlayID = findOverlayIDRayIntersection(Camera.computePickRay(event.x, event.y)); var intersection = findRayIntersection(Camera.computePickRay(event.x, event.y));
if (overlayID) { if (intersection && intersection.type == 'overlay') {
touchOverlayID = overlayID; touchOverlayID = intersection.obj.overlayID;
touchEntityID = null; touchEntityID = null;
return; } else if (intersection && intersection.type == 'entity') {
} touchEntityID = intersection.obj.entityID;
var entityID = findEntityIDRayIntersection(Camera.computePickRay(event.x, event.y));
if (entityID) {
touchEntityID = entityID;
touchOverlayID = null; touchOverlayID = null;
return;
} }
} }
function touchEnd(event) { function touchEnd(event) {
var overlayID = findOverlayIDRayIntersection(Camera.computePickRay(event.x, event.y)); var intersection = findRayIntersection(Camera.computePickRay(event.x, event.y));
if (touchOverlayID && overlayID == touchOverlayID) { if (intersection && intersection.type == 'overlay' && touchOverlayID == intersection.obj.overlayID) {
var propertiesToGet = {}; var propertiesToGet = {};
propertiesToGet[overlayID] = ['url']; propertiesToGet[overlayID] = ['url'];
var properties = Overlays.getOverlaysProperties(propertiesToGet); var properties = Overlays.getOverlaysProperties(propertiesToGet);
if (properties[overlayID].url) { if (properties[overlayID].url) {
Window.openUrl(properties[overlayID].url); Window.openUrl(properties[overlayID].url);
} }
} } else if (intersection && intersection.type == 'entity' && touchEntityID == intersection.obj.entityID) {
var properties = Entities.getEntityProperties(touchEntityID, ["sourceUrl"]);
var entityID = findEntityIDRayIntersection(Camera.computePickRay(event.x, event.y));
if (touchEntityID && entityID == touchEntityID) {
var properties = Entities.getEntityProperties(entityID, ["sourceUrl"]);
if (properties.sourceUrl) { if (properties.sourceUrl) {
Window.openUrl(properties.sourceUrl); Window.openUrl(properties.sourceUrl);
} }