@@ -52,107 +52,34 @@ void SoftBodyRenderingServerHandler::prepare(RID p_mesh, int p_surface) {
5252 uint32_t skin_stride;
5353 RS::get_singleton ()->mesh_surface_make_offsets_from_format (surface_data.format , surface_data.vertex_count , surface_data.index_count , surface_offsets, vertex_stride, normal_tangent_stride, attrib_stride, skin_stride);
5454
55- buffer[0 ] = surface_data.vertex_data ;
56- vertex_count = surface_data.vertex_count ;
55+ buffer = surface_data.vertex_data ;
5756 stride = vertex_stride;
5857 normal_stride = normal_tangent_stride;
5958 offset_vertices = surface_offsets[RS::ARRAY_VERTEX];
6059 offset_normal = surface_offsets[RS::ARRAY_NORMAL];
61-
62- buffer_curr = &buffer[0 ];
63- buffer_prev = &buffer[1 ];
6460}
6561
6662void SoftBodyRenderingServerHandler::clear () {
67- aabb_prev = AABB ();
68- aabb_curr = AABB ();
69- buffer[0 ].resize (0 );
70- buffer[1 ].resize (0 );
71- buffer_curr = nullptr ;
72- buffer_prev = nullptr ;
73- buffer_interp.resize (0 );
74- vertex_count = 0 ;
63+ buffer.resize (0 );
7564 stride = 0 ;
7665 normal_stride = 0 ;
7766 offset_vertices = 0 ;
7867 offset_normal = 0 ;
7968
80- aabb_last = AABB ();
8169 surface = 0 ;
8270 mesh = RID ();
8371}
8472
8573void SoftBodyRenderingServerHandler::open () {
86- write_buffer = buffer_curr-> ptrw ();
74+ write_buffer = buffer. ptrw ();
8775}
8876
8977void SoftBodyRenderingServerHandler::close () {
9078 write_buffer = nullptr ;
9179}
9280
93- void SoftBodyRenderingServerHandler::fti_pump () {
94- if (buffer_prev->is_empty ()) {
95- buffer_prev->resize (buffer_curr->size ());
96- }
97- SWAP (buffer_prev, buffer_curr);
98- aabb_prev = aabb_curr;
99- }
100-
101- void SoftBodyRenderingServerHandler::commit_changes (real_t p_interpolation_fraction) {
102- real_t f = p_interpolation_fraction;
103- AABB aabb_interp = aabb_curr;
104-
105- if (p_interpolation_fraction < 1 ) {
106- if (buffer_interp.is_empty ()) {
107- buffer_interp.resize (buffer_curr->size ());
108- }
109-
110- // AABB.
111- if (aabb_prev != AABB () && aabb_curr != AABB ()) {
112- aabb_interp = AABB (aabb_prev.position .lerp (aabb_curr.position , f), aabb_prev.size .lerp (aabb_curr.size , f));
113- }
114-
115- const float *vertex_prev = reinterpret_cast <const float *>(buffer_prev->ptr () + offset_vertices);
116- const float *vertex_curr = reinterpret_cast <const float *>(buffer_curr->ptr () + offset_vertices);
117- float *vertex_interp = reinterpret_cast <float *>(buffer_interp.ptrw () + offset_vertices);
118-
119- const uint32_t *normal_prev = reinterpret_cast <const uint32_t *>(buffer_prev->ptr () + offset_normal);
120- const uint32_t *normal_curr = reinterpret_cast <const uint32_t *>(buffer_curr->ptr () + offset_normal);
121- uint32_t *normal_interp = reinterpret_cast <uint32_t *>(buffer_interp.ptrw () + offset_normal);
122-
123- uint32_t stride_units = stride / sizeof (float );
124- uint32_t normal_stride_units = normal_stride / sizeof (uint32_t );
125-
126- for (uint32_t i = 0 ; i < vertex_count; i++) {
127- // Vertex.
128- vertex_interp[0 ] = Math::lerp (vertex_prev[0 ], vertex_curr[0 ], (float )f);
129- vertex_interp[1 ] = Math::lerp (vertex_prev[1 ], vertex_curr[1 ], (float )f);
130- vertex_interp[2 ] = Math::lerp (vertex_prev[2 ], vertex_curr[2 ], (float )f);
131-
132- vertex_prev += stride_units;
133- vertex_curr += stride_units;
134- vertex_interp += stride_units;
135-
136- // Normal.
137- Vector2 prev = Vector2 ((normal_prev[0 ] & 0xffff ) / 65535 .0f , (normal_prev[0 ] >> 16 ) / 65535 .0f );
138- Vector2 curr = Vector2 ((normal_curr[0 ] & 0xffff ) / 65535 .0f , (normal_curr[0 ] >> 16 ) / 65535 .0f );
139- Vector2 interp = Vector3::octahedron_decode (prev).lerp (Vector3::octahedron_decode (curr), f).octahedron_encode ();
140- uint32_t n = 0 ;
141- n |= (uint16_t )CLAMP (interp.x * 65535 , 0 , 65535 );
142- n |= (uint16_t )CLAMP (interp.y * 65535 , 0 , 65535 ) << 16 ;
143- normal_interp[0 ] = n;
144-
145- normal_prev += normal_stride_units;
146- normal_curr += normal_stride_units;
147- normal_interp += normal_stride_units;
148- }
149- }
150-
151- if (aabb_interp != aabb_last) {
152- RS::get_singleton ()->mesh_set_custom_aabb (mesh, aabb_interp);
153- aabb_last = aabb_interp;
154- }
155- RS::get_singleton ()->mesh_surface_update_vertex_region (mesh, surface, 0 , p_interpolation_fraction < 1 ? buffer_interp : *buffer_curr);
81+ void SoftBodyRenderingServerHandler::commit_changes () {
82+ RS::get_singleton ()->mesh_surface_update_vertex_region (mesh, surface, 0 , buffer);
15683}
15784
15885void SoftBodyRenderingServerHandler::set_vertex (int p_vertex_id, const Vector3 &p_vertex) {
@@ -171,7 +98,7 @@ void SoftBodyRenderingServerHandler::set_normal(int p_vertex_id, const Vector3 &
17198}
17299
173100void SoftBodyRenderingServerHandler::set_aabb (const AABB &p_aabb) {
174- aabb_curr = p_aabb;
101+ RS::get_singleton ()-> mesh_set_custom_aabb (mesh, p_aabb) ;
175102}
176103
177104SoftBody3D::PinnedPoint::PinnedPoint () {
@@ -347,19 +274,7 @@ void SoftBody3D::_notification(int p_what) {
347274 PhysicsServer3D::get_singleton ()->soft_body_set_space (physics_rid, space);
348275 _prepare_physics_server ();
349276 } break ;
350- case NOTIFICATION_INTERNAL_PROCESS: {
351- if (is_inside_tree () && is_physics_interpolated_and_enabled ()) {
352- _commit_soft_mesh (Engine::get_singleton ()->get_physics_interpolation_fraction ());
353- }
354- } break ;
355- case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
356- if (is_inside_tree ()) {
357- _update_soft_mesh ();
358- if (!is_physics_interpolated_and_enabled ()) {
359- _commit_soft_mesh (1 );
360- }
361- }
362- } break ;
277+
363278 case NOTIFICATION_READY: {
364279 if (!parent_collision_ignore.is_empty ()) {
365280 add_collision_exception_with (get_node (parent_collision_ignore));
@@ -372,24 +287,15 @@ void SoftBody3D::_notification(int p_what) {
372287 return ;
373288 }
374289
375- if (!simulation_started) {
376- // Avoid rendering mesh at the origin before simulation.
377- return ;
378- }
379-
380290 PhysicsServer3D::get_singleton ()->soft_body_set_transform (physics_rid, get_global_transform ());
381291
382- // Soft body renders mesh in global space.
383292 set_notify_transform (false );
293+ // Required to be top level with Transform at center of world in order to modify RenderingServer only to support custom Transform
384294 set_as_top_level (true );
385295 set_transform (Transform3D ());
386296 set_notify_transform (true );
387297 } break ;
388- case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
389- if (mesh.is_valid () && rendering_server_handler->is_ready (mesh->get_rid ())) {
390- rendering_server_handler->fti_pump ();
391- }
392- } break ;
298+
393299 case NOTIFICATION_VISIBILITY_CHANGED: {
394300 _update_pickable ();
395301 } break ;
@@ -492,13 +398,6 @@ void SoftBody3D::_bind_methods() {
492398 BIND_ENUM_CONSTANT (DISABLE_MODE_KEEP_ACTIVE);
493399}
494400
495- void SoftBody3D::_physics_interpolated_changed () {
496- if (mesh.is_valid () && rendering_server_handler->is_ready (mesh->get_rid ())) {
497- rendering_server_handler->fti_pump ();
498- }
499- MeshInstance3D::_physics_interpolated_changed ();
500- }
501-
502401PackedStringArray SoftBody3D::get_configuration_warnings () const {
503402 PackedStringArray warnings = MeshInstance3D::get_configuration_warnings ();
504403
@@ -510,6 +409,10 @@ PackedStringArray SoftBody3D::get_configuration_warnings() const {
510409}
511410
512411void SoftBody3D::_update_physics_server () {
412+ if (!simulation_started) {
413+ return ;
414+ }
415+
513416 _update_cache_pin_points_datas ();
514417 // Submit bone attachment
515418 const int pinned_points_indices_size = pinned_points.size ();
@@ -521,7 +424,7 @@ void SoftBody3D::_update_physics_server() {
521424 }
522425}
523426
524- void SoftBody3D::_update_soft_mesh () {
427+ void SoftBody3D::_draw_soft_mesh () {
525428 if (mesh.is_null ()) {
526429 return ;
527430 }
@@ -535,31 +438,20 @@ void SoftBody3D::_update_soft_mesh() {
535438
536439 if (!rendering_server_handler->is_ready (mesh_rid)) {
537440 rendering_server_handler->prepare (mesh_rid, 0 );
538- PhysicsServer3D::get_singleton ()->soft_body_set_transform (physics_rid, get_global_transform ());
539- // Soft body renders mesh in global space.
540- set_as_top_level (true );
541- set_transform (Transform3D ());
542- simulation_started = true ;
543- }
544441
545- if (!simulation_started) {
546- return ;
442+ // / Necessary in order to render the mesh correctly (Soft body nodes are in global space)
443+ simulation_started = true ;
444+ callable_mp ((Node3D *)this , &Node3D::set_as_top_level).call_deferred (true );
445+ callable_mp ((Node3D *)this , &Node3D::set_transform).call_deferred (Transform3D ());
547446 }
548447
549448 _update_physics_server ();
550449
551- if (is_physics_interpolated_and_enabled ()) {
552- rendering_server_handler->fti_pump ();
553- }
554450 rendering_server_handler->open ();
555451 PhysicsServer3D::get_singleton ()->soft_body_update_rendering_server (physics_rid, rendering_server_handler);
556452 rendering_server_handler->close ();
557- }
558453
559- void SoftBody3D::_commit_soft_mesh (real_t p_interpolation_fraction) {
560- if (mesh.is_valid () && rendering_server_handler->is_ready (mesh->get_rid ())) {
561- rendering_server_handler->commit_changes (p_interpolation_fraction);
562- }
454+ rendering_server_handler->commit_changes ();
563455}
564456
565457void SoftBody3D::_prepare_physics_server () {
@@ -582,12 +474,12 @@ void SoftBody3D::_prepare_physics_server() {
582474 mesh_rid = mesh->get_rid ();
583475 }
584476 PhysicsServer3D::get_singleton ()->soft_body_set_mesh (physics_rid, mesh_rid);
585- set_process_internal (is_physics_interpolated_and_enabled ());
586- set_physics_process_internal (true );
477+ RS::get_singleton ()->connect (" frame_pre_draw" , callable_mp (this , &SoftBody3D::_draw_soft_mesh));
587478 } else {
588479 PhysicsServer3D::get_singleton ()->soft_body_set_mesh (physics_rid, RID ());
589- set_process_internal (false );
590- set_physics_process_internal (false );
480+ if (RS::get_singleton ()->is_connected (" frame_pre_draw" , callable_mp (this , &SoftBody3D::_draw_soft_mesh))) {
481+ RS::get_singleton ()->disconnect (" frame_pre_draw" , callable_mp (this , &SoftBody3D::_draw_soft_mesh));
482+ }
591483 }
592484}
593485
0 commit comments