From ea4c8a5731c66d55f0cf522534cfc237ee5f9854 Mon Sep 17 00:00:00 2001 From: Segey Lapin Date: Fri, 15 Oct 2021 16:18:30 +0300 Subject: [PATCH] Refactored optimized --- modules/world/roads.cpp | 52 +++++++++++++++++++++-------------------- modules/world/roads.h | 10 ++++---- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/modules/world/roads.cpp b/modules/world/roads.cpp index 280660a..f23c5e7 100644 --- a/modules/world/roads.cpp +++ b/modules/world/roads.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -17,7 +18,7 @@ void Roads::_get_property_list(List *p_list) const { p_list->push_back(PropertyInfo(Variant::OBJECT, "road_data", PROPERTY_HINT_RESOURCE_TYPE, "PackedScene")); p_list->push_back(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve")); - p_list->push_back(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise")); + p_list->push_back(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "FastNoiseLite")); } bool Roads::_get(const StringName &p_name, Variant &r_ret) const { @@ -69,17 +70,6 @@ void Roads::sort_angle(Vector &sort_data) sorter.compare.vertices = vertices.write().ptr(); sorter.sort(sort_data.ptrw(), sort_data.size()); } -int Roads::find_edge(int a, int b) -{ - int i; - RoadGrid *rg = RoadsData::get_singleton()->get_road_grid(); - for (i = 0; i < rg->map_hedges.size(); i++) { - if (rg->map_hedges[i]->a == a && - rg->map_hedges[i]->b == b) - return i; - } - return -1; -} void Roads::setup_vshapes() { int i, j; @@ -203,9 +193,8 @@ void Roads::update_all() RoadsData::get_singleton()->set_curve(curve); rg->build(curve, noise); } - printf("vertices: %d\n", rg->diagram_vertices.size()); - printf("heights: %d\n", rg->diagram_vertex_heights.size()); - printf("edges: %d\n", rg->map_hedges.size()); + printf("vertices: %d\n", rg->get_diagram_vertex_count()); + printf("edges: %d\n", rg->get_map_hedges_count()); setup_vshapes(); } } @@ -485,13 +474,14 @@ void Roads::add_scene_element(Node *root, Node *xnode, const Vector3 &p2, Refget_camera()->get_global_transform(); + if (get_viewport() && get_viewport()->get_camera()) + cam_xform = get_viewport()->get_camera()->get_global_transform(); AABB camarea; - camarea.position = xform.origin; + camarea.position = cam_xform.origin; camarea.grow_by(550.0f); int i; List active_vshapes; - printf("camera %f %f %f\n", xform.origin.x, xform.origin.y, xform.origin.z); + printf("camera %f %f %f\n", cam_xform.origin.x, cam_xform.origin.y, cam_xform.origin.z); for (i = 0; i < vshapes.size(); i++) { if (active_vshapes.size() > 32) break; @@ -540,7 +530,7 @@ void Roads::_notification(int p_what) { switch(p_what) { case NOTIFICATION_PROCESS: - if ((counter % 100) == 0) + if ((counter % 60) == 0) process_vshapes(); counter++; break; @@ -601,7 +591,7 @@ void RoadsData::_bind_methods() ClassDB::bind_method(D_METHOD("get_road_grid"), &RoadsData::get_road_grid); ClassDB::bind_method(D_METHOD("get_sdf", "x", "y", "z"), &RoadsData::get_sdf); } -void RoadsData::set_noise(Ref noise) +void RoadsData::set_noise(Ref noise) { this->noise = noise; } @@ -613,15 +603,27 @@ float RoadsData::get_sdf(int x, int y, int z) { if (!curve.is_valid() || !noise.is_valid()) return (float)y; + if (sdf_data.has(x * 50000 + z)) + return (float)y - sdf_data[x * 50000 + z]; + float ret; float n = curve->interpolate_baked(0.5f + noise->get_noise_2d(x, z) * 0.5f); n = CLAMP(n, -1000.0f, 1000.0f); + /* this is for height value; for caves/tunnels other logic is needed */ Vector2 ifl = rg->get_influence(x, z, 32.0f); if (ifl.x > 0.0f) { - if (n <= ifl.y - 0.5f) - return (float)y - ifl.y - 0.6f; - else - return (float)y - ifl.y; + sdf_mutex.lock(); + if (n <= ifl.y - 0.5f) { + ret = (float)y - ifl.y - 0.6f; + sdf_data[x * 50000 + z] = ifl.y + 0.6f; + } else { + ret = (float)y - ifl.y; + sdf_data[x * 50000 + z] = ifl.y; + } + sdf_mutex.unlock(); + goto out; } - return y - n; + ret = y - n; +out: + return ret; } diff --git a/modules/world/roads.h b/modules/world/roads.h index 1b71393..f4f88ec 100644 --- a/modules/world/roads.h +++ b/modules/world/roads.h @@ -12,7 +12,7 @@ class Roads: public MeshInstance { protected: Mutex mutex; Ref curve; - Ref noise; + Ref noise; Ref mat; Ref road_data; HashMap mesh_data; @@ -37,6 +37,7 @@ protected: struct thread_data thread; static void generate_threaded(void *p_userdata); StaticBody *body; + Transform cam_xform; #if 0 struct edge_data { int a; @@ -65,7 +66,6 @@ protected: void extrude_direct(Array &out, const Array &arrays, const struct edge_data *data) const; void extrude_vshape(Array &out, const Array &arrays, const struct vshape *data) const; #endif - int find_edge(int a, int b); void _notification(int p_what); int counter; friend class RoadsData; @@ -94,7 +94,9 @@ protected: RoadGrid *rg; static void _bind_methods(); Ref curve; - Ref noise; + Ref noise; + Mutex sdf_mutex; + HashMap sdf_data; public: RoadsData(); ~RoadsData(); @@ -102,7 +104,7 @@ public: static void create_singleton(); static void destroy_singleton(); RoadGrid *get_road_grid() const; - void set_noise(Ref noise); + void set_noise(Ref noise); void set_curve(Ref curve); float get_sdf(int x, int y, int z); };