2017-11-21 08:36:32 +08:00
|
|
|
/**************************************************************************/
|
2022-08-26 01:35:52 +08:00
|
|
|
/* soft_body_3d.h */
|
2017-11-21 08:36:32 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2018-08-30 04:38:13 +08:00
|
|
|
/* https://godotengine.org */
|
2017-11-21 08:36:32 +08:00
|
|
|
/**************************************************************************/
|
2018-08-30 04:38:13 +08:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2017-11-21 08:36:32 +08:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2022-08-26 01:35:52 +08:00
|
|
|
#ifndef SOFT_BODY_3D_H
|
|
|
|
#define SOFT_BODY_3D_H
|
2017-11-21 08:36:32 +08:00
|
|
|
|
2020-03-27 05:49:16 +08:00
|
|
|
#include "scene/3d/mesh_instance_3d.h"
|
2021-03-12 11:33:46 +08:00
|
|
|
#include "servers/physics_server_3d.h"
|
2017-11-21 08:36:32 +08:00
|
|
|
|
2022-08-06 02:35:08 +08:00
|
|
|
class PhysicsBody3D;
|
2022-08-26 01:35:52 +08:00
|
|
|
class SoftBody3D;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
2022-08-26 01:35:52 +08:00
|
|
|
class SoftBodyRenderingServerHandler : public PhysicsServer3DRenderingServerHandler {
|
|
|
|
friend class SoftBody3D;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
RID mesh;
|
2021-02-08 05:29:31 +08:00
|
|
|
int surface = 0;
|
2020-02-18 05:06:54 +08:00
|
|
|
Vector<uint8_t> buffer;
|
2021-02-08 05:29:31 +08:00
|
|
|
uint32_t stride = 0;
|
2023-08-30 03:04:32 +08:00
|
|
|
uint32_t normal_stride = 0;
|
2021-02-08 05:29:31 +08:00
|
|
|
uint32_t offset_vertices = 0;
|
|
|
|
uint32_t offset_normal = 0;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
2021-02-08 05:29:31 +08:00
|
|
|
uint8_t *write_buffer = nullptr;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
private:
|
2022-08-26 01:35:52 +08:00
|
|
|
SoftBodyRenderingServerHandler();
|
2021-10-06 23:25:11 +08:00
|
|
|
bool is_ready(RID p_mesh_rid) const { return mesh.is_valid() && mesh == p_mesh_rid; }
|
2017-11-21 08:36:32 +08:00
|
|
|
void prepare(RID p_mesh_rid, int p_surface);
|
|
|
|
void clear();
|
|
|
|
void open();
|
|
|
|
void close();
|
|
|
|
void commit_changes();
|
|
|
|
|
|
|
|
public:
|
2023-09-20 04:20:39 +08:00
|
|
|
void set_vertex(int p_vertex_id, const Vector3 &p_vertex) override;
|
|
|
|
void set_normal(int p_vertex_id, const Vector3 &p_normal) override;
|
2021-03-12 11:33:46 +08:00
|
|
|
void set_aabb(const AABB &p_aabb) override;
|
2017-11-21 08:36:32 +08:00
|
|
|
};
|
|
|
|
|
2022-08-26 01:35:52 +08:00
|
|
|
class SoftBody3D : public MeshInstance3D {
|
|
|
|
GDCLASS(SoftBody3D, MeshInstance3D);
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
public:
|
2021-06-18 09:09:40 +08:00
|
|
|
enum DisableMode {
|
|
|
|
DISABLE_MODE_REMOVE,
|
|
|
|
DISABLE_MODE_KEEP_ACTIVE,
|
|
|
|
};
|
|
|
|
|
2017-11-21 08:36:32 +08:00
|
|
|
struct PinnedPoint {
|
2020-05-12 23:01:17 +08:00
|
|
|
int point_index = -1;
|
2017-11-21 08:36:32 +08:00
|
|
|
NodePath spatial_attachment_path;
|
2020-05-12 23:01:17 +08:00
|
|
|
Node3D *spatial_attachment = nullptr; // Cache
|
2018-07-28 01:10:16 +08:00
|
|
|
Vector3 offset;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
PinnedPoint();
|
|
|
|
PinnedPoint(const PinnedPoint &obj_tocopy);
|
2021-11-30 22:19:26 +08:00
|
|
|
void operator=(const PinnedPoint &obj);
|
2017-11-21 08:36:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2022-08-26 01:35:52 +08:00
|
|
|
SoftBodyRenderingServerHandler *rendering_server_handler = nullptr;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
RID physics_rid;
|
|
|
|
|
2021-06-18 09:09:40 +08:00
|
|
|
DisableMode disable_mode = DISABLE_MODE_REMOVE;
|
|
|
|
|
2021-10-21 01:53:25 +08:00
|
|
|
RID owned_mesh;
|
2020-05-12 23:01:17 +08:00
|
|
|
uint32_t collision_mask = 1;
|
|
|
|
uint32_t collision_layer = 1;
|
2017-11-21 08:36:32 +08:00
|
|
|
NodePath parent_collision_ignore;
|
2020-02-18 05:06:54 +08:00
|
|
|
Vector<PinnedPoint> pinned_points;
|
2020-05-12 23:01:17 +08:00
|
|
|
bool simulation_started = false;
|
|
|
|
bool pinned_points_cache_dirty = true;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
Ref<ArrayMesh> debug_mesh_cache;
|
2021-02-08 05:29:31 +08:00
|
|
|
class MeshInstance3D *debug_mesh = nullptr;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
2021-02-08 05:29:31 +08:00
|
|
|
bool capture_input_on_drag = false;
|
2020-05-12 23:01:17 +08:00
|
|
|
bool ray_pickable = true;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
void _update_pickable();
|
|
|
|
|
2021-10-21 01:53:25 +08:00
|
|
|
void _update_physics_server();
|
|
|
|
void _draw_soft_mesh();
|
|
|
|
|
|
|
|
void _prepare_physics_server();
|
|
|
|
void _become_mesh_owner();
|
|
|
|
|
2017-11-21 08:36:32 +08:00
|
|
|
protected:
|
|
|
|
bool _set(const StringName &p_name, const Variant &p_value);
|
|
|
|
bool _get(const StringName &p_name, Variant &r_ret) const;
|
|
|
|
void _get_property_list(List<PropertyInfo> *p_list) const;
|
|
|
|
|
|
|
|
bool _set_property_pinned_points_indices(const Array &p_indices);
|
|
|
|
bool _set_property_pinned_points_attachment(int p_item, const String &p_what, const Variant &p_value);
|
|
|
|
bool _get_property_pinned_points(int p_item, const String &p_what, Variant &r_ret) const;
|
|
|
|
|
|
|
|
void _notification(int p_what);
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2024-07-19 12:50:31 +08:00
|
|
|
#ifndef DISABLE_DEPRECATED
|
|
|
|
void _pin_point_bind_compat_94684(int p_point_index, bool pin, const NodePath &p_spatial_attachment_path = NodePath());
|
|
|
|
static void _bind_compatibility_methods();
|
|
|
|
#endif
|
|
|
|
|
2024-02-18 02:03:21 +08:00
|
|
|
PackedStringArray get_configuration_warnings() const override;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
public:
|
2021-03-12 11:33:46 +08:00
|
|
|
RID get_physics_rid() const { return physics_rid; }
|
|
|
|
|
2017-11-21 08:36:32 +08:00
|
|
|
void set_collision_mask(uint32_t p_mask);
|
|
|
|
uint32_t get_collision_mask() const;
|
|
|
|
|
|
|
|
void set_collision_layer(uint32_t p_layer);
|
|
|
|
uint32_t get_collision_layer() const;
|
|
|
|
|
2021-08-12 07:01:38 +08:00
|
|
|
void set_collision_layer_value(int p_layer_number, bool p_value);
|
|
|
|
bool get_collision_layer_value(int p_layer_number) const;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
2021-08-12 07:01:38 +08:00
|
|
|
void set_collision_mask_value(int p_layer_number, bool p_value);
|
|
|
|
bool get_collision_mask_value(int p_layer_number) const;
|
2017-11-21 08:36:32 +08:00
|
|
|
|
2021-06-18 09:09:40 +08:00
|
|
|
void set_disable_mode(DisableMode p_mode);
|
|
|
|
DisableMode get_disable_mode() const;
|
|
|
|
|
2017-11-21 08:36:32 +08:00
|
|
|
void set_parent_collision_ignore(const NodePath &p_parent_collision_ignore);
|
|
|
|
const NodePath &get_parent_collision_ignore() const;
|
|
|
|
|
2020-02-18 05:06:54 +08:00
|
|
|
void set_pinned_points_indices(Vector<PinnedPoint> p_pinned_points_indices);
|
|
|
|
Vector<PinnedPoint> get_pinned_points_indices();
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
void set_simulation_precision(int p_simulation_precision);
|
|
|
|
int get_simulation_precision();
|
|
|
|
|
|
|
|
void set_total_mass(real_t p_total_mass);
|
|
|
|
real_t get_total_mass();
|
|
|
|
|
|
|
|
void set_linear_stiffness(real_t p_linear_stiffness);
|
|
|
|
real_t get_linear_stiffness();
|
|
|
|
|
|
|
|
void set_pressure_coefficient(real_t p_pressure_coefficient);
|
|
|
|
real_t get_pressure_coefficient();
|
|
|
|
|
|
|
|
void set_damping_coefficient(real_t p_damping_coefficient);
|
|
|
|
real_t get_damping_coefficient();
|
|
|
|
|
|
|
|
void set_drag_coefficient(real_t p_drag_coefficient);
|
|
|
|
real_t get_drag_coefficient();
|
|
|
|
|
2022-08-06 02:35:08 +08:00
|
|
|
TypedArray<PhysicsBody3D> get_collision_exceptions();
|
2017-11-21 08:36:32 +08:00
|
|
|
void add_collision_exception_with(Node *p_node);
|
|
|
|
void remove_collision_exception_with(Node *p_node);
|
|
|
|
|
|
|
|
Vector3 get_point_transform(int p_point_index);
|
|
|
|
|
|
|
|
void pin_point_toggle(int p_point_index);
|
2024-07-19 12:50:31 +08:00
|
|
|
void pin_point(int p_point_index, bool pin, const NodePath &p_spatial_attachment_path = NodePath(), int p_insert_at = -1);
|
2017-11-21 08:36:32 +08:00
|
|
|
bool is_point_pinned(int p_point_index) const;
|
|
|
|
|
2023-01-22 06:06:02 +08:00
|
|
|
void _pin_point_deferred(int p_point_index, bool pin, const NodePath p_spatial_attachment_path);
|
|
|
|
|
2017-11-21 08:36:32 +08:00
|
|
|
void set_ray_pickable(bool p_ray_pickable);
|
|
|
|
bool is_ray_pickable() const;
|
|
|
|
|
2022-08-26 01:35:52 +08:00
|
|
|
SoftBody3D();
|
|
|
|
~SoftBody3D();
|
2017-11-21 08:36:32 +08:00
|
|
|
|
|
|
|
private:
|
2018-07-28 01:10:16 +08:00
|
|
|
void _make_cache_dirty();
|
2017-11-21 08:36:32 +08:00
|
|
|
void _update_cache_pin_points_datas();
|
2018-08-03 23:44:09 +08:00
|
|
|
|
2017-11-21 08:36:32 +08:00
|
|
|
void _pin_point_on_physics_server(int p_point_index, bool pin);
|
2024-07-19 12:50:31 +08:00
|
|
|
void _add_pinned_point(int p_point_index, const NodePath &p_spatial_attachment_path, int p_insert_at = -1);
|
2018-08-03 23:44:09 +08:00
|
|
|
void _reset_points_offsets();
|
|
|
|
|
2017-11-21 08:36:32 +08:00
|
|
|
void _remove_pinned_point(int p_point_index);
|
|
|
|
int _get_pinned_point(int p_point_index, PinnedPoint *&r_point) const;
|
|
|
|
int _has_pinned_point(int p_point_index) const;
|
|
|
|
};
|
|
|
|
|
2022-08-26 01:35:52 +08:00
|
|
|
VARIANT_ENUM_CAST(SoftBody3D::DisableMode);
|
2021-06-18 09:09:40 +08:00
|
|
|
|
2022-08-26 01:35:52 +08:00
|
|
|
#endif // SOFT_BODY_3D_H
|