Added Material support for PropDataProp.

This commit is contained in:
Relintai 2024-03-17 23:42:39 +01:00
parent f3745546c1
commit 5ae8d147f0
7 changed files with 149 additions and 10 deletions

View File

@ -7,8 +7,34 @@
<tutorials>
</tutorials>
<methods>
<method name="material_add">
<return type="void" />
<argument index="0" name="value" type="Material" />
<description>
</description>
</method>
<method name="material_get">
<return type="Material" />
<argument index="0" name="index" type="int" />
<description>
</description>
</method>
<method name="material_get_num" qualifiers="const">
<return type="int" />
<description>
</description>
</method>
<method name="materials_clear">
<return type="void" />
<description>
</description>
</method>
</methods>
<members>
<member name="is_merger" type="bool" setter="set_is_merger" getter="get_is_merger" default="false">
</member>
<member name="materials" type="Array" setter="materials_set" getter="materials_get" default="[ ]">
</member>
<member name="prop" type="PropData" setter="set_prop" getter="get_prop">
</member>
<member name="snap_axis" type="Vector3" setter="set_snap_axis" getter="get_snap_axis" default="Vector3( 0, 1, 0 )">

View File

@ -1033,3 +1033,7 @@ void PropInstanceMerger::_bind_methods() {
ClassDB::bind_method(D_METHOD("free_meshes"), &PropInstanceMerger::free_meshes);
ClassDB::bind_method(D_METHOD("free_colliders"), &PropInstanceMerger::free_colliders);
}
PropInstanceMerger::ColliderBody::ColliderBody() {
owns_shape = false;
}

View File

@ -43,6 +43,7 @@
#include "props/prop_data.h"
class MeshDataInstance;
class Shape;
class PropInstanceMerger : public PropInstance {
GDCLASS(PropInstanceMerger, PropInstance);
@ -143,9 +144,7 @@ protected:
RID shape_rid;
bool owns_shape;
ColliderBody() {
owns_shape = false;
}
ColliderBody();
};
struct MeshEntry {

View File

@ -763,3 +763,7 @@ void PropInstancePropJob::_bind_methods() {
ClassDB::bind_method(D_METHOD("_physics_process", "delta"), &PropInstancePropJob::_physics_process);
}
PropInstancePropJob::CollisionShapeEntry::CollisionShapeEntry() {
owns_shape = false;
}

View File

@ -128,9 +128,7 @@ protected:
Transform transform;
bool owns_shape;
CollisionShapeEntry() {
owns_shape = false;
}
CollisionShapeEntry();
};
Ref<PropMaterialCache> _material_cache;

View File

@ -32,8 +32,11 @@
#include "prop_data_prop.h"
#include "../prop_instance.h"
#include "../prop_instance_merger.h"
#include "prop_data.h"
#include "scene/resources/material/material.h"
#include "modules/modules_enabled.gen.h"
Ref<PropData> PropDataProp::get_prop() const {
@ -43,6 +46,52 @@ void PropDataProp::set_prop(const Ref<PropData> value) {
_prop = value;
}
//Materials
Ref<Material> PropDataProp::material_get(const int index) {
ERR_FAIL_INDEX_V(index, _materials.size(), Ref<Material>(NULL));
return _materials[index];
}
void PropDataProp::material_add(const Ref<Material> &value) {
ERR_FAIL_COND(!value.is_valid());
_materials.push_back(value);
}
int PropDataProp::material_get_num() const {
return _materials.size();
}
void PropDataProp::materials_clear() {
_materials.clear();
}
Vector<Variant> PropDataProp::materials_get() {
Vector<Variant> r;
for (int i = 0; i < _materials.size(); i++) {
r.push_back(_materials[i].get_ref_ptr());
}
return r;
}
void PropDataProp::materials_set(const Vector<Variant> &materials) {
_materials.clear();
for (int i = 0; i < materials.size(); i++) {
Ref<Material> material = Ref<Material>(materials[i]);
_materials.push_back(material);
}
}
bool PropDataProp::get_is_merger() {
return _is_merger;
}
void PropDataProp::set_is_merger(bool value) {
_is_merger = value;
}
bool PropDataProp::get_snap_to_mesh() {
return _snap_to_mesh;
}
@ -79,6 +128,20 @@ void PropDataProp::_processor_process(Ref<PropData> prop_data, Node *node, const
l.instance();
l->set_prop(i->get_prop_data());
l->set_transform(transform * i->get_transform());
PropInstanceMerger *im = Object::cast_to<PropInstanceMerger>(node);
if (im) {
l->materials_set(im->materials_get());
l->set_is_merger(true);
} else {
Ref<Material> mat = i->get_material();
if (mat.is_valid()) {
l->material_add(mat);
}
}
prop_data->add_prop(l);
return;
@ -86,15 +149,30 @@ void PropDataProp::_processor_process(Ref<PropData> prop_data, Node *node, const
}
Node *PropDataProp::_processor_get_node_for(const Transform &transform) {
if (_is_merger) {
PropInstanceMerger *i = memnew(PropInstanceMerger);
i->set_prop_data(get_prop());
i->set_transform(get_transform());
i->materials_set(materials_get());
return i;
} else {
PropInstance *i = memnew(PropInstance);
i->set_prop_data(get_prop());
i->set_transform(get_transform());
if (_materials.size() > 0) {
i->set_material(_materials[0]);
}
return i;
}
}
PropDataProp::PropDataProp() {
_is_merger = false;
_snap_to_mesh = false;
_snap_axis = Vector3(0, 1, 0);
}
@ -109,6 +187,20 @@ void PropDataProp::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_prop", "value"), &PropDataProp::set_prop);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "prop", PROPERTY_HINT_RESOURCE_TYPE, "PropData"), "set_prop", "get_prop");
///Materials
ClassDB::bind_method(D_METHOD("material_get", "index"), &PropDataProp::material_get);
ClassDB::bind_method(D_METHOD("material_add", "value"), &PropDataProp::material_add);
ClassDB::bind_method(D_METHOD("material_get_num"), &PropDataProp::material_get_num);
ClassDB::bind_method(D_METHOD("materials_clear"), &PropDataProp::materials_clear);
ClassDB::bind_method(D_METHOD("materials_get"), &PropDataProp::materials_get);
ClassDB::bind_method(D_METHOD("materials_set"), &PropDataProp::materials_set);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "23/20:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get");
ClassDB::bind_method(D_METHOD("get_is_merger"), &PropDataProp::get_is_merger);
ClassDB::bind_method(D_METHOD("set_is_merger", "value"), &PropDataProp::set_is_merger);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_merger"), "set_is_merger", "get_is_merger");
ClassDB::bind_method(D_METHOD("get_snap_to_mesh"), &PropDataProp::get_snap_to_mesh);
ClassDB::bind_method(D_METHOD("set_snap_to_mesh", "value"), &PropDataProp::set_snap_to_mesh);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_to_mesh"), "set_snap_to_mesh", "get_snap_to_mesh");

View File

@ -43,6 +43,8 @@
#include "../../texture_packer/texture_packer.h"
#endif
class Material;
class PropDataProp : public PropDataEntry {
GDCLASS(PropDataProp, PropDataEntry);
@ -50,6 +52,18 @@ public:
Ref<PropData> get_prop() const;
void set_prop(const Ref<PropData> value);
///Materials
Ref<Material> material_get(const int index);
void material_add(const Ref<Material> &value);
int material_get_num() const;
void materials_clear();
Vector<Variant> materials_get();
void materials_set(const Vector<Variant> &materials);
bool get_is_merger();
void set_is_merger(bool value);
bool get_snap_to_mesh();
void set_snap_to_mesh(bool value);
@ -71,9 +85,11 @@ protected:
static void _bind_methods();
private:
bool _is_merger;
bool _snap_to_mesh;
Vector3 _snap_axis;
Ref<PropData> _prop;
Vector<Ref<Material>> _materials;
};
#endif