diff --git a/SCsub b/SCsub index 2a4f86f..6b3abd9 100644 --- a/SCsub +++ b/SCsub @@ -23,7 +23,10 @@ env.add_source_files(env.modules_sources,"world/voxel_chunk.cpp") env.add_source_files(env.modules_sources,"meshers/cubic_mesher/voxel_mesher_cubic.cpp") env.add_source_files(env.modules_sources,"meshers/cubic_mesher/voxel_cube_points.cpp") -env.add_source_files(env.modules_sources,"props/voxelman_prop_entry.cpp") env.add_source_files(env.modules_sources,"props/voxelman_prop.cpp") +env.add_source_files(env.modules_sources,"props/voxelman_prop_entry.cpp") +env.add_source_files(env.modules_sources,"props/voxelman_prop_scene.cpp") +env.add_source_files(env.modules_sources,"props/voxelman_prop_mesh.cpp") +env.add_source_files(env.modules_sources,"props/voxelman_prop_light.cpp") env.add_source_files(env.modules_sources,"level_generator/voxelman_level_generator.cpp") diff --git a/props/voxelman_prop_entry.cpp b/props/voxelman_prop_entry.cpp index 70e1add..29f46e9 100644 --- a/props/voxelman_prop_entry.cpp +++ b/props/voxelman_prop_entry.cpp @@ -1,118 +1,21 @@ #include "voxelman_prop_entry.h" -#include "voxelman_prop.h" - -Ref VoxelmanPropEntry::get_mesh() const { - return _mesh; +Transform VoxelmanPropEntry::get_transform() const { + return _transform; } -void VoxelmanPropEntry::set_mesh(const Ref mesh) { - _mesh = mesh; -} - -int VoxelmanPropEntry::get_has_light() const { - return _has_light; -} -void VoxelmanPropEntry::set_has_light(const int value){ - _has_light = value; -} - -Color VoxelmanPropEntry::get_light_color() const { - return _light_color; -} -void VoxelmanPropEntry::set_light_color(const Color value) { - _light_color = value; -} - -int VoxelmanPropEntry::get_light_size() const { - return _light_size; -} -void VoxelmanPropEntry::set_light_size(const int value) { - _has_light = value; -} - -Ref VoxelmanPropEntry::get_scene() const { - return _scene; -} -void VoxelmanPropEntry::set_scene(const Ref value) { - _scene = value; -} - -Vector3 VoxelmanPropEntry::get_position() const { - return _position; -} -void VoxelmanPropEntry::set_position(const Vector3 value) { - _position = value; -} - -Vector3 VoxelmanPropEntry::get_rotation() const { - return _rotation; -} -void VoxelmanPropEntry::set_rotation(const Vector3 value) { - _rotation = value; -} - -Vector3 VoxelmanPropEntry::get_scale() const { - return _scale; -} -void VoxelmanPropEntry::set_scale(const Vector3 value) { - _scale = value; -} - -Ref VoxelmanPropEntry::get_prop() const { - return _prop; -} -void VoxelmanPropEntry::set_prop(const Ref prop) { - _prop = prop; +void VoxelmanPropEntry::set_transform(const Transform value) { + _transform = value; } VoxelmanPropEntry::VoxelmanPropEntry() { - _has_light = false; - _light_size = 5; - _scale = Vector3(1.0, 1.0, 1.0); } VoxelmanPropEntry::~VoxelmanPropEntry() { - if (_mesh.is_valid()) - _mesh.unref(); - if (_scene.is_valid()) - _scene.unref(); } void VoxelmanPropEntry::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_mesh"), &VoxelmanPropEntry::get_mesh); - ClassDB::bind_method(D_METHOD("set_mesh", "value"), &VoxelmanPropEntry::set_mesh); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "MeshDataResource"), "set_mesh", "get_mesh"); - - ClassDB::bind_method(D_METHOD("get_has_light"), &VoxelmanPropEntry::get_has_light); - ClassDB::bind_method(D_METHOD("set_has_light", "value"), &VoxelmanPropEntry::set_has_light); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "has_light"), "set_has_light", "get_has_light"); - - ClassDB::bind_method(D_METHOD("get_light_color"), &VoxelmanPropEntry::get_light_color); - ClassDB::bind_method(D_METHOD("set_light_color", "value"), &VoxelmanPropEntry::set_light_color); - ADD_PROPERTY(PropertyInfo(Variant::COLOR, "light_color"), "set_light_color", "get_light_color"); - - ClassDB::bind_method(D_METHOD("get_light_size"), &VoxelmanPropEntry::get_light_size); - ClassDB::bind_method(D_METHOD("set_light_size", "value"), &VoxelmanPropEntry::set_light_size); - ADD_PROPERTY(PropertyInfo(Variant::INT, "light_size"), "set_light_size", "get_light_size"); - - ClassDB::bind_method(D_METHOD("get_scene"), &VoxelmanPropEntry::get_scene); - ClassDB::bind_method(D_METHOD("set_scene", "value"), &VoxelmanPropEntry::set_scene); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scene", PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"), "set_scene", "get_scene"); - - ClassDB::bind_method(D_METHOD("get_prop"), &VoxelmanPropEntry::get_prop); - ClassDB::bind_method(D_METHOD("set_prop", "value"), &VoxelmanPropEntry::set_prop); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "prop", PROPERTY_HINT_RESOURCE_TYPE, "VoxelmanProp"), "set_prop", "get_prop"); - - ClassDB::bind_method(D_METHOD("get_position"), &VoxelmanPropEntry::get_position); - ClassDB::bind_method(D_METHOD("set_position", "value"), &VoxelmanPropEntry::set_position); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position"), "set_position", "get_position"); - - ClassDB::bind_method(D_METHOD("get_rotation"), &VoxelmanPropEntry::get_rotation); - ClassDB::bind_method(D_METHOD("set_rotation", "value"), &VoxelmanPropEntry::set_rotation); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation"), "set_rotation", "get_rotation"); - - ClassDB::bind_method(D_METHOD("get_scale"), &VoxelmanPropEntry::get_scale); - ClassDB::bind_method(D_METHOD("set_scale", "value"), &VoxelmanPropEntry::set_scale); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale"), "set_scale", "get_scale"); + ClassDB::bind_method(D_METHOD("get_transform"), &VoxelmanPropEntry::get_transform); + ClassDB::bind_method(D_METHOD("set_transform", "value"), &VoxelmanPropEntry::set_transform); + ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform"), "set_transform", "get_transform"); } diff --git a/props/voxelman_prop_entry.h b/props/voxelman_prop_entry.h index e8bbbfb..768b83a 100644 --- a/props/voxelman_prop_entry.h +++ b/props/voxelman_prop_entry.h @@ -1,45 +1,16 @@ #ifndef VOXELMAN_PROP_DATA_H #define VOXELMAN_PROP_DATA_H -#include "core/reference.h" -#include "core/math/vector3.h" -#include "core/color.h" -#include "scene/resources/packed_scene.h" - -#include "../../entity_spell_system/meshes/mesh_data_resource.h" - -class VoxelmanProp; +#include "core/resource.h" +#include "core/math/transform.h" class VoxelmanPropEntry : public Resource { GDCLASS(VoxelmanPropEntry, Resource); public: - Ref get_mesh() const; - void set_mesh(const Ref mesh); - int get_has_light() const; - void set_has_light(const int value); - - Color get_light_color() const; - void set_light_color(const Color value); - - int get_light_size() const; - void set_light_size(const int value); - - Ref get_scene() const; - void set_scene(const Ref value); - - Vector3 get_position() const; - void set_position(const Vector3 value); - - Vector3 get_rotation() const; - void set_rotation(const Vector3 value); - - Vector3 get_scale() const; - void set_scale(const Vector3 value); - - Ref get_prop() const; - void set_prop(const Ref prop); + Transform get_transform() const; + void set_transform(const Transform value); VoxelmanPropEntry(); ~VoxelmanPropEntry(); @@ -48,19 +19,8 @@ protected: static void _bind_methods(); private: - Ref _mesh; - bool _has_light; - Color _light_color; - int _light_size; - - Ref _scene; - - Ref _prop; - - Vector3 _position; - Vector3 _rotation; - Vector3 _scale; + Transform _transform; }; #endif diff --git a/props/voxelman_prop_light.cpp b/props/voxelman_prop_light.cpp new file mode 100644 index 0000000..a3e8da5 --- /dev/null +++ b/props/voxelman_prop_light.cpp @@ -0,0 +1,32 @@ +#include "voxelman_prop_light.h" + + +Color VoxelmanPropLight::get_light_color() const { + return _light_color; +} +void VoxelmanPropLight::set_light_color(const Color value) { + _light_color = value; +} + +int VoxelmanPropLight::get_light_size() const { + return _light_size; +} +void VoxelmanPropLight::set_light_size(const int value) { + _light_size = value; +} + +VoxelmanPropLight::VoxelmanPropLight() { + _light_size = 5; +} +VoxelmanPropLight::~VoxelmanPropLight() { +} + +void VoxelmanPropLight::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_light_color"), &VoxelmanPropLight::get_light_color); + ClassDB::bind_method(D_METHOD("set_light_color", "value"), &VoxelmanPropLight::set_light_color); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "light_color"), "set_light_color", "get_light_color"); + + ClassDB::bind_method(D_METHOD("get_light_size"), &VoxelmanPropLight::get_light_size); + ClassDB::bind_method(D_METHOD("set_light_size", "value"), &VoxelmanPropLight::set_light_size); + ADD_PROPERTY(PropertyInfo(Variant::INT, "light_size"), "set_light_size", "get_light_size"); +} diff --git a/props/voxelman_prop_light.h b/props/voxelman_prop_light.h new file mode 100644 index 0000000..5664dd8 --- /dev/null +++ b/props/voxelman_prop_light.h @@ -0,0 +1,29 @@ +#ifndef VOXELMAN_PROP_LIGHT_H +#define VOXELMAN_PROP_LIGHT_H + +#include "voxelman_prop_entry.h" + +#include "core/color.h" + +class VoxelmanPropLight : public VoxelmanPropEntry { + GDCLASS(VoxelmanPropLight, VoxelmanPropEntry); + +public: + Color get_light_color() const; + void set_light_color(const Color value); + + int get_light_size() const; + void set_light_size(const int value); + + VoxelmanPropLight(); + ~VoxelmanPropLight(); + +protected: + static void _bind_methods(); + +private: + Color _light_color; + int _light_size; +}; + +#endif diff --git a/props/voxelman_prop_mesh.cpp b/props/voxelman_prop_mesh.cpp new file mode 100644 index 0000000..039c5df --- /dev/null +++ b/props/voxelman_prop_mesh.cpp @@ -0,0 +1,21 @@ +#include "voxelman_prop_mesh.h" + +Ref VoxelmanPropMesh::get_mesh() const { + return _mesh; +} +void VoxelmanPropMesh::set_mesh(const Ref mesh) { + _mesh = mesh; +} + +VoxelmanPropMesh::VoxelmanPropMesh() { +} +VoxelmanPropMesh::~VoxelmanPropMesh() { + if (_mesh.is_valid()) + _mesh.unref(); +} + +void VoxelmanPropMesh::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_mesh"), &VoxelmanPropMesh::get_mesh); + ClassDB::bind_method(D_METHOD("set_mesh", "value"), &VoxelmanPropMesh::set_mesh); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "MeshDataResource"), "set_mesh", "get_mesh"); +} diff --git a/props/voxelman_prop_mesh.h b/props/voxelman_prop_mesh.h new file mode 100644 index 0000000..e99df3d --- /dev/null +++ b/props/voxelman_prop_mesh.h @@ -0,0 +1,25 @@ +#ifndef VOXELMAN_PROP_MESH_H +#define VOXELMAN_PROP_MESH_H + +#include "voxelman_prop_entry.h" + +#include "../../entity_spell_system/meshes/mesh_data_resource.h" + +class VoxelmanPropMesh : public VoxelmanPropEntry { + GDCLASS(VoxelmanPropMesh, VoxelmanPropEntry); + +public: + Ref get_mesh() const; + void set_mesh(const Ref mesh); + + VoxelmanPropMesh(); + ~VoxelmanPropMesh(); + +protected: + static void _bind_methods(); + +private: + Ref _mesh; +}; + +#endif diff --git a/props/voxelman_prop_scene.cpp b/props/voxelman_prop_scene.cpp new file mode 100644 index 0000000..53efdfe --- /dev/null +++ b/props/voxelman_prop_scene.cpp @@ -0,0 +1,21 @@ +#include "voxelman_prop_scene.h" + +Ref VoxelmanPropScene::get_scene() const { + return _scene; +} +void VoxelmanPropScene::set_scene(const Ref value) { + _scene = value; +} + +VoxelmanPropScene::VoxelmanPropScene() { +} +VoxelmanPropScene::~VoxelmanPropScene() { + if (_scene.is_valid()) + _scene.unref(); +} + +void VoxelmanPropScene::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_scene"), &VoxelmanPropScene::get_scene); + ClassDB::bind_method(D_METHOD("set_scene", "value"), &VoxelmanPropScene::set_scene); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scene", PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"), "set_scene", "get_scene"); +} diff --git a/props/voxelman_prop_scene.h b/props/voxelman_prop_scene.h new file mode 100644 index 0000000..b4e1c1b --- /dev/null +++ b/props/voxelman_prop_scene.h @@ -0,0 +1,25 @@ +#ifndef VOXELMAN_PROP_SCENE_H +#define VOXELMAN_PROP_SCENE_H + +#include "voxelman_prop_entry.h" + +#include "scene/resources/packed_scene.h" + +class VoxelmanPropScene : public VoxelmanPropEntry { + GDCLASS(VoxelmanPropScene, VoxelmanPropEntry); + +public: + Ref get_scene() const; + void set_scene(const Ref value); + + VoxelmanPropScene(); + ~VoxelmanPropScene(); + +protected: + static void _bind_methods(); + +private: + Ref _scene; +}; + +#endif diff --git a/register_types.cpp b/register_types.cpp index 64a388c..96c8c3b 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -16,8 +16,11 @@ #include "meshers/cubic_mesher/voxel_mesher_cubic.h" #include "meshers/cubic_mesher/voxel_cube_points.h" -#include "props/voxelman_prop_entry.h" #include "props/voxelman_prop.h" +#include "props/voxelman_prop_entry.h" +#include "props/voxelman_prop_scene.h" +#include "props/voxelman_prop_mesh.h" +#include "props/voxelman_prop_light.h" #include "level_generator/voxelman_level_generator.h" @@ -39,8 +42,11 @@ void register_voxelman_types() { ClassDB::register_class(); ClassDB::register_class(); - ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); } diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 485924e..0924df7 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -275,23 +275,23 @@ void VoxelChunk::add_lights_into(Array target) { void VoxelChunk::add_unique_lights_into(Array target) { for (int i = 0; i < _voxel_lights.size(); ++i) { - Ref l = _voxel_lights.get(i); - - bool append = true; - for (int j = 0; j < target.size(); ++j) { - Ref l2 = target.get(j); - - if (!l2.is_valid()) - continue; - - if (l2->get_world_position() == l->get_world_position() && l2->get_size() == l->get_size()) { - append = false; - break; - } - } - - if (append) - target.append(l); + Ref l = _voxel_lights.get(i); + + bool append = true; + for (int j = 0; j < target.size(); ++j) { + Ref l2 = target.get(j); + + if (!l2.is_valid()) + continue; + + if (l2->get_world_position() == l->get_world_position() && l2->get_size() == l->get_size()) { + append = false; + break; + } + } + + if (append) + target.append(l); } } @@ -335,9 +335,7 @@ void VoxelChunk::clear_baked_lights() { void VoxelChunk::add_prop_mesh(const Ref mesh, const Vector3 position, const Vector3 rotation, const Vector3 scale) { VCPropData data; - data.position = position; - data.rotation = rotation; - data.scale = scale; + data.transform = Transform(Basis(rotation).scaled(scale), position); data.mesh = mesh; @@ -347,9 +345,7 @@ void VoxelChunk::add_prop_mesh(const Ref mesh, const Vector3 p void VoxelChunk::add_prop_spawned(const Ref scene, const Vector3 position, const Vector3 rotation, const Vector3 scale) { VCPropData data; - data.position = position; - data.rotation = rotation; - data.scale = scale; + data.transform = Transform(Basis(rotation).scaled(scale), position); data.scene = scene; @@ -359,9 +355,7 @@ void VoxelChunk::add_prop_spawned(const Ref scene, const Vector3 po void VoxelChunk::add_prop(const Ref prop, const Vector3 position, const Vector3 rotation, const Vector3 scale) { VCPropData data; - data.position = position; - data.rotation = rotation; - data.scale = scale; + data.transform = Transform(Basis(rotation).scaled(scale), position); data.mesh = prop; @@ -413,19 +407,7 @@ void VoxelChunk::process_props() { for (int i = 0; i < _props.size(); ++i) { VCPropData prop = _props[i]; - Transform transform(Basis(prop.rotation).scaled(prop.scale), prop.position); - - if (prop.mesh.is_valid()) { - _mesher->add_mesh_data_resource_transform(prop.mesh, transform); - } - - if (prop.prop.is_valid()) { - process_prop(prop.prop, transform); - } - - if (prop.scene.is_valid()) { - spawn_prop(prop.scene, transform); - } + process_prop(prop.prop, prop.transform); } _mesher->bake_colors(_buffer); @@ -433,29 +415,16 @@ void VoxelChunk::process_props() { _mesher->build_mesh(_prop_mesh_rid); } -void VoxelChunk::process_prop_light(Ref prop, const Transform transform) { +void VoxelChunk::process_prop_light(Ref prop, const Transform transform) { ERR_FAIL_COND(!prop.is_valid()); - for (int i = 0; i < prop->get_prop_count(); ++i) { - Ref data = prop->get_prop(i); + Transform pt = prop->get_transform(); - if (!data.is_valid()) - continue; + pt = transform * pt; - Transform pt(Basis(data->get_rotation()).scaled(data->get_scale()), data->get_position()); + Vector3 lp = pt.xform_inv(Vector3()); - pt = transform * pt; - - if (data->get_has_light()) { - Vector3 lp = pt.xform_inv(Vector3()); - - create_voxel_light(data->get_light_color(), data->get_light_size(), (int)lp.x, (int)lp.y, (int)lp.z); - } - - if (data->get_prop().is_valid()) { - process_prop_light(data->get_prop(), pt); - } - } + create_voxel_light(prop->get_light_color(), prop->get_light_size(), (int)lp.x, (int)lp.y, (int)lp.z); } void VoxelChunk::process_prop(Ref prop, const Transform transform) { @@ -467,18 +436,21 @@ void VoxelChunk::process_prop(Ref prop, const Transform transform) if (!data.is_valid()) continue; - Transform pt(Basis(data->get_rotation()).scaled(data->get_scale()), data->get_position()); + Transform pt = data->get_transform(); - if (data->get_mesh().is_valid()) { - _mesher->add_mesh_data_resource_transform(data->get_mesh(), pt); + Ref mesh = data; + if (mesh.is_valid()) { + _mesher->add_mesh_data_resource_transform(mesh->get_mesh(), pt); } - if (data->get_scene().is_valid()) { - spawn_prop(data->get_scene(), pt); + Ref scene = data; + if (scene.is_valid()) { + spawn_prop(scene->get_scene(), pt); } - if (data->get_prop().is_valid()) { - process_prop(data->get_prop(), pt); + Ref light = data; + if (light.is_valid()) { + process_prop_light(data, transform); } } } @@ -875,4 +847,3 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("draw_debug_voxel_lights"), &VoxelChunk::draw_debug_voxel_lights); } - diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 737e321..1d57bab 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -28,6 +28,9 @@ #include "../../entity_spell_system/meshes/mesh_data_resource.h" #include "../props/voxelman_prop.h" #include "../props/voxelman_prop_entry.h" +#include "../props/voxelman_prop_scene.h" +#include "../props/voxelman_prop_mesh.h" +#include "../props/voxelman_prop_light.h" class VoxelWorld; @@ -113,7 +116,7 @@ public: void process_prop_lights(); void process_props(); void process_prop(Ref prop, const Transform transform = Transform()); - void process_prop_light(Ref prop, const Transform transform = Transform()); + void process_prop_light(Ref prop, const Transform transform = Transform()); void spawn_prop(const Ref scene, const Transform transform = Transform()); void build_prop_meshes(); @@ -144,9 +147,7 @@ public: protected: struct VCPropData { - Vector3 position; - Vector3 rotation; - Vector3 scale; + Transform transform; Ref mesh; Ref prop;