From b4119f4fe978540048c80cb95f16145b4d43fdda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 20 Mar 2017 19:58:22 +0100 Subject: [PATCH] Implement time scaling for the rasterizer which affects 3D particles and the TIME uniform --- drivers/gles2/rasterizer_gles2.cpp | 20 ++++++++++++++++---- drivers/gles2/rasterizer_gles2.h | 4 ++++ servers/visual/rasterizer.h | 2 ++ servers/visual/visual_server_raster.cpp | 5 +++++ servers/visual/visual_server_raster.h | 2 ++ servers/visual/visual_server_wrap_mt.h | 1 + servers/visual_server.cpp | 2 ++ servers/visual_server.h | 2 ++ 8 files changed, 34 insertions(+), 4 deletions(-) diff --git a/drivers/gles2/rasterizer_gles2.cpp b/drivers/gles2/rasterizer_gles2.cpp index 94ff103daac..5a86efed0de 100644 --- a/drivers/gles2/rasterizer_gles2.cpp +++ b/drivers/gles2/rasterizer_gles2.cpp @@ -3950,7 +3950,12 @@ void RasterizerGLES2::begin_frame() { double time = (OS::get_singleton()->get_ticks_usec() / 1000); // get msec time /= 1000.0; // make secs - time_delta = time - last_time; + if (frame != 0) { + time_delta = time_scale * (time - last_time); + } else { + time_delta = 0.0f; + } + scaled_time += time_delta; last_time = time; frame++; @@ -4960,7 +4965,7 @@ bool RasterizerGLES2::_setup_material(const Geometry *p_geometry, const Material DEBUG_TEST_ERROR("Material arameters"); if (p_material->shader_cache->uses_time) { - material_shader.set_uniform(MaterialShaderGLES2::TIME, Math::fmod(last_time, shader_time_rollback)); + material_shader.set_uniform(MaterialShaderGLES2::TIME, Math::fmod(scaled_time, shader_time_rollback)); draw_next_frame = true; } //if uses TIME - draw_next_frame=true @@ -5003,7 +5008,7 @@ bool RasterizerGLES2::_setup_material(const Geometry *p_geometry, const Material material_shader.set_uniform(MaterialShaderGLES2::FOG_COLOR_END, Vector3(col_end.r, col_end.g, col_end.b)); } - //material_shader.set_uniform(MaterialShaderGLES2::TIME,Math::fmod(last_time,300.0)); + //material_shader.set_uniform(MaterialShaderGLES2::TIME,Math::fmod(scaled_time,300.0)); //if uses TIME - draw_next_frame=true return rebind; @@ -8784,7 +8789,7 @@ void RasterizerGLES2::_canvas_item_setup_shader_uniforms(CanvasItemMaterial *mat } if (shader->uses_time) { - canvas_shader.set_uniform(CanvasShaderGLES2::TIME, Math::fmod(last_time, shader_time_rollback)); + canvas_shader.set_uniform(CanvasShaderGLES2::TIME, Math::fmod(scaled_time, shader_time_rollback)); draw_next_frame = true; } //if uses TIME - draw_next_frame=true @@ -9361,6 +9366,11 @@ bool RasterizerGLES2::is_canvas_light_occluder(const RID &p_rid) const { return false; } +void RasterizerGLES2::set_time_scale(float p_scale) { + + time_scale = p_scale; +} + void RasterizerGLES2::free(const RID &p_rid) { if (texture_owner.owns(p_rid)) { @@ -10384,6 +10394,7 @@ void RasterizerGLES2::init() { #endif shader_time_rollback = GLOBAL_DEF("rasterizer/shader_time_rollback", 300); + time_scale = 1.0f; using_canvas_bg = false; _update_framebuffer(); @@ -10716,6 +10727,7 @@ RasterizerGLES2::RasterizerGLES2(bool p_compress_arrays, bool p_keep_ram_copy, b base_framebuffer = 0; frame = 0; + scaled_time = 0.0; draw_next_frame = false; use_framebuffers = true; framebuffer.active = false; diff --git a/drivers/gles2/rasterizer_gles2.h b/drivers/gles2/rasterizer_gles2.h index 0ef14bc5d75..ab522b3169d 100644 --- a/drivers/gles2/rasterizer_gles2.h +++ b/drivers/gles2/rasterizer_gles2.h @@ -1250,6 +1250,7 @@ class RasterizerGLES2 : public Rasterizer { Size2 window_size; VS::ViewportRect viewport; double last_time; + double scaled_time; double time_delta; uint64_t frame; uint64_t scene_pass; @@ -1258,6 +1259,7 @@ class RasterizerGLES2 : public Rasterizer { VS::ScenarioDebugMode current_debug; RID overdraw_material; float shader_time_rollback; + float time_scale; mutable MaterialShaderGLES2 material_shader; mutable CanvasShaderGLES2 canvas_shader; @@ -1652,6 +1654,8 @@ public: virtual bool is_canvas_light_occluder(const RID &p_rid) const; + virtual void set_time_scale(float p_scale); + virtual void free(const RID &p_rid); virtual void init(); diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index 51bc4d0f99b..c7b0ab69e92 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -1046,6 +1046,8 @@ public: virtual bool is_canvas_light_occluder(const RID &p_rid) const = 0; + virtual void set_time_scale(float p_scale) = 0; + virtual void free(const RID &p_rid) = 0; virtual void init() = 0; diff --git a/servers/visual/visual_server_raster.cpp b/servers/visual/visual_server_raster.cpp index 568e81b9f97..bd010cad2fd 100644 --- a/servers/visual/visual_server_raster.cpp +++ b/servers/visual/visual_server_raster.cpp @@ -7016,6 +7016,11 @@ Color VisualServerRaster::get_default_clear_color() const { return clear_color; } +void VisualServerRaster::set_time_scale(float p_scale) { + + rasterizer->set_time_scale(p_scale); +} + void VisualServerRaster::set_boot_image(const Image &p_image, const Color &p_color, bool p_scale) { if (p_image.empty()) diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index e06aad2be2b..ebf4ceef096 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -1257,6 +1257,8 @@ public: virtual void set_default_clear_color(const Color &p_color); virtual Color get_default_clear_color() const; + virtual void set_time_scale(float p_scale); + VisualServerRaster(Rasterizer *p_rasterizer); ~VisualServerRaster(); }; diff --git a/servers/visual/visual_server_wrap_mt.h b/servers/visual/visual_server_wrap_mt.h index 965f7e670a7..ef6c530a788 100644 --- a/servers/visual/visual_server_wrap_mt.h +++ b/servers/visual/visual_server_wrap_mt.h @@ -688,6 +688,7 @@ public: FUNC3(set_boot_image, const Image &, const Color &, bool); FUNC1(set_default_clear_color, const Color &); FUNC0RC(Color, get_default_clear_color); + FUNC1(set_time_scale, float); FUNC0R(RID, get_test_cube); diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp index b3dac724c41..29cf5f5c518 100644 --- a/servers/visual_server.cpp +++ b/servers/visual_server.cpp @@ -534,6 +534,8 @@ void VisualServer::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_default_clear_color"), &VisualServer::set_default_clear_color); ObjectTypeDB::bind_method(_MD("get_default_clear_color"), &VisualServer::get_default_clear_color); + ObjectTypeDB::bind_method(_MD("set_time_scale"), &VisualServer::set_time_scale); + ObjectTypeDB::bind_method(_MD("get_render_info"), &VisualServer::get_render_info); BIND_CONSTANT(NO_INDEX_ARRAY); diff --git a/servers/visual_server.h b/servers/visual_server.h index 8ada611a5bb..7885302d689 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -1137,6 +1137,8 @@ public: virtual void set_default_clear_color(const Color &p_color) = 0; virtual Color get_default_clear_color() const = 0; + virtual void set_time_scale(float p_scale) = 0; + enum Features { FEATURE_SHADERS, FEATURE_MULTITHREADED,