content/hifi-content/robin/dev/usefulUtils/scaleAvatar/V3/scaleAvatar.js
2022-02-14 02:04:11 +01:00

115 lines
4.1 KiB
JavaScript

(function () {
var CONTROLLER_MAPPING_LEFT = "com.highfidelity.scaleAvatarLeft",
CONTROLLER_MAPPING_RIGHT = "com.highfidelity.scaleAvatarRight";
var isRightPressed = false;
var isLeftPressed = false;
var PRESSED_MIN_VALUE = 0.8;
var DEBUG = true;
function rightOnGripPress(value) {
if (!isRightPressed && value >= PRESSED_MIN_VALUE) { // not already pressed and value > 0.8
isRightPressed = true;
if (DEBUG) {
print("right pressed!");
}
} else if (isRightPressed && value < PRESSED_MIN_VALUE) {
isRightPressed = false;
}
handleBothGripPressed();
}
function leftOnGripPress(value) {
if (!isLeftPressed && value >= PRESSED_MIN_VALUE) { // not already pressed and value > 0.8
isLeftPressed = true;
if (DEBUG) {
print("left pressed!");
}
} else if (isLeftPressed && value < PRESSED_MIN_VALUE) {
isLeftPressed = false;
}
handleBothGripPressed();
}
var SCALE_BUFFER = 1; // increase/decrease the sensitivity of scaling
var PRESSED_INTERVAL_MS = 25;
var isPressed = false;
var initialDistance = null;
var initialScale = null;
var pressedInterval = null;
function handleBothGripPressed() {
if (isRightPressed && isLeftPressed && isPressed) {
// setup done, do not need to continue
// or either left or right is not pressed
return;
}
if (isRightPressed && isLeftPressed) {
if (!isPressed) {
// Both pressed initially
initialDistance = distanceBetweenHands();
isPressed = true;
initialScale = MyAvatar.scale;
// Setup pressed interval
pressedInterval = Script.setInterval(function() {
if (isPressed) {
if (DEBUG) {
print("init" + JSON.stringify(initialDistance));
print("distance" + JSON.stringify(distanceBetweenHands()));
}
var currentDistance = distanceBetweenHands();
var deltaDistance = currentDistance - initialDistance;
MyAvatar.scale = initialScale + deltaDistance * SCALE_BUFFER;
}
}, PRESSED_INTERVAL_MS);
}
} else {
// remove pressed interval
if (pressedInterval) {
if (DEBUG) {
print("CLEAR INTERVAL");
}
Script.clearInterval(pressedInterval);
pressedInterval = null;
isPressed = false;
initialDistance = null;
}
}
}
// Get the distance between the avatar's hands
function distanceBetweenHands() {
var rightHand = Controller.getPoseValue(Controller.Standard.RightHand).translation;
var leftHand = Controller.getPoseValue(Controller.Standard.LeftHand).translation;
return Vec3.distance(rightHand, leftHand) / MyAvatar.sensorToWorldScale;
}
function setupBothControllerMappings() {
var controllerMapping = Controller.newMapping(CONTROLLER_MAPPING_LEFT);
controllerMapping.from(Controller.Standard.LeftGrip).to(leftOnGripPress);
Controller.enableMapping(CONTROLLER_MAPPING_LEFT);
controllerMapping = Controller.newMapping(CONTROLLER_MAPPING_RIGHT);
controllerMapping.from(Controller.Standard.RightGrip).to(rightOnGripPress);
Controller.enableMapping(CONTROLLER_MAPPING_RIGHT);
}
function unload() {
Controller.disableMapping(CONTROLLER_MAPPING_LEFT);
Controller.disableMapping(CONTROLLER_MAPPING_RIGHT);
if (pressedInterval) {
Script.clearInterval(pressedInterval);
}
}
setupBothControllerMappings();
Script.scriptEnding.connect(unload);
})();