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

Remove scene code in physics servers #53311

Merged
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
2 changes: 1 addition & 1 deletion modules/bullet/bullet_physics_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ RID BulletPhysicsServer3D::soft_body_get_space(RID p_body) const {
return space->get_self();
}

void BulletPhysicsServer3D::soft_body_set_mesh(RID p_body, const REF &p_mesh) {
void BulletPhysicsServer3D::soft_body_set_mesh(RID p_body, RID p_mesh) {
SoftBodyBullet *body = soft_body_owner.get_or_null(p_body);
ERR_FAIL_COND(!body);

Expand Down
2 changes: 1 addition & 1 deletion modules/bullet/bullet_physics_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class BulletPhysicsServer3D : public PhysicsServer3D {
virtual void soft_body_set_space(RID p_body, RID p_space) override;
virtual RID soft_body_get_space(RID p_body) const override;

virtual void soft_body_set_mesh(RID p_body, const REF &p_mesh) override;
virtual void soft_body_set_mesh(RID p_body, RID p_mesh) override;

virtual AABB soft_body_get_bounds(RID p_body) const override;

Expand Down
33 changes: 19 additions & 14 deletions modules/bullet/soft_body_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@

#include "bullet_types_converter.h"
#include "bullet_utilities.h"
#include "scene/3d/soft_body_3d.h"
#include "space_bullet.h"

#include "servers/rendering_server.h"

SoftBodyBullet::SoftBodyBullet() :
CollisionObjectBullet(CollisionObjectBullet::TYPE_SOFT_BODY) {}

Expand Down Expand Up @@ -105,24 +106,26 @@ void SoftBodyBullet::update_rendering_server(RenderingServerHandler *p_rendering
p_rendering_server_handler->set_aabb(aabb);
}

void SoftBodyBullet::set_soft_mesh(const Ref<Mesh> &p_mesh) {
if (p_mesh.is_null()) {
soft_mesh.unref();
} else {
soft_mesh = p_mesh;
}
void SoftBodyBullet::set_soft_mesh(RID p_mesh) {
destroy_soft_body();

soft_mesh = p_mesh;

if (soft_mesh.is_null()) {
destroy_soft_body();
return;
}

Array arrays = soft_mesh->surface_get_arrays(0);
ERR_FAIL_COND(!(soft_mesh->surface_get_format(0) & RS::ARRAY_FORMAT_INDEX));
set_trimesh_body_shape(arrays[RS::ARRAY_INDEX], arrays[RS::ARRAY_VERTEX]);
Array arrays = RenderingServer::get_singleton()->mesh_surface_get_arrays(soft_mesh, 0);

bool success = set_trimesh_body_shape(arrays[RS::ARRAY_INDEX], arrays[RS::ARRAY_VERTEX]);
if (!success) {
destroy_soft_body();
}
}

void SoftBodyBullet::destroy_soft_body() {
soft_mesh = RID();

if (!bt_soft_body) {
return;
}
Expand Down Expand Up @@ -289,9 +292,9 @@ void SoftBodyBullet::set_drag_coefficient(real_t p_val) {
}
}

void SoftBodyBullet::set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector3> p_vertices) {
/// Assert the current soft body is destroyed
destroy_soft_body();
bool SoftBodyBullet::set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector3> p_vertices) {
ERR_FAIL_COND_V(p_indices.is_empty(), false);
ERR_FAIL_COND_V(p_vertices.is_empty(), false);

/// Parse visual server indices to physical indices.
/// Merge all overlapping vertices and create a map of physical vertices to visual server
Expand Down Expand Up @@ -363,6 +366,8 @@ void SoftBodyBullet::set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector
bt_soft_body = btSoftBodyHelpers::CreateFromTriMesh(fake_world_info, &bt_vertices[0], &bt_triangles[0], triangles_size, false);
setup_soft_body();
}

return true;
}

void SoftBodyBullet::setup_soft_body() {
Expand Down
8 changes: 3 additions & 5 deletions modules/bullet/soft_body_bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#define SOFT_BODY_BULLET_H

#include "collision_object_bullet.h"
#include "scene/resources/material.h" // TODO remove this please

#ifdef None
/// This is required to remove the macro None defined by x11 compiler because this word "None" is used internally by Bullet
Expand All @@ -42,7 +41,6 @@

#include "BulletSoftBody/btSoftBodyHelpers.h"
#include "collision_object_bullet.h"
#include "scene/resources/mesh.h"
#include "servers/physics_server_3d.h"

#ifdef x11_None
Expand All @@ -64,7 +62,7 @@ class SoftBodyBullet : public CollisionObjectBullet {
btSoftBody::Material *mat0 = nullptr; // This is just a copy of pointer managed by btSoftBody
bool isScratched = false;

Ref<Mesh> soft_mesh;
RID soft_mesh;

int simulation_precision = 5;
real_t total_mass = 1.;
Expand Down Expand Up @@ -100,7 +98,7 @@ class SoftBodyBullet : public CollisionObjectBullet {

void update_rendering_server(RenderingServerHandler *p_rendering_server_handler);

void set_soft_mesh(const Ref<Mesh> &p_mesh);
void set_soft_mesh(RID p_mesh);
void destroy_soft_body();

// Special function. This function has bad performance
Expand Down Expand Up @@ -139,7 +137,7 @@ class SoftBodyBullet : public CollisionObjectBullet {
_FORCE_INLINE_ real_t get_drag_coefficient() const { return drag_coefficient; }

private:
void set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector3> p_vertices);
bool set_trimesh_body_shape(Vector<int> p_indices, Vector<Vector3> p_vertices);
void setup_soft_body();

void pin_node(int p_node_index);
Expand Down
8 changes: 4 additions & 4 deletions scene/3d/soft_dynamic_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ void SoftDynamicBody3D::prepare_physics_server() {
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
if (get_mesh().is_valid()) {
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, get_mesh());
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, get_mesh()->get_rid());
} else {
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, nullptr);
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, RID());
}

return;
Expand All @@ -444,10 +444,10 @@ void SoftDynamicBody3D::prepare_physics_server() {

if (get_mesh().is_valid() && (is_enabled() || (disable_mode != DISABLE_MODE_REMOVE))) {
become_mesh_owner();
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, get_mesh());
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, get_mesh()->get_rid());
RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &SoftDynamicBody3D::_draw_soft_mesh));
} else {
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, nullptr);
PhysicsServer3D::get_singleton()->soft_body_set_mesh(physics_rid, RID());
if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &SoftDynamicBody3D::_draw_soft_mesh))) {
RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &SoftDynamicBody3D::_draw_soft_mesh));
}
Expand Down
2 changes: 1 addition & 1 deletion servers/physics_3d/physics_server_3d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ real_t PhysicsServer3DSW::soft_body_get_drag_coefficient(RID p_body) const {
return soft_body->get_drag_coefficient();
}

void PhysicsServer3DSW::soft_body_set_mesh(RID p_body, const REF &p_mesh) {
void PhysicsServer3DSW::soft_body_set_mesh(RID p_body, RID p_mesh) {
SoftBody3DSW *soft_body = soft_body_owner.get_or_null(p_body);
ERR_FAIL_COND(!soft_body);

Expand Down
2 changes: 1 addition & 1 deletion servers/physics_3d/physics_server_3d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class PhysicsServer3DSW : public PhysicsServer3D {
virtual void soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) override;
virtual real_t soft_body_get_drag_coefficient(RID p_body) const override;

virtual void soft_body_set_mesh(RID p_body, const REF &p_mesh) override;
virtual void soft_body_set_mesh(RID p_body, RID p_mesh) override;

virtual AABB soft_body_get_bounds(RID p_body) const override;

Expand Down
2 changes: 1 addition & 1 deletion servers/physics_3d/physics_server_3d_wrap_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class PhysicsServer3DWrapMT : public PhysicsServer3D {
FUNC2(soft_body_set_drag_coefficient, RID, real_t);
FUNC1RC(real_t, soft_body_get_drag_coefficient, RID);

FUNC2(soft_body_set_mesh, RID, const REF &);
FUNC2(soft_body_set_mesh, RID, RID);

FUNC1RC(AABB, soft_body_get_bounds, RID);

Expand Down
14 changes: 9 additions & 5 deletions servers/physics_3d/soft_body_3d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "core/math/geometry_3d.h"
#include "core/templates/map.h"
#include "servers/rendering_server.h"

// Based on Bullet soft body.

Expand Down Expand Up @@ -127,7 +128,7 @@ void SoftBody3DSW::set_space(Space3DSW *p_space) {
}
}

void SoftBody3DSW::set_mesh(const Ref<Mesh> &p_mesh) {
void SoftBody3DSW::set_mesh(RID p_mesh) {
destroy();

soft_mesh = p_mesh;
Expand All @@ -136,13 +137,11 @@ void SoftBody3DSW::set_mesh(const Ref<Mesh> &p_mesh) {
return;
}

Array arrays = soft_mesh->surface_get_arrays(0);
ERR_FAIL_COND(!(soft_mesh->surface_get_format(0) & RS::ARRAY_FORMAT_INDEX));
Array arrays = RenderingServer::get_singleton()->mesh_surface_get_arrays(soft_mesh, 0);

bool success = create_from_trimesh(arrays[RS::ARRAY_INDEX], arrays[RS::ARRAY_VERTEX]);
bool success = create_from_trimesh(arrays[RenderingServer::ARRAY_INDEX], arrays[RenderingServer::ARRAY_VERTEX]);
if (!success) {
destroy();
soft_mesh = Ref<Mesh>();
}
}

Expand Down Expand Up @@ -467,6 +466,9 @@ Vector3 SoftBody3DSW::get_face_normal(uint32_t p_face_index) const {
}

bool SoftBody3DSW::create_from_trimesh(const Vector<int> &p_indices, const Vector<Vector3> &p_vertices) {
ERR_FAIL_COND_V(p_indices.is_empty(), false);
ERR_FAIL_COND_V(p_vertices.is_empty(), false);

uint32_t node_count = 0;
LocalVector<Vector3> vertices;
const int visual_vertex_count(p_vertices.size());
Expand Down Expand Up @@ -1227,6 +1229,8 @@ void SoftBody3DSW::deinitialize_shape() {
}

void SoftBody3DSW::destroy() {
soft_mesh = RID();

map_visual_to_physics.clear();

node_tree.clear();
Expand Down
5 changes: 2 additions & 3 deletions servers/physics_3d/soft_body_3d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
#include "core/templates/local_vector.h"
#include "core/templates/set.h"
#include "core/templates/vset.h"
#include "scene/resources/mesh.h"

class Constraint3DSW;

class SoftBody3DSW : public CollisionObject3DSW {
Ref<Mesh> soft_mesh;
RID soft_mesh;

struct Node {
Vector3 s; // Source position
Expand Down Expand Up @@ -159,7 +158,7 @@ class SoftBody3DSW : public CollisionObject3DSW {

virtual void set_space(Space3DSW *p_space);

void set_mesh(const Ref<Mesh> &p_mesh);
void set_mesh(RID p_mesh);

void update_rendering_server(RenderingServerHandler *p_rendering_server_handler);

Expand Down
2 changes: 1 addition & 1 deletion servers/physics_server_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class PhysicsServer3D : public Object {
virtual void soft_body_set_space(RID p_body, RID p_space) = 0;
virtual RID soft_body_get_space(RID p_body) const = 0;

virtual void soft_body_set_mesh(RID p_body, const REF &p_mesh) = 0;
virtual void soft_body_set_mesh(RID p_body, RID p_mesh) = 0;

virtual AABB soft_body_get_bounds(RID p_body) const = 0;

Expand Down