Cleanups to PropData's api.

This commit is contained in:
Relintai 2020-07-05 15:13:43 +02:00
parent a4a7949bc8
commit 4a10f1d0fa
6 changed files with 51 additions and 144 deletions

View File

@ -88,148 +88,19 @@ void PropData::set_props(const Vector<Variant> &props) {
}
}
#if TEXTURE_PACKER_PRESENT
void PropData::add_textures_into(Ref<TexturePacker> texture_packer) {
ERR_FAIL_COND(!texture_packer.is_valid());
for (int i = 0; i < _props.size(); ++i) {
Ref<PropDataEntry> entry = _props.get(i);
/*
Ref<PropDataMesh> pmesh = entry;
if (pmesh.is_valid() && pmesh->get_texture().is_valid()) {
texture_packer->add_texture(pmesh->get_texture());
}
*/
Ref<PropDataProp> pdataprop = entry;
if (pdataprop.is_valid() && pdataprop->get_prop().is_valid()) {
pdataprop->get_prop()->add_textures_into(texture_packer);
if (entry.is_valid()) {
entry->add_textures_into(texture_packer);
}
}
}
void PropData::add_prop_lights_into(Ref<VoxelChunk> chunk, Transform parent_transform, bool allow_snap) {
ERR_FAIL_COND(!chunk.is_valid());
for (int i = 0; i < _props.size(); ++i) {
Ref<PropDataEntry> entry = _props.get(i);
Ref<PropDataLight> pl = entry;
if (pl.is_valid()) {
Transform t = parent_transform * pl->get_transform();
Vector3 px = t.origin / chunk->get_voxel_scale();
Ref<VoxelLight> vl;
vl.instance();
vl->set_world_position(px.x + chunk->get_position_x() * chunk->get_size_x(), px.y + chunk->get_position_y() * chunk->get_size_y(), px.z + chunk->get_position_z() * chunk->get_size_z());
vl->set_color(pl->get_light_color());
vl->set_size(pl->get_light_size());
chunk->bake_light(vl);
}
Ref<PropDataProp> pdataprop = entry;
if (pdataprop.is_valid() && pdataprop->get_prop().is_valid()) {
Ref<PropData> pd = pdataprop->get_prop();
if (allow_snap) {
if (pd->get_snap_to_mesh())
print_error(pd->get_name());
pd->add_prop_lights_into(chunk, get_next_snapped_prop_transform(chunk->get_voxel_world(), parent_transform * pdataprop->get_transform(), pd->get_snap_to_mesh(), pd->get_snap_axis()), allow_snap);
} else {
pd->add_prop_lights_into(chunk, parent_transform * pdataprop->get_transform(), allow_snap);
}
}
}
}
void PropData::add_meshes_into(Ref<VoxelMesher> mesher, Ref<TexturePacker> 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<PropDataEntry> entry = _props.get(i);
Ref<PropDataMesh> pmesh = entry;
if (pmesh.is_valid()) {
Rect2 reg = Rect2(0, 0, 1, 1);
if (pmesh->get_texture().is_valid()) {
Ref<AtlasTexture> 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<PropDataProp> 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<VoxelMesher> mesher, Ref<TexturePacker> texture_packer, Transform parent_transform, Node *snap_spatial) {
Spatial *s = Object::cast_to<Spatial>(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) {
Vector3 pos = s->to_global(parent_transform.origin);
Vector3 world_snap_axis = s->to_global(parent_transform.xform(snap_axis));
Vector3 world_snap_dir = world_snap_axis - pos;
world_snap_dir *= 100;
PhysicsDirectSpaceState *space_state = s->get_world()->get_direct_space_state();
ERR_FAIL_COND_V(space_state == NULL, parent_transform);
PhysicsDirectSpaceState::RayResult res;
if (space_state->intersect_ray(pos - world_snap_dir, pos + world_snap_dir, res, Set<RID>(), 1)) {
parent_transform.origin = s->to_local(res.position);
}
}
return parent_transform;
}
Transform PropData::get_next_snapped_prop_transform_bind(Node *spatial, Transform parent_transform, bool snap_to_mesh, Vector3 snap_axis) {
Spatial *s = Object::cast_to<Spatial>(spatial);
ERR_FAIL_COND_V(!ObjectDB::instance_validate(s), parent_transform);
return get_next_snapped_prop_transform(s, parent_transform, snap_to_mesh, snap_axis);
}
#endif
PropData::PropData() {
_id = 0;
@ -260,10 +131,7 @@ void PropData::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_props", "props"), &PropData::set_props);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "props", PROPERTY_HINT_NONE, "17/17:PropDataEntry", PROPERTY_USAGE_DEFAULT, "PropDataEntry"), "set_props", "get_props");
#if TEXTURE_PACKER_PRESENT
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);
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);
#endif
}

View File

@ -36,7 +36,9 @@ SOFTWARE.
#include "../../voxelman/meshers/voxel_mesher.h"
#if TEXTURE_PACKER_PRESENT
#include "../../texture_packer/texture_packer.h"
#endif
class Spatial;
class VoxelChunk;
@ -64,13 +66,9 @@ public:
Vector<Variant> get_props();
void set_props(const Vector<Variant> &props);
#if TEXTURE_PACKER_PRESENT
void add_textures_into(Ref<TexturePacker> texture_packer);
void add_prop_lights_into(Ref<VoxelChunk> chunk, Transform parent_transform, bool allow_snap);
void add_meshes_into(Ref<VoxelMesher> mesher, Ref<TexturePacker> texture_packer, Transform parent_transform, Spatial *snap_spatial = NULL);
void add_meshes_into_bind(Ref<VoxelMesher> mesher, Ref<TexturePacker> 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);
#endif
PropData();
~PropData();

View File

@ -29,6 +29,13 @@ void PropDataEntry::set_transform(const Transform &value) {
_transform = value;
}
#if TEXTURE_PACKER_PRESENT
void PropDataEntry::add_textures_into(Ref<TexturePacker> texture_packer) {
if (has_method("_add_textures_into"))
call("_add_textures_into", texture_packer);
}
#endif
PropDataEntry::PropDataEntry() {
}
PropDataEntry::~PropDataEntry() {
@ -38,4 +45,10 @@ void PropDataEntry::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_transform"), &PropDataEntry::get_transform);
ClassDB::bind_method(D_METHOD("set_transform", "value"), &PropDataEntry::set_transform);
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform"), "set_transform", "get_transform");
#if TEXTURE_PACKER_PRESENT
BIND_VMETHOD(MethodInfo("_add_textures_into", PropertyInfo(Variant::OBJECT, "texture_packer", PROPERTY_HINT_RESOURCE_TYPE, "TexturePacker")));
ClassDB::bind_method(D_METHOD("add_textures_into", "texture_packer"), &PropDataEntry::add_textures_into);
#endif
}

View File

@ -26,6 +26,10 @@ SOFTWARE.
#include "core/math/transform.h"
#include "core/resource.h"
#if TEXTURE_PACKER_PRESENT
#include "../../texture_packer/texture_packer.h"
#endif
class PropDataEntry : public Resource {
GDCLASS(PropDataEntry, Resource);
@ -33,6 +37,10 @@ public:
Transform get_transform() const;
void set_transform(const Transform &value);
#if TEXTURE_PACKER_PRESENT
void add_textures_into(Ref<TexturePacker> texture_packer);
#endif
PropDataEntry();
~PropDataEntry();

View File

@ -43,6 +43,14 @@ void PropDataProp::set_snap_axis(Vector3 value) {
_snap_axis = value;
}
#if TEXTURE_PACKER_PRESENT
void PropDataProp::_add_textures_into(Ref<TexturePacker> texture_packer) {
if (get_prop().is_valid()) {
get_prop()->add_textures_into(texture_packer);
}
}
#endif
PropDataProp::PropDataProp() {
_snap_to_mesh = false;
_snap_axis = Vector3(0, 1, 0);
@ -64,4 +72,8 @@ void PropDataProp::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_snap_axis"), &PropDataProp::get_snap_axis);
ClassDB::bind_method(D_METHOD("set_snap_axis", "value"), &PropDataProp::set_snap_axis);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "snap_axis"), "set_snap_axis", "get_snap_axis");
#if TEXTURE_PACKER_PRESENT
ClassDB::bind_method(D_METHOD("_add_textures_into", "texture_packer"), &PropDataProp::_add_textures_into);
#endif
}

View File

@ -28,6 +28,10 @@ SOFTWARE.
#include "prop_data.h"
#if TEXTURE_PACKER_PRESENT
#include "../../texture_packer/texture_packer.h"
#endif
class PropDataProp : public PropDataEntry {
GDCLASS(PropDataProp, PropDataEntry);
@ -41,6 +45,10 @@ public:
Vector3 get_snap_axis();
void set_snap_axis(Vector3 value);
#if TEXTURE_PACKER_PRESENT
void _add_textures_into(Ref<TexturePacker> texture_packer);
#endif
PropDataProp();
~PropDataProp();