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 PAN_RATE = 50.0;
var Y_AXIS = { x: 0, y: 1, z: 0 };
var X_AXIS = { x: 1, y: 0, z: 0 };
var Y_AXIS = {
x: 0,
y: 1,
z: 0
};
var X_AXIS = {
x: 1,
y: 0,
z: 0
};
var LOOK_AT_TIME = 500;
var alt = false;
var shift = false;
@ -34,6 +44,7 @@ var control = false;
var isActive = false;
var oldMode = Camera.mode;
var noMode = 0;
var orbitMode = 1;
var radialMode = 2;
@ -46,9 +57,21 @@ var mouseLastX = 0;
var mouseLastY = 0;
var center = { x: 0, y: 0, z: 0 };
var position = { x: 0, y: 0, z: 0 };
var vector = { x: 0, y: 0, z: 0 };
var center = {
x: 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 azimuth = 0.0;
var altitude = 0.0;
@ -56,6 +79,10 @@ var altitude = 0.0;
var avatarPosition;
var avatarOrientation;
var rotatingTowardsTarget = false;
var targetCamOrientation;
var oldPosition, oldOrientation;
function orientationOf(vector) {
var direction,
@ -76,9 +103,11 @@ function handleRadialMode(dx, dy) {
radius = 1;
}
vector = { x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
vector = {
x: (Math.cos(altitude) * Math.cos(azimuth)) * 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);
Camera.setPosition(position);
Camera.setOrientation(orientationOf(vector));
@ -94,9 +123,11 @@ function handleOrbitMode(dx, dy) {
altitude = -PI / 2.0;
}
vector = { x:(Math.cos(altitude) * Math.cos(azimuth)) * radius,
y:Math.sin(altitude) * radius,
z:(Math.cos(altitude) * Math.sin(azimuth)) * radius };
vector = {
x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
y: Math.sin(altitude) * radius,
z: (Math.cos(altitude) * Math.sin(azimuth)) * radius
};
position = Vec3.sum(center, vector);
Camera.setPosition(position);
Camera.setOrientation(orientationOf(vector));
@ -108,7 +139,7 @@ function handlePanMode(dx, dy) {
var right = Quat.getRight(Camera.getOrientation());
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);
position = Vec3.sum(position, dv);
@ -119,13 +150,17 @@ function handlePanMode(dx, dy) {
function saveCameraState() {
oldMode = Camera.mode;
var oldPosition = Camera.getPosition();
oldPosition = Camera.getPosition();
oldOrientation = Camera.getOrientation();
Camera.mode = "independent";
Camera.setPosition(oldPosition);
}
function restoreCameraState() {
Camera.mode = oldMode;
Camera.setPosition(oldPosition);
Camera.setOrientation(oldOrientation);
}
function handleModes() {
@ -202,6 +237,8 @@ function keyReleaseEvent(event) {
if (event.text == "ALT") {
alt = false;
changed = true;
mode = noMode;
restoreCameraState();
}
if (event.text == "CONTROL") {
control = false;
@ -226,7 +263,7 @@ function mousePressEvent(event) {
// Compute trajectories related values
var pickRay = Camera.computePickRay(mouseLastX, mouseLastY);
var modelIntersection = Entities.findRayIntersection(pickRay);
var modelIntersection = Entities.findRayIntersection(pickRay, true);
position = Camera.getPosition();
@ -238,29 +275,25 @@ function mousePressEvent(event) {
if (modelIntersection.intersects && modelIntersection.accurate) {
distance = modelIntersection.distance;
center = modelIntersection.properties.position;
center = modelIntersection.intersection;
string = "Inspecting model";
}
if ((distance == -1 || Vec3.length(Vec3.subtract(avatarTarget, position)) < distance) &&
(avatarTarget.x != 0 || avatarTarget.y != 0 || avatarTarget.z != 0)) {
distance = Vec3.length(Vec3.subtract(avatarTarget, position));
center = avatarTarget;
string = "Inspecting avatar";
}
if (distance == -1) {
return;
}
//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));
print(string);
isActive = true;
}
}
}
function mouseReleaseEvent(event) {
@ -270,7 +303,7 @@ function mouseReleaseEvent(event) {
}
function mouseMoveEvent(event) {
if (isActive && mode != noMode) {
if (isActive && mode != noMode && !rotatingTowardsTarget) {
if (mode == radialMode) {
handleRadialMode(event.x - mouseLastX, event.y - mouseLastY);
}
@ -281,13 +314,21 @@ function mouseMoveEvent(event) {
handlePanMode(event.x - mouseLastX, event.y - mouseLastY);
}
}
mouseLastX = event.x;
mouseLastY = event.y;
}
}
function update() {
handleModes();
if (rotatingTowardsTarget) {
rotateTowardsTarget();
}
}
function rotateTowardsTarget() {
var newOrientation = Quat.mix(Camera.getOrientation(), targetCamOrientation, .1);
Camera.setOrientation(newOrientation);
}
function scriptEnding() {