Ported the lod level updater code from broken seals.

This commit is contained in:
Relintai 2020-04-17 22:09:55 +02:00
parent 2d5b9ed029
commit 23d9acb5d4
3 changed files with 97 additions and 1 deletions

View File

@ -31,6 +31,51 @@ _FORCE_INLINE_ void VoxelWorldDefault::set_build_flags(const int flags) {
_build_flags = flags; _build_flags = flags;
} }
float VoxelWorldDefault::get_lod_update_interval() const {
return _lod_update_interval;
}
void VoxelWorldDefault::set_lod_update_interval(const float value) {
_lod_update_interval = value;
}
void VoxelWorldDefault::update_lods() {
call("_update_lods");
}
void VoxelWorldDefault::_update_lods() {
if (!get_player() || !ObjectDB::instance_validate(get_player())) {
return;
}
Vector3 ppos = get_player()->get_transform().origin;
int ppx = int(ppos.x / (get_chunk_size_x() * get_voxel_scale()));
int ppy = int(ppos.y / (get_chunk_size_y() * get_voxel_scale()));
int ppz = int(ppos.z / (get_chunk_size_z() * get_voxel_scale()));
for (int i = 0; i < get_chunk_count(); ++i) {
Ref<VoxelChunkDefault> c = get_chunk_index(i);
if (!c.is_valid())
continue;
int dx = abs(ppx - c->get_position_x());
int dy = abs(ppy - c->get_position_y());
int dz = abs(ppz - c->get_position_z());
int mr = MAX(MAX(dx, dy), dz);
if (mr <= 1)
c->set_current_lod_level(0);
else if (mr == 2)
c->set_current_lod_level(1);
else if (mr == 3) // || mr == 4)
c->set_current_lod_level(2);
else
c->set_current_lod_level(3);
}
}
Ref<VoxelChunk> VoxelWorldDefault::_create_chunk(int x, int y, int z, Ref<VoxelChunk> chunk) { Ref<VoxelChunk> VoxelWorldDefault::_create_chunk(int x, int y, int z, Ref<VoxelChunk> chunk) {
if (!chunk.is_valid()) { if (!chunk.is_valid()) {
@ -70,6 +115,8 @@ int VoxelWorldDefault::_get_channel_index_info(const VoxelWorld::ChannelTypeInfo
} }
VoxelWorldDefault::VoxelWorldDefault() { VoxelWorldDefault::VoxelWorldDefault() {
_lod_update_timer = 0;
_lod_update_interval = 0.5;
_build_flags = VoxelChunkDefault::BUILD_FLAG_CREATE_COLLIDER | VoxelChunkDefault::BUILD_FLAG_CREATE_LODS; _build_flags = VoxelChunkDefault::BUILD_FLAG_CREATE_COLLIDER | VoxelChunkDefault::BUILD_FLAG_CREATE_LODS;
set_data_margin_start(1); set_data_margin_start(1);
@ -79,10 +126,47 @@ VoxelWorldDefault::VoxelWorldDefault() {
VoxelWorldDefault ::~VoxelWorldDefault() { VoxelWorldDefault ::~VoxelWorldDefault() {
} }
void VoxelWorldDefault::_notification(int p_what) {
VoxelWorld::_notification(p_what);
switch (p_what) {
case NOTIFICATION_INTERNAL_PROCESS: {
if ((get_build_flags() & VoxelChunkDefault::BUILD_FLAG_CREATE_LODS) == 0)
return;
if (!get_player()) {
return;
}
if (!ObjectDB::instance_validate(get_player())) {
set_player(NULL);
return;
}
_lod_update_timer += get_process_delta_time();
if (_lod_update_timer >= _lod_update_interval) {
_lod_update_timer = 0;
update_lods();
}
} break;
}
}
void VoxelWorldDefault::_bind_methods() { void VoxelWorldDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("_chunk_added", "chunk"), &VoxelWorldDefault::_chunk_added); ClassDB::bind_method(D_METHOD("_chunk_added", "chunk"), &VoxelWorldDefault::_chunk_added);
ClassDB::bind_method(D_METHOD("get_build_flags"), &VoxelWorldDefault::get_build_flags); ClassDB::bind_method(D_METHOD("get_build_flags"), &VoxelWorldDefault::get_build_flags);
ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &VoxelWorldDefault::set_build_flags); ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &VoxelWorldDefault::set_build_flags);
ADD_PROPERTY(PropertyInfo(Variant::INT, "build_flags", PROPERTY_HINT_FLAGS, VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS), "set_build_flags", "get_build_flags"); ADD_PROPERTY(PropertyInfo(Variant::INT, "build_flags", PROPERTY_HINT_FLAGS, VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS), "set_build_flags", "get_build_flags");
ClassDB::bind_method(D_METHOD("get_lod_update_interval"), &VoxelWorldDefault::get_lod_update_interval);
ClassDB::bind_method(D_METHOD("set_lod_update_interval", "value"), &VoxelWorldDefault::set_lod_update_interval);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "lod_update_interval"), "set_lod_update_interval", "get_lod_update_interval");
BIND_VMETHOD(MethodInfo("_update_lods"));
ClassDB::bind_method(D_METHOD("update_lods"), &VoxelWorldDefault::update_lods);
ClassDB::bind_method(D_METHOD("_update_lods"), &VoxelWorldDefault::_update_lods);
} }

View File

@ -32,18 +32,28 @@ public:
int get_build_flags() const; int get_build_flags() const;
void set_build_flags(const int flags); void set_build_flags(const int flags);
float get_lod_update_interval() const;
void set_lod_update_interval(const float value);
void update_lods();
VoxelWorldDefault(); VoxelWorldDefault();
~VoxelWorldDefault(); ~VoxelWorldDefault();
protected: protected:
void _update_lods();
Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk); Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk);
virtual void _chunk_added(Ref<VoxelChunk> chunk); virtual void _chunk_added(Ref<VoxelChunk> chunk);
int _get_channel_index_info(const ChannelTypeInfo channel_type); int _get_channel_index_info(const ChannelTypeInfo channel_type);
virtual void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
private: private:
int _build_flags; int _build_flags;
float _lod_update_timer;
float _lod_update_interval;
}; };
#endif #endif

View File

@ -752,6 +752,8 @@ void VoxelWorld::_generate_chunk(Ref<VoxelChunk> chunk) {
void VoxelWorld::_notification(int p_what) { void VoxelWorld::_notification(int p_what) {
switch (p_what) { switch (p_what) {
case NOTIFICATION_ENTER_TREE: { case NOTIFICATION_ENTER_TREE: {
set_player_bind(get_node_or_null(get_player_path()));
set_process_internal(true); set_process_internal(true);
set_physics_process_internal(true); set_physics_process_internal(true);
set_notify_transform(true); set_notify_transform(true);
@ -926,7 +928,7 @@ void VoxelWorld::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_player"), &VoxelWorld::get_player); ClassDB::bind_method(D_METHOD("get_player"), &VoxelWorld::get_player);
ClassDB::bind_method(D_METHOD("set_player", "player"), &VoxelWorld::set_player_bind); ClassDB::bind_method(D_METHOD("set_player", "player"), &VoxelWorld::set_player_bind);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "player", PROPERTY_HINT_RESOURCE_TYPE, "Spatial"), "set_player", "get_player"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "player", PROPERTY_HINT_RESOURCE_TYPE, "Spatial", 0), "set_player", "get_player");
ClassDB::bind_method(D_METHOD("get_chunks"), &VoxelWorld::get_chunks); ClassDB::bind_method(D_METHOD("get_chunks"), &VoxelWorld::get_chunks);
ClassDB::bind_method(D_METHOD("set_chunks"), &VoxelWorld::set_chunks); ClassDB::bind_method(D_METHOD("set_chunks"), &VoxelWorld::set_chunks);