Optimized, updated

This commit is contained in:
Segey Lapin
2021-10-15 18:22:02 +03:00
parent ea4c8a5731
commit ccee4fb686
5 changed files with 230 additions and 166 deletions

View File

@@ -54,121 +54,6 @@ bool Roads::_set(const StringName &p_name, const Variant &p_value)
return update;
}
void Roads::sort_angle(Vector<int> &sort_data)
{
struct comparator {
Vector3 *vertices;
bool operator()(int a, int b) const {
Vector3 p1 = vertices[a];
Vector3 p2 = vertices[b];
Vector2 rp1(p1.x, p1.z);
Vector2 rp2(p2.x, p2.z);
return rp1.angle() < rp2.angle();
}
};
SortArray<int, struct comparator> sorter;
sorter.compare.vertices = vertices.write().ptr();
sorter.sort(sort_data.ptrw(), sort_data.size());
}
void Roads::setup_vshapes()
{
int i, j;
RoadGrid *rg = RoadsData::get_singleton()->get_road_grid();
vertices.resize(rg->diagram_vertices.size());
for (i = 0; i < vertices.size(); i++) {
vertices.write()[i].x = rg->diagram_vertices[i].x;
vertices.write()[i].y = rg->diagram_vertex_heights[i];
vertices.write()[i].z = rg->diagram_vertices[i].y;
}
List<struct vshape> vdata_list;
for (i = 0; i < rg->map_hedges.size(); i++) {
for (j = 0; j < rg->map_hedges.size(); j++) {
if (i == j)
continue;
if (rg->map_hedges[i]->b !=
rg->map_hedges[j]->a)
continue;
if (rg->map_hedges[i]->site !=
rg->map_hedges[j]->site)
continue;
int a, b1, b2;
struct vshape v;
/* star topology */
a = rg->map_hedges[i]->b;
b1 = rg->map_hedges[i]->a;
b2 = rg->map_hedges[j]->b;
v.e1 = i;
v.e2 = j;
v.site = rg->map_hedges[i]->site;
v.area.position = vertices[a];
v.area.expand_to(vertices[b1] + Vector3(0, 1, 0));
v.area.expand_to(vertices[b2] + Vector3(0, -1, 0));
v.instance = -1;
Vector3 p1 = vertices[rg->map_hedges[v.e1]->a];
Vector3 p2 = vertices[rg->map_hedges[v.e1]->b];
Vector3 p3 = vertices[rg->map_hedges[v.e2]->b];
p1 = (p2 + (p1 - p2) * 0.5f).snapped(Vector3(4.0f, 0.1f, 4.0f));
p3 = (p2 + (p3 - p2) * 0.5f).snapped(Vector3(4.0f, 0.1f, 4.0f));
p2 = p2.snapped(Vector3(4.0f, 0.1f, 4.0f));
v.p1 = p1;
v.p2 = p2;
v.p3 = p3;
/* add v-shape only if we can actually generate it */
if (v.p1.distance_squared_to(v.p2) > 2.0f &&
v.p2.distance_squared_to(v.p3) > 2.0f &&
v.p1.distance_squared_to(v.p3) > 2.0f)
vdata_list.push_back(v);
}
}
vshapes.resize(vdata_list.size());
for (i = 0; i < vdata_list.size(); i++)
vshapes.write()[i] = vdata_list[i];
for (i = 0; i < vshapes.size(); i++) {
for (j = 0; j < vshapes.size(); j++) {
if (i == j)
continue;
if (vshapes[i].e1 == vshapes[j].e1)
vshapes.write()[j].p1 = vshapes[i].p1;
if (vshapes[i].e2 == vshapes[j].e1)
vshapes.write()[j].p1 = vshapes[i].p3;
if (vshapes[i].e1 == vshapes[j].e2)
vshapes.write()[j].p3 = vshapes[i].p1;
if (vshapes[i].e2 == vshapes[j].e2)
vshapes.write()[j].p3 = vshapes[i].p3;
}
}
for (i = 0; i < vshapes.size(); i++) {
const struct vshape &v = vshapes[i];
assert(rg->map_hedges[v.e1]->site == rg->map_hedges[v.e2]->site);
assert(v.e1 >= 0 && v.e2 >= 0 && v.e1 != v.e2);
int e1a = rg->map_hedges[vshapes[i].e1]->a;
int e1b = rg->map_hedges[vshapes[i].e1]->b;
int e2a = rg->map_hedges[vshapes[i].e2]->a;
int e2b = rg->map_hedges[vshapes[i].e2]->b;
printf("vshape %d: %d: %d: %f %f %f -> %d: %f %f %f -> %d: %d: %f %f %f -> %d: %f %f %f\n",
i,
vshapes[i].e1,
e1a,
vertices[e1a].x,
vertices[e1a].y,
vertices[e1a].z,
e1b,
vertices[e1b].x,
vertices[e1b].y,
vertices[e1b].z,
vshapes[i].e2,
e2a,
vertices[e2a].x,
vertices[e2a].y,
vertices[e2a].z,
e2b,
vertices[e2b].x,
vertices[e2b].y,
vertices[e2b].z
);
}
}
void Roads::update_all()
{
int i;
@@ -195,7 +80,7 @@ void Roads::update_all()
}
printf("vertices: %d\n", rg->get_diagram_vertex_count());
printf("edges: %d\n", rg->get_map_hedges_count());
setup_vshapes();
rg->setup_vshapes();
}
}
@@ -476,11 +361,13 @@ void Roads::process_vshapes()
{
if (get_viewport() && get_viewport()->get_camera())
cam_xform = get_viewport()->get_camera()->get_global_transform();
RoadGrid *rg = RoadsData::get_singleton()->get_road_grid();
AABB camarea;
camarea.position = cam_xform.origin;
camarea.grow_by(550.0f);
int i;
List<int> active_vshapes;
const PoolVector<struct RoadGrid::vshape> &vshapes = rg->get_vshapes();
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)
@@ -504,7 +391,7 @@ void Roads::process_vshapes()
List<int>::Element *e;
for (e = active_vshapes.front(); e; e = e->next()) {
i = e->get();
const struct vshape &v = vshapes[i];
const struct RoadGrid::vshape &v = vshapes[i];
assert(v.p1.distance_squared_to(v.p2) > 2.0f);
assert(v.p2.distance_squared_to(v.p3) > 2.0f);
assert(v.p1.distance_squared_to(v.p3) > 2.0f);
@@ -546,14 +433,16 @@ void Roads::generate_threaded(void *p_userdata)
struct thread_data *data = (struct thread_data *)p_userdata;
Roads *obj = Object::cast_to<Roads>(data->root);
obj->mutex.lock();
Vector3 p1 = obj->vshapes[data->vshape].p1;
Vector3 p2 = obj->vshapes[data->vshape].p2;
Vector3 p3 = obj->vshapes[data->vshape].p3;
RoadGrid *rg = RoadsData::get_singleton()->get_road_grid();
const PoolVector<RoadGrid::vshape> &vshapes = rg->get_vshapes();
Vector3 p1 = vshapes[data->vshape].p1;
Vector3 p2 = vshapes[data->vshape].p2;
Vector3 p3 = vshapes[data->vshape].p3;
obj->mutex.unlock();
int instance = obj->make_vmesh(obj, data->mat, data->mesh, data->xmi, p1, p2, p3, data->width, data->flags, data->sidewalk_width);
assert(instance >= 0);
obj->mutex.lock();
obj->vshapes.write()[data->vshape].instance = instance;
rg->get_vshapes().write()[data->vshape].instance = instance;
obj->mutex.unlock();
}