From 9e2d71169bdd716ac4db657bb92850a21d28597b Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 28 Jun 2020 21:31:26 +0200 Subject: [PATCH] Add prop uv rect query into the library, and make add_mesh_data_resource use it. --- library/voxelman_library.cpp | 4 ++++ library/voxelman_library.h | 2 ++ library/voxelman_library_merger.cpp | 12 ++++++++++++ library/voxelman_library_merger.h | 2 ++ world/voxel_chunk.cpp | 12 ++++++++---- world/voxel_chunk.h | 2 +- 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/library/voxelman_library.cpp b/library/voxelman_library.cpp index 311d646..527d5f3 100644 --- a/library/voxelman_library.cpp +++ b/library/voxelman_library.cpp @@ -222,6 +222,10 @@ int VoxelmanLibrary::get_num_props() const { } void VoxelmanLibrary::clear_props() { } + +Rect2 VoxelmanLibrary::get_prop_uv_rect(const Ref &texture) { + return Rect2(0, 0, 1, 1); +} #endif //Rects diff --git a/library/voxelman_library.h b/library/voxelman_library.h index b50842a..f55308d 100644 --- a/library/voxelman_library.h +++ b/library/voxelman_library.h @@ -101,6 +101,8 @@ public: virtual void remove_prop(const int id); virtual int get_num_props() const; virtual void clear_props(); + + virtual Rect2 get_prop_uv_rect(const Ref &texture); #endif virtual void refresh_rects(); diff --git a/library/voxelman_library_merger.cpp b/library/voxelman_library_merger.cpp index 7a81361..6a27398 100644 --- a/library/voxelman_library_merger.cpp +++ b/library/voxelman_library_merger.cpp @@ -180,6 +180,18 @@ Vector VoxelmanLibraryMerger::get_props() { void VoxelmanLibraryMerger::set_props(const Vector &props) { VARIANT_ARRAY_SET(props, _props, PropData); } + +Rect2 VoxelmanLibraryMerger::get_prop_uv_rect(const Ref &texture) { + if (!texture.is_valid()) + return Rect2(0, 0, 1, 1); + + Ref at = _prop_packer->get_texture(texture); + + if (!at.is_valid()) + return Rect2(0, 0, 1, 1); + + return at->get_region(); +} #endif void VoxelmanLibraryMerger::refresh_rects() { diff --git a/library/voxelman_library_merger.h b/library/voxelman_library_merger.h index 493bb22..a36a4ec 100644 --- a/library/voxelman_library_merger.h +++ b/library/voxelman_library_merger.h @@ -75,6 +75,8 @@ public: Vector get_props(); void set_props(const Vector &props); + + Rect2 get_prop_uv_rect(const Ref &texture); #endif void refresh_rects(); diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index f79d600..5052115 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -711,17 +711,21 @@ void VoxelChunk::clear_props() { } #if MESH_DATA_RESOURCE_PRESENT -int VoxelChunk::add_mesh_data_resource(const Transform &transform, const Ref &mesh, const Ref &texture, const Rect2 &uv_rect, const Color &color) { +int VoxelChunk::add_mesh_data_resource(const Transform &local_transform, const Ref &mesh, const Ref &texture, const Color &color) { ERR_FAIL_COND_V(!mesh.is_valid(), 0); int index = _mesh_data_resources.size(); MeshDataResourceEntry e; - e.transform = transform; + e.transform = local_transform; e.mesh = mesh; e.texture = texture; e.color = color; - e.uv_rect = uv_rect; + + if (get_library().is_valid() && texture.is_valid()) + e.uv_rect = get_library()->get_prop_uv_rect(texture); + else + e.uv_rect = Rect2(0, 0, 1, 1); _mesh_data_resources.push_back(e); @@ -1133,7 +1137,7 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("clear_props"), &VoxelChunk::clear_props); #if MESH_DATA_RESOURCE_PRESENT - ClassDB::bind_method(D_METHOD("add_mesh_data_resource", "transform", "mesh", "texture", "uv_rect", "color"), &VoxelChunk::add_mesh_data_resource, DEFVAL(Ref()), DEFVAL(Rect2(0, 0, 1, 1)), DEFVAL(Color(1, 1, 1, 1))); + ClassDB::bind_method(D_METHOD("add_mesh_data_resource", "local_transform", "mesh", "texture", "color"), &VoxelChunk::add_mesh_data_resource, DEFVAL(Ref()), DEFVAL(Color(1, 1, 1, 1))); ClassDB::bind_method(D_METHOD("get_mesh_data_resource", "index"), &VoxelChunk::get_mesh_data_resource); ClassDB::bind_method(D_METHOD("set_mesh_data_resource", "index", "mesh"), &VoxelChunk::set_mesh_data_resource); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index b9af458..7ed8956 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -200,7 +200,7 @@ public: void clear_props(); #if MESH_DATA_RESOURCE_PRESENT - int add_mesh_data_resource(const Transform &transform, const Ref &mesh, const Ref &texture = Ref(), const Rect2 &uv_rect = Rect2(0, 0, 1, 1), const Color &color = Color(1, 1, 1, 1)); + int add_mesh_data_resource(const Transform &local_transform, const Ref &mesh, const Ref &texture = Ref(), const Color &color = Color(1, 1, 1, 1)); Ref get_mesh_data_resource(const int index); void set_mesh_data_resource(const int index, const Ref &mesh);