Skip to content

feat region streaming #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ void Terrain3D::__physics_process(const double p_delta) {
_grab_camera();
}

// If camera has moved enough, re-center the terrain on it.
if (is_instance_valid(_camera_instance_id) && _camera->is_inside_tree()) {
Vector3 cam_pos = _camera->get_global_position();
Vector2 cam_pos_2d = Vector2(cam_pos.x, cam_pos.z);
RS->material_set_param(_material->get_material_rid(), "_camera_pos", cam_pos);

if (_camera_last_position.distance_to(cam_pos_2d) > 0.2f) {
if (_mesher) {
_mesher->snap(cam_pos);
Expand All @@ -132,6 +132,20 @@ void Terrain3D::__physics_process(const double p_delta) {
_collision->update();
}
}

if (_data) {
_data->_process_streaming();
}

if (_enable_streaming && _data) {
static double stream_timer = 0.0;
stream_timer += p_delta;

if (stream_timer >= 0.1) {
stream_timer = 0.0;
_data->update_streaming(cam_pos);
}
}
}
}

Expand Down Expand Up @@ -798,6 +812,47 @@ void Terrain3D::set_warning(const uint8_t p_warning, const bool p_enabled) {
update_configuration_warnings();
}

void Terrain3D::set_enable_streaming(const bool p_enabled) {
LOG(INFO, "Setting enable streaming: ", p_enabled);
_enable_streaming = p_enabled;
if (_data) {
_data->set_enable_streaming(p_enabled);
}
}

bool Terrain3D::get_enable_streaming() const {
return _enable_streaming;
}

void Terrain3D::set_streaming_rings(const int p_rings) {
if (!_data) {
return;
}

int rings = CLAMP(p_rings, 1, 15);
_streaming_rings = rings;
_data->set_streaming_rings(rings);
}

int Terrain3D::get_streaming_rings() const {
return _streaming_rings;
}


void Terrain3D::set_streaming_distance(const real_t p_distance) {

if (!_data) {
return;
}

int rings = CLAMP(int(p_distance / 250.0f), 1, 20);
set_streaming_rings(rings);
}

real_t Terrain3D::get_streaming_distance() const {
return _streaming_rings * 250.0f;
}

PackedStringArray Terrain3D::_get_configuration_warnings() const {
PackedStringArray psa;
if (_data_directory.is_empty()) {
Expand Down Expand Up @@ -1015,6 +1070,10 @@ void Terrain3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_label_distance"), &Terrain3D::get_label_distance);
ClassDB::bind_method(D_METHOD("set_label_size", "size"), &Terrain3D::set_label_size);
ClassDB::bind_method(D_METHOD("get_label_size"), &Terrain3D::get_label_size);
ClassDB::bind_method(D_METHOD("set_enable_streaming", "enabled"), &Terrain3D::set_enable_streaming);
ClassDB::bind_method(D_METHOD("get_enable_streaming"), &Terrain3D::get_enable_streaming);
ClassDB::bind_method(D_METHOD("set_streaming_rings", "rings"), &Terrain3D::set_streaming_rings);
ClassDB::bind_method(D_METHOD("get_streaming_rings"), &Terrain3D::get_streaming_rings);

// Collision
ClassDB::bind_method(D_METHOD("set_collision_mode", "mode"), &Terrain3D::set_collision_mode);
Expand Down Expand Up @@ -1115,6 +1174,8 @@ void Terrain3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "label_distance", PROPERTY_HINT_RANGE, "0.0,10000.0,0.5,or_greater"), "set_label_distance", "get_label_distance");
ADD_PROPERTY(PropertyInfo(Variant::INT, "label_size", PROPERTY_HINT_RANGE, "24,128,1"), "set_label_size", "get_label_size");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid"), "set_show_region_grid", "get_show_region_grid");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_streaming"), "set_enable_streaming", "get_enable_streaming");
ADD_PROPERTY(PropertyInfo(Variant::INT, "streaming_rings", PROPERTY_HINT_RANGE, "1,15,1"), "set_streaming_rings", "get_streaming_rings");

ADD_GROUP("Collision", "");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mode", PROPERTY_HINT_ENUM, "Disabled,Dynamic / Game,Dynamic / Editor,Full / Game,Full / Editor"), "set_collision_mode", "get_collision_mode");
Expand Down Expand Up @@ -1160,4 +1221,7 @@ void Terrain3D::_bind_methods() {

ADD_SIGNAL(MethodInfo("material_changed"));
ADD_SIGNAL(MethodInfo("assets_changed"));

ClassDB::bind_method(D_METHOD("set_streaming_distance", "distance"), &Terrain3D::set_streaming_distance);
ClassDB::bind_method(D_METHOD("get_streaming_distance"), &Terrain3D::get_streaming_distance);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No distance property has been exposed.

}
16 changes: 15 additions & 1 deletion src/terrain_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Terrain3D : public Node3D {
};

private:
String _version = "1.1.0-dev";
String _version = "1.0.0";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't have been changed.

String _data_directory;
bool _is_inside_world = false;
bool _initialized = false;
Expand Down Expand Up @@ -78,6 +78,10 @@ class Terrain3D : public Node3D {
real_t _cull_margin = 0.0f;
bool _free_editor_textures = true;

// Region Streaming
bool _enable_streaming = false;
int _streaming_rings = 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need these in Terrain3D and Terrain3DData. You can have the alias in the first and the variable in the second. See collision_mode for instance.


// Mouse cursor
SubViewport *_mouse_vp = nullptr;
Camera3D *_mouse_cam = nullptr;
Expand Down Expand Up @@ -176,6 +180,16 @@ class Terrain3D : public Node3D {
void set_show_instances(const bool p_visible) { _mmi_parent ? _mmi_parent->set_visible(p_visible) : void(); }
bool get_show_instances() const { return _mmi_parent ? _mmi_parent->is_visible() : false; }

// Region Streaming
void set_enable_streaming(const bool p_enabled);
bool get_enable_streaming() const;
void set_streaming_rings(const int p_rings);
int get_streaming_rings() const;

// Backward compatibility
void set_streaming_distance(const real_t p_distance);
real_t get_streaming_distance() const;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is newly created, why is it already backwards compatible?


// Utility
Vector3 get_intersection(const Vector3 &p_src_pos, const Vector3 &p_direction, const bool p_gpu_mode = false);
Ref<Mesh> bake_mesh(const int p_lod, const Terrain3DData::HeightFilter p_filter = Terrain3DData::HEIGHT_FILTER_NEAREST) const;
Expand Down
Loading