Merge pull request #4931 from ericrius1/inspect

inspect now uses accurate picking and camera slerps to looks at targe…
This commit is contained in:
Philip Rosedale 2015-05-21 17:15:30 -07:00
commit e0e1744d2e
3 changed files with 301 additions and 238 deletions

View file

@ -18,3 +18,4 @@ Script.load("notifications.js");
Script.load("look.js");
Script.load("users.js");
Script.load("grab.js");
Script.load("pointer.js");

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;
@ -46,9 +56,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 +78,10 @@ var altitude = 0.0;
var avatarPosition;
var avatarOrientation;
var rotatingTowardsTarget = false;
var targetCamOrientation;
var oldPosition, oldOrientation;
function orientationOf(vector) {
var direction,
@ -76,9 +102,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 +122,11 @@ function handleOrbitMode(dx, dy) {
altitude = -PI / 2.0;
}
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));
@ -119,13 +149,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 +236,8 @@ function keyReleaseEvent(event) {
if (event.text == "ALT") {
alt = false;
changed = true;
mode = noMode;
restoreCameraState();
}
if (event.text == "CONTROL") {
control = false;
@ -226,7 +262,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 +274,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 +302,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 +313,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() {

View file

@ -1,7 +1,7 @@
var lineEntityID = null;
var lineIsRezzed = false;
var altHeld = false;
var lineCreated = false;
function nearLinePoint(targetPosition) {
var handPosition = MyAvatar.getRightPalmPosition();
@ -40,7 +40,11 @@ function createOrUpdateLine(event) {
type: "Line",
position: nearLinePoint(intersection.intersection),
dimensions: dim,
color: { red: 255, green: 255, blue: 255 },
color: {
red: 255,
green: 255,
blue: 255
},
lifetime: 15 // if someone crashes while pointing, don't leave the line there forever.
});
}
@ -51,11 +55,12 @@ function createOrUpdateLine(event) {
function mousePressEvent(event) {
if (!event.isLeftButton) {
if (!event.isLeftButton || altHeld) {
return;
}
Controller.mouseMoveEvent.connect(mouseMoveEvent);
createOrUpdateLine(event);
lineCreated = true;
}
@ -65,13 +70,30 @@ function mouseMoveEvent(event) {
function mouseReleaseEvent(event) {
if (!event.isLeftButton) {
if (!lineCreated) {
return;
}
Controller.mouseMoveEvent.disconnect(mouseMoveEvent);
removeLine();
lineCreated = false;
}
function keyPressEvent(event) {
if (event.text == "ALT") {
altHeld = true;
}
}
function keyReleaseEvent(event) {
if (event.text == "ALT") {
altHeld = false;
}
}
Controller.mousePressEvent.connect(mousePressEvent);
Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
Controller.keyPressEvent.connect(keyPressEvent);
Controller.keyReleaseEvent.connect(keyReleaseEvent);