add fade between depths in depth reticle

This commit is contained in:
Brad Hefta-Gaub 2016-02-26 12:01:59 -08:00
parent 783892ac2e
commit 6d9c585604

View file

@ -12,17 +12,24 @@
var APPARENT_2D_OVERLAY_DEPTH = 1.0;
var APPARENT_MAXIMUM_DEPTH = 100.0; // this is a depth at which things all seem sufficiently distant
var lastDepthCheckTime = 0;
var lastDepthCheckTime = Date.now();
var desiredDepth = APPARENT_2D_OVERLAY_DEPTH;
var wasDepth = Reticle.depth; // depth at the time we changed our desired depth
var desiredDepthLastSet = lastDepthCheckTime; // time we changed our desired depth
var TIME_BETWEEN_DEPTH_CHECKS = 100;
var TIME_TO_FADE_DEPTH = 50;
Script.update.connect(function(deltaTime) {
var TIME_BETWEEN_DEPTH_CHECKS = 100;
var timeSinceLastDepthCheck = Date.now() - lastDepthCheckTime;
var now = Date.now();
var timeSinceLastDepthCheck = now - lastDepthCheckTime;
if (timeSinceLastDepthCheck > TIME_BETWEEN_DEPTH_CHECKS) {
var newDesiredDepth = desiredDepth;
lastDepthCheckTime = now;
var reticlePosition = Reticle.position;
// first check the 2D Overlays
if (Reticle.pointingAtSystemOverlay || Overlays.getOverlayAtPoint(reticlePosition)) {
Reticle.setDepth(APPARENT_2D_OVERLAY_DEPTH);
newDesiredDepth = APPARENT_2D_OVERLAY_DEPTH;
} else {
var pickRay = Camera.computePickRay(reticlePosition.x, reticlePosition.y);
@ -37,11 +44,39 @@ Script.update.connect(function(deltaTime) {
// If either the overlays or entities intersect, then set the reticle depth to
// the distance of intersection
if (result.intersects) {
Reticle.setDepth(result.distance);
newDesiredDepth = result.distance;
} else {
// if nothing intersects... set the depth to some sufficiently large depth
Reticle.setDepth(APPARENT_MAXIMUM_DEPTH);
newDesiredDepth = APPARENT_MAXIMUM_DEPTH;
}
}
// If the desired depth has changed, reset our fade start time
if (desiredDepth != newDesiredDepth) {
desiredDepthLastSet = now;
desiredDepth = newDesiredDepth;
wasDepth = Reticle.depth;
}
}
// move the reticle toward the desired depth
if (desiredDepth != Reticle.depth) {
// determine the time between now, and when we set our determined our desiredDepth
var elapsed = now - desiredDepthLastSet;
var distanceToFade = desiredDepth - wasDepth;
var percentElapsed = Math.min(1, elapsed / TIME_TO_FADE_DEPTH);
// special case to handle no fade settings
if (TIME_TO_FADE_DEPTH == 0) {
percentElapsed = 1;
}
var depthDelta = distanceToFade * percentElapsed;
var newDepth = wasDepth + depthDelta;
if (percentElapsed == 1) {
newDepth = desiredDepth;
}
Reticle.setDepth(newDepth);
}
});