added oldMode as global vairable

This commit is contained in:
Eric Levin 2015-05-21 18:08:04 -07:00
parent 986eea7c86
commit ce1ccf1c40

View file

@ -25,8 +25,18 @@ var ALTITUDE_RATE = 200.0;
var RADIUS_RATE = 1.0 / 100.0; var RADIUS_RATE = 1.0 / 100.0;
var PAN_RATE = 50.0; var PAN_RATE = 50.0;
var Y_AXIS = { x: 0, y: 1, z: 0 }; var Y_AXIS = {
var X_AXIS = { x: 1, y: 0, z: 0 }; x: 0,
y: 1,
z: 0
};
var X_AXIS = {
x: 1,
y: 0,
z: 0
};
var LOOK_AT_TIME = 500;
var alt = false; var alt = false;
var shift = false; var shift = false;
@ -34,6 +44,7 @@ var control = false;
var isActive = false; var isActive = false;
var oldMode = Camera.mode;
var noMode = 0; var noMode = 0;
var orbitMode = 1; var orbitMode = 1;
var radialMode = 2; var radialMode = 2;
@ -46,9 +57,21 @@ var mouseLastX = 0;
var mouseLastY = 0; var mouseLastY = 0;
var center = { x: 0, y: 0, z: 0 }; var center = {
var position = { x: 0, y: 0, z: 0 }; x: 0,
var vector = { x: 0, y: 0, z: 0 }; y: 0,
z: 0
};
var position = {
x: 0,
y: 0,
z: 0
};
var vector = {
x: 0,
y: 0,
z: 0
};
var radius = 0.0; var radius = 0.0;
var azimuth = 0.0; var azimuth = 0.0;
var altitude = 0.0; var altitude = 0.0;
@ -56,244 +79,262 @@ var altitude = 0.0;
var avatarPosition; var avatarPosition;
var avatarOrientation; var avatarOrientation;
var rotatingTowardsTarget = false;
var targetCamOrientation;
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 = { x: (Math.cos(altitude) * Math.cos(azimuth)) * radius, vector = {
y: Math.sin(altitude) * radius, x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
z: (Math.cos(altitude) * Math.sin(azimuth)) * radius }; y: Math.sin(altitude) * radius,
position = Vec3.sum(center, vector); z: (Math.cos(altitude) * Math.sin(azimuth)) * radius
Camera.setPosition(position); };
Camera.setOrientation(orientationOf(vector)); position = Vec3.sum(center, vector);
Camera.setPosition(position);
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 = { x:(Math.cos(altitude) * Math.cos(azimuth)) * radius, vector = {
y:Math.sin(altitude) * radius, x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
z:(Math.cos(altitude) * Math.sin(azimuth)) * radius }; y: Math.sin(altitude) * radius,
position = Vec3.sum(center, vector); z: (Math.cos(altitude) * Math.sin(azimuth)) * radius
Camera.setPosition(position); };
Camera.setOrientation(orientationOf(vector)); position = Vec3.sum(center, vector);
Camera.setPosition(position);
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;
var oldPosition = Camera.getPosition(); oldPosition = Camera.getPosition();
Camera.mode = "independent"; oldOrientation = Camera.getOrientation();
Camera.setPosition(oldPosition);
Camera.mode = "independent";
Camera.setPosition(oldPosition);
} }
function restoreCameraState() { function restoreCameraState() {
Camera.mode = oldMode; Camera.mode = oldMode;
Camera.setPosition(oldPosition);
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 (newMode == detachedMode && mode != detachedMode) {
avatarPosition = MyAvatar.position;
avatarOrientation = MyAvatar.orientation;
}
// if leaving detachMode
if (mode == detachedMode && newMode == detachedMode &&
(avatarPosition.x != MyAvatar.position.x ||
avatarPosition.y != MyAvatar.position.y ||
avatarPosition.z != MyAvatar.position.z ||
avatarOrientation.x != MyAvatar.orientation.x ||
avatarOrientation.y != MyAvatar.orientation.y ||
avatarOrientation.z != MyAvatar.orientation.z ||
avatarOrientation.w != MyAvatar.orientation.w)) {
newMode = noMode;
} }
}
if (mode == noMode && newMode != noMode && Camera.mode == "independent") { // if entering detachMode
newMode = noMode; if (newMode == detachedMode && mode != detachedMode) {
} avatarPosition = MyAvatar.position;
avatarOrientation = MyAvatar.orientation;
}
// if leaving detachMode
if (mode == detachedMode && newMode == detachedMode &&
(avatarPosition.x != MyAvatar.position.x ||
avatarPosition.y != MyAvatar.position.y ||
avatarPosition.z != MyAvatar.position.z ||
avatarOrientation.x != MyAvatar.orientation.x ||
avatarOrientation.y != MyAvatar.orientation.y ||
avatarOrientation.z != MyAvatar.orientation.z ||
avatarOrientation.w != MyAvatar.orientation.w)) {
newMode = noMode;
}
// if leaving noMode if (mode == noMode && newMode != noMode && Camera.mode == "independent") {
if (mode == noMode && newMode != noMode) { newMode = noMode;
saveCameraState(); }
}
// if entering noMode // if leaving noMode
if (newMode == noMode && mode != noMode) { if (mode == noMode && newMode != noMode) {
restoreCameraState(); saveCameraState();
} }
// if entering noMode
mode = newMode; if (newMode == noMode && mode != noMode) {
restoreCameraState();
}
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;
if (event.text == "CONTROL") { restoreCameraState();
control = false; }
changed = true; if (event.text == "CONTROL") {
} control = false;
if (event.text == "SHIFT") { changed = true;
shift = false; }
changed = true; if (event.text == "SHIFT") {
} shift = false;
changed = true;
if (changed) { }
handleModes();
} if (changed) {
handleModes();
}
} }
function mousePressEvent(event) { function mousePressEvent(event) {
if (alt && !isActive) { if (alt && !isActive) {
mouseLastX = event.x; mouseLastX = event.x;
mouseLastY = event.y; mouseLastY = event.y;
// Compute trajectories related values // Compute trajectories related values
var pickRay = Camera.computePickRay(mouseLastX, mouseLastY); var pickRay = Camera.computePickRay(mouseLastX, mouseLastY);
var modelIntersection = Entities.findRayIntersection(pickRay); var modelIntersection = Entities.findRayIntersection(pickRay, true);
position = Camera.getPosition(); position = Camera.getPosition();
var avatarTarget = MyAvatar.getTargetAvatarPosition(); var avatarTarget = MyAvatar.getTargetAvatarPosition();
var distance = -1; var distance = -1;
var string; var string;
if (modelIntersection.intersects && modelIntersection.accurate) { if (modelIntersection.intersects && modelIntersection.accurate) {
distance = modelIntersection.distance; distance = modelIntersection.distance;
center = modelIntersection.properties.position; center = modelIntersection.intersection;
string = "Inspecting model"; string = "Inspecting model";
} //We've selected our target, now orbit towards it automatically
rotatingTowardsTarget = true;
if ((distance == -1 || Vec3.length(Vec3.subtract(avatarTarget, position)) < distance) && //calculate our target cam rotation
(avatarTarget.x != 0 || avatarTarget.y != 0 || avatarTarget.z != 0)) { Script.setTimeout(function() {
distance = Vec3.length(Vec3.subtract(avatarTarget, position)); rotatingTowardsTarget = false;
center = avatarTarget; }, LOOK_AT_TIME);
string = "Inspecting avatar";
} vector = Vec3.subtract(position, center);
targetCamOrientation = orientationOf(vector);
if (distance == -1) { radius = Vec3.length(vector);
return; azimuth = Math.atan2(vector.z, vector.x);
} altitude = Math.asin(vector.y / Vec3.length(vector));
vector = Vec3.subtract(position, center); isActive = true;
radius = Vec3.length(vector);
azimuth = Math.atan2(vector.z, vector.x);
altitude = Math.asin(vector.y / Vec3.length(vector));
print(string);
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) { 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);
}
mouseLastX = event.x;
mouseLastY = event.y;
} }
if (mode == orbitMode) {
handleOrbitMode(event.x - mouseLastX, event.y - mouseLastY);
}
if (mode == panningMode) {
handlePanMode(event.x - mouseLastX, event.y - mouseLastY);
}
}
mouseLastX = event.x;
mouseLastY = event.y;
} }
function update() { function update() {
handleModes(); handleModes();
if (rotatingTowardsTarget) {
rotateTowardsTarget();
}
}
function rotateTowardsTarget() {
var newOrientation = Quat.mix(Camera.getOrientation(), targetCamOrientation, .1);
Camera.setOrientation(newOrientation);
} }
function scriptEnding() { function scriptEnding() {
if (mode != noMode) { if (mode != noMode) {
restoreCameraState(); restoreCameraState();
} }
} }
Controller.keyPressEvent.connect(keyPressEvent); Controller.keyPressEvent.connect(keyPressEvent);