Skip to content

Commit a9ac00f

Browse files
committed
Added multimesh physics interpolation for 2D transforms (MultiMeshInstance2D)
Replicated MultimeshInstance3D behaviour to MultiMeshInstance2D and added mesh_storage for 2D transform Also call VisualInstance::_physics_interpolated_changed() Use Node instead of VisualInstance. Addded comment Fixed typo :) Using CanvasItem instead of Node now. Made CanvasItem::_physics_interpolated_changed() protected
1 parent 46c495c commit a9ac00f

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

scene/2d/multimesh_instance_2d.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,24 @@
4242
Callable MultiMeshInstance2D::_navmesh_source_geometry_parsing_callback;
4343
RID MultiMeshInstance2D::_navmesh_source_geometry_parser;
4444

45+
void MultiMeshInstance2D::_refresh_interpolated() {
46+
if (is_inside_tree() && multimesh.is_valid()) {
47+
bool interpolated = is_physics_interpolated_and_enabled();
48+
multimesh->set_physics_interpolated(interpolated);
49+
}
50+
}
51+
52+
void MultiMeshInstance2D::_physics_interpolated_changed() {
53+
CanvasItem::_physics_interpolated_changed();
54+
_refresh_interpolated();
55+
}
56+
4557
void MultiMeshInstance2D::_notification(int p_what) {
4658
switch (p_what) {
59+
case NOTIFICATION_ENTER_TREE: {
60+
_refresh_interpolated();
61+
break;
62+
}
4763
case NOTIFICATION_DRAW: {
4864
if (multimesh.is_valid()) {
4965
draw_multimesh(multimesh, texture);
@@ -75,6 +91,7 @@ void MultiMeshInstance2D::set_multimesh(const Ref<MultiMesh> &p_multimesh) {
7591
// Connect to the multimesh so the AABB can update when instance transforms are changed.
7692
if (multimesh.is_valid()) {
7793
multimesh->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
94+
_refresh_interpolated();
7895
}
7996
queue_redraw();
8097
}

scene/2d/multimesh_instance_2d.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class MultiMeshInstance2D : public Node2D {
4343

4444
Ref<Texture2D> texture;
4545

46+
void _refresh_interpolated();
47+
4648
protected:
49+
virtual void _physics_interpolated_changed() override;
4750
void _notification(int p_what);
4851
static void _bind_methods();
4952

scene/main/canvas_item.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ class CanvasItem : public Node {
140140

141141
void _notify_transform(CanvasItem *p_node);
142142

143-
virtual void _physics_interpolated_changed() override;
144-
145143
static CanvasItem *current_item_drawn;
146144
friend class Viewport;
147145
void _refresh_texture_repeat_cache() const;
@@ -157,6 +155,8 @@ class CanvasItem : public Node {
157155
bool _get(const StringName &p_name, Variant &r_ret) const;
158156
void _get_property_list(List<PropertyInfo> *p_list) const;
159157

158+
virtual void _physics_interpolated_changed() override;
159+
160160
virtual void _update_self_texture_repeat(RS::CanvasItemTextureRepeat p_texture_repeat);
161161
virtual void _update_self_texture_filter(RS::CanvasItemTextureFilter p_texture_filter);
162162

servers/rendering/storage/mesh_storage.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,37 @@ void RendererMeshStorage::multimesh_instance_set_transform(RID p_multimesh, int
118118
}
119119

120120
void RendererMeshStorage::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
121+
MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
122+
if (mmi && mmi->interpolated) {
123+
ERR_FAIL_COND(p_index >= mmi->_num_instances);
124+
ERR_FAIL_COND(mmi->_vf_size_xform != 8);
125+
126+
int start = p_index * mmi->_stride;
127+
float *ptr = mmi->_data_curr.ptrw();
128+
ptr += start;
129+
130+
const Transform2D &t = p_transform;
131+
132+
ptr[0] = t.columns[0][0];
133+
ptr[1] = t.columns[1][0];
134+
ptr[2] = 0;
135+
ptr[3] = t.columns[2][0];
136+
ptr[4] = t.columns[0][1];
137+
ptr[5] = t.columns[1][1];
138+
ptr[6] = 0;
139+
ptr[7] = t.columns[2][1];
140+
141+
_multimesh_add_to_interpolation_lists(p_multimesh, *mmi);
142+
143+
#if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
144+
if (!Engine::get_singleton()->is_in_physics_frame()) {
145+
PHYSICS_INTERPOLATION_WARNING("MultiMesh interpolation is being triggered from outside physics process, this might lead to issues");
146+
}
147+
#endif
148+
149+
return;
150+
}
151+
121152
_multimesh_instance_set_transform_2d(p_multimesh, p_index, p_transform);
122153
}
123154

0 commit comments

Comments
 (0)