From 4ad2faca8682deabe74ae84e0c3c9202d735c8be Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 19 Jul 2019 20:54:09 +0200 Subject: [PATCH] Started reworking the mesher, and the cunk's interface. --- meshers/voxel_mesher.cpp | 6 ++-- meshers/voxel_mesher.h | 2 +- world/voxel_chunk.cpp | 75 +++++++++++++++++++++++++--------------- world/voxel_chunk.h | 56 ++++++++++++++++++------------ 4 files changed, 85 insertions(+), 54 deletions(-) diff --git a/meshers/voxel_mesher.cpp b/meshers/voxel_mesher.cpp index 6c93e9e..0174656 100644 --- a/meshers/voxel_mesher.cpp +++ b/meshers/voxel_mesher.cpp @@ -93,9 +93,11 @@ void VoxelMesher::add_buffer(Ref voxels) { call("_add_buffer", voxels); } -void VoxelMesher::add_mesh_data_resource(Transform local_transform, Ref mesh) { +void VoxelMesher::add_mesh_data_resource(Ref mesh, const Vector3 position, const Vector3 rotation, const Vector3 scale) { ERR_FAIL_COND(mesh->get_array().size() == 0); + Transform local_transform = Transform(Basis(rotation).scaled(scale), position); + Array verts = mesh->get_array().get(Mesh::ARRAY_VERTEX); for (int i = 0; i < verts.size(); ++i) { @@ -458,7 +460,7 @@ void VoxelMesher::_bind_methods() { BIND_VMETHOD(MethodInfo("_bake_colors", PropertyInfo(Variant::OBJECT, "buffer", PROPERTY_HINT_RESOURCE_TYPE, "VoxelBuffer"))); ClassDB::bind_method(D_METHOD("add_buffer", "buffer"), &VoxelMesher::add_buffer); - ClassDB::bind_method(D_METHOD("add_mesh_data_resource", "local_transform", "mesh"), &VoxelMesher::add_mesh_data_resource); + ClassDB::bind_method(D_METHOD("add_mesh_data_resource", "mesh", "position", "rotation", "scale"), &VoxelMesher::add_mesh_data_resource, DEFVAL(Vector3(1.0, 1.0, 1.0)), DEFVAL(Vector3()), DEFVAL(Vector3())); ClassDB::bind_method(D_METHOD("bake_colors", "buffer"), &VoxelMesher::bake_colors); ClassDB::bind_method(D_METHOD("get_voxel_scale"), &VoxelMesher::get_voxel_scale); diff --git a/meshers/voxel_mesher.h b/meshers/voxel_mesher.h index 4fc5cb0..ba28b5b 100644 --- a/meshers/voxel_mesher.h +++ b/meshers/voxel_mesher.h @@ -36,7 +36,7 @@ public: void reset(); void add_buffer(Ref voxels); - void add_mesh_data_resource(Transform local_transform, Ref mesh); + void add_mesh_data_resource(Ref mesh, const Vector3 position = Vector3(0, 0, 0), const Vector3 rotation = Vector3(0, 0, 0), const Vector3 scale = Vector3(1.0, 1.0, 1.0)); void bake_colors(Ref voxels); float get_voxel_scale() const; diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 422df35..0cc735f 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -158,7 +158,7 @@ void VoxelChunk::build() { _mesher->reset(); - if (_props.size() > 0) { + if (_meshes.size() > 0) { build_prop_mesh(); if (get_create_collider()) { @@ -225,15 +225,6 @@ void VoxelChunk::remove_colliders() { } } -void VoxelChunk::set_enabled(bool p_enabled) { - - _enabled = p_enabled; -} - -bool VoxelChunk::is_enabled() const { - return _enabled; -} - void VoxelChunk::add_lights(Array lights) { for (int i = 0; i < lights.size(); ++i) { Ref light = Ref(lights.get(i)); @@ -323,18 +314,46 @@ void VoxelChunk::remove_meshes() { } } -void VoxelChunk::add_prop(const Transform local_transform, const Ref mesh) { +void VoxelChunk::add_prop_mesh(const Ref mesh, const Vector3 position, const Vector3 rotation, const Vector3 scale) { VCPropData data; - data.transform = local_transform; - data.mesh_data = mesh; + data.position = position; + data.rotation = rotation; + data.scale = scale; + + data.mesh = mesh; - _props.push_back(data); + _meshes.push_back(data); } + +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.scene = scene; + + _meshes.push_back(data); +} + +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.mesh = prop; + + _meshes.push_back(prop); +} + void VoxelChunk::clear_props() { - _props.clear(); + _meshes.clear(); } -void VoxelChunk::create_prop_mesh() { +void VoxelChunk::allocate_prop_mesh() { ERR_FAIL_COND(_voxel_world == NULL); ERR_FAIL_COND(!get_library().is_valid()); ERR_FAIL_COND(!get_library()->get_prop_material().is_valid()); @@ -354,7 +373,7 @@ void VoxelChunk::create_prop_mesh() { VS::get_singleton()->instance_set_transform(_prop_mesh_instance_rid, Transform(Basis(), Vector3(_chunk_position.x * _chunk_size.x * _voxel_scale, _chunk_position.y * _chunk_size.y * _voxel_scale, _chunk_position.z * _chunk_size.z * _voxel_scale))); } -void VoxelChunk::remove_prop_mesh() { +void VoxelChunk::free_prop_mesh() { if (_prop_mesh_instance_rid != RID()) { VS::get_singleton()->free(_prop_mesh_instance_rid); VS::get_singleton()->free(_prop_mesh_rid); @@ -368,8 +387,8 @@ void VoxelChunk::build_prop_mesh() { create_prop_mesh(); } - for (int i = 0; i < _props.size(); ++i) { - _mesher->add_mesh_data_resource(_props[i].transform, _props[i].mesh_data); + for (int i = 0; i < _meshes.size(); ++i) { + _mesher->add_mesh_data_resource(_meshes[i].transform, _meshes[i].mesh_data); } _mesher->bake_colors(_buffer); @@ -421,7 +440,7 @@ void VoxelChunk::add_spawned_prop(const Ref scene) { get_voxel_world()->add_child(n); n->set_owner(get_voxel_world()); - _spawned_props.push_back(n); + _spawned_meshes.push_back(n); } void VoxelChunk::add_spawned_prop_spatial(const Transform transform, const Ref scene) { ERR_FAIL_COND(!scene.is_valid()); @@ -434,7 +453,7 @@ void VoxelChunk::add_spawned_prop_spatial(const Transform transform, const Ref

add_child(n); n->set_owner(get_voxel_world()); - _spawned_props.push_back(n); + _spawned_meshes.push_back(n); Spatial *spatial = Object::cast_to(n); @@ -442,12 +461,12 @@ void VoxelChunk::add_spawned_prop_spatial(const Transform transform, const Ref

set_transform(transform); } -void VoxelChunk::clear_spawned_props() { - for (int i = 0; i < _spawned_props.size(); ++i) { - _spawned_props[i]->queue_delete(); +void VoxelChunk::clear_spawned_meshes() { + for (int i = 0; i < _spawned_meshes.size(); ++i) { + _spawned_meshes[i]->queue_delete(); } - _spawned_props.clear(); + _spawned_meshes.clear(); } void VoxelChunk::create_debug_immediate_geometry() { @@ -476,7 +495,7 @@ void VoxelChunk::free() { remove_colliders(); remove_prop_mesh(); remove_prop_colliders(); - clear_spawned_props(); + clear_spawned_meshes(); } void VoxelChunk::draw_cross_voxels(Vector3 pos) { @@ -681,14 +700,14 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("clear_baked_lights"), &VoxelChunk::clear_baked_lights); ClassDB::bind_method(D_METHOD("add_prop", "transform", "mesh"), &VoxelChunk::add_prop); - ClassDB::bind_method(D_METHOD("clear_props"), &VoxelChunk::clear_props); + ClassDB::bind_method(D_METHOD("clear_meshes"), &VoxelChunk::clear_meshes); ClassDB::bind_method(D_METHOD("create_prop_mesh"), &VoxelChunk::create_prop_mesh); ClassDB::bind_method(D_METHOD("remove_prop_mesh"), &VoxelChunk::remove_prop_mesh); ClassDB::bind_method(D_METHOD("build_prop_mesh"), &VoxelChunk::build_prop_mesh); ClassDB::bind_method(D_METHOD("add_spawned_prop", "scene"), &VoxelChunk::add_spawned_prop); ClassDB::bind_method(D_METHOD("add_spawned_prop_spatial", "transform", "scene"), &VoxelChunk::add_spawned_prop_spatial); - ClassDB::bind_method(D_METHOD("clear_spawned_props"), &VoxelChunk::clear_spawned_props); + ClassDB::bind_method(D_METHOD("clear_spawned_meshes"), &VoxelChunk::clear_spawned_meshes); ClassDB::bind_method(D_METHOD("build"), &VoxelChunk::build); ClassDB::bind_method(D_METHOD("finalize_mesh"), &VoxelChunk::finalize_mesh); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 0f48591..ea24b02 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -26,6 +26,8 @@ #include "voxel_buffer.h" #include "../../entity_spell_system/meshes/mesh_data_resource.h" +#include "../props/voxelman_prop.h" +#include "../props/voxelman_prop_data.h" class VoxelChunk : public Reference { GDCLASS(VoxelChunk, Reference); @@ -87,37 +89,40 @@ public: void build_collider(); void remove_colliders(); - void set_enabled(bool p_enabled); - bool is_enabled() const; - void add_lights(Array lights); void add_voxel_light(Ref light); void remove_voxel_light(Ref light); void clear_voxel_lights(); - void get_lights(Array lights); + void append_lights(Array lights); + Array get_lights(); - void bake_lights(); + void bake_lights(); void bake_light(Ref light); void clear_baked_lights(); - - void create_meshes(); - void remove_meshes(); - - void add_prop(const Transform transform, const Ref mesh); + + void add_prop_mesh(const Ref mesh, const Vector3 position = Vector3(), const Vector3 rotation = Vector3(), const Vector3 scale = Vector3(1.0, 1.0, 1.0)); + void add_prop_spawned(const Ref scene, const Vector3 position = Vector3(), const Vector3 rotation = Vector3(), const Vector3 scale = Vector3(1.0, 1.0, 1.0)); + void add_prop(const Ref prop, const Vector3 position = Vector3(), const Vector3 rotation = Vector3(), const Vector3 scale = Vector3(1.0, 1.0, 1.0)); void clear_props(); - void create_prop_mesh(); - void remove_prop_mesh(); - void build_prop_mesh(); - - void create_prop_colliders(); + + void build_props(); + + void build_prop_mesh(); void build_prop_collider(); - void remove_prop_colliders(); - - void add_spawned_prop(const Ref scene); - void add_spawned_prop_spatial(const Transform transform, const Ref scene); - void clear_spawned_props(); + + void spawn_spawned_props(); + void free_spawned_props(); + void allocate_main_mesh(); + void free_main_mesh(); + + void allocate_prop_mesh(); + void free_prop_mesh(); + + void allocate_prop_colliders(); + void free_prop_colliders(); + void create_debug_immediate_geometry(); void free_debug_immediate_geometry(); @@ -133,10 +138,15 @@ public: protected: struct VCPropData { - Transform transform; - Ref mesh_data; + Vector3 position; + Vector3 rotation; + Vector3 scale; + + Ref mesh; + Ref prop; + Ref scene; }; - + protected: static void _bind_methods();