Started reworking the mesher, and the cunk's interface.

This commit is contained in:
Relintai 2019-07-19 20:54:09 +02:00
parent 6b4de4cf80
commit 4ad2faca86
4 changed files with 85 additions and 54 deletions

View File

@ -93,9 +93,11 @@ void VoxelMesher::add_buffer(Ref<VoxelBuffer> voxels) {
call("_add_buffer", voxels); call("_add_buffer", voxels);
} }
void VoxelMesher::add_mesh_data_resource(Transform local_transform, Ref<MeshDataResource> mesh) { void VoxelMesher::add_mesh_data_resource(Ref<MeshDataResource> mesh, const Vector3 position, const Vector3 rotation, const Vector3 scale) {
ERR_FAIL_COND(mesh->get_array().size() == 0); 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); Array verts = mesh->get_array().get(Mesh::ARRAY_VERTEX);
for (int i = 0; i < verts.size(); ++i) { 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"))); 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_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("bake_colors", "buffer"), &VoxelMesher::bake_colors);
ClassDB::bind_method(D_METHOD("get_voxel_scale"), &VoxelMesher::get_voxel_scale); ClassDB::bind_method(D_METHOD("get_voxel_scale"), &VoxelMesher::get_voxel_scale);

View File

@ -36,7 +36,7 @@ public:
void reset(); void reset();
void add_buffer(Ref<VoxelBuffer> voxels); void add_buffer(Ref<VoxelBuffer> voxels);
void add_mesh_data_resource(Transform local_transform, Ref<MeshDataResource> mesh); void add_mesh_data_resource(Ref<MeshDataResource> 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<VoxelBuffer> voxels); void bake_colors(Ref<VoxelBuffer> voxels);
float get_voxel_scale() const; float get_voxel_scale() const;

View File

@ -158,7 +158,7 @@ void VoxelChunk::build() {
_mesher->reset(); _mesher->reset();
if (_props.size() > 0) { if (_meshes.size() > 0) {
build_prop_mesh(); build_prop_mesh();
if (get_create_collider()) { 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) { void VoxelChunk::add_lights(Array lights) {
for (int i = 0; i < lights.size(); ++i) { for (int i = 0; i < lights.size(); ++i) {
Ref<VoxelLight> light = Ref<VoxelLight>(lights.get(i)); Ref<VoxelLight> light = Ref<VoxelLight>(lights.get(i));
@ -323,18 +314,46 @@ void VoxelChunk::remove_meshes() {
} }
} }
void VoxelChunk::add_prop(const Transform local_transform, const Ref<MeshDataResource> mesh) { void VoxelChunk::add_prop_mesh(const Ref<MeshDataResource> mesh, const Vector3 position, const Vector3 rotation, const Vector3 scale) {
VCPropData data; VCPropData data;
data.transform = local_transform; data.position = position;
data.mesh_data = mesh; data.rotation = rotation;
data.scale = scale;
data.mesh = mesh;
_props.push_back(data); _meshes.push_back(data);
} }
void VoxelChunk::add_prop_spawned(const Ref<PackedScene> 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<VoxelmanProp> 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() { 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(_voxel_world == NULL);
ERR_FAIL_COND(!get_library().is_valid()); ERR_FAIL_COND(!get_library().is_valid());
ERR_FAIL_COND(!get_library()->get_prop_material().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))); 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()) { if (_prop_mesh_instance_rid != RID()) {
VS::get_singleton()->free(_prop_mesh_instance_rid); VS::get_singleton()->free(_prop_mesh_instance_rid);
VS::get_singleton()->free(_prop_mesh_rid); VS::get_singleton()->free(_prop_mesh_rid);
@ -368,8 +387,8 @@ void VoxelChunk::build_prop_mesh() {
create_prop_mesh(); create_prop_mesh();
} }
for (int i = 0; i < _props.size(); ++i) { for (int i = 0; i < _meshes.size(); ++i) {
_mesher->add_mesh_data_resource(_props[i].transform, _props[i].mesh_data); _mesher->add_mesh_data_resource(_meshes[i].transform, _meshes[i].mesh_data);
} }
_mesher->bake_colors(_buffer); _mesher->bake_colors(_buffer);
@ -421,7 +440,7 @@ void VoxelChunk::add_spawned_prop(const Ref<PackedScene> scene) {
get_voxel_world()->add_child(n); get_voxel_world()->add_child(n);
n->set_owner(get_voxel_world()); 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<PackedScene> scene) { void VoxelChunk::add_spawned_prop_spatial(const Transform transform, const Ref<PackedScene> scene) {
ERR_FAIL_COND(!scene.is_valid()); ERR_FAIL_COND(!scene.is_valid());
@ -434,7 +453,7 @@ void VoxelChunk::add_spawned_prop_spatial(const Transform transform, const Ref<P
get_voxel_world()->add_child(n); get_voxel_world()->add_child(n);
n->set_owner(get_voxel_world()); n->set_owner(get_voxel_world());
_spawned_props.push_back(n); _spawned_meshes.push_back(n);
Spatial *spatial = Object::cast_to<Spatial>(n); Spatial *spatial = Object::cast_to<Spatial>(n);
@ -442,12 +461,12 @@ void VoxelChunk::add_spawned_prop_spatial(const Transform transform, const Ref<P
spatial->set_transform(transform); spatial->set_transform(transform);
} }
void VoxelChunk::clear_spawned_props() { void VoxelChunk::clear_spawned_meshes() {
for (int i = 0; i < _spawned_props.size(); ++i) { for (int i = 0; i < _spawned_meshes.size(); ++i) {
_spawned_props[i]->queue_delete(); _spawned_meshes[i]->queue_delete();
} }
_spawned_props.clear(); _spawned_meshes.clear();
} }
void VoxelChunk::create_debug_immediate_geometry() { void VoxelChunk::create_debug_immediate_geometry() {
@ -476,7 +495,7 @@ void VoxelChunk::free() {
remove_colliders(); remove_colliders();
remove_prop_mesh(); remove_prop_mesh();
remove_prop_colliders(); remove_prop_colliders();
clear_spawned_props(); clear_spawned_meshes();
} }
void VoxelChunk::draw_cross_voxels(Vector3 pos) { 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("clear_baked_lights"), &VoxelChunk::clear_baked_lights);
ClassDB::bind_method(D_METHOD("add_prop", "transform", "mesh"), &VoxelChunk::add_prop); 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("create_prop_mesh"), &VoxelChunk::create_prop_mesh);
ClassDB::bind_method(D_METHOD("remove_prop_mesh"), &VoxelChunk::remove_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("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", "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("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("build"), &VoxelChunk::build);
ClassDB::bind_method(D_METHOD("finalize_mesh"), &VoxelChunk::finalize_mesh); ClassDB::bind_method(D_METHOD("finalize_mesh"), &VoxelChunk::finalize_mesh);

View File

@ -26,6 +26,8 @@
#include "voxel_buffer.h" #include "voxel_buffer.h"
#include "../../entity_spell_system/meshes/mesh_data_resource.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 { class VoxelChunk : public Reference {
GDCLASS(VoxelChunk, Reference); GDCLASS(VoxelChunk, Reference);
@ -87,37 +89,40 @@ public:
void build_collider(); void build_collider();
void remove_colliders(); void remove_colliders();
void set_enabled(bool p_enabled);
bool is_enabled() const;
void add_lights(Array lights); void add_lights(Array lights);
void add_voxel_light(Ref<VoxelLight> light); void add_voxel_light(Ref<VoxelLight> light);
void remove_voxel_light(Ref<VoxelLight> light); void remove_voxel_light(Ref<VoxelLight> light);
void clear_voxel_lights(); 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<VoxelLight> light); void bake_light(Ref<VoxelLight> light);
void clear_baked_lights(); void clear_baked_lights();
void create_meshes(); void add_prop_mesh(const Ref<MeshDataResource> mesh, const Vector3 position = Vector3(), const Vector3 rotation = Vector3(), const Vector3 scale = Vector3(1.0, 1.0, 1.0));
void remove_meshes(); void add_prop_spawned(const Ref<PackedScene> scene, const Vector3 position = Vector3(), const Vector3 rotation = Vector3(), const Vector3 scale = Vector3(1.0, 1.0, 1.0));
void add_prop(const Ref<VoxelmanProp> prop, const Vector3 position = Vector3(), const Vector3 rotation = Vector3(), const Vector3 scale = Vector3(1.0, 1.0, 1.0));
void add_prop(const Transform transform, const Ref<MeshDataResource> mesh);
void clear_props(); void clear_props();
void create_prop_mesh();
void remove_prop_mesh(); void build_props();
void build_prop_mesh();
void build_prop_mesh();
void create_prop_colliders();
void build_prop_collider(); void build_prop_collider();
void remove_prop_colliders();
void spawn_spawned_props();
void add_spawned_prop(const Ref<PackedScene> scene); void free_spawned_props();
void add_spawned_prop_spatial(const Transform transform, const Ref<PackedScene> scene);
void clear_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 create_debug_immediate_geometry();
void free_debug_immediate_geometry(); void free_debug_immediate_geometry();
@ -133,10 +138,15 @@ public:
protected: protected:
struct VCPropData { struct VCPropData {
Transform transform; Vector3 position;
Ref<MeshDataResource> mesh_data; Vector3 rotation;
Vector3 scale;
Ref<MeshDataResource> mesh;
Ref<VoxelmanProp> prop;
Ref<PackedScene> scene;
}; };
protected: protected:
static void _bind_methods(); static void _bind_methods();