gfx-shader: Additional stable time values

'Time.x' gets inaccurate if OBS Studio is running for more than two hours, therefore we have to do something to fix it. By allowing the shader code to control when things loop using 'Time.y' (0..1) and 'Time.z' (the number of times 'Time.y' wrapped back to 0), a much more stable animation can be achieved.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-03-28 22:17:33 +01:00
parent 7d5c4361e3
commit 79406b0b08
2 changed files with 16 additions and 5 deletions

View File

@ -38,7 +38,7 @@ gfx::shader::shader::shader(obs_source_t* self, shader_mode mode)
_width_type(size_type::Percent), _width_value(1.0), _height_type(size_type::Percent), _height_value(1.0), _width_type(size_type::Percent), _width_value(1.0), _height_type(size_type::Percent), _height_value(1.0),
_time(0), _random(), _have_current_params(false) _time(0), _time_loop(0), _loops(0), _random(), _have_current_params(false)
{ {
_random.seed(static_cast<unsigned long long>(time(NULL))); _random.seed(static_cast<unsigned long long>(time(NULL)));
} }
@ -362,6 +362,15 @@ bool gfx::shader::shader::tick(float_t time)
// Update State // Update State
_time += time; _time += time;
_time_loop += time;
if (_time_loop > 1.) {
_time_loop -= 1.;
// Loops
_loops += 1;
if (_loops >= 4194304)
_loops = -_loops;
}
return false; return false;
} }
@ -379,7 +388,7 @@ void gfx::shader::shader::prepare_render()
if (gs::effect_parameter el = _shader.get_parameter("Time"); el != nullptr) { if (gs::effect_parameter el = _shader.get_parameter("Time"); el != nullptr) {
if (el.get_type() == gs::effect_parameter::type::Float4) { if (el.get_type() == gs::effect_parameter::type::Float4) {
el.set_float4( el.set_float4(
_time, 0, 0, _time, _time_loop, static_cast<float_t>(_loops),
static_cast<float_t>(static_cast<double_t>(_random()) static_cast<float_t>(static_cast<double_t>(_random())
/ static_cast<double_t>(std::numeric_limits<unsigned long long>::max()))); / static_cast<double_t>(std::numeric_limits<unsigned long long>::max())));
} }

View File

@ -77,6 +77,8 @@ namespace gfx {
// Cache // Cache
float_t _time; float_t _time;
float_t _time_loop;
int32_t _loops;
std::mt19937_64 _random; std::mt19937_64 _random;
bool _have_current_params; bool _have_current_params;