tidy inspect.js

This commit is contained in:
Triplelexx 2018-03-16 20:13:02 +00:00
parent 59a088f226
commit 563be9ab91

View file

@ -26,14 +26,14 @@ var RADIUS_RATE = 1.0 / 100.0;
var PAN_RATE = 250.0; var PAN_RATE = 250.0;
var Y_AXIS = { var Y_AXIS = {
x: 0, x: 0,
y: 1, y: 1,
z: 0 z: 0
}; };
var X_AXIS = { var X_AXIS = {
x: 1, x: 1,
y: 0, y: 0,
z: 0 z: 0
}; };
var LOOK_AT_TIME = 500; var LOOK_AT_TIME = 500;
@ -58,19 +58,19 @@ var mouseLastY = 0;
var center = { var center = {
x: 0, x: 0,
y: 0, y: 0,
z: 0 z: 0
}; };
var position = { var position = {
x: 0, x: 0,
y: 0, y: 0,
z: 0 z: 0
}; };
var vector = { var vector = {
x: 0, x: 0,
y: 0, y: 0,
z: 0 z: 0
}; };
var radius = 0.0; var radius = 0.0;
var azimuth = 0.0; var azimuth = 0.0;
@ -83,258 +83,249 @@ var rotatingTowardsTarget = false;
var targetCamOrientation; var targetCamOrientation;
var oldPosition, oldOrientation; var oldPosition, oldOrientation;
function orientationOf(vector) { function orientationOf(vector) {
var direction, var direction,
yaw, yaw,
pitch; pitch;
direction = Vec3.normalize(vector); direction = Vec3.normalize(vector);
yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS); yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS);
pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS); pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS);
return Quat.multiply(yaw, pitch); return Quat.multiply(yaw, pitch);
} }
function handleRadialMode(dx, dy) { function handleRadialMode(dx, dy) {
azimuth += dx / AZIMUTH_RATE; azimuth += dx / AZIMUTH_RATE;
radius += radius * dy * RADIUS_RATE; radius += radius * dy * RADIUS_RATE;
if (radius < 1) { if (radius < 1) {
radius = 1; radius = 1;
} }
vector = { vector = {
x: (Math.cos(altitude) * Math.cos(azimuth)) * radius, x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
y: Math.sin(altitude) * radius, y: Math.sin(altitude) * radius,
z: (Math.cos(altitude) * Math.sin(azimuth)) * radius z: (Math.cos(altitude) * Math.sin(azimuth)) * radius
}; };
position = Vec3.sum(center, vector); position = Vec3.sum(center, vector);
Camera.setPosition(position); Camera.setPosition(position);
Camera.setOrientation(orientationOf(vector)); Camera.setOrientation(orientationOf(vector));
} }
function handleOrbitMode(dx, dy) { function handleOrbitMode(dx, dy) {
azimuth += dx / AZIMUTH_RATE; azimuth += dx / AZIMUTH_RATE;
altitude += dy / ALTITUDE_RATE; altitude += dy / ALTITUDE_RATE;
if (altitude > PI / 2.0) { if (altitude > PI / 2.0) {
altitude = PI / 2.0; altitude = PI / 2.0;
} }
if (altitude < -PI / 2.0) { if (altitude < -PI / 2.0) {
altitude = -PI / 2.0; altitude = -PI / 2.0;
} }
vector = { vector = {
x: (Math.cos(altitude) * Math.cos(azimuth)) * radius, x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
y: Math.sin(altitude) * radius, y: Math.sin(altitude) * radius,
z: (Math.cos(altitude) * Math.sin(azimuth)) * radius z: (Math.cos(altitude) * Math.sin(azimuth)) * radius
}; };
position = Vec3.sum(center, vector); position = Vec3.sum(center, vector);
Camera.setPosition(position); Camera.setPosition(position);
Camera.setOrientation(orientationOf(vector)); Camera.setOrientation(orientationOf(vector));
} }
function handlePanMode(dx, dy) { function handlePanMode(dx, dy) {
var up = Quat.getUp(Camera.getOrientation()); var up = Quat.getUp(Camera.getOrientation());
var right = Quat.getRight(Camera.getOrientation()); var right = Quat.getRight(Camera.getOrientation());
var distance = Vec3.length(vector); var distance = Vec3.length(vector);
var dv = Vec3.sum(Vec3.multiply(up, distance * dy / PAN_RATE), Vec3.multiply(right, -distance * dx / PAN_RATE)); var dv = Vec3.sum(Vec3.multiply(up, distance * dy / PAN_RATE), Vec3.multiply(right, -distance * dx / PAN_RATE));
center = Vec3.sum(center, dv); center = Vec3.sum(center, dv);
position = Vec3.sum(position, dv); position = Vec3.sum(position, dv);
Camera.setPosition(position); Camera.setPosition(position);
Camera.setOrientation(orientationOf(vector)); Camera.setOrientation(orientationOf(vector));
} }
function saveCameraState() { function saveCameraState() {
oldMode = Camera.mode; oldMode = Camera.mode;
oldPosition = Camera.getPosition(); oldPosition = Camera.getPosition();
oldOrientation = Camera.getOrientation(); oldOrientation = Camera.getOrientation();
Camera.mode = "independent"; Camera.mode = "independent";
Camera.setPosition(oldPosition); Camera.setPosition(oldPosition);
} }
function restoreCameraState() { function restoreCameraState() {
Camera.mode = oldMode; Camera.mode = oldMode;
Camera.setPosition(oldPosition); Camera.setPosition(oldPosition);
Camera.setOrientation(oldOrientation); Camera.setOrientation(oldOrientation);
} }
function handleModes() { function handleModes() {
var newMode = (mode == noMode) ? noMode : detachedMode; var newMode = (mode == noMode) ? noMode : detachedMode;
if (alt) { if (alt) {
if (control) { if (control) {
if (shift) { if (shift) {
newMode = panningMode; newMode = panningMode;
} else { } else {
newMode = orbitMode; newMode = orbitMode;
} }
} else { } else {
newMode = radialMode; newMode = radialMode;
}
} }
}
// if entering detachMode // if entering detachMode
if (newMode == detachedMode && mode != detachedMode) { if (newMode == detachedMode && mode != detachedMode) {
avatarPosition = MyAvatar.position; avatarPosition = MyAvatar.position;
avatarOrientation = MyAvatar.orientation; avatarOrientation = MyAvatar.orientation;
} }
// if leaving detachMode // if leaving detachMode
if (mode == detachedMode && newMode == detachedMode && if (mode == detachedMode && newMode == detachedMode &&
(avatarPosition.x != MyAvatar.position.x || (avatarPosition.x != MyAvatar.position.x ||
avatarPosition.y != MyAvatar.position.y || avatarPosition.y != MyAvatar.position.y ||
avatarPosition.z != MyAvatar.position.z || avatarPosition.z != MyAvatar.position.z ||
avatarOrientation.x != MyAvatar.orientation.x || avatarOrientation.x != MyAvatar.orientation.x ||
avatarOrientation.y != MyAvatar.orientation.y || avatarOrientation.y != MyAvatar.orientation.y ||
avatarOrientation.z != MyAvatar.orientation.z || avatarOrientation.z != MyAvatar.orientation.z ||
avatarOrientation.w != MyAvatar.orientation.w)) { avatarOrientation.w != MyAvatar.orientation.w)) {
newMode = noMode; newMode = noMode;
} }
if (mode == noMode && newMode != noMode && Camera.mode == "independent") { if (mode == noMode && newMode != noMode && Camera.mode == "independent") {
newMode = noMode; newMode = noMode;
} }
// if leaving noMode // if leaving noMode
if (mode == noMode && newMode != noMode) { if (mode == noMode && newMode != noMode) {
saveCameraState(); saveCameraState();
} }
// if entering noMode // if entering noMode
if (newMode == noMode && mode != noMode) { if (newMode == noMode && mode != noMode) {
restoreCameraState(); restoreCameraState();
} }
mode = newMode; mode = newMode;
} }
function keyPressEvent(event) { function keyPressEvent(event) {
var changed = false; var changed = false;
if (event.text == "ALT") { if (event.text == "ALT") {
alt = true; alt = true;
changed = true; changed = true;
} }
if (event.text == "CONTROL") { if (event.text == "CONTROL") {
control = true; control = true;
changed = true; changed = true;
} }
if (event.text == "SHIFT") { if (event.text == "SHIFT") {
shift = true; shift = true;
changed = true; changed = true;
} }
if (changed) { if (changed) {
handleModes(); handleModes();
} }
} }
function keyReleaseEvent(event) { function keyReleaseEvent(event) {
var changed = false; var changed = false;
if (event.text == "ALT") { if (event.text == "ALT") {
alt = false; alt = false;
changed = true; changed = true;
mode = noMode; mode = noMode;
restoreCameraState(); restoreCameraState();
} }
if (event.text == "CONTROL") { if (event.text == "CONTROL") {
control = false; control = false;
changed = true; changed = true;
} }
if (event.text == "SHIFT") { if (event.text == "SHIFT") {
shift = false; shift = false;
changed = true; changed = true;
}
if (changed) {
handleModes();
}
}
function mousePressEvent(event) {
if (alt && !isActive) {
mouseLastX = event.x;
mouseLastY = event.y;
// Compute trajectories related values
var pickRay = Camera.computePickRay(mouseLastX, mouseLastY);
var modelIntersection = Entities.findRayIntersection(pickRay, true);
position = Camera.getPosition();
var avatarTarget = MyAvatar.getTargetAvatarPosition();
var distance = -1;
var string;
if (modelIntersection.intersects && modelIntersection.accurate) {
distance = modelIntersection.distance;
center = modelIntersection.intersection;
string = "Inspecting model";
//We've selected our target, now orbit towards it automatically
rotatingTowardsTarget = true;
//calculate our target cam rotation
Script.setTimeout(function() {
rotatingTowardsTarget = false;
}, LOOK_AT_TIME);
vector = Vec3.subtract(position, center);
targetCamOrientation = orientationOf(vector);
radius = Vec3.length(vector);
azimuth = Math.atan2(vector.z, vector.x);
altitude = Math.asin(vector.y / Vec3.length(vector));
isActive = true;
} }
} if (changed) {
handleModes();
}
}
function mousePressEvent(event) {
if (alt && !isActive) {
mouseLastX = event.x;
mouseLastY = event.y;
// Compute trajectories related values
var pickRay = Camera.computePickRay(mouseLastX, mouseLastY);
var modelIntersection = Entities.findRayIntersection(pickRay, true);
position = Camera.getPosition();
var distance = -1;
var string = "";
if (modelIntersection.intersects && modelIntersection.accurate) {
distance = modelIntersection.distance;
center = modelIntersection.intersection;
string = "Inspecting model";
// We've selected our target, now orbit towards it automatically
rotatingTowardsTarget = true;
// calculate our target cam rotation
Script.setTimeout(function () {
rotatingTowardsTarget = false;
}, LOOK_AT_TIME);
vector = Vec3.subtract(position, center);
targetCamOrientation = orientationOf(vector);
radius = Vec3.length(vector);
azimuth = Math.atan2(vector.z, vector.x);
altitude = Math.asin(vector.y / Vec3.length(vector));
isActive = true;
}
}
} }
function mouseReleaseEvent(event) { function mouseReleaseEvent(event) {
if (isActive) { if (isActive) {
isActive = false; isActive = false;
} }
} }
function mouseMoveEvent(event) { function mouseMoveEvent(event) {
if (isActive && mode != noMode && !rotatingTowardsTarget) { if (isActive && mode != noMode && !rotatingTowardsTarget) {
if (mode == radialMode) { if (mode == radialMode) {
handleRadialMode(event.x - mouseLastX, event.y - mouseLastY); handleRadialMode(event.x - mouseLastX, event.y - mouseLastY);
}
if (mode == orbitMode) {
handleOrbitMode(event.x - mouseLastX, event.y - mouseLastY);
}
if (mode == panningMode) {
handlePanMode(event.x - mouseLastX, event.y - mouseLastY);
}
} }
if (mode == orbitMode) { mouseLastX = event.x;
handleOrbitMode(event.x - mouseLastX, event.y - mouseLastY); mouseLastY = event.y;
}
if (mode == panningMode) {
handlePanMode(event.x - mouseLastX, event.y - mouseLastY);
}
}
mouseLastX = event.x;
mouseLastY = event.y;
} }
function update() { function update() {
handleModes(); handleModes();
if (rotatingTowardsTarget) { if (rotatingTowardsTarget) {
rotateTowardsTarget(); rotateTowardsTarget();
} }
} }
function rotateTowardsTarget() { function rotateTowardsTarget() {
var newOrientation = Quat.mix(Camera.getOrientation(), targetCamOrientation, .1); var newOrientation = Quat.mix(Camera.getOrientation(), targetCamOrientation, 0.1);
Camera.setOrientation(newOrientation); Camera.setOrientation(newOrientation);
} }
function scriptEnding() { function scriptEnding() {
if (mode != noMode) { if (mode != noMode) {
restoreCameraState(); restoreCameraState();
} }
} }
Controller.keyPressEvent.connect(keyPressEvent); Controller.keyPressEvent.connect(keyPressEvent);
@ -345,4 +336,4 @@ Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
Controller.mouseMoveEvent.connect(mouseMoveEvent); Controller.mouseMoveEvent.connect(mouseMoveEvent);
Script.update.connect(update); Script.update.connect(update);
Script.scriptEnding.connect(scriptEnding); Script.scriptEnding.connect(scriptEnding);