Refactored optimized
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <cassert>
|
||||
#include <core/resource.h>
|
||||
#include <core/os/os.h>
|
||||
#include <core/sort_array.h>
|
||||
#include <scene/resources/packed_scene.h>
|
||||
#include <scene/main/viewport.h>
|
||||
@@ -17,7 +18,7 @@ void Roads::_get_property_list(List<PropertyInfo> *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<int> &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, Ref<Co
|
||||
|
||||
void Roads::process_vshapes()
|
||||
{
|
||||
Transform xform = get_viewport()->get_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<int> 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<OpenSimplexNoise> noise)
|
||||
void RoadsData::set_noise(Ref<FastNoiseLite> 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;
|
||||
}
|
||||
return y - n;
|
||||
sdf_mutex.unlock();
|
||||
goto out;
|
||||
}
|
||||
ret = y - n;
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ class Roads: public MeshInstance {
|
||||
protected:
|
||||
Mutex mutex;
|
||||
Ref<Curve> curve;
|
||||
Ref<OpenSimplexNoise> noise;
|
||||
Ref<FastNoiseLite> noise;
|
||||
Ref<Material> mat;
|
||||
Ref<PackedScene> road_data;
|
||||
HashMap<String, Array> 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> curve;
|
||||
Ref<OpenSimplexNoise> noise;
|
||||
Ref<FastNoiseLite> noise;
|
||||
Mutex sdf_mutex;
|
||||
HashMap<int, float> 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<OpenSimplexNoise> noise);
|
||||
void set_noise(Ref<FastNoiseLite> noise);
|
||||
void set_curve(Ref<Curve> curve);
|
||||
float get_sdf(int x, int y, int z);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user