Can use mouse for debugging/development when you don't have a working hydra.

This commit is contained in:
Howard Stearns 2016-04-24 20:32:21 -07:00
parent b88f478dc4
commit 11e2e209e2

View file

@ -1,3 +1,7 @@
"use strict";
/*jslint vars: true, plusplus: true*/
/*globals Script, Overlays, Controller, Reticle, HMD, Camera, MyAvatar, Settings, Vec3, Quat, print */
//
// handControllerPointer.js
// examples/controllers
@ -16,10 +20,31 @@
// Cursor all the time when uncradled. (E.g., not just when blue ray is on, or five seconds after movement, etc.)
// Button 3 is left-mouse, button 4 is right-mouse.
var counter = 0, skip = 50;
function debug() { // Display the arguments not just [Object object].
if (skip && (counter++ % skip)) { return; }
print.apply(null, [].map.call(arguments, JSON.stringify));
}
// Keep track of the vertical fieldOfView setting:
var DEFAULT_VERTICAL_FIELD_OF_VIEW = 45; // degrees
var SETTINGS_CHANGE_RECHECK_INTERVAL = 10 * 1000; // milliseconds
var verticalFieldOfView = Settings.getValue('fieldOfView') || DEFAULT_VERTICAL_FIELD_OF_VIEW;
var settingsChecker = Script.setInterval(function () {
verticalFieldOfView = Settings.getValue('fieldOfView') || DEFAULT_VERTICAL_FIELD_OF_VIEW;
}, SETTINGS_CHANGE_RECHECK_INTERVAL);
Script.scriptEnding.connect(function () { Script.clearInterval(settingsChecker); });
function getViewportDimensions() {
return Controller.getViewportDimensions();
}
// Define shimable functions for getting hand controller and setting cursor, for the normal
// case of having a hand controller. Alternative are at the bottom of the file.
var getControllerPose = Controller.getPoseValue;
var setCursor = Reticle.setPosition;
// Generalized HUD utilities, with or without HDM:
function calculateRayUICollisionPoint(position, direction) {
// Answer the 3D intersection of the HUD by the given ray, or falsey if no intersection.
if (HMD.active) {
@ -36,29 +61,24 @@ function calculateRayUICollisionPoint(position, direction) {
var scale = numerator / denominator;
return Vec3.sum(position, Vec3.multiply(scale, direction));
}
var DEFAULT_VERTICAL_FIELD_OF_VIEW = 45; // degrees
var SETTINGS_CHANGE_RECHECK_INTERVAL = 10 * 1000; // milliseconds
var verticalHalfFieldOfView = (Settings.getValue('fieldOfView') || DEFAULT_VERTICAL_FIELD_OF_VIEW) / 2;
var settingsChecker = Script.setInterval(function () {
verticalHalfFieldOfView = (Settings.getValue('fieldOfView') || DEFAULT_VERTICAL_FIELD_OF_VIEW) / 2;
}, SETTINGS_CHANGE_RECHECK_INTERVAL);
var DEGREES_TO_HALF_RADIANS = Math.PI / 360;
function overlayFromWorldPoint(point) {
// Answer the 2d pixel-space location in the HUD that covers the given 3D point.
if (HMD.active) {
return HMD.overlayFromWorldPoint(point);
}
// Find the yaw and pitch from camera to position, as a fraction of view frustrum, and multiply by current screen size.
var hudNormal = Quat.getFront(Camera.getOrientation());
var cameraToPoint = Vec3.subtract(point, Camera.getPosition());
var eulerDegrees = Quat.safeEulerAngles(Quat.rotationBetween(hudNormal, cameraToPoint));
var size = Reticle.maximumPosition;
var horizontalHalfFieldOfView = size.x * verticalHalfFieldOfView / size.y;
return {
x: size.x * (eulerDegrees.x / horizontalHalfFieldOfView + 0.5),
y: size.y * (eulerDegrees.y / verticalHalfFieldOfView + 0.5)
};
var cameraX = Vec3.dot(cameraToPoint, Quat.getRight(Camera.getOrientation()));
var cameraY = Vec3.dot(cameraToPoint, Quat.getUp(Camera.getOrientation()));
var size = getViewportDimensions();
var hudHeight = 2 * Math.tan(verticalFieldOfView * DEGREES_TO_HALF_RADIANS);
var hudWidth = hudHeight * size.x / size.y;
var horizontalPixels = size.x * (cameraX / hudWidth + 0.5);
var verticalPixels = size.y * (1 - (cameraY / hudHeight + 0.5));
return { x: horizontalPixels, y: verticalPixels };
}
// Synthesize left and right mouse click from controller:
var MAPPING_NAME = Script.resolvePath('');
var mapping = Controller.newMapping(MAPPING_NAME);
function mapToAction(controller, button, action) {
@ -68,30 +88,30 @@ function mapToAction(controller, button, action) {
mapToAction('Hydra', 'R3', 'ReticleClick');
mapToAction('Hydra', 'R4', 'ContextMenu');
mapping.enable();
Script.scriptEnding.connect(mapping.disable);
var terminatingBall = Overlays.addOverlay("sphere", { // Same properties as handControllerGrab search sphere
size: 0.011,
color: {red: 10, green: 10, blue: 255},
alpha: 0.5,
alpha: 0.8,
solid: true,
visible: true
});
var counter = 0;
function update() {
if (Controller.getValue(Controller.Standard.RT)) { return; } // Interferes with other scripts.
var hand = Controller.Standard.RightHand,
controllerPose = Controller.getPoseValue(hand);
var hand = Controller.Standard.RightHand;
var controllerPose = getControllerPose(hand);
if (!controllerPose.valid) { return; } // Controller is cradled.
var controllerPosition = Vec3.sum(Vec3.multiplyQbyV(MyAvatar.orientation, controllerPose.translation),
MyAvatar.position);
// This gets point direction right, but if you want general quaternion it would be more complicated:
var controllerDirection = Quat.getUp(Quat.multiply(MyAvatar.orientation, controllerPose.rotation));
var hudPoint3d = HMD.calculateRayUICollisionPoint(controllerPosition, controllerDirection);
//Overlays.editOverlay(terminatingBall, {position: hudPoint3d}); // FIXME remove
// This gets point direction right, but if you want general quaternion it would be more complicated:
var controllerDirection = Quat.getUp(Quat.multiply(MyAvatar.orientation, controllerPose.rotation));
var hudPoint3d = calculateRayUICollisionPoint(controllerPosition, controllerDirection);
if (!hudPoint3d) { return; } // E.g., parallel to the screen.
var hudPoint2d = HMD.overlayFromWorldPoint(hudPoint3d);
if (!(counter++ % 50)) debug(hudPoint3d, hudPoint2d);
Reticle.setPosition(hudPoint2d);
Overlays.editOverlay(terminatingBall, {position: hudPoint3d});
setCursor(overlayFromWorldPoint(hudPoint3d));
}
var UPDATE_INTERVAL = 20; // milliseconds. Script.update is too frequent.
@ -99,6 +119,52 @@ var updater = Script.setInterval(update, UPDATE_INTERVAL);
Script.scriptEnding.connect(function () {
Overlays.deleteOverlay(terminatingBall);
Script.clearInterval(updater);
Script.clearInterval(settingsChecker);
mapping.disable();
});
// The rest of this is for debugging without working hand controllers, using a line from camera to mouse, and an image for cursor.
var CONTROLLER_ROTATION = Quat.fromPitchYawRollDegrees(90, 180, -90);
if (!Controller.Hardware.Hydra) {
var mouseKeeper = {x: 0, y: 0};
var onMouseMove = function (event) { mouseKeeper.x = event.x; mouseKeeper.y = event.y; };
Controller.mouseMoveEvent.connect(onMouseMove);
Script.scriptEnding.connect(function () { Controller.mouseMoveEvent.disconnect(onMouseMove); });
getControllerPose = function () {
var size = getViewportDimensions();
var handPoint = Vec3.subtract(Camera.getPosition(), MyAvatar.position); // Pretend controller is at camera
// In world-space 3D meters:
var rotation = Camera.getOrientation();
var normal = Quat.getFront(rotation);
var hudHeight = 2 * Math.tan(verticalFieldOfView * DEGREES_TO_HALF_RADIANS);
var hudWidth = hudHeight * size.x / size.y;
var rightFraction = mouseKeeper.x / size.x - 0.5;
var rightMeters = rightFraction * hudWidth;
var upFraction = mouseKeeper.y / size.y - 0.5;
var upMeters = upFraction * hudHeight * -1;
var right = Vec3.multiply(Quat.getRight(rotation), rightMeters);
var up = Vec3.multiply(Quat.getUp(rotation), upMeters);
var direction = Vec3.sum(normal, Vec3.sum(right, up));
var mouseRotation = Quat.rotationBetween(normal, direction);
var controllerRotation = Quat.multiply(Quat.multiply(mouseRotation, rotation), CONTROLLER_ROTATION);
var inverseAvatar = Quat.inverse(MyAvatar.orientation);
return {
valid: true,
translation: Vec3.multiplyQbyV(inverseAvatar, handPoint),
rotation: Quat.multiply(inverseAvatar, controllerRotation)
};
};
// We can't set the mouse if we're using the mouse as a fake controller. So stick an image where we would be putting the mouse.
var reticleHalfSize = 16;
var fakeReticle = Overlays.addOverlay("image", {
imageURL: "http://s3.amazonaws.com/hifi-public/images/delete.png",
width: 2 * reticleHalfSize,
height: 2 * reticleHalfSize,
alpha: 0.7
});
Script.scriptEnding.connect(function () { Overlays.deleteOverlay(fakeReticle); });
setCursor = function (hudPoint2d) {
Overlays.editOverlay(fakeReticle, {x: hudPoint2d.x - reticleHalfSize, y: hudPoint2d.y - reticleHalfSize});
};
}