Add lod falloff property to VoxelWorldDefault. Also improved _update_lods().

This commit is contained in:
Relintai 2020-08-03 19:05:40 +02:00
parent 768728139e
commit b95f055acd
3 changed files with 28 additions and 11 deletions

View File

@ -1645,6 +1645,8 @@ void VoxelChunkDefault::_build_phase(int phase) {
} }
#endif #endif
set_current_lod_level(get_current_lod_level());
call_deferred("update_transforms"); call_deferred("update_transforms");
next_phase(); next_phase();

View File

@ -53,6 +53,13 @@ void VoxelWorldDefault::update_lods() {
call("_update_lods"); call("_update_lods");
} }
int VoxelWorldDefault::get_chunk_lod_falloff() const {
return _chunk_lod_falloff;
}
void VoxelWorldDefault::set_chunk_lod_falloff(const int value) {
_chunk_lod_falloff = value;
}
void VoxelWorldDefault::_update_lods() { void VoxelWorldDefault::_update_lods() {
if (!get_player() || !INSTANCE_VALIDATE(get_player())) { if (!get_player() || !INSTANCE_VALIDATE(get_player())) {
return; return;
@ -60,9 +67,9 @@ void VoxelWorldDefault::_update_lods() {
Vector3 ppos = get_player()->get_transform().origin; Vector3 ppos = get_player()->get_transform().origin;
int ppx = int(ppos.x / (get_chunk_size_x() * get_voxel_scale())); 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 ppy = int(ppos.y / get_chunk_size_y() / get_voxel_scale());
int ppz = int(ppos.z / (get_chunk_size_z() * get_voxel_scale())); int ppz = int(ppos.z / get_chunk_size_z() / get_voxel_scale());
for (int i = 0; i < get_chunk_count(); ++i) { for (int i = 0; i < get_chunk_count(); ++i) {
Ref<VoxelChunkDefault> c = get_chunk_index(i); Ref<VoxelChunkDefault> c = get_chunk_index(i);
@ -76,14 +83,13 @@ void VoxelWorldDefault::_update_lods() {
int mr = MAX(MAX(dx, dy), dz); int mr = MAX(MAX(dx, dy), dz);
if (mr <= 1) mr -= _chunk_lod_falloff;
c->set_current_lod_level(0);
else if (mr == 2) //Todo 3 should be _num_lod, but it's NYI, because chunk can only handle 3 lod levels for now -> FQMS needs to be fixed
c->set_current_lod_level(1); mr = CLAMP(mr, 0, 3);
else if (mr == 3) // || mr == 4)
c->set_current_lod_level(2); if (c->get_current_lod_level() != mr)
else c->set_current_lod_level(mr);
c->set_current_lod_level(3);
} }
} }
@ -123,6 +129,7 @@ int VoxelWorldDefault::_get_channel_index_info(const VoxelWorld::ChannelTypeInfo
} }
VoxelWorldDefault::VoxelWorldDefault() { VoxelWorldDefault::VoxelWorldDefault() {
_chunk_lod_falloff = 2;
_lod_update_timer = 0; _lod_update_timer = 0;
_lod_update_interval = 0.5; _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;
@ -174,6 +181,10 @@ void VoxelWorldDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_lod_update_interval", "value"), &VoxelWorldDefault::set_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"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "lod_update_interval"), "set_lod_update_interval", "get_lod_update_interval");
ClassDB::bind_method(D_METHOD("get_chunk_lod_falloff"), &VoxelWorldDefault::get_chunk_lod_falloff);
ClassDB::bind_method(D_METHOD("set_chunk_lod_falloff", "value"), &VoxelWorldDefault::set_chunk_lod_falloff);
ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_lod_falloff"), "set_chunk_lod_falloff", "get_chunk_lod_falloff");
BIND_VMETHOD(MethodInfo("_update_lods")); 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);
ClassDB::bind_method(D_METHOD("_update_lods"), &VoxelWorldDefault::_update_lods); ClassDB::bind_method(D_METHOD("_update_lods"), &VoxelWorldDefault::_update_lods);

View File

@ -35,6 +35,9 @@ public:
float get_lod_update_interval() const; float get_lod_update_interval() const;
void set_lod_update_interval(const float value); void set_lod_update_interval(const float value);
int get_chunk_lod_falloff() const;
void set_chunk_lod_falloff(const int value);
void update_lods(); void update_lods();
VoxelWorldDefault(); VoxelWorldDefault();
@ -54,6 +57,7 @@ private:
int _build_flags; int _build_flags;
float _lod_update_timer; float _lod_update_timer;
float _lod_update_interval; float _lod_update_interval;
int _chunk_lod_falloff;
}; };
#endif #endif