Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Physics interpolation (2D): Add support for CPUParticles2D #89866

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
363 changes: 216 additions & 147 deletions scene/2d/cpu_particles_2d.cpp

Large diffs are not rendered by default.

56 changes: 54 additions & 2 deletions scene/2d/cpu_particles_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,20 @@ class CPUParticles2D : public Node2D {
bool emitting = false;
bool active = false;

struct Particle {
struct ParticleBase {
Transform2D transform;
Color color;
real_t custom[4] = {};
void blank() {
for (int n = 0; n < 4; n++) {
custom[n] = real_t(0.0);
}
}
};

// Warning - beware of adding non-trivial types to this structure
// as it is zeroed to initialize in set_amount().
struct Particle : public ParticleBase {
real_t rotation = 0.0;
Vector2 velocity;
bool active = false;
Expand All @@ -97,6 +107,12 @@ class CPUParticles2D : public Node2D {
Color base_color;

uint32_t seed = 0;

void copy_to(ParticleBase &r_other) {
r_other.transform = transform;
r_other.color = color;
memcpy(r_other.custom, custom, sizeof(real_t) * 4);
}
};

double time = 0.0;
Expand All @@ -108,7 +124,9 @@ class CPUParticles2D : public Node2D {
RID multimesh;

Vector<Particle> particles;
Vector<Particle> particles_prev;
Vector<float> particle_data;
Vector<float> particle_data_prev;
Vector<int> particle_order;

struct SortLifetime {
Expand Down Expand Up @@ -176,12 +194,15 @@ class CPUParticles2D : public Node2D {

Vector2 gravity = Vector2(0, 980);

void _update_internal();
void _update_internal(bool p_on_physics_tick);
void _particles_process(double p_delta);
void _particle_process(Particle &r_p, const Transform2D &p_emission_xform, double p_local_delta, float &r_tv);
void _update_particle_data_buffer();

Mutex update_mutex;

bool _interpolated = false;

void _update_render_thread();

void _update_mesh_texture();
Expand All @@ -190,6 +211,37 @@ class CPUParticles2D : public Node2D {

void _texture_changed();

void _refresh_interpolation_state();

void _fill_particle_data(const ParticleBase &p_source, float *r_dest, bool p_active) const {
if (p_active) {
Transform2D t = p_source.transform;

r_dest[0] = t.columns[0][0];
r_dest[1] = t.columns[1][0];
r_dest[2] = 0;
r_dest[3] = t.columns[2][0];
r_dest[4] = t.columns[0][1];
r_dest[5] = t.columns[1][1];
r_dest[6] = 0;
r_dest[7] = t.columns[2][1];

Color c = p_source.color;

r_dest[8] = c.r;
r_dest[9] = c.g;
r_dest[10] = c.b;
r_dest[11] = c.a;

r_dest[12] = p_source.custom[0];
r_dest[13] = p_source.custom[1];
r_dest[14] = p_source.custom[2];
r_dest[15] = p_source.custom[3];
} else {
memset(r_dest, 0, sizeof(float) * 16);
}
}

protected:
static void _bind_methods();
void _notification(int p_what);
Expand Down
4 changes: 3 additions & 1 deletion scene/2d/node_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ void Node2D::set_transform(const Transform2D &p_transform) {
transform = p_transform;
_set_xform_dirty(true);

RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
if (!_is_using_identity_transform()) {
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
}

_notify_transform();
}
Expand Down
18 changes: 18 additions & 0 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,24 @@ int CanvasItem::get_light_mask() const {
return light_mask;
}

void CanvasItem::set_use_identity_transform(bool p_enable) {
// Prevent sending item transforms to RenderingServer when using global coordinates.
_set_use_identity_transform(p_enable);

// Let RenderingServer know not to concatenate the parent transform during the render.
RenderingServer::get_singleton()->canvas_item_set_ignore_parent_transform(get_canvas_item(), p_enable);

if (is_inside_tree()) {
if (p_enable) {
// Make sure item is using identity transform in server.
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), Transform2D());
} else {
// Make sure item transform is up to date in server if switching identity transform off.
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), get_transform());
}
}
}

void CanvasItem::item_rect_changed(bool p_size_changed) {
ERR_MAIN_THREAD_GUARD;
if (p_size_changed) {
Expand Down
2 changes: 2 additions & 0 deletions scene/main/canvas_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class CanvasItem : public Node {
}
}

void set_use_identity_transform(bool p_enable);

void item_rect_changed(bool p_size_changed = true);

void _notification(int p_what);
Expand Down
4 changes: 4 additions & 0 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3350,6 +3350,10 @@ void Node::request_ready() {
data.ready_first = true;
}

void Node::_set_use_identity_transform(bool p_enabled) {
data.use_identity_transform = p_enabled;
}

void Node::_call_input(const Ref<InputEvent> &p_event) {
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
GDVIRTUAL_CALL(_input, p_event);
Expand Down
7 changes: 7 additions & 0 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ class Node : public Object {
// is switched on.
bool physics_interpolated : 1;

// For certain nodes (e.g. CPUParticles2D in global mode) it can be useful to not send the
// canvas item transform to the RenderingServer, and handle the particles in world space.
bool use_identity_transform : 1;

bool parent_owned : 1;
bool in_constructor : 1;
bool use_placeholder : 1;
Expand Down Expand Up @@ -333,6 +337,9 @@ class Node : public Object {
void _set_owner_nocheck(Node *p_owner);
void _set_name_nocheck(const StringName &p_name);

void _set_use_identity_transform(bool p_enabled);
_FORCE_INLINE_ bool _is_using_identity_transform() const { return data.use_identity_transform; }

//call from SceneTree
void _call_input(const Ref<InputEvent> &p_event);
void _call_shortcut_input(const Ref<InputEvent> &p_event);
Expand Down
19 changes: 18 additions & 1 deletion servers/rendering/renderer_canvas_cull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
void RendererCanvasCull::_render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, uint32_t p_canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info) {
RENDER_TIMESTAMP("Cull CanvasItem Tree");

// This is used to avoid passing the camera transform down the rendering
// function calls, as it won't be used in 99% of cases, because the camera
// transform is normally concatenated with the item global transform.
_current_camera_transform = p_transform;

memset(z_list, 0, z_range * sizeof(RendererCanvasRender::Item *));
memset(z_last_list, 0, z_range * sizeof(RendererCanvasRender::Item *));

Expand Down Expand Up @@ -253,7 +258,12 @@ void RendererCanvasCull::_cull_canvas_item(Item *p_canvas_item, const Transform2
TransformInterpolator::interpolate_transform_2d(ci->xform_prev, ci->xform_curr, final_xform, f);
}

Transform2D parent_xform = p_parent_xform;
Transform2D parent_xform;
if (!p_canvas_item->ignore_parent_xform) {
parent_xform = p_parent_xform;
} else {
parent_xform = _current_camera_transform;
}

Point2 repeat_size = p_repeat_size;
int repeat_times = p_repeat_times;
Expand Down Expand Up @@ -589,6 +599,13 @@ void RendererCanvasCull::canvas_item_set_draw_behind_parent(RID p_item, bool p_e
canvas_item->behind = p_enable;
}

void RendererCanvasCull::canvas_item_set_ignore_parent_transform(RID p_item, bool p_enable) {
Item *canvas_item = canvas_item_owner.get_or_null(p_item);
ERR_FAIL_NULL(canvas_item);

canvas_item->ignore_parent_xform = p_enable;
}

void RendererCanvasCull::canvas_item_set_update_when_visible(RID p_item, bool p_update) {
Item *canvas_item = canvas_item_owner.get_or_null(p_item);
ERR_FAIL_NULL(canvas_item);
Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/renderer_canvas_cull.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ class RendererCanvasCull {
RendererCanvasRender::Item **z_list;
RendererCanvasRender::Item **z_last_list;

Transform2D _current_camera_transform;

public:
void render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, const Rect2 &p_clip_rect, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_transforms_to_pixel, bool p_snap_2d_vertices_to_pixel, uint32_t p_canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info = nullptr);

Expand Down Expand Up @@ -228,6 +230,7 @@ class RendererCanvasCull {
void canvas_item_set_self_modulate(RID p_item, const Color &p_color);

void canvas_item_set_draw_behind_parent(RID p_item, bool p_enable);
void canvas_item_set_ignore_parent_transform(RID p_item, bool p_enable);

void canvas_item_set_update_when_visible(RID p_item, bool p_update);

Expand Down
2 changes: 2 additions & 0 deletions servers/rendering/renderer_canvas_render.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class RendererCanvasRender {
bool update_when_visible : 1;
bool on_interpolate_transform_list : 1;
bool interpolated : 1;
bool ignore_parent_xform : 1;

struct CanvasGroup {
RS::CanvasGroupMode mode;
Expand Down Expand Up @@ -485,6 +486,7 @@ class RendererCanvasRender {
repeat_source = false;
on_interpolate_transform_list = false;
interpolated = true;
ignore_parent_xform = false;
}
virtual ~Item() {
clear();
Expand Down
1 change: 1 addition & 0 deletions servers/rendering/rendering_server_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ class RenderingServerDefault : public RenderingServer {
FUNC2(canvas_item_set_self_modulate, RID, const Color &)

FUNC2(canvas_item_set_draw_behind_parent, RID, bool)
FUNC2(canvas_item_set_ignore_parent_transform, RID, bool)

FUNC6(canvas_item_add_line, RID, const Point2 &, const Point2 &, const Color &, float, bool)
FUNC5(canvas_item_add_polyline, RID, const Vector<Point2> &, const Vector<Color> &, float, bool)
Expand Down
1 change: 1 addition & 0 deletions servers/rendering_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3219,6 +3219,7 @@ void RenderingServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("canvas_item_set_modulate", "item", "color"), &RenderingServer::canvas_item_set_modulate);
ClassDB::bind_method(D_METHOD("canvas_item_set_self_modulate", "item", "color"), &RenderingServer::canvas_item_set_self_modulate);
ClassDB::bind_method(D_METHOD("canvas_item_set_draw_behind_parent", "item", "enabled"), &RenderingServer::canvas_item_set_draw_behind_parent);
ClassDB::bind_method(D_METHOD("canvas_item_set_ignore_parent_transform", "item", "enabled"), &RenderingServer::canvas_item_set_ignore_parent_transform);
ClassDB::bind_method(D_METHOD("canvas_item_set_interpolated", "item", "interpolated"), &RenderingServer::canvas_item_set_interpolated);
ClassDB::bind_method(D_METHOD("canvas_item_reset_physics_interpolation", "item"), &RenderingServer::canvas_item_reset_physics_interpolation);
ClassDB::bind_method(D_METHOD("canvas_item_transform_physics_interpolation", "item", "transform"), &RenderingServer::canvas_item_transform_physics_interpolation);
Expand Down
1 change: 1 addition & 0 deletions servers/rendering_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ class RenderingServer : public Object {
virtual void canvas_item_set_visibility_layer(RID p_item, uint32_t p_visibility_layer) = 0;

virtual void canvas_item_set_draw_behind_parent(RID p_item, bool p_enable) = 0;
virtual void canvas_item_set_ignore_parent_transform(RID p_item, bool p_enable) = 0;

enum NinePatchAxisMode {
NINE_PATCH_STRETCH,
Expand Down
Loading