519 lines
16 KiB
JavaScript
Raw Normal View History

2016-08-23 12:34:32 -07:00
"use strict";
// Created by james b. pollack @imgntn on 7/2/2016
// Copyright 2016 High Fidelity, Inc.
//
2016-08-08 12:20:48 -07:00
// 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
2016-07-01 17:26:52 -07:00
2016-08-23 12:34:32 -07:00
(function() { // BEGIN LOCAL_SCOPE
2016-06-30 16:05:44 -07:00
var inTeleportMode = false;
2016-06-29 15:59:51 -07:00
var SMOOTH_ARRIVAL_SPACING = 33;
var NUMBER_OF_STEPS = 6;
2016-09-12 15:57:57 -07:00
2016-08-04 16:08:51 -07:00
var TARGET_MODEL_URL = Script.resolvePath("../assets/models/teleport-destination.fbx");
var TOO_CLOSE_MODEL_URL = Script.resolvePath("../assets/models/teleport-cancel.fbx");
2016-07-01 15:19:37 -07:00
var TARGET_MODEL_DIMENSIONS = {
2016-07-01 17:26:52 -07:00
x: 1.15,
2016-07-05 09:22:19 -07:00
y: 0.5,
2016-07-01 17:26:52 -07:00
z: 1.15
2016-07-06 13:20:42 -07:00
};
var COLORS_TELEPORT_SEAT = {
red: 0,
green: 0,
blue: 255
}
var COLORS_TELEPORT_CAN_TELEPORT = {
2016-07-22 13:25:33 -07:00
red: 97,
green: 247,
blue: 255
}
var COLORS_TELEPORT_CANNOT_TELEPORT = {
2016-07-22 13:25:33 -07:00
red: 0,
green: 121,
blue: 141
};
var COLORS_TELEPORT_CANCEL = {
2016-08-04 16:08:51 -07:00
red: 255,
green: 184,
blue: 73
};
2016-08-18 17:24:18 -07:00
var TELEPORT_CANCEL_RANGE = 1;
2016-08-18 13:35:51 -07:00
var COOL_IN_DURATION = 500;
const handInfo = {
right: {
controllerInput: Controller.Standard.RightHand
},
left: {
controllerInput: Controller.Standard.LeftHand
}
};
2016-06-30 16:05:44 -07:00
function ThumbPad(hand) {
this.hand = hand;
var _thisPad = this;
2016-06-29 15:59:51 -07:00
2016-06-30 16:05:44 -07:00
this.buttonPress = function(value) {
_thisPad.buttonValue = value;
2016-06-30 16:05:44 -07:00
};
}
2016-06-29 15:59:51 -07:00
2016-06-30 16:05:44 -07:00
function Trigger(hand) {
this.hand = hand;
var _this = this;
2016-06-29 15:59:51 -07:00
2016-06-30 16:05:44 -07:00
this.buttonPress = function(value) {
_this.buttonValue = value;
};
2016-06-29 15:59:51 -07:00
2016-06-30 16:05:44 -07:00
this.down = function() {
var down = _this.buttonValue === 1 ? 1.0 : 0.0;
return down
2016-06-30 16:05:44 -07:00
};
2016-06-29 15:59:51 -07:00
}
2016-08-18 14:36:52 -07:00
var coolInTimeout = null;
var TELEPORTER_STATES = {
IDLE: 'idle',
COOL_IN: 'cool_in',
TARGETTING_INVALID: 'targetting_invalid',
}
var TARGET = {
NONE: 'none', // Not currently targetting anything
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
}
2016-06-30 16:05:44 -07:00
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.telporterMappingInternalName = 'Hifi-Teleporter-Internal-Dev-' + Math.random();
this.teleportMappingInternal = Controller.newMapping(this.telporterMappingInternalName);
2016-07-01 15:19:37 -07:00
// 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
});
2016-07-01 00:48:29 -07:00
this.enableMappings = function() {
Controller.enableMapping(this.telporterMappingInternalName);
2016-06-30 16:05:44 -07:00
};
2016-06-29 18:28:39 -07:00
2016-06-30 16:05:44 -07:00
this.disableMappings = function() {
Controller.disableMapping(teleporter.telporterMappingInternalName);
};
2016-06-29 18:28:39 -07:00
this.cleanup = function() {
this.disableMappings();
Overlays.deleteOverlay(this.targetOverlay);
this.targetOverlay = null;
Overlays.deleteOverlay(this.cancelOverlay);
this.cancelOverlay = null;
this.hideOverlayBeams();
if (this.updateConnected === true) {
Script.update.disconnect(this, this.update);
}
};
2016-06-30 16:05:44 -07:00
this.enterTeleportMode = function(hand) {
if (inTeleportMode === true) {
return;
}
if (isDisabled === 'both' || isDisabled === hand) {
2016-08-02 15:40:19 -07:00
return;
}
2016-08-02 21:26:04 -07:00
inTeleportMode = true;
2016-08-18 14:36:52 -07:00
if (coolInTimeout !== null) {
Script.clearTimeout(coolInTimeout);
}
this.state = TELEPORTER_STATES.COOL_IN;
2016-08-18 14:36:52 -07:00
coolInTimeout = Script.setTimeout(function() {
if (_this.state === TELEPORTER_STATES.COOL_IN) {
_this.state = TELEPORTER_STATES.TARGETTING;
}
2016-08-18 13:35:51 -07:00
}, COOL_IN_DURATION)
2016-08-02 21:26:04 -07:00
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);
2016-08-04 23:45:43 -07:00
}
this.disableMappings();
this.hideOverlayBeams();
this.hideTargetOverlay();
this.hideCancelOverlay();
2016-08-04 23:45:43 -07:00
this.updateConnected = null;
this.state = TELEPORTER_STATES.IDLE;
inTeleportMode = false;
2016-08-04 23:45:43 -07:00
};
this.hideOverlayBeams = function() {
for (key in this.overlayLines) {
if (this.overlayLines[key] !== null) {
Overlays.deleteOverlay(this.overlayLines[key]);
this.overlayLines[key] = null;
}
}
2016-07-06 15:16:31 -07:00
}
2016-07-08 13:37:59 -07:00
this.update = function() {
if (_this.state === TELEPORTER_STATES.IDLE) {
2016-08-02 12:25:24 -07:00
return;
}
2016-07-07 12:12:22 -07:00
// 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) :
2016-08-17 15:08:43 -07:00
Quat.multiply(MyAvatar.headOrientation, Quat.angleAxis(-90, {
x: 1,
y: 0,
z: 0
}));
var pickRay = {
origin: handPosition,
direction: Quat.getUp(handRotation),
};
var intersection = Entities.findRayIntersection(pickRay, true, [], [this.targetEntity], true, true);
var teleportLocationType = getTeleportTargetType(intersection);
2016-07-06 15:16:31 -07:00
if (teleportLocationType === TARGET.NONE) {
_this.hideTargetOverlay();
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) {
this.hideTargetOverlay();
2016-06-30 16:05:44 -07:00
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_CANCEL);
this.updateCancelOverlay(intersection);
} else if (teleportLocationType === TARGET.SURFACE) {
if (this.state === TELEPORTER_STATES.COOL_IN) {
this.hideTargetOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_CANCEL);
this.updateCancelOverlay(intersection);
2016-08-04 16:08:51 -07:00
} else {
this.hideCancelOverlay();
2016-08-18 14:08:31 -07:00
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_CAN_TELEPORT);
this.updateTargetOverlay(intersection);
}
} else if (teleportLocationType === TARGET.SEAT) {
this.hideCancelOverlay();
this.updateLineOverlay(_this.activeHand, pickRay.origin, intersection.intersection, COLORS_TELEPORT_SEAT);
this.updateTargetOverlay(intersection);
2016-07-01 17:26:52 -07:00
}
2016-07-06 15:16:31 -07:00
2016-08-04 16:08:51 -07:00
if (((_this.activeHand == 'left' ? leftPad : rightPad).buttonValue === 0) && inTeleportMode === true) {
_this.exitTeleportMode();
_this.hideCancelOverlay();
_this.hideTargetOverlay();
2016-08-18 14:08:31 -07:00
if (teleportLocationType === TARGET.NONE || teleportLocationType === TARGET.INVALID || this.state === 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.position = intersection.intersection;
HMD.centerUI();
}
2016-07-06 15:16:31 -07:00
}
};
this.updateLineOverlay = function(hand, closePoint, farPoint, color) {
if (this.overlayLines[hand] === null) {
2016-07-06 15:16:31 -07:00
var lineProperties = {
start: closePoint,
end: farPoint,
color: color,
2016-08-02 21:26:04 -07:00
ignoreRayIntersection: true,
2016-07-06 15:16:31 -07:00
visible: true,
alpha: 1,
solid: true,
drawInFront: true,
2016-07-06 15:16:31 -07:00
glow: 1.0
};
this.overlayLines[hand] = Overlays.addOverlay("line3d", lineProperties);
2016-07-06 12:31:03 -07:00
} else {
var success = Overlays.editOverlay(this.overlayLines[hand], {
2016-07-06 15:16:31 -07:00
start: closePoint,
end: farPoint,
2016-08-02 21:26:04 -07:00
color: color
2016-07-06 15:16:31 -07:00
});
}
};
2016-07-06 13:20:42 -07:00
this.hideCancelOverlay = function() {
Overlays.editOverlay(this.cancelOverlay, { visible: false });
};
this.hideTargetOverlay = function() {
Overlays.editOverlay(this.targetOverlay, { visible: false });
};
this.updateTargetOverlay = function(intersection) {
2016-08-08 12:20:48 -07:00
var rotation = Quat.lookAt(intersection.intersection, MyAvatar.position, Vec3.UP);
var euler = Quat.safeEulerAngles(rotation);
2016-06-30 16:05:44 -07:00
var position = {
x: intersection.intersection.x,
2016-07-06 13:20:42 -07:00
y: intersection.intersection.y + TARGET_MODEL_DIMENSIONS.y / 2,
2016-06-30 16:05:44 -07:00
z: intersection.intersection.z
2016-08-08 12:20:48 -07:00
};
2016-08-04 16:08:51 -07:00
2016-08-04 23:45:43 -07:00
var towardUs = Quat.fromPitchYawRollDegrees(0, euler.y, 0);
2016-08-04 16:35:50 -07:00
Overlays.editOverlay(this.targetOverlay, {
visible: true,
2016-08-04 16:35:50 -07:00
position: position,
2016-08-08 12:20:48 -07:00
rotation: towardUs
2016-08-04 16:35:50 -07:00
});
2016-07-01 15:19:53 -07:00
2016-08-04 23:45:43 -07:00
};
this.updateCancelOverlay = function(intersection) {
2016-08-08 12:20:48 -07:00
var rotation = Quat.lookAt(intersection.intersection, MyAvatar.position, Vec3.UP);
var euler = Quat.safeEulerAngles(rotation);
2016-08-04 23:45:43 -07:00
var position = {
x: intersection.intersection.x,
y: intersection.intersection.y + TARGET_MODEL_DIMENSIONS.y / 2,
z: intersection.intersection.z
2016-08-08 12:20:48 -07:00
};
2016-08-04 23:45:43 -07:00
var towardUs = Quat.fromPitchYawRollDegrees(0, euler.y, 0);
Overlays.editOverlay(this.cancelOverlay, {
visible: true,
2016-08-04 23:45:43 -07:00
position: position,
2016-08-08 12:20:48 -07:00
rotation: towardUs
2016-08-04 23:45:43 -07:00
});
2016-06-30 16:05:44 -07:00
};
2016-06-29 18:28:39 -07:00
}
2016-06-29 15:59:51 -07:00
2016-06-30 16:05:44 -07:00
//related to repositioning the avatar after you teleport
2016-06-29 18:28:39 -07:00
function getAvatarFootOffset() {
var data = getJointData();
var upperLeg, lowerLeg, foot, toe, toeTop;
data.forEach(function(d) {
var jointName = d.joint;
if (jointName === "RightUpLeg") {
upperLeg = d.translation.y;
} else if (jointName === "RightLeg") {
2016-06-29 18:28:39 -07:00
lowerLeg = d.translation.y;
} else if (jointName === "RightFoot") {
2016-06-29 18:28:39 -07:00
foot = d.translation.y;
} else if (jointName === "RightToeBase") {
2016-06-29 18:28:39 -07:00
toe = d.translation.y;
} else if (jointName === "RightToe_End") {
2016-08-02 21:26:04 -07:00
toeTop = d.translation.y;
2016-06-29 18:28:39 -07:00
}
});
2016-06-29 18:28:39 -07:00
var offset = upperLeg + lowerLeg + foot + toe + toeTop;
offset = offset / 100;
2016-08-02 21:26:04 -07:00
return offset;
2016-06-29 18:28:39 -07:00
};
function getJointData() {
var allJointData = [];
var jointNames = MyAvatar.jointNames;
jointNames.forEach(function(joint, index) {
var translation = MyAvatar.getJointTranslation(index);
var rotation = MyAvatar.getJointRotation(index)
allJointData.push({
joint: joint,
index: index,
translation: translation,
rotation: rotation
});
});
return allJointData;
};
2016-07-01 11:12:20 -07:00
2016-06-30 16:05:44 -07:00
var leftPad = new ThumbPad('left');
var rightPad = new ThumbPad('right');
var leftTrigger = new Trigger('left');
var rightTrigger = new Trigger('right');
2016-07-01 11:12:20 -07:00
var mappingName, teleportMapping;
2016-08-02 19:02:20 -07:00
var TELEPORT_DELAY = 0;
2016-07-07 12:12:22 -07:00
2016-08-02 18:53:36 -07:00
function isMoving() {
var LY = Controller.getValue(Controller.Standard.LY);
var LX = Controller.getValue(Controller.Standard.LX);
if (LY !== 0 || LX !== 0) {
2016-08-02 21:26:04 -07:00
return true;
} else {
2016-08-02 21:26:04 -07:00
return false;
}
2016-08-08 12:20:48 -07:00
};
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.
const MAX_ANGLE_FROM_UP_TO_TELEPORT = 70;
function getTeleportTargetType(intersection) {
if (!intersection.intersects) {
return TARGET.NONE;
}
var userData = Entities.getEntityProperties(intersection.entityID, 'userData').userData;
var data = parseJSON(userData);
print("Data is:", intersection.entityID, userData);
if (data !== undefined && data.seat !== undefined) {
return TARGET.SEAT;
}
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;
}
2016-08-08 12:20:48 -07:00
};
2016-08-04 16:08:51 -07:00
2016-07-08 13:37:59 -07:00
function registerMappings() {
2016-07-07 12:12:22 -07:00
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);
2016-08-02 12:25:24 -07:00
teleportMapping.from(Controller.Standard.LeftPrimaryThumb)
2016-08-02 12:26:37 -07:00
.to(function(value) {
2016-08-02 12:52:35 -07:00
if (isDisabled === 'left' || isDisabled === 'both') {
2016-08-02 12:26:37 -07:00
return;
}
2016-08-02 15:40:19 -07:00
if (leftTrigger.down()) {
return;
}
2016-08-02 18:53:36 -07:00
if (isMoving() === true) {
return;
}
teleporter.enterTeleportMode('left')
2016-08-02 12:25:24 -07:00
return;
2016-08-02 12:26:37 -07:00
});
2016-08-02 12:25:24 -07:00
teleportMapping.from(Controller.Standard.RightPrimaryThumb)
2016-08-02 12:26:37 -07:00
.to(function(value) {
2016-08-02 12:52:35 -07:00
if (isDisabled === 'right' || isDisabled === 'both') {
2016-08-02 12:26:37 -07:00
return;
}
2016-08-02 15:40:19 -07:00
if (rightTrigger.down()) {
return;
}
2016-08-02 18:53:36 -07:00
if (isMoving() === true) {
return;
}
2016-08-02 21:26:04 -07:00
teleporter.enterTeleportMode('right')
2016-08-02 12:25:24 -07:00
return;
2016-08-02 12:26:37 -07:00
});
2016-08-08 12:20:48 -07:00
};
2016-06-30 16:05:44 -07:00
2016-07-08 13:37:59 -07:00
registerMappings();
2016-07-07 12:39:11 -07:00
2016-06-30 16:05:44 -07:00
var teleporter = new Teleporter();
Controller.enableMapping(mappingName);
function cleanup() {
teleportMapping.disable();
teleporter.cleanup();
2016-08-02 12:25:24 -07:00
}
Script.scriptEnding.connect(cleanup);
2016-08-02 12:25:24 -07:00
var isDisabled = false;
var handleHandMessages = function(channel, message, sender) {
var data;
if (sender === MyAvatar.sessionUUID) {
if (channel === 'Hifi-Teleport-Disabler') {
2016-08-02 12:52:35 -07:00
if (message === 'both') {
isDisabled = 'both';
}
if (message === 'left') {
isDisabled = 'left';
}
if (message === 'right') {
isDisabled = 'right'
2016-08-02 12:25:24 -07:00
}
2016-08-02 12:52:35 -07:00
if (message === 'none') {
2016-08-02 12:25:24 -07:00
isDisabled = false;
}
}
}
}
Messages.subscribe('Hifi-Teleport-Disabler');
2016-08-23 12:34:32 -07:00
Messages.messageReceived.connect(handleHandMessages);
}()); // END LOCAL_SCOPE