From 802e6023367e24890dc504b205d3e0fcb1cad3f2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 8 Nov 2019 14:37:48 +0100 Subject: [PATCH] PropData can setup meshes into the mesher as well now. --- props/prop_data.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++--- props/prop_data.h | 7 ++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/props/prop_data.cpp b/props/prop_data.cpp index 3d4e633..b8b1bc1 100644 --- a/props/prop_data.cpp +++ b/props/prop_data.cpp @@ -1,7 +1,7 @@ #include "prop_data.h" -#include "prop_data_prop.h" #include "../world/voxel_chunk.h" +#include "prop_data_prop.h" bool PropData::get_snap_to_mesh() { return _snap_to_mesh; @@ -114,8 +114,6 @@ void PropData::add_prop_lights_into(VoxelChunk *chunk, Transform parent_transfor } else { pd->add_prop_lights_into(chunk, parent_transform * pdataprop->get_transform(), allow_snap); } - - } } } @@ -127,6 +125,60 @@ void PropData::add_prop_lights_into_bind(Node *chunk, Transform parent_transform add_prop_lights_into(c, parent_transform, allow_snap); } +void PropData::add_meshes_into(Ref mesher, Ref texture_packer, Transform parent_transform, Spatial *snap_spatial) { + ERR_FAIL_COND(!mesher.is_valid()); + ERR_FAIL_COND(!texture_packer.is_valid()); + ERR_FAIL_COND(texture_packer->get_generated_texture_count() == 0); + ERR_FAIL_COND(snap_spatial != NULL && !ObjectDB::instance_validate(snap_spatial)); + + Vector2 texsize = texture_packer->get_generated_texture(0)->get_size(); + + for (int i = 0; i < _props.size(); ++i) { + Ref entry = _props.get(i); + + Ref pmesh = entry; + + if (pmesh.is_valid()) { + + Rect2 reg = Rect2(0, 0, 1, 1); + + if (pmesh->get_texture().is_valid()) { + + Ref at = texture_packer->get_texture(pmesh->get_texture()); + + reg = at->get_region(); + + reg.position.x /= texsize.x; + reg.position.y /= texsize.y; + reg.size.x /= texsize.x; + reg.size.y /= texsize.y; + } + + if (snap_spatial != NULL) + mesher->add_mesh_data_resource_transform(pmesh->get_mesh(), get_next_snapped_prop_transform(snap_spatial, parent_transform * pmesh->get_transform(), pmesh->get_snap_to_mesh(), pmesh->get_snap_axis()), reg); + else + mesher->add_mesh_data_resource_transform(pmesh->get_mesh(), parent_transform * pmesh->get_transform(), reg); + } + + Ref pdataprop = entry; + + if (pdataprop.is_valid() && pdataprop->get_prop().is_valid()) { + + if (snap_spatial != NULL) + pdataprop->get_prop()->add_meshes_into(mesher, texture_packer, get_next_snapped_prop_transform(snap_spatial, parent_transform * pdataprop->get_transform(), pdataprop->get_snap_to_mesh(), pdataprop->get_snap_axis()), snap_spatial); + else + pdataprop->get_prop()->add_meshes_into(mesher, texture_packer, parent_transform * pmesh->get_transform(), snap_spatial); + } + + } +} +void PropData::add_meshes_into_bind(Ref mesher, Ref texture_packer, Transform parent_transform, Node *snap_spatial) { + Spatial *s = Object::cast_to(snap_spatial); + + ERR_FAIL_COND(s != NULL && !ObjectDB::instance_validate(s)); + + add_meshes_into(mesher, texture_packer, parent_transform, s); +} Transform PropData::get_next_snapped_prop_transform(Spatial *s, Transform parent_transform, bool snap_to_mesh, Vector3 snap_axis) { if (snap_to_mesh) { @@ -187,5 +239,7 @@ void PropData::_bind_methods() { ClassDB::bind_method(D_METHOD("add_textures_into", "texture_packer"), &PropData::add_textures_into); ClassDB::bind_method(D_METHOD("add_prop_lights_into", "chunk", "parent_transform", "allow_snap"), &PropData::add_prop_lights_into_bind); + ClassDB::bind_method(D_METHOD("add_meshes_into", "mesher", "texture_packer", "parent_transform", "snap_spatial"), &PropData::add_meshes_into_bind); + ClassDB::bind_method(D_METHOD("get_next_snapped_prop_transform", "spatial", "parent_transform", "snap_to_mesh", "snap_axis"), &PropData::get_next_snapped_prop_transform_bind); } diff --git a/props/prop_data.h b/props/prop_data.h index ef9d5e2..7a372e9 100644 --- a/props/prop_data.h +++ b/props/prop_data.h @@ -4,12 +4,17 @@ #include "core/reference.h" #include "core/vector.h" #include "core/math/transform.h" +#include "core/math/vector2.h" +#include "core/math/vector3.h" +#include "core/math/rect2.h" #include "servers/physics_server.h" #include "prop_data_entry.h" #include "prop_data_mesh.h" +#include "../meshers/voxel_mesher.h" + #include "../../texture_packer/texture_packer.h" class Spatial; @@ -38,6 +43,8 @@ public: void add_textures_into(Ref texture_packer); void add_prop_lights_into(VoxelChunk *chunk, Transform parent_transform, bool allow_snap); void add_prop_lights_into_bind(Node *chunk, Transform parent_transform, bool allow_snap); + void add_meshes_into(Ref mesher, Ref texture_packer, Transform parent_transform, Spatial *snap_spatial = NULL); + void add_meshes_into_bind(Ref mesher, Ref texture_packer, Transform parent_transform, Node *snap_spatial = NULL); Transform get_next_snapped_prop_transform(Spatial *s, Transform parent_transform, bool snap_to_mesh, Vector3 snap_axis); Transform get_next_snapped_prop_transform_bind(Node *spatial, Transform parent_transform, bool snap_to_mesh, Vector3 snap_axis);