Fix issues with fading by using the system time instead of fps

This commit is contained in:
Ylva Selling
2022-03-23 18:45:57 -04:00
parent aed059a147
commit 33ddd94426
3 changed files with 84 additions and 59 deletions
+15 -6
View File
@@ -27,6 +27,7 @@
#include <openspace/documentation/documentation.h>
#include <deque>
#include <chrono>
namespace openspace {
@@ -38,7 +39,6 @@ class SceneGraphNode;
class TargetBrowserPair {
public:
constexpr static const float FadeThreshold = 0.01f;
constexpr static const float DeltaTimeThreshold = 0.03f;
TargetBrowserPair(SceneGraphNode* target, ScreenSpaceSkyBrowser* browser);
@@ -52,7 +52,8 @@ public:
// Animation
void startAnimation(glm::dvec3 coordsEnd, float fovEnd, bool shouldLockAfter = true);
void incrementallyAnimateToCoordinate(double deltaTime);
void incrementallyFade(float goalState, float fadeTime, float deltaTime);
void startFading(float goal, float fadeTime);
void incrementallyFade(float deltaTime);
// Mouse interaction
bool checkMouseIntersection(const glm::vec2& mousePosition);
glm::vec2 selectedScreenSpacePosition() const;
@@ -69,12 +70,13 @@ public:
void centerTargetOnScreen();
void incrementallyAnimateTarget(float deltaTime);
bool hasFinishedFading(float goalState) const;
bool hasFinishedFading() const;
bool isFacingCamera() const;
bool isUsingRadiusAzimuthElevation() const;
bool isEnabled() const;
void setEnabled(bool enable);
void setOpacity(float opacity);
void setIsSyncedWithWwt(bool isSynced);
void setVerticalFov(float vfov);
void setEquatorialAim(const glm::dvec2& aim);
@@ -111,9 +113,6 @@ public:
const TargetBrowserPair& rhs);
private:
bool isTargetFadeFinished(float goalState) const;
bool isBrowserFadeFinished(float goalState) const;
void aimTargetGalactic(glm::dvec3 direction);
// Selection
@@ -123,10 +122,20 @@ private:
RenderableSkyTarget* _targetRenderable = nullptr;
ScreenSpaceSkyBrowser* _browser = nullptr;
SceneGraphNode* _targetNode = nullptr;
// Animation
glm::dvec3 _animationStart = glm::dvec3(0);
glm::dvec3 _animationEnd = glm::dvec3(0);
bool _shouldLockAfterAnimation = false;
bool _targetIsAnimated = false;
// Fading
float _goal = 1.0f;
float _startTarget = 1.0f;
float _startBrowser = 1.0f;
std::chrono::milliseconds _fadeTime = std::chrono::milliseconds(2000);
std::chrono::system_clock::time_point _fadeStart;
bool _isFading = false;
glm::dvec2 _equatorialAim = glm::dvec2(0.0);
glm::ivec3 _borderColor = glm::ivec3(255);
+27 -22
View File
@@ -167,15 +167,28 @@ SkyBrowserModule::SkyBrowserModule()
bool vizModeChanged = _isCameraInSolarSystem != camWasInSolarSystem;
// Visualization mode changed. Start fading
if (vizModeChanged) {
_isFading = true;
if (vizModeChanged && !_isCameraInSolarSystem) {
// Camera moved into the solar system
if (!_isCameraInSolarSystem) {
_goal = Transparency::Transparent;
}
else {
_goal = Transparency::Opaque;
}
_isFading = true;
_goal = Transparency::Transparent;
float transparency = [](Transparency goal) {
switch (goal) {
case Transparency::Transparent:
return 0.f;
case Transparency::Opaque:
return 1.f;
}
}(_goal);
std::for_each(
_targetsBrowsers.begin(),
_targetsBrowsers.end(),
[&](const std::unique_ptr<TargetBrowserPair>& pair) {
pair->startFading(transparency, 2.f);
}
);
}
double deltaTime = global::windowDelegate->deltaTime();
// Fade pairs if the camera moved in or out the solar system
@@ -421,26 +434,18 @@ void SkyBrowserModule::incrementallyRotateCamera(double deltaTime, double animat
void SkyBrowserModule::incrementallyFadeBrowserTargets(Transparency goal,
float deltaTime)
{
float transparency = [](Transparency goal) {
switch (goal) {
case Transparency::Transparent:
return 0.f;
case Transparency::Opaque:
return 1.f;
}
}(goal);
bool isAllFinished{ false };
{
bool isAllFinished = true;
for (std::unique_ptr<TargetBrowserPair>& pair : _targetsBrowsers) {
if (pair->isEnabled()) {
bool isPairFinished = pair->hasFinishedFading(transparency);
bool isPairFinished = pair->hasFinishedFading();
if (!isPairFinished) {
pair->incrementallyFade(transparency, FadingTime, deltaTime);
pair->incrementallyFade(deltaTime);
}
else if (isPairFinished && goal == Transparency::Transparent) {
pair->setEnabled(false);
pair->setOpacity(1.0);
}
isAllFinished &= isPairFinished;
}
+42 -31
View File
@@ -25,6 +25,7 @@
#include <modules/skybrowser/include/targetbrowserpair.h>
#include <modules/skybrowser/include/screenspaceskybrowser.h>
#include <modules/skybrowser/skybrowsermodule.h>
#include <modules/skybrowser/include/renderableskytarget.h>
#include <modules/skybrowser/include/utility.h>
#include <modules/skybrowser/include/wwtdatahandler.h>
@@ -67,17 +68,6 @@ void TargetBrowserPair::highlight(const glm::ivec3& color) {
_targetRenderable->highlight(color);
}
bool TargetBrowserPair::isTargetFadeFinished(float goalState) const {
float targetDiff = abs(_targetRenderable->opacity() - goalState);
return targetDiff < FadeThreshold;
}
bool TargetBrowserPair::isBrowserFadeFinished(float goalState) const {
float browserDiff = abs(_browser->opacity() - goalState);
return browserDiff < FadeThreshold;
}
void TargetBrowserPair::aimTargetGalactic(glm::dvec3 direction) {
std::string id = _targetNode->identifier();
// Uris for properties
@@ -150,10 +140,17 @@ void TargetBrowserPair::synchronizeAim() {
void TargetBrowserPair::setEnabled(bool enable) {
_browser->setEnabled(enable);
_targetRenderable->property("Enabled")->set(false);
}
void TargetBrowserPair::setOpacity(float opacity)
{
_browser->property("Opacity")->set(opacity);
_targetRenderable->property("Opacity")->set(opacity);
}
bool TargetBrowserPair::isEnabled() const {
return _targetRenderable->isEnabled() && _browser->isEnabled();
return _targetRenderable->isEnabled() || _browser->isEnabled();
}
void TargetBrowserPair::initialize() {
@@ -290,6 +287,16 @@ void TargetBrowserPair::incrementallyAnimateToCoordinate(double deltaTime) {
}
}
void TargetBrowserPair::startFading(float goal, float fadeTime)
{
_startTarget = _targetRenderable->opacity();
_startBrowser = _browser->opacity();
_goal = goal;
_fadeTime = std::chrono::milliseconds(static_cast<int>(fadeTime * 1000));
_fadeStart = std::chrono::system_clock::now();
_isFading = true;
}
void TargetBrowserPair::startAnimation(glm::dvec3 galacticCoords, float fovEnd,
bool shouldLockAfter)
{
@@ -343,8 +350,8 @@ void TargetBrowserPair::centerTargetOnScreen() {
startAnimation(viewDirection, currentFov, false);
}
bool TargetBrowserPair::hasFinishedFading(float goalState) const {
return isTargetFadeFinished(goalState) && isBrowserFadeFinished(goalState);
bool TargetBrowserPair::hasFinishedFading() const {
return !_isFading;
}
bool TargetBrowserPair::isFacingCamera() const {
@@ -363,26 +370,30 @@ ScreenSpaceSkyBrowser* TargetBrowserPair::browser() const {
return _browser;
}
void TargetBrowserPair::incrementallyFade(float goalState, float fadeTime,
float deltaTime)
void TargetBrowserPair::incrementallyFade(float deltaTime)
{
float opacityDelta = static_cast<float>(deltaTime / fadeTime);
if (_targetRenderable->opacity() > goalState) {
opacityDelta *= -1.f;
}
if (!isTargetFadeFinished(goalState)) {
_targetRenderable->setOpacity(_targetRenderable->opacity() + opacityDelta);
using namespace std::chrono;
system_clock::time_point now = system_clock::now();
std::chrono::duration<double, std::milli> timeSpent = now - _fadeStart;
if (timeSpent.count() > _fadeTime.count()) {
_isFading = false;
_browser->setOpacity(_goal);
_targetRenderable->setOpacity(_goal);
}
else {
_targetRenderable->setOpacity(goalState);
}
if (!isBrowserFadeFinished(goalState)) {
_browser->setOpacity(_browser->opacity() + opacityDelta);
}
else {
_browser->setOpacity(goalState);
float percentage = timeSpent / _fadeTime;
float newOpacityTarget;
float newOpacityBrowser;
if (_goal > _startTarget || _goal > _startBrowser) {
newOpacityTarget, newOpacityBrowser = _goal * percentage;
}
else {
newOpacityTarget = _startTarget * (1.f - percentage);
newOpacityBrowser = _startBrowser * (1.f - percentage);
}
_browser->setOpacity(newOpacityBrowser);
_targetRenderable->setOpacity(newOpacityTarget);
}
}