From eb3c65499828d28d74ccdd201998c2929ea425e9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 5 Jul 2020 12:18:00 +0200 Subject: [PATCH] Finished up MeshDataInstance. --- SCsub | 6 ++ nodes/mesh_data_instance.cpp | 137 ++++++++++++++++++++++++++--------- nodes/mesh_data_instance.h | 23 ++++-- 3 files changed, 123 insertions(+), 43 deletions(-) diff --git a/SCsub b/SCsub index a70226a..d504ae8 100644 --- a/SCsub +++ b/SCsub @@ -1,7 +1,13 @@ +import os + Import('env') module_env = env.Clone() + +if os.path.isdir('../texture_packer'): + module_env.Append(CPPDEFINES=['TEXTURE_PACKER_PRESENT']) + module_env.add_source_files(env.modules_sources,"register_types.cpp") module_env.add_source_files(env.modules_sources,"mesh_data_resource.cpp") diff --git a/nodes/mesh_data_instance.cpp b/nodes/mesh_data_instance.cpp index 8295612..3589f5e 100644 --- a/nodes/mesh_data_instance.cpp +++ b/nodes/mesh_data_instance.cpp @@ -1,5 +1,11 @@ #include "mesh_data_instance.h" +#if TEXTURE_PACKER_PRESENT +#include "../../texture_packer/texture_resource/packer_image_resource.h" +#endif + +#include "core/image.h" + bool MeshDataInstance::get_snap_to_mesh() const { return _snap_to_mesh; } @@ -14,11 +20,19 @@ void MeshDataInstance::set_snap_axis(const Vector3 &value) { _snap_axis = value; } -Ref MeshDataInstance::get_mesh() { +Ref MeshDataInstance::get_mesh_data() { return _mesh; } -void MeshDataInstance::set_mesh(const Ref &mesh) { +void MeshDataInstance::set_mesh_data(const Ref &mesh) { + if (_mesh == mesh) { + return; + } + _mesh = mesh; + + if (_mesh.is_valid() && is_inside_tree()) { + setup_mesh(); + } } Ref MeshDataInstance::get_texture() { @@ -26,6 +40,81 @@ Ref MeshDataInstance::get_texture() { } void MeshDataInstance::set_texture(const Ref &texture) { _texture = texture; + + setup_material_texture(); +} + +Ref MeshDataInstance::get_material() { + return _material; +} +void MeshDataInstance::set_material(const Ref &mat) { + _material = mat; + + setup_material_texture(); +} + +void MeshDataInstance::setup_mesh() { + if (!_mesh.is_valid()) { + return; + } + + Array arr = _mesh->get_array(); + + Ref mesh = get_mesh(); + + if (!mesh.is_valid()) { + mesh.instance(); + } + + for (int i = 0; i < mesh->get_surface_count(); ++i) { + mesh->surface_remove(i); + } + + mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr); + + if (_material.is_valid() && mesh->get_surface_count() > 0) { + mesh->surface_set_material(0, _material); + } + + if (get_mesh() == mesh) { + return; + } + + set_mesh(mesh); +} + +void MeshDataInstance::setup_material_texture() { + if (!_texture.is_valid()) { + return; + } + + if (_material.is_valid()) { + Ref sm = _material; + + if (!sm.is_valid()) { + return; + } + +#if TEXTURE_PACKER_PRESENT + Ref r = _texture; + + if (r.is_valid()) { + Ref i = r->get_data(); + + Ref tex; + tex.instance(); + tex->create_from_image(i, 0); + + if (sm.is_valid()) { + sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, tex); + } + + return; + } +#endif + + sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, _texture); + } } MeshDataInstance::MeshDataInstance() { @@ -36,42 +125,16 @@ MeshDataInstance::~MeshDataInstance() { _mesh.unref(); _texture.unref(); } - +/* void MeshDataInstance::notification(int p_what) { - /* switch (p_what) { - case NOTIFICATION_READY: { + case NOTIFICATION_ENTER_TREE: { if (get_parent() == NULL) return; } - //fallthrough - case NOTIFICATION_PARENTED: { - Node *n = this; - - while (n) { - - PropInstance *pi = Object::cast_to(n); - - if (pi) { - _owner = pi; - pi->register_prop_mesh_data_instance(this); - return; - } - - n = n->get_parent(); - } - - } break; - case NOTIFICATION_EXIT_TREE: - case NOTIFICATION_UNPARENTED: { - if (_owner) { - _owner->unregister_prop_mesh_data_instance(this); - } - break; - } - }*/ + } } - +*/ void MeshDataInstance::_bind_methods() { ClassDB::bind_method(D_METHOD("get_snap_to_mesh"), &MeshDataInstance::get_snap_to_mesh); ClassDB::bind_method(D_METHOD("set_snap_to_mesh", "value"), &MeshDataInstance::set_snap_to_mesh); @@ -81,11 +144,15 @@ void MeshDataInstance::_bind_methods() { ClassDB::bind_method(D_METHOD("set_snap_axis", "value"), &MeshDataInstance::set_snap_axis); ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "snap_axis"), "set_snap_axis", "get_snap_axis"); - ClassDB::bind_method(D_METHOD("get_mesh"), &MeshDataInstance::get_mesh); - ClassDB::bind_method(D_METHOD("set_mesh", "value"), &MeshDataInstance::set_mesh); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "MeshDataResource"), "set_mesh", "get_mesh"); + ClassDB::bind_method(D_METHOD("get_mesh_data"), &MeshDataInstance::get_mesh_data); + ClassDB::bind_method(D_METHOD("set_mesh_data", "value"), &MeshDataInstance::set_mesh_data); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_data", PROPERTY_HINT_RESOURCE_TYPE, "MeshDataResource"), "set_mesh_data", "get_mesh_data"); ClassDB::bind_method(D_METHOD("get_texture"), &MeshDataInstance::get_texture); ClassDB::bind_method(D_METHOD("set_texture", "value"), &MeshDataInstance::set_texture); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); + + ClassDB::bind_method(D_METHOD("get_material"), &MeshDataInstance::get_material); + ClassDB::bind_method(D_METHOD("set_material", "value"), &MeshDataInstance::set_material); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_material", "get_material"); } diff --git a/nodes/mesh_data_instance.h b/nodes/mesh_data_instance.h index 6c575fa..36fc08a 100644 --- a/nodes/mesh_data_instance.h +++ b/nodes/mesh_data_instance.h @@ -27,11 +27,11 @@ SOFTWARE. #include "scene/resources/texture.h" #if VERSION_MAJOR < 4 -#include "scene/3d/spatial.h" +#include "scene/3d/mesh_instance.h" #else -#include "scene/3d/node_3d.h" +#include "scene/3d/mesh_instance_3d.h" -#define Spatial Node3D +#define MeshInstance MeshInstance3D #define Texture Texture2D #endif @@ -41,8 +41,8 @@ SOFTWARE. class PropInstance; -class MeshDataInstance : public Spatial { - GDCLASS(MeshDataInstance, Spatial); +class MeshDataInstance : public MeshInstance { + GDCLASS(MeshDataInstance, MeshInstance); public: bool get_snap_to_mesh() const; @@ -51,17 +51,23 @@ public: Vector3 get_snap_axis() const; void set_snap_axis(const Vector3 &value); - Ref get_mesh(); - void set_mesh(const Ref &mesh); + Ref get_mesh_data(); + void set_mesh_data(const Ref &mesh); Ref get_texture(); void set_texture(const Ref &texture); + Ref get_material(); + void set_material(const Ref &mat); + + void setup_mesh(); + void setup_material_texture(); + MeshDataInstance(); ~MeshDataInstance(); protected: - void notification(int p_what); + //void notification(int p_what); static void _bind_methods(); private: @@ -69,6 +75,7 @@ private: Vector3 _snap_axis; Ref _mesh; Ref _texture; + Ref _material; }; #endif