Added collision layer and mask support to PropInstance.

This commit is contained in:
Relintai 2021-08-25 22:21:34 +02:00
parent 7ccdbd474c
commit c6f6871fc2
5 changed files with 75 additions and 2 deletions

View File

@ -51,6 +51,32 @@ void PropInstance::set_material(const Ref<Material> &material) {
_material = material;
}
void PropInstance::set_collision_layer(uint32_t p_layer) {
_collision_layer = p_layer;
collision_layer_changed();
}
uint32_t PropInstance::get_collision_layer() const {
return _collision_layer;
}
void PropInstance::set_collision_mask(uint32_t p_mask) {
_collision_mask = p_mask;
collision_mask_changed();
}
uint32_t PropInstance::get_collision_mask() const {
return _collision_mask;
}
void PropInstance::collision_layer_changed() {
}
void PropInstance::collision_mask_changed() {
}
void PropInstance::init_materials() {
call("_init_materials");
}
@ -228,6 +254,9 @@ void PropInstance::_prop_preprocess(Transform transform, const Ref<PropData> &pr
PropInstance::PropInstance() {
_build_queued = false;
_building = false;
_collision_layer = 1;
_collision_mask = 1;
}
PropInstance::~PropInstance() {
@ -255,6 +284,16 @@ void PropInstance::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_material", "material"), &PropInstance::set_material);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_material", "get_material");
ClassDB::bind_method(D_METHOD("get_collision_layer"), &PropInstance::get_collision_layer);
ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &PropInstance::set_collision_layer);
ClassDB::bind_method(D_METHOD("get_collision_mask"), &PropInstance::get_collision_mask);
ClassDB::bind_method(D_METHOD("set_collision_mask", "layer"), &PropInstance::set_collision_mask);
ADD_GROUP("Collision", "collision_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
BIND_VMETHOD(MethodInfo("_prop_preprocess",
PropertyInfo(Variant::TRANSFORM, "tarnsform"),
PropertyInfo(Variant::OBJECT, "prop_data", PROPERTY_HINT_RESOURCE_TYPE, "PropData")));

View File

@ -49,6 +49,15 @@ public:
Ref<Material> get_material();
void set_material(const Ref<Material> &material);
void set_collision_layer(uint32_t p_layer);
uint32_t get_collision_layer() const;
void set_collision_mask(uint32_t p_mask);
uint32_t get_collision_mask() const;
virtual void collision_layer_changed();
virtual void collision_mask_changed();
void init_materials();
virtual void _init_materials();
@ -73,6 +82,9 @@ protected:
Ref<PropData> _prop_data;
Ref<Material> _material;
uint32_t _collision_layer;
uint32_t _collision_mask;
bool _build_queued;
bool _building;
};

View File

@ -694,6 +694,25 @@ void PropInstanceMerger::_prop_preprocess(Transform transform, const Ref<PropDat
}
}
void PropInstanceMerger::collision_layer_changed() {
for (int i = 0; i < _colliders.size(); ++i) {
const ColliderBody &c = _colliders[i];
if (c.body != RID()) {
PhysicsServer::get_singleton()->body_set_collision_layer(c.body, _collision_layer);
}
}
}
void PropInstanceMerger::collision_mask_changed() {
for (int i = 0; i < _colliders.size(); ++i) {
const ColliderBody &c = _colliders[i];
if (c.body != RID()) {
PhysicsServer::get_singleton()->body_set_collision_mask(c.body, _collision_mask);
}
}
}
PropInstanceMerger::PropInstanceMerger() {
_build_queued = false;
_auto_lod = true;

View File

@ -122,6 +122,9 @@ public:
void _prop_preprocess(Transform tarnsform, const Ref<PropData> &prop);
void collision_layer_changed();
void collision_mask_changed();
PropInstanceMerger();
~PropInstanceMerger();

View File

@ -234,8 +234,8 @@ void PropInstancePropJob::phase_physics_process() {
PhysicsServer::get_singleton()->body_add_shape(body, e.shape->get_rid());
//TODO store the layer mask somewhere
PhysicsServer::get_singleton()->body_set_collision_layer(body, 1);
PhysicsServer::get_singleton()->body_set_collision_mask(body, 1);
PhysicsServer::get_singleton()->body_set_collision_layer(body, _prop_instace->get_collision_layer());
PhysicsServer::get_singleton()->body_set_collision_mask(body, _prop_instace->get_collision_mask());
if (_prop_instace->is_inside_tree() && _prop_instace->is_inside_world()) {
Ref<World> world = _prop_instace->GET_WORLD();