Skip to content

Commit

Permalink
Forward-declare VoxelInstanceLibraryMultiMeshItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Jun 9, 2024
1 parent 6ebf995 commit e04ce22
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 40 deletions.
1 change: 1 addition & 0 deletions editor/instancer/voxel_instancer_stat_view.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "voxel_instancer_stat_view.h"
#include "../../terrain/instancing/voxel_instance_library.h"
#include "../../terrain/instancing/voxel_instance_library_item.h"
#include "../../terrain/instancing/voxel_instancer.h"
#include "../../util/godot/classes/node.h"
#include "../../util/godot/classes/tree.h"
Expand Down
1 change: 1 addition & 0 deletions register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "terrain/fixed_lod/voxel_terrain_multiplayer_synchronizer.h"
#include "terrain/instancing/voxel_instance_component.h"
#include "terrain/instancing/voxel_instance_library.h"
#include "terrain/instancing/voxel_instance_library_multimesh_item.h"
#include "terrain/instancing/voxel_instance_library_scene_item.h"
#include "terrain/instancing/voxel_instancer.h"
#include "terrain/instancing/voxel_instancer_rigidbody.h"
Expand Down
2 changes: 1 addition & 1 deletion terrain/instancing/voxel_instance_library.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "voxel_instance_library.h"
#include "../../util/containers/container_funcs.h"
#include "../../util/profiling.h"
#include "voxel_instancer.h"
#include "voxel_instance_library_item.h"
#include <algorithm>
#ifdef ZN_GODOT_EXTENSION
#include "../../util/godot/core/array.h"
Expand Down
13 changes: 5 additions & 8 deletions terrain/instancing/voxel_instance_library_multimesh_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const char *VoxelInstanceLibraryMultiMeshItem::SCENE_SETTINGS_GROUP_NAME = "Scen

namespace {

Array serialize_collision_shape_infos(const StdVector<VoxelInstanceLibraryMultiMeshItem::CollisionShapeInfo> &infos) {
Array serialize_collision_shape_infos(const StdVector<CollisionShapeInfo> &infos) {
Array a;
for (unsigned int i = 0; i < infos.size(); ++i) {
const VoxelInstanceLibraryMultiMeshItem::CollisionShapeInfo &info = infos[i];
const CollisionShapeInfo &info = infos[i];
ERR_FAIL_COND_V(info.shape.is_null(), Array());
// TODO Shape might or might not be shared, could have odd side-effects,
// but not sure how to properly fix these edge cases without convoluted code
Expand All @@ -27,14 +27,11 @@ Array serialize_collision_shape_infos(const StdVector<VoxelInstanceLibraryMultiM
return a;
}

bool deserialize_collision_shape_infos(
Array a,
StdVector<VoxelInstanceLibraryMultiMeshItem::CollisionShapeInfo> &out_infos
) {
bool deserialize_collision_shape_infos(Array a, StdVector<CollisionShapeInfo> &out_infos) {
ERR_FAIL_COND_V(a.size() % 2 != 0, false);

for (int i = 0; i < a.size(); i += 2) {
VoxelInstanceLibraryMultiMeshItem::CollisionShapeInfo info;
CollisionShapeInfo info;
info.shape = a[i];
info.transform = a[i + 1];

Expand Down Expand Up @@ -452,7 +449,7 @@ bool setup_from_template(Node *root, VoxelInstanceLibraryMultiMeshItem::Settings
CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(physics_body->get_child(i));

if (cs != nullptr) {
VoxelInstanceLibraryMultiMeshItem::CollisionShapeInfo info;
CollisionShapeInfo info;
info.shape = cs->get_shape();
info.transform = cs->get_transform();

Expand Down
54 changes: 29 additions & 25 deletions terrain/instancing/voxel_instance_library_multimesh_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,39 @@

namespace zylann::voxel {

struct CollisionShapeInfo {
Transform3D transform;
Ref<Shape3D> shape;
};

struct InstanceLibraryMultiMeshItemSettings {
static const int MAX_MESH_LODS = 4;

FixedArray<Ref<Mesh>, MAX_MESH_LODS> mesh_lods;
unsigned int mesh_lod_count = 1;
int render_layer = 1;

// It is preferred to have materials on the mesh already,
// but this is in case OBJ meshes are used, which often dont have a material of their own
Ref<Material> material_override;

RenderingServer::ShadowCastingSetting shadow_casting_setting = RenderingServer::SHADOW_CASTING_SETTING_ON;
GeometryInstance3D::GIMode gi_mode = GeometryInstance3D::GIMode::GI_MODE_STATIC;

int collision_mask = 1;
int collision_layer = 1;
StdVector<CollisionShapeInfo> collision_shapes;
// Groups that will be added to colliders if they use nodes
StdVector<StringName> group_names;
};

// Settings for a model that can be used by VoxelInstancer
class VoxelInstanceLibraryMultiMeshItem : public VoxelInstanceLibraryItem {
GDCLASS(VoxelInstanceLibraryMultiMeshItem, VoxelInstanceLibraryItem)
public:
static const int MAX_MESH_LODS = 4;
using Settings = InstanceLibraryMultiMeshItemSettings;

static const int MAX_MESH_LODS = Settings::MAX_MESH_LODS;
static const char *MANUAL_SETTINGS_GROUP_NAME;
static const char *SCENE_SETTINGS_GROUP_NAME;

Expand All @@ -26,11 +54,6 @@ class VoxelInstanceLibraryMultiMeshItem : public VoxelInstanceLibraryItem {
// which is square, so the circular area covered by mesh lods can actually extend a bit further if desired.
static constexpr float MAX_DISTANCE_RATIO = 2.f;

struct CollisionShapeInfo {
Transform3D transform;
Ref<Shape3D> shape;
};

VoxelInstanceLibraryMultiMeshItem();

void set_mesh(Ref<Mesh> mesh, int mesh_lod_index);
Expand Down Expand Up @@ -77,25 +100,6 @@ class VoxelInstanceLibraryMultiMeshItem : public VoxelInstanceLibraryItem {

// Internal

struct Settings {
FixedArray<Ref<Mesh>, MAX_MESH_LODS> mesh_lods;
unsigned int mesh_lod_count = 1;
int render_layer = 1;

// It is preferred to have materials on the mesh already,
// but this is in case OBJ meshes are used, which often dont have a material of their own
Ref<Material> material_override;

RenderingServer::ShadowCastingSetting shadow_casting_setting = RenderingServer::SHADOW_CASTING_SETTING_ON;
GeometryInstance3D::GIMode gi_mode = GeometryInstance3D::GIMode::GI_MODE_STATIC;

int collision_mask = 1;
int collision_layer = 1;
StdVector<CollisionShapeInfo> collision_shapes;
// Groups that will be added to colliders if they use nodes
StdVector<StringName> group_names;
};

// If a scene is assigned to the item, returns settings converted from it.
// If no scene is assigned, returns manual settings.
const Settings &get_multimesh_settings() const;
Expand Down
6 changes: 3 additions & 3 deletions terrain/instancing/voxel_instancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "load_instance_block_task.h"
#include "voxel_instance_component.h"
#include "voxel_instance_generator.h"
#include "voxel_instance_library_multimesh_item.h"
#include "voxel_instance_library_scene_item.h"
#include "voxel_instancer_quick_reloading_cache.h"
#include "voxel_instancer_rigidbody.h"
Expand Down Expand Up @@ -1289,8 +1290,7 @@ void VoxelInstancer::update_block_from_transforms( //
}

// Update bodies
Span<const VoxelInstanceLibraryMultiMeshItem::CollisionShapeInfo> collision_shapes =
to_span(settings.collision_shapes);
Span<const CollisionShapeInfo> collision_shapes = to_span(settings.collision_shapes);
if (collision_shapes.size() > 0) {
ZN_PROFILE_SCOPE_NAMED("Update multimesh bodies");

Expand Down Expand Up @@ -1323,7 +1323,7 @@ void VoxelInstancer::update_block_from_transforms( //
body->set_collision_mask(settings.collision_mask);

for (unsigned int i = 0; i < collision_shapes.size(); ++i) {
const VoxelInstanceLibraryMultiMeshItem::CollisionShapeInfo &shape_info = collision_shapes[i];
const CollisionShapeInfo &shape_info = collision_shapes[i];
CollisionShape3D *cs = memnew(CollisionShape3D);
cs->set_shape(shape_info.shape);
cs->set_transform(shape_info.transform);
Expand Down
5 changes: 3 additions & 2 deletions terrain/instancing/voxel_instancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "../../util/memory/memory.h"
#include "instance_library_item_listener.h"
#include "up_mode.h"
#include "voxel_instance_library_multimesh_item.h"

#ifdef TOOLS_ENABLED
#include "../../util/godot/core/version.h"
Expand All @@ -34,13 +33,15 @@ class VoxelNode;
class VoxelInstancerRigidBody;
class VoxelInstanceComponent;
class VoxelInstanceLibrary;
class VoxelInstanceLibraryItem;
class VoxelInstanceLibrarySceneItem;
class VoxelTool;
class SaveBlockDataTask;
class BufferedTaskScheduler;
struct InstanceBlockData;
struct VoxelInstancerQuickReloadingCache;
struct VoxelInstancerTaskOutputQueue;
struct InstanceLibraryMultiMeshItemSettings;

// Note: a large part of this node could be made generic to support the sole idea of instancing within octants?
// Even nodes like gridmaps could be rebuilt on top of this, if its concept of "grid" was decoupled.
Expand Down Expand Up @@ -220,7 +221,7 @@ class VoxelInstancer : public Node3D, public IInstanceLibraryItemListener {

static void update_mesh_from_mesh_lod(
Block &block,
const VoxelInstanceLibraryMultiMeshItem::Settings &settings,
const InstanceLibraryMultiMeshItemSettings &settings,
bool hide_beyond_max_lod,
bool instancer_is_visible
);
Expand Down
3 changes: 2 additions & 1 deletion terrain/variable_lod/voxel_lod_terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../../util/godot/classes/engine.h"
#include "../../util/godot/classes/mesh_instance_3d.h"
#include "../../util/godot/classes/node.h"
#include "../../util/godot/classes/packed_scene.h"
#include "../../util/godot/classes/resource_saver.h"
#include "../../util/godot/classes/scene_tree.h"
#include "../../util/godot/classes/script.h"
Expand Down Expand Up @@ -1232,7 +1233,7 @@ void VoxelLodTerrain::process(float delta) {
VoxelLodTerrainUpdateData &update_data = *_update_data;
update_data.viewers.clear();
VoxelEngine::get_singleton().for_each_viewer(
[&update_data](ViewerID id, const VoxelEngine::Viewer &viewer) {
[&update_data](ViewerID id, const VoxelEngine::Viewer &viewer) { //
update_data.viewers.push_back({ id, viewer });
}
);
Expand Down

0 comments on commit e04ce22

Please sign in to comment.