diff --git a/doc/classes/RenderingServer.xml b/doc/classes/RenderingServer.xml index 1230b1d03..ae210761c 100644 --- a/doc/classes/RenderingServer.xml +++ b/doc/classes/RenderingServer.xml @@ -273,11 +273,12 @@ + Add texture rect regions as an animation to the [CanvasItem]'s draw commands. You need to pass an Array of Arrays, the inner Arrays are the frames. The 0th argument is the frame time, the rest are the arguments of [member canvas_item_add_texture_rect_region] in order. This Animation might lag behind by a frame sometimes, using it is not recommended for highly precise gameplay related things, it's more suited for decorations. - The Array should like this: + The Array should look like this: [code] [ [ @@ -292,7 +293,6 @@ ], # ... More Frames ][/code] - diff --git a/servers/rendering/rendering_server_canvas.cpp b/servers/rendering/rendering_server_canvas.cpp index f31a6f2f7..a9d4fa7a3 100644 --- a/servers/rendering/rendering_server_canvas.cpp +++ b/servers/rendering/rendering_server_canvas.cpp @@ -39,6 +39,8 @@ #include "rendering_server_raster.h" #include "rendering_server_viewport.h" +#include "core/math/random_pcg.h" + static const int z_range = RS::CANVAS_ITEM_Z_MAX - RS::CANVAS_ITEM_Z_MIN + 1; void RenderingServerCanvas::_render_canvas_item_tree(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights) { @@ -1512,7 +1514,7 @@ void RenderingServerCanvas::canvas_item_add_multimesh(RID p_item, RID p_mesh, RI } } -void RenderingServerCanvas::canvas_item_add_texture_rect_animation(RID p_item, const Array &p_animation_data) { +void RenderingServerCanvas::canvas_item_add_texture_rect_animation(RID p_item, const Array &p_animation_data, const bool p_randomize_start_time) { // Important! if (p_animation_data.size() == 0) { return; @@ -1591,6 +1593,12 @@ void RenderingServerCanvas::canvas_item_add_texture_rect_animation(RID p_item, c } ra->total_time = total_frame_time; + + if (p_randomize_start_time) { + RandomPCG r; + r.randomize(); + ra->time_elapsed = r.randf() * ra->total_time; + } canvas_item->rect_dirty = true; canvas_item->commands.push_back(ra); diff --git a/servers/rendering/rendering_server_canvas.h b/servers/rendering/rendering_server_canvas.h index c9dcd8b67..35d901c1d 100644 --- a/servers/rendering/rendering_server_canvas.h +++ b/servers/rendering/rendering_server_canvas.h @@ -247,7 +247,7 @@ public: void canvas_item_add_triangle_array(RID p_item, const Vector &p_indices, const Vector &p_points, const Vector &p_colors, const Vector &p_uvs = Vector(), const Vector &p_bones = Vector(), const Vector &p_weights = Vector(), RID p_texture = RID(), int p_count = -1, RID p_normal_map = RID(), bool p_antialiased = false, bool p_antialiasing_use_indices = false); void canvas_item_add_mesh(RID p_item, const RID &p_mesh, const Transform2D &p_transform = Transform2D(), const Color &p_modulate = Color(1, 1, 1), RID p_texture = RID(), RID p_normal_map = RID()); void canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture = RID(), RID p_normal_map = RID()); - void canvas_item_add_texture_rect_animation(RID p_item, const Array &p_animation_data); + void canvas_item_add_texture_rect_animation(RID p_item, const Array &p_animation_data, const bool p_randomize_start_time = false); void canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform); void canvas_item_add_clip_ignore(RID p_item, bool p_ignore); diff --git a/servers/rendering/rendering_server_raster.h b/servers/rendering/rendering_server_raster.h index eac3a363c..38ceabb5c 100644 --- a/servers/rendering/rendering_server_raster.h +++ b/servers/rendering/rendering_server_raster.h @@ -612,7 +612,7 @@ public: BIND12(canvas_item_add_triangle_array, RID, const Vector &, const Vector &, const Vector &, const Vector &, const Vector &, const Vector &, RID, int, RID, bool, bool) BIND6(canvas_item_add_mesh, RID, const RID &, const Transform2D &, const Color &, RID, RID) BIND4(canvas_item_add_multimesh, RID, RID, RID, RID) - BIND2(canvas_item_add_texture_rect_animation, RID, const Array &) + BIND3(canvas_item_add_texture_rect_animation, RID, const Array &, const bool) BIND2(canvas_item_add_set_transform, RID, const Transform2D &) BIND2(canvas_item_add_clip_ignore, RID, bool) diff --git a/servers/rendering/rendering_server_wrap_mt.h b/servers/rendering/rendering_server_wrap_mt.h index a7e38b5f3..524f2aeb4 100644 --- a/servers/rendering/rendering_server_wrap_mt.h +++ b/servers/rendering/rendering_server_wrap_mt.h @@ -514,7 +514,7 @@ public: FUNC7(canvas_item_add_texture_rect, RID, const Rect2 &, RID, bool, const Color &, bool, RID) FUNC8(canvas_item_add_texture_rect_region, RID, const Rect2 &, RID, const Rect2 &, const Color &, bool, RID, bool) FUNC7(canvas_item_add_texture_multirect_region, RID, const Vector &, RID, const Vector &, const Color &, uint32_t, RID) - FUNC2(canvas_item_add_texture_rect_animation, RID, const Array &) + FUNC3(canvas_item_add_texture_rect_animation, RID, const Array &, const bool) FUNC11(canvas_item_add_nine_patch, RID, const Rect2 &, const Rect2 &, RID, const Vector2 &, const Vector2 &, NinePatchAxisMode, NinePatchAxisMode, bool, const Color &, RID) FUNC7(canvas_item_add_primitive, RID, const Vector &, const Vector &, const Vector &, RID, float, RID) FUNC7(canvas_item_add_polygon, RID, const Vector &, const Vector &, const Vector &, RID, RID, bool) diff --git a/servers/rendering_server.cpp b/servers/rendering_server.cpp index 9271e62c3..7e75f4653 100644 --- a/servers/rendering_server.cpp +++ b/servers/rendering_server.cpp @@ -2155,7 +2155,7 @@ void RenderingServer::_bind_methods() { ClassDB::bind_method(D_METHOD("canvas_item_add_triangle_array", "item", "indices", "points", "colors", "uvs", "bones", "weights", "texture", "count", "normal_map", "antialiased", "antialiasing_use_indices"), &RenderingServer::canvas_item_add_triangle_array, DEFVAL(Vector()), DEFVAL(Vector()), DEFVAL(Vector()), DEFVAL(RID()), DEFVAL(-1), DEFVAL(RID()), DEFVAL(false), DEFVAL(false)); ClassDB::bind_method(D_METHOD("canvas_item_add_mesh", "item", "mesh", "transform", "modulate", "texture", "normal_map"), &RenderingServer::canvas_item_add_mesh, DEFVAL(Transform2D()), DEFVAL(Color(1, 1, 1)), DEFVAL(RID()), DEFVAL(RID())); ClassDB::bind_method(D_METHOD("canvas_item_add_multimesh", "item", "mesh", "texture", "normal_map"), &RenderingServer::canvas_item_add_multimesh, DEFVAL(RID())); - ClassDB::bind_method(D_METHOD("canvas_item_add_texture_rect_animation", "item", "animation_data"), &RenderingServer::canvas_item_add_texture_rect_animation); + ClassDB::bind_method(D_METHOD("canvas_item_add_texture_rect_animation", "item", "animation_data", "randomize_start_time"), &RenderingServer::canvas_item_add_texture_rect_animation, DEFVAL(false)); ClassDB::bind_method(D_METHOD("canvas_item_add_set_transform", "item", "transform"), &RenderingServer::canvas_item_add_set_transform); ClassDB::bind_method(D_METHOD("canvas_item_add_clip_ignore", "item", "ignore"), &RenderingServer::canvas_item_add_clip_ignore); ClassDB::bind_method(D_METHOD("canvas_item_set_sort_children_by_y", "item", "enabled"), &RenderingServer::canvas_item_set_sort_children_by_y); diff --git a/servers/rendering_server.h b/servers/rendering_server.h index 861145338..f1c2b658b 100644 --- a/servers/rendering_server.h +++ b/servers/rendering_server.h @@ -943,7 +943,7 @@ public: virtual void canvas_item_add_triangle_array(RID p_item, const Vector &p_indices, const Vector &p_points, const Vector &p_colors, const Vector &p_uvs = Vector(), const Vector &p_bones = Vector(), const Vector &p_weights = Vector(), RID p_texture = RID(), int p_count = -1, RID p_normal_map = RID(), bool p_antialiased = false, bool p_antialiasing_use_indices = false) = 0; virtual void canvas_item_add_mesh(RID p_item, const RID &p_mesh, const Transform2D &p_transform = Transform2D(), const Color &p_modulate = Color(1, 1, 1), RID p_texture = RID(), RID p_normal_map = RID()) = 0; virtual void canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture = RID(), RID p_normal_map = RID()) = 0; - virtual void canvas_item_add_texture_rect_animation(RID p_item, const Array &p_animation_data) = 0; + virtual void canvas_item_add_texture_rect_animation(RID p_item, const Array &p_animation_data, const bool p_randomize_start_time = false) = 0; virtual void canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform) = 0; virtual void canvas_item_add_clip_ignore(RID p_item, bool p_ignore) = 0;