added faceAvatar and centerEndY, working on updating teleport.js, style fixes

This commit is contained in:
SamGondelman 2017-07-17 13:07:42 -07:00
parent af12b5a4bf
commit ae99be0350
9 changed files with 740 additions and 203 deletions

View file

@ -10,44 +10,56 @@
//
#include "LaserPointer.h"
#include "RayPickManager.h"
#include "JointRayPick.h"
#include "StaticRayPick.h"
#include "Application.h"
#include "avatar/AvatarManager.h"
LaserPointer::LaserPointer(const QString& jointName, const glm::vec3& posOffset, const glm::vec3& dirOffset, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool enabled) :
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled) :
_renderingEnabled(enabled),
_renderStates(renderStates)
_renderStates(renderStates),
_faceAvatar(faceAvatar),
_centerEndY(centerEndY)
{
_rayPickUID = RayPickManager::getInstance().addRayPick(std::make_shared<JointRayPick>(jointName, posOffset, dirOffset, filter, maxDistance, enabled));
for (auto& state : _renderStates.keys()) {
if (!enabled || state != _currentRenderState)
disableRenderState(state);
if (!enabled || state != _currentRenderState) {
disableRenderState(state);
}
}
}
LaserPointer::LaserPointer(const glm::vec3& position, const glm::vec3& direction, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool enabled) :
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled) :
_renderingEnabled(enabled),
_renderStates(renderStates)
_renderStates(renderStates),
_faceAvatar(faceAvatar),
_centerEndY(centerEndY)
{
_rayPickUID = RayPickManager::getInstance().addRayPick(std::make_shared<StaticRayPick>(position, direction, filter, maxDistance, enabled));
for (auto& state : _renderStates.keys()) {
if (!enabled || state != _currentRenderState)
if (!enabled || state != _currentRenderState) {
disableRenderState(state);
}
}
}
LaserPointer::~LaserPointer() {
RayPickManager::getInstance().removeRayPick(_rayPickUID);
for (RenderState& renderState : _renderStates) {
if (!renderState.getStartID().isNull()) qApp->getOverlays().deleteOverlay(renderState.getStartID());
if (!renderState.getPathID().isNull()) qApp->getOverlays().deleteOverlay(renderState.getPathID());
if (!renderState.getEndID().isNull()) qApp->getOverlays().deleteOverlay(renderState.getEndID());
if (!renderState.getStartID().isNull()) {
qApp->getOverlays().deleteOverlay(renderState.getStartID());
}
if (!renderState.getPathID().isNull()) {
qApp->getOverlays().deleteOverlay(renderState.getPathID());
}
if (!renderState.getEndID().isNull()) {
qApp->getOverlays().deleteOverlay(renderState.getEndID());
}
}
}
@ -59,18 +71,18 @@ void LaserPointer::enable() {
void LaserPointer::disable() {
RayPickManager::getInstance().disableRayPick(_rayPickUID);
_renderingEnabled = false;
if (!_currentRenderState.isEmpty() && _renderStates.contains(_currentRenderState)) disableRenderState(_currentRenderState);
if (!_currentRenderState.isEmpty() && _renderStates.contains(_currentRenderState)) {
disableRenderState(_currentRenderState);
}
}
void LaserPointer::setRenderState(const QString& state) {
if (!_currentRenderState.isEmpty() && _renderStates.contains(_currentRenderState)) disableRenderState(_currentRenderState);
if (!_currentRenderState.isEmpty() && _renderStates.contains(_currentRenderState)) {
disableRenderState(_currentRenderState);
}
_currentRenderState = state;
}
const RayPickResult& LaserPointer::getPrevRayPickResult() {
return RayPickManager::getInstance().getPrevRayPickResult(_rayPickUID);
}
void LaserPointer::disableRenderState(const QString& renderState) {
if (!_renderStates[renderState].getStartID().isNull()) {
QVariantMap startProps;
@ -103,7 +115,8 @@ void LaserPointer::update() {
startProps.insert("ignoreRayIntersection", _renderStates[_currentRenderState].doesStartIgnoreRays());
qApp->getOverlays().editOverlay(_renderStates[_currentRenderState].getStartID(), startProps);
}
QVariant end = vec3toVariant(pickRay.origin + pickRay.direction * prevRayPickResult.distance);
glm::vec3 endVec = pickRay.origin + pickRay.direction * prevRayPickResult.distance;
QVariant end = vec3toVariant(endVec);
if (!_renderStates[_currentRenderState].getPathID().isNull()) {
QVariantMap pathProps;
pathProps.insert("start", vec3toVariant(pickRay.origin));
@ -114,7 +127,16 @@ void LaserPointer::update() {
}
if (!_renderStates[_currentRenderState].getEndID().isNull()) {
QVariantMap endProps;
endProps.insert("position", end);
if (_centerEndY) {
endProps.insert("position", end);
} else {
glm::vec3 dim = vec3FromVariant(qApp->getOverlays().getProperty(_renderStates[_currentRenderState].getEndID(), "dimensions").value);
endProps.insert("position", vec3toVariant(endVec + glm::vec3(0, 0.5f * dim.y, 0)));
}
if (_faceAvatar) {
glm::quat rotation = glm::inverse(glm::quat_cast(glm::lookAt(endVec, DependencyManager::get<AvatarManager>()->getMyAvatar()->getPosition(), Vectors::UP)));
endProps.insert("rotation", quatToVariant(glm::quat(glm::radians(glm::vec3(0, glm::degrees(safeEulerAngles(rotation)).y, 0)))));
}
endProps.insert("visible", true);
endProps.insert("ignoreRayIntersection", _renderStates[_currentRenderState].doesEndIgnoreRays());
qApp->getOverlays().editOverlay(_renderStates[_currentRenderState].getEndID(), endProps);
@ -127,7 +149,13 @@ void LaserPointer::update() {
RenderState::RenderState(const OverlayID& startID, const OverlayID& pathID, const OverlayID& endID) :
_startID(startID), _pathID(pathID), _endID(endID)
{
if (!_startID.isNull()) _startIgnoreRays = qApp->getOverlays().getOverlay(_startID)->getProperty("ignoreRayIntersection").toBool();
if (!_pathID.isNull()) _pathIgnoreRays = qApp->getOverlays().getOverlay(_pathID)->getProperty("ignoreRayIntersection").toBool();
if (!_endID.isNull()) _endIgnoreRays = qApp->getOverlays().getOverlay(_endID)->getProperty("ignoreRayIntersection").toBool();
if (!_startID.isNull()) {
_startIgnoreRays = qApp->getOverlays().getOverlay(_startID)->getProperty("ignoreRayIntersection").toBool();
}
if (!_pathID.isNull()) {
_pathIgnoreRays = qApp->getOverlays().getOverlay(_pathID)->getProperty("ignoreRayIntersection").toBool();
}
if (!_endID.isNull()) {
_endIgnoreRays = qApp->getOverlays().getOverlay(_endID)->getProperty("ignoreRayIntersection").toBool();
}
}

View file

@ -14,6 +14,7 @@
#include <QString>
#include "glm/glm.hpp"
#include "ui/overlays/Overlay.h"
#include "RayPickManager.h"
class RayPickResult;
@ -44,15 +45,15 @@ class LaserPointer {
public:
LaserPointer(const QString& jointName, const glm::vec3& posOffset, const glm::vec3& dirOffset, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool enabled);
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled);
LaserPointer(const glm::vec3& position, const glm::vec3& direction, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool enabled);
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled);
~LaserPointer();
unsigned int getUID() { return _rayPickUID; }
void enable();
void disable();
const RayPickResult& getPrevRayPickResult();
const RayPickResult& getPrevRayPickResult() { return RayPickManager::getInstance().getPrevRayPickResult(_rayPickUID); }
void setRenderState(const QString& state);
void disableRenderState(const QString& renderState);
@ -63,6 +64,9 @@ private:
bool _renderingEnabled;
QString _currentRenderState { "" };
QHash<QString, RenderState> _renderStates;
bool _faceAvatar;
bool _centerEndY;
unsigned int _rayPickUID;
};

View file

@ -18,15 +18,16 @@ LaserPointerManager& LaserPointerManager::getInstance() {
}
unsigned int LaserPointerManager::createLaserPointer(const QString& jointName, const glm::vec3& posOffset, const glm::vec3& dirOffset, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool enabled) {
std::shared_ptr<LaserPointer> laserPointer = std::make_shared<LaserPointer>(jointName, posOffset, dirOffset, filter, maxDistance, renderStates, enabled);
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled) {
std::shared_ptr<LaserPointer> laserPointer = std::make_shared<LaserPointer>(jointName, posOffset, dirOffset, filter, maxDistance, renderStates, faceAvatar, centerEndY, enabled);
unsigned int uid = laserPointer->getUID();
_laserPointers[uid] = laserPointer;
return uid;
}
unsigned int LaserPointerManager::createLaserPointer(const glm::vec3& position, const glm::vec3& direction, const uint16_t filter, const float maxDistance, const QHash<QString, RenderState>& renderStates, const bool enabled) {
std::shared_ptr<LaserPointer> laserPointer = std::make_shared<LaserPointer>(position, direction, filter, maxDistance, renderStates, enabled);
unsigned int LaserPointerManager::createLaserPointer(const glm::vec3& position, const glm::vec3& direction, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled) {
std::shared_ptr<LaserPointer> laserPointer = std::make_shared<LaserPointer>(position, direction, filter, maxDistance, renderStates, faceAvatar, centerEndY, enabled);
unsigned int uid = laserPointer->getUID();
_laserPointers[uid] = laserPointer;
return uid;
@ -50,7 +51,7 @@ void LaserPointerManager::setRenderState(unsigned int uid, const QString & rende
}
}
const RayPickResult& LaserPointerManager::getPrevRayPickResult(const unsigned int uid) {
const RayPickResult LaserPointerManager::getPrevRayPickResult(const unsigned int uid) {
if (_laserPointers.contains(uid)) {
return _laserPointers[uid]->getPrevRayPickResult();
}

View file

@ -26,14 +26,14 @@ public:
static LaserPointerManager& getInstance();
unsigned int createLaserPointer(const QString& jointName, const glm::vec3& posOffset, const glm::vec3& dirOffset, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool enabled);
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled);
unsigned int createLaserPointer(const glm::vec3& position, const glm::vec3& direction, const uint16_t filter, const float maxDistance,
const QHash<QString, RenderState>& renderStates, const bool enabled);
const QHash<QString, RenderState>& renderStates, const bool faceAvatar, const bool centerEndY, const bool enabled);
void removeLaserPointer(const unsigned int uid) { _laserPointers.remove(uid); }
void enableLaserPointer(const unsigned int uid);
void disableLaserPointer(const unsigned int uid);
void setRenderState(unsigned int uid, const QString& renderState);
const RayPickResult& getPrevRayPickResult(const unsigned int uid);
const RayPickResult getPrevRayPickResult(const unsigned int uid);
void update();

View file

@ -35,6 +35,16 @@ uint32_t LaserPointerScriptingInterface::createLaserPointer(const QVariant& prop
maxDistance = propertyMap["maxDistance"].toFloat();
}
bool faceAvatar = false;
if (propertyMap["faceAvatar"].isValid()) {
faceAvatar = propertyMap["faceAvatar"].toBool();
}
bool centerEndY = true;
if (propertyMap["centerEndY"].isValid()) {
centerEndY = propertyMap["centerEndY"].toBool();
}
bool enabled = false;
if (propertyMap["enabled"].isValid()) {
enabled = propertyMap["enabled"].toBool();
@ -94,7 +104,7 @@ uint32_t LaserPointerScriptingInterface::createLaserPointer(const QVariant& prop
dirOffset = vec3FromVariant(propertyMap["dirOffset"]);
}
return LaserPointerManager::getInstance().createLaserPointer(jointName, posOffset, dirOffset, filter, maxDistance, renderStates, enabled);
return LaserPointerManager::getInstance().createLaserPointer(jointName, posOffset, dirOffset, filter, maxDistance, renderStates, faceAvatar, centerEndY, enabled);
} else if (propertyMap["position"].isValid()) {
glm::vec3 position = vec3FromVariant(propertyMap["position"]);
@ -103,7 +113,7 @@ uint32_t LaserPointerScriptingInterface::createLaserPointer(const QVariant& prop
direction = vec3FromVariant(propertyMap["direction"]);
}
return LaserPointerManager::getInstance().createLaserPointer(position, direction, filter, maxDistance, renderStates, enabled);
return LaserPointerManager::getInstance().createLaserPointer(position, direction, filter, maxDistance, renderStates, faceAvatar, centerEndY, enabled);
}
return 0;

View file

@ -178,7 +178,7 @@ void RayPickManager::disableRayPick(const unsigned int uid) {
}
}
const PickRay& RayPickManager::getPickRay(const unsigned int uid) {
const PickRay RayPickManager::getPickRay(const unsigned int uid) {
if (_rayPicks.contains(uid)) {
bool valid;
PickRay pickRay = _rayPicks[uid]->getPickRay(valid);
@ -189,7 +189,7 @@ const PickRay& RayPickManager::getPickRay(const unsigned int uid) {
return PickRay();
}
const RayPickResult& RayPickManager::getPrevRayPickResult(const unsigned int uid) {
const RayPickResult RayPickManager::getPrevRayPickResult(const unsigned int uid) {
// TODO:
// does this need to lock the individual ray? what happens with concurrent set/get?
if (_rayPicks.contains(uid)) {

View file

@ -63,8 +63,8 @@ public:
void removeRayPick(const unsigned int uid);
void enableRayPick(const unsigned int uid);
void disableRayPick(const unsigned int uid);
const PickRay& getPickRay(const unsigned int uid);
const RayPickResult& getPrevRayPickResult(const unsigned int uid);
const PickRay getPickRay(const unsigned int uid);
const RayPickResult getPrevRayPickResult(const unsigned int uid);
private:
QHash<unsigned int, std::shared_ptr<RayPick>> _rayPicks;

View file

@ -0,0 +1,538 @@
"use strict";
// Created by james b. pollack @imgntn on 7/2/2016
// Copyright 2016 High Fidelity, Inc.
//
// Creates a beam and target and then teleports you there. Release when its close to you to cancel.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
(function() { // BEGIN LOCAL_SCOPE
var inTeleportMode = false;
var SMOOTH_ARRIVAL_SPACING = 33;
var NUMBER_OF_STEPS = 6;
var TARGET_MODEL_URL = Script.resolvePath("../assets/models/teleport-destination.fbx");
var TOO_CLOSE_MODEL_URL = Script.resolvePath("../assets/models/teleport-cancel.fbx");
var SEAT_MODEL_URL = Script.resolvePath("../assets/models/teleport-seat.fbx");
var TARGET_MODEL_DIMENSIONS = {
x: 1.15,
y: 0.5,
z: 1.15
};
var COLORS_TELEPORT_SEAT = {
red: 255,
green: 0,
blue: 170
};
var COLORS_TELEPORT_CAN_TELEPORT = {
red: 97,
green: 247,
blue: 255
};
var COLORS_TELEPORT_CANNOT_TELEPORT = {
red: 0,
green: 121,
blue: 141
};
var COLORS_TELEPORT_CANCEL = {
red: 255,
green: 184,
blue: 73
};
var TELEPORT_CANCEL_RANGE = 1;
var COOL_IN_DURATION = 500;
var handInfo = {
right: {
controllerInput: Controller.Standard.RightHand
},
left: {
controllerInput: Controller.Standard.LeftHand
}
};
function ThumbPad(hand) {
this.hand = hand;
var _thisPad = this;
this.buttonPress = function(value) {
_thisPad.buttonValue = value;
};
}
function Trigger(hand) {
this.hand = hand;
var _this = this;
this.buttonPress = function(value) {
_this.buttonValue = value;
};
this.down = function() {
var down = _this.buttonValue === 1 ? 1.0 : 0.0;
return down;
};
}
var coolInTimeout = null;
var ignoredEntities = [];
var TELEPORTER_STATES = {
IDLE: 'idle',
COOL_IN: 'cool_in',
TARGETTING: 'targetting',
TARGETTING_INVALID: 'targetting_invalid',
};
var TARGET = {
NONE: 'none', // Not currently targetting anything
INVISIBLE: 'invisible', // The current target is an invvsible surface
INVALID: 'invalid', // The current target is invalid (wall, ceiling, etc.)
SURFACE: 'surface', // The current target is a valid surface
SEAT: 'seat', // The current target is a seat
};
function Teleporter() {
var _this = this;
this.active = false;
this.state = TELEPORTER_STATES.IDLE;
this.currentTarget = TARGET.INVALID;
this.overlayLines = {
left: null,
right: null,
};
this.updateConnected = null;
this.activeHand = null;
this.teleporterMappingInternalName = 'Hifi-Teleporter-Internal-Dev-' + Math.random();
this.teleportMappingInternal = Controller.newMapping(this.teleporterMappingInternalName);
// Setup overlays
this.cancelOverlay = Overlays.addOverlay("model", {
url: TOO_CLOSE_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
visible: false
});
this.targetOverlay = Overlays.addOverlay("model", {
url: TARGET_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
visible: false
});
this.seatOverlay = Overlays.addOverlay("model", {
url: SEAT_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
visible: false
});
this.enableMappings = function() {
Controller.enableMapping(this.teleporterMappingInternalName);
};
this.disableMappings = function() {
Controller.disableMapping(teleporter.teleporterMappingInternalName);
};
this.cleanup = function() {
this.disableMappings();
Overlays.deleteOverlay(this.targetOverlay);
this.targetOverlay = null;
Overlays.deleteOverlay(this.cancelOverlay);
this.cancelOverlay = null;
Overlays.deleteOverlay(this.seatOverlay);
this.seatOverlay = null;
this.deleteOverlayBeams();
if (this.updateConnected === true) {
Script.update.disconnect(this, this.update);
}
};
this.enterTeleportMode = function(hand) {
if (inTeleportMode === true) {
return;
}
if (isDisabled === 'both' || isDisabled === hand) {
return;
}
inTeleportMode = true;
if (coolInTimeout !== null) {
Script.clearTimeout(coolInTimeout);
}
this.state = TELEPORTER_STATES.COOL_IN;
coolInTimeout = Script.setTimeout(function() {
if (_this.state === TELEPORTER_STATES.COOL_IN) {
_this.state = TELEPORTER_STATES.TARGETTING;
}
}, COOL_IN_DURATION);
this.activeHand = hand;
this.enableMappings();
Script.update.connect(this, this.update);
this.updateConnected = true;
};
this.exitTeleportMode = function(value) {
if (this.updateConnected === true) {
Script.update.disconnect(this, this.update);
}
this.disableMappings();
this.deleteOverlayBeams();
this.hideTargetOverlay();
this.hideCancelOverlay();
this.updateConnected = null;
this.state = TELEPORTER_STATES.IDLE;
inTeleportMode = false;
};
this.deleteOverlayBeams = function() {
for (var key in this.overlayLines) {
if (this.overlayLines[key] !== null) {
Overlays.deleteOverlay(this.overlayLines[key]);
this.overlayLines[key] = null;
}
}
};
this.update = function() {
if (_this.state === TELEPORTER_STATES.IDLE) {
return;
}
// Get current hand pose information so that we can get the direction of the teleport beam
var pose = Controller.getPoseValue(handInfo[_this.activeHand].controllerInput);
var handPosition = pose.valid ? Vec3.sum(Vec3.multiplyQbyV(MyAvatar.orientation, pose.translation), MyAvatar.position) : MyAvatar.getHeadPosition();
var handRotation = pose.valid ? Quat.multiply(MyAvatar.orientation, pose.rotation) :
Quat.multiply(MyAvatar.headOrientation, Quat.angleAxis(-90, {
x: 1,
y: 0,
z: 0
}));
var pickRay = {
origin: handPosition,
direction: Quat.getUp(handRotation),
};
// We do up to 2 ray picks to find a teleport location.
// There are 2 types of teleport locations we are interested in:
// 1. A visible floor. This can be any entity surface that points within some degree of "up"
// 2. A seat. The seat can be visible or invisible.
//
// * In the first pass we pick against visible and invisible entities so that we can find invisible seats.
// We might hit an invisible entity that is not a seat, so we need to do a second pass.
// * In the second pass we pick against visible entities only.
//
var intersection = Entities.findRayIntersection(pickRay, true, [], [this.targetEntity].concat(ignoredEntities), false, true);
var teleportLocationType = getTeleportTargetType(intersection);
if (teleportLocationType === TARGET.INVISIBLE) {
intersection = Entities.findRayIntersection(pickRay, true, [], [this.targetEntity].concat(ignoredEntities), true, true);
teleportLocationType = getTeleportTargetType(intersection);
}
if (teleportLocationType === TARGET.NONE) {
this.hideTargetOverlay();
this.hideCancelOverlay();
this.hideSeatOverlay();
var farPosition = Vec3.sum(pickRay.origin, Vec3.multiply(pickRay.direction, 50));
this.updateLineOverlay(_this.activeHand, pickRay.origin, farPosition, COLORS_TELEPORT_CANNOT_TELEPORT);
} else if (teleportLocationType === TARGET.INVALID || teleportLocationType === TARGET.INVISIBLE) {
this.hideTargetOverlay();
this.hideSeatOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_CANCEL);
this.updateDestinationOverlay(this.cancelOverlay, intersection);
} else if (teleportLocationType === TARGET.SURFACE) {
if (this.state === TELEPORTER_STATES.COOL_IN) {
this.hideTargetOverlay();
this.hideSeatOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_CANCEL);
this.updateDestinationOverlay(this.cancelOverlay, intersection);
} else {
this.hideCancelOverlay();
this.hideSeatOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection,
COLORS_TELEPORT_CAN_TELEPORT);
this.updateDestinationOverlay(this.targetOverlay, intersection);
}
} else if (teleportLocationType === TARGET.SEAT) {
this.hideCancelOverlay();
this.hideTargetOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_SEAT);
this.updateDestinationOverlay(this.seatOverlay, intersection);
}
if (((_this.activeHand === 'left' ? leftPad : rightPad).buttonValue === 0) && inTeleportMode === true) {
// remember the state before we exit teleport mode and set it back to IDLE
var previousState = this.state;
this.exitTeleportMode();
this.hideCancelOverlay();
this.hideTargetOverlay();
this.hideSeatOverlay();
if (teleportLocationType === TARGET.NONE || teleportLocationType === TARGET.INVALID || previousState === TELEPORTER_STATES.COOL_IN) {
// Do nothing
} else if (teleportLocationType === TARGET.SEAT) {
Entities.callEntityMethod(intersection.entityID, 'sit');
} else if (teleportLocationType === TARGET.SURFACE) {
var offset = getAvatarFootOffset();
intersection.intersection.y += offset;
MyAvatar.goToLocation(intersection.intersection, false, {x: 0, y: 0, z: 0, w: 1}, false);
HMD.centerUI();
MyAvatar.centerBody();
}
}
};
this.updateLineOverlay = function(hand, closePoint, farPoint, color) {
if (this.overlayLines[hand] === null) {
var lineProperties = {
start: closePoint,
end: farPoint,
color: color,
ignoreRayIntersection: true,
visible: true,
alpha: 1,
solid: true,
drawInFront: true,
glow: 1.0
};
this.overlayLines[hand] = Overlays.addOverlay("line3d", lineProperties);
} else {
Overlays.editOverlay(this.overlayLines[hand], {
start: closePoint,
end: farPoint,
color: color
});
}
};
this.hideCancelOverlay = function() {
Overlays.editOverlay(this.cancelOverlay, { visible: false });
};
this.hideTargetOverlay = function() {
Overlays.editOverlay(this.targetOverlay, { visible: false });
};
this.hideSeatOverlay = function() {
Overlays.editOverlay(this.seatOverlay, { visible: false });
};
this.updateDestinationOverlay = function(overlayID, intersection) {
var rotation = Quat.lookAt(intersection.intersection, MyAvatar.position, Vec3.UP);
var euler = Quat.safeEulerAngles(rotation);
var position = {
x: intersection.intersection.x,
y: intersection.intersection.y + TARGET_MODEL_DIMENSIONS.y / 2,
z: intersection.intersection.z
};
var towardUs = Quat.fromPitchYawRollDegrees(0, euler.y, 0);
Overlays.editOverlay(overlayID, {
visible: true,
position: position,
rotation: towardUs
});
};
}
// related to repositioning the avatar after you teleport
var FOOT_JOINT_NAMES = ["RightToe_End", "RightToeBase", "RightFoot"];
var DEFAULT_ROOT_TO_FOOT_OFFSET = 0.5;
function getAvatarFootOffset() {
// find a valid foot jointIndex
var footJointIndex = -1;
var i, l = FOOT_JOINT_NAMES.length;
for (i = 0; i < l; i++) {
footJointIndex = MyAvatar.getJointIndex(FOOT_JOINT_NAMES[i]);
if (footJointIndex != -1) {
break;
}
}
if (footJointIndex != -1) {
// default vertical offset from foot to avatar root.
return -MyAvatar.getAbsoluteDefaultJointTranslationInObjectFrame(footJointIndex).y;
} else {
return DEFAULT_ROOT_TO_FOOT_OFFSET * MyAvatar.scale;
}
}
var leftPad = new ThumbPad('left');
var rightPad = new ThumbPad('right');
var leftTrigger = new Trigger('left');
var rightTrigger = new Trigger('right');
var mappingName, teleportMapping;
var TELEPORT_DELAY = 0;
function isMoving() {
var LY = Controller.getValue(Controller.Standard.LY);
var LX = Controller.getValue(Controller.Standard.LX);
if (LY !== 0 || LX !== 0) {
return true;
} else {
return false;
}
}
function parseJSON(json) {
try {
return JSON.parse(json);
} catch (e) {
return undefined;
}
}
// When determininig whether you can teleport to a location, the normal of the
// point that is being intersected with is looked at. If this normal is more
// than MAX_ANGLE_FROM_UP_TO_TELEPORT degrees from <0, 1, 0> (straight up), then
// you can't teleport there.
var MAX_ANGLE_FROM_UP_TO_TELEPORT = 70;
function getTeleportTargetType(intersection) {
if (!intersection.intersects) {
return TARGET.NONE;
}
var props = Entities.getEntityProperties(intersection.entityID, ['userData', 'visible']);
var data = parseJSON(props.userData);
if (data !== undefined && data.seat !== undefined) {
var avatarUuid = Uuid.fromString(data.seat.user);
if (Uuid.isNull(avatarUuid) || !AvatarList.getAvatar(avatarUuid)) {
return TARGET.SEAT;
} else {
return TARGET.INVALID;
}
}
if (!props.visible) {
return TARGET.INVISIBLE;
}
var surfaceNormal = intersection.surfaceNormal;
var adj = Math.sqrt(surfaceNormal.x * surfaceNormal.x + surfaceNormal.z * surfaceNormal.z);
var angleUp = Math.atan2(surfaceNormal.y, adj) * (180 / Math.PI);
if (angleUp < (90 - MAX_ANGLE_FROM_UP_TO_TELEPORT) ||
angleUp > (90 + MAX_ANGLE_FROM_UP_TO_TELEPORT) ||
Vec3.distance(MyAvatar.position, intersection.intersection) <= TELEPORT_CANCEL_RANGE) {
return TARGET.INVALID;
} else {
return TARGET.SURFACE;
}
}
function registerMappings() {
mappingName = 'Hifi-Teleporter-Dev-' + Math.random();
teleportMapping = Controller.newMapping(mappingName);
teleportMapping.from(Controller.Standard.RT).peek().to(rightTrigger.buttonPress);
teleportMapping.from(Controller.Standard.LT).peek().to(leftTrigger.buttonPress);
teleportMapping.from(Controller.Standard.RightPrimaryThumb).peek().to(rightPad.buttonPress);
teleportMapping.from(Controller.Standard.LeftPrimaryThumb).peek().to(leftPad.buttonPress);
teleportMapping.from(Controller.Standard.LeftPrimaryThumb)
.to(function(value) {
if (isDisabled === 'left' || isDisabled === 'both') {
return;
}
if (leftTrigger.down()) {
return;
}
if (isMoving() === true) {
return;
}
teleporter.enterTeleportMode('left');
return;
});
teleportMapping.from(Controller.Standard.RightPrimaryThumb)
.to(function(value) {
if (isDisabled === 'right' || isDisabled === 'both') {
return;
}
if (rightTrigger.down()) {
return;
}
if (isMoving() === true) {
return;
}
teleporter.enterTeleportMode('right');
return;
});
}
registerMappings();
var teleporter = new Teleporter();
Controller.enableMapping(mappingName);
function cleanup() {
teleportMapping.disable();
teleporter.cleanup();
}
Script.scriptEnding.connect(cleanup);
var isDisabled = false;
var handleTeleportMessages = function(channel, message, sender) {
if (sender === MyAvatar.sessionUUID) {
if (channel === 'Hifi-Teleport-Disabler') {
if (message === 'both') {
isDisabled = 'both';
}
if (message === 'left') {
isDisabled = 'left';
}
if (message === 'right') {
isDisabled = 'right';
}
if (message === 'none') {
isDisabled = false;
}
} else if (channel === 'Hifi-Teleport-Ignore-Add' && !Uuid.isNull(message) && ignoredEntities.indexOf(message) === -1) {
ignoredEntities.push(message);
} else if (channel === 'Hifi-Teleport-Ignore-Remove' && !Uuid.isNull(message)) {
var removeIndex = ignoredEntities.indexOf(message);
if (removeIndex > -1) {
ignoredEntities.splice(removeIndex, 1);
}
}
}
};
Messages.subscribe('Hifi-Teleport-Disabler');
Messages.subscribe('Hifi-Teleport-Ignore-Add');
Messages.subscribe('Hifi-Teleport-Ignore-Remove');
Messages.messageReceived.connect(handleTeleportMessages);
}()); // END LOCAL_SCOPE

View file

@ -37,12 +37,6 @@ var COLORS_TELEPORT_CAN_TELEPORT = {
blue: 255
};
var COLORS_TELEPORT_CANNOT_TELEPORT = {
red: 0,
green: 121,
blue: 141
};
var COLORS_TELEPORT_CANCEL = {
red: 255,
green: 184,
@ -52,14 +46,55 @@ var COLORS_TELEPORT_CANCEL = {
var TELEPORT_CANCEL_RANGE = 1;
var COOL_IN_DURATION = 500;
var handInfo = {
right: {
controllerInput: Controller.Standard.RightHand
},
left: {
controllerInput: Controller.Standard.LeftHand
}
var cancelPath = {
type: "line3d",
color: COLORS_TELEPORT_CANCEL,
ignoreRayIntersection: true,
alpha: 1,
solid: true,
drawInFront: true,
glow: 1.0
};
var teleportPath = {
type: "line3d",
color: COLORS_TELEPORT_CAN_TELEPORT,
ignoreRayIntersection: true,
alpha: 1,
solid: true,
drawInFront: true,
glow: 1.0
};
var seatPath = {
type: "line3d",
color: COLORS_TELEPORT_SEAT,
ignoreRayIntersection: true,
alpha: 1,
solid: true,
drawInFront: true,
glow: 1.0
};
var cancelEnd = {
type: "model",
url: TOO_CLOSE_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
ignoreRayIntersection: true
};
var teleportEnd = {
type: "model",
url: TARGET_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
ignoreRayIntersection: true
};
var seatEnd = {
type: "model",
url: SEAT_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
ignoreRayIntersection: true
}
var teleportRenderStates = [{name: "cancel", path: cancelPath, end: cancelEnd},
{name: "teleport", path: teleportPath, end: teleportEnd},
{name: "seat", path: seatPath, end: seatEnd}];
function ThumbPad(hand) {
this.hand = hand;
@ -108,33 +143,41 @@ function Teleporter() {
this.state = TELEPORTER_STATES.IDLE;
this.currentTarget = TARGET.INVALID;
this.overlayLines = {
left: null,
right: null,
};
this.teleportRayLeftVisible = LaserPointers.createLaserPointer({
joint: "LeftHand",
filter: RayPick.PICK_ENTITIES,
faceAvatar: true,
centerEndY: false,
renderStates: teleportRenderStates
});
this.teleportRayLeftInvisible = LaserPointers.createLaserPointer({
joint: "LeftHand",
filter: RayPick.PICK_ENTITIES | RayPick.PICK_INCLUDE_INVISIBLE,
faceAvatar: true,
centerEndY: false,
renderStates: teleportRenderStates
});
this.teleportRayRightVisible = LaserPointers.createLaserPointer({
joint: "RightHand",
filter: RayPick.PICK_ENTITIES,
faceAvatar: true,
centerEndY: false,
renderStates: teleportRenderStates
});
this.teleportRayRightInvisible = LaserPointers.createLaserPointer({
joint: "RightHand",
filter: RayPick.PICK_ENTITIES | RayPick.PICK_INCLUDE_INVISIBLE,
faceAvatar: true,
centerEndY: false,
renderStates: teleportRenderStates
});
this.updateConnected = null;
this.activeHand = null;
this.teleporterMappingInternalName = 'Hifi-Teleporter-Internal-Dev-' + Math.random();
this.teleportMappingInternal = Controller.newMapping(this.teleporterMappingInternalName);
// Setup overlays
this.cancelOverlay = Overlays.addOverlay("model", {
url: TOO_CLOSE_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
visible: false
});
this.targetOverlay = Overlays.addOverlay("model", {
url: TARGET_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
visible: false
});
this.seatOverlay = Overlays.addOverlay("model", {
url: SEAT_MODEL_URL,
dimensions: TARGET_MODEL_DIMENSIONS,
visible: false
});
this.enableMappings = function() {
Controller.enableMapping(this.teleporterMappingInternalName);
};
@ -146,16 +189,11 @@ function Teleporter() {
this.cleanup = function() {
this.disableMappings();
Overlays.deleteOverlay(this.targetOverlay);
this.targetOverlay = null;
LaserPointers.removeLaserPointer(this.teleportRayLeftVisible);
LaserPointers.removeLaserPointer(this.teleportRayLeftInvisible);
LaserPointers.removeLaserPointer(this.teleportRayRightVisible);
LaserPointers.removeLaserPointer(this.teleportRayRightInvisible);
Overlays.deleteOverlay(this.cancelOverlay);
this.cancelOverlay = null;
Overlays.deleteOverlay(this.seatOverlay);
this.seatOverlay = null;
this.deleteOverlayBeams();
if (this.updateConnected === true) {
Script.update.disconnect(this, this.update);
}
@ -175,6 +213,14 @@ function Teleporter() {
Script.clearTimeout(coolInTimeout);
}
if (hand === 'right') {
LaserPointers.enableLaserPointer(_this.teleportRayRightVisible);
LaserPointers.enableLaserPointer(_this.teleportRayRightInvisible);
} else {
LaserPointers.enableLaserPointer(_this.teleportRayLeftVisible);
LaserPointers.enableLaserPointer(_this.teleportRayLeftInvisible);
}
this.state = TELEPORTER_STATES.COOL_IN;
coolInTimeout = Script.setTimeout(function() {
if (_this.state === TELEPORTER_STATES.COOL_IN) {
@ -194,44 +240,21 @@ function Teleporter() {
}
this.disableMappings();
this.deleteOverlayBeams();
this.hideTargetOverlay();
this.hideCancelOverlay();
LaserPointers.disableLaserPointer(this.teleportRayLeftVisible);
LaserPointers.disableLaserPointer(this.teleportRayLeftInvisible);
LaserPointers.disableLaserPointer(this.teleportRayRightVisible);
LaserPointers.disableLaserPointer(this.teleportRayRightInvisible);
this.updateConnected = null;
this.state = TELEPORTER_STATES.IDLE;
inTeleportMode = false;
};
this.deleteOverlayBeams = function() {
for (var key in this.overlayLines) {
if (this.overlayLines[key] !== null) {
Overlays.deleteOverlay(this.overlayLines[key]);
this.overlayLines[key] = null;
}
}
};
this.update = function() {
if (_this.state === TELEPORTER_STATES.IDLE) {
return;
}
// Get current hand pose information so that we can get the direction of the teleport beam
var pose = Controller.getPoseValue(handInfo[_this.activeHand].controllerInput);
var handPosition = pose.valid ? Vec3.sum(Vec3.multiplyQbyV(MyAvatar.orientation, pose.translation), MyAvatar.position) : MyAvatar.getHeadPosition();
var handRotation = pose.valid ? Quat.multiply(MyAvatar.orientation, pose.rotation) :
Quat.multiply(MyAvatar.headOrientation, Quat.angleAxis(-90, {
x: 1,
y: 0,
z: 0
}));
var pickRay = {
origin: handPosition,
direction: Quat.getUp(handRotation),
};
// We do up to 2 ray picks to find a teleport location.
// There are 2 types of teleport locations we are interested in:
// 1. A visible floor. This can be any entity surface that points within some degree of "up"
@ -241,48 +264,28 @@ function Teleporter() {
// We might hit an invisible entity that is not a seat, so we need to do a second pass.
// * In the second pass we pick against visible entities only.
//
var intersection = Entities.findRayIntersection(pickRay, true, [], [this.targetEntity].concat(ignoredEntities), false, true);
var result = (_this.activeHand === 'right') ? LaserPointers.getPrevRayPickResult(_this.teleportRayRightInvisible) :
LaserPointers.getPrevRayPickResult(_this.teleportRayLeftInvisible);
var teleportLocationType = getTeleportTargetType(intersection);
var teleportLocationType = getTeleportTargetType(result);
if (teleportLocationType === TARGET.INVISIBLE) {
intersection = Entities.findRayIntersection(pickRay, true, [], [this.targetEntity].concat(ignoredEntities), true, true);
teleportLocationType = getTeleportTargetType(intersection);
result = (_this.activeHand === 'right') ? LaserPointers.getPrevRayPickResult(_this.teleportRayRightVisible) :
LaserPointers.getPrevRayPickResult(_this.teleportRayLeftVisible);
teleportLocationType = getTeleportTargetType(result);
}
if (teleportLocationType === TARGET.NONE) {
this.hideTargetOverlay();
this.hideCancelOverlay();
this.hideSeatOverlay();
var farPosition = Vec3.sum(pickRay.origin, Vec3.multiply(pickRay.direction, 50));
this.updateLineOverlay(_this.activeHand, pickRay.origin, farPosition, COLORS_TELEPORT_CANNOT_TELEPORT);
this.setTeleportState(_this.activeHand, "", "");
} else if (teleportLocationType === TARGET.INVALID || teleportLocationType === TARGET.INVISIBLE) {
this.hideTargetOverlay();
this.hideSeatOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_CANCEL);
this.updateDestinationOverlay(this.cancelOverlay, intersection);
this.setTeleportState(_this.activeHand, "", "cancel");
} else if (teleportLocationType === TARGET.SURFACE) {
if (this.state === TELEPORTER_STATES.COOL_IN) {
this.hideTargetOverlay();
this.hideSeatOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_CANCEL);
this.updateDestinationOverlay(this.cancelOverlay, intersection);
this.setTeleportState(_this.activeHand, "cancel", "");
} else {
this.hideCancelOverlay();
this.hideSeatOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection,
COLORS_TELEPORT_CAN_TELEPORT);
this.updateDestinationOverlay(this.targetOverlay, intersection);
this.setTeleportState(_this.activeHand, "teleport", "");
}
} else if (teleportLocationType === TARGET.SEAT) {
this.hideCancelOverlay();
this.hideTargetOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_SEAT);
this.updateDestinationOverlay(this.seatOverlay, intersection);
this.setTeleportState(_this.activeHand, "", "seat");
}
@ -290,79 +293,30 @@ function Teleporter() {
// remember the state before we exit teleport mode and set it back to IDLE
var previousState = this.state;
this.exitTeleportMode();
this.hideCancelOverlay();
this.hideTargetOverlay();
this.hideSeatOverlay();
if (teleportLocationType === TARGET.NONE || teleportLocationType === TARGET.INVALID || previousState === TELEPORTER_STATES.COOL_IN) {
// Do nothing
} else if (teleportLocationType === TARGET.SEAT) {
Entities.callEntityMethod(intersection.entityID, 'sit');
Entities.callEntityMethod(result.objectID, 'sit');
} else if (teleportLocationType === TARGET.SURFACE) {
var offset = getAvatarFootOffset();
intersection.intersection.y += offset;
MyAvatar.goToLocation(intersection.intersection, false, {x: 0, y: 0, z: 0, w: 1}, false);
result.intersection.y += offset;
MyAvatar.goToLocation(result.intersection, false, {x: 0, y: 0, z: 0, w: 1}, false);
HMD.centerUI();
MyAvatar.centerBody();
}
}
};
this.updateLineOverlay = function(hand, closePoint, farPoint, color) {
if (this.overlayLines[hand] === null) {
var lineProperties = {
start: closePoint,
end: farPoint,
color: color,
ignoreRayIntersection: true,
visible: true,
alpha: 1,
solid: true,
drawInFront: true,
glow: 1.0
};
this.overlayLines[hand] = Overlays.addOverlay("line3d", lineProperties);
this.setTeleportState = function(hand, visibleState, invisibleState) {
if (hand === 'right') {
LaserPointers.setRenderState(_this.teleportRayRightVisible, visibleState);
LaserPointers.setRenderState(_this.teleportRayRightInvisible, invisibleState);
} else {
Overlays.editOverlay(this.overlayLines[hand], {
start: closePoint,
end: farPoint,
color: color
});
LaserPointers.setRenderState(_this.teleportRayLeftVisible, visibleState);
LaserPointers.setRenderState(_this.teleportRayLeftInvisible, invisibleState);
}
};
this.hideCancelOverlay = function() {
Overlays.editOverlay(this.cancelOverlay, { visible: false });
};
this.hideTargetOverlay = function() {
Overlays.editOverlay(this.targetOverlay, { visible: false });
};
this.hideSeatOverlay = function() {
Overlays.editOverlay(this.seatOverlay, { visible: false });
};
this.updateDestinationOverlay = function(overlayID, intersection) {
var rotation = Quat.lookAt(intersection.intersection, MyAvatar.position, Vec3.UP);
var euler = Quat.safeEulerAngles(rotation);
var position = {
x: intersection.intersection.x,
y: intersection.intersection.y + TARGET_MODEL_DIMENSIONS.y / 2,
z: intersection.intersection.z
};
var towardUs = Quat.fromPitchYawRollDegrees(0, euler.y, 0);
Overlays.editOverlay(overlayID, {
visible: true,
position: position,
rotation: towardUs
});
};
}
// related to repositioning the avatar after you teleport
@ -418,12 +372,12 @@ function parseJSON(json) {
// than MAX_ANGLE_FROM_UP_TO_TELEPORT degrees from <0, 1, 0> (straight up), then
// you can't teleport there.
var MAX_ANGLE_FROM_UP_TO_TELEPORT = 70;
function getTeleportTargetType(intersection) {
if (!intersection.intersects) {
function getTeleportTargetType(result) {
if (result.type == RayPick.INTERSECTED_NONE) {
return TARGET.NONE;
}
var props = Entities.getEntityProperties(intersection.entityID, ['userData', 'visible']);
var props = Entities.getEntityProperties(result.objectID, ['userData', 'visible']);
var data = parseJSON(props.userData);
if (data !== undefined && data.seat !== undefined) {
var avatarUuid = Uuid.fromString(data.seat.user);
@ -438,13 +392,13 @@ function getTeleportTargetType(intersection) {
return TARGET.INVISIBLE;
}
var surfaceNormal = intersection.surfaceNormal;
var surfaceNormal = result.surfaceNormal;
var adj = Math.sqrt(surfaceNormal.x * surfaceNormal.x + surfaceNormal.z * surfaceNormal.z);
var angleUp = Math.atan2(surfaceNormal.y, adj) * (180 / Math.PI);
if (angleUp < (90 - MAX_ANGLE_FROM_UP_TO_TELEPORT) ||
angleUp > (90 + MAX_ANGLE_FROM_UP_TO_TELEPORT) ||
Vec3.distance(MyAvatar.position, intersection.intersection) <= TELEPORT_CANCEL_RANGE) {
Vec3.distance(MyAvatar.position, result.intersection) <= TELEPORT_CANCEL_RANGE) {
return TARGET.INVALID;
} else {
return TARGET.SURFACE;
@ -520,6 +474,8 @@ var handleTeleportMessages = function(channel, message, sender) {
isDisabled = false;
}
} else if (channel === 'Hifi-Teleport-Ignore-Add' && !Uuid.isNull(message) && ignoredEntities.indexOf(message) === -1) {
// TODO:
// add ability to ignore entities to LaserPointers
ignoredEntities.push(message);
} else if (channel === 'Hifi-Teleport-Ignore-Remove' && !Uuid.isNull(message)) {
var removeIndex = ignoredEntities.indexOf(message);