Update; refactor of initial code

This commit is contained in:
Segey Lapin
2021-10-22 15:43:37 +03:00
parent da73a164a0
commit 2945dd1904
8 changed files with 397 additions and 151 deletions

View File

@@ -172,17 +172,20 @@ Ref<Curve3D> Roads::build_curve(Vector3 p1, Vector3 p2, Vector3 p3, float total_
return curve3;
}
Array Roads::curve_mesh(PoolVector<Vector3> points, float width, int flags, float sidewalk_sidth)
Array Roads::curve_mesh(PoolVector<Vector3> points, float width1, float width2, int flags, float sidewalk_width)
{
float tx = 0.0f, total_width = 0.0f, t = 0.0f, l;
float tx = 0.0f, total_width1 = 0.0f, total_width2 = 0.0f, t = 0.0f, l;
int i;
Array ret;
all_offsets();
PoolVector<String> parts_list = build_item_list(width, flags, sidewalk_sidth);
for (i = 0; i < parts_list.size(); i++)
total_width += offsets[parts_list[i]].x;
assert(total_width >= 3.0f);
Ref<Curve3D> curve3 = build_curve(points[0], points[1], points[2], total_width);
PoolVector<String> parts_list1 = build_item_list(width1, flags, sidewalk_width);
for (i = 0; i < parts_list1.size(); i++)
total_width1 += offsets[parts_list1[i]].x;
PoolVector<String> parts_list2 = build_item_list(width2, flags, sidewalk_width);
for (i = 0; i < parts_list2.size(); i++)
total_width2 += offsets[parts_list2[i]].x;
assert(total_width1 >= 3.0f && total_width2 >= 3.0f);
Ref<Curve3D> curve3 = build_curve(points[0], points[1], points[2], total_width2);
l = curve3->get_baked_length();
assert(l > 0.0f);
PoolVector<Vector3> new_verts, new_normals;
@@ -193,9 +196,9 @@ Array Roads::curve_mesh(PoolVector<Vector3> points, float width, int flags, floa
while (t <= l) {
tx = 0.0f;
int part = 0;
while (tx < total_width) {
while (tx < total_width2) {
int k;
Array data = mesh_data[parts_list[part]];
Array data = mesh_data[parts_list2[part]];
int b = new_verts.size();
PoolVector<Vector3> verts = data[Mesh::ARRAY_VERTEX];
PoolVector<Vector3> normals = data[Mesh::ARRAY_NORMAL];
@@ -226,7 +229,7 @@ Array Roads::curve_mesh(PoolVector<Vector3> points, float width, int flags, floa
Vector3 n = xform.xform(normals[k]);
if (right < 0.15f &&
((flags & FLAGS_INTERSECTION) != 0) &&
nvert.distance_squared_to(points[1]) < total_width * total_width * 2.5f) {
nvert.distance_squared_to(points[1]) < total_width2 * total_width2 * 2.5f) {
new_verts.write()[b + k] = points[1];
new_normals.write()[b + k] = Vector3(0.0f, 1.0f, 0.0f);
} else {
@@ -240,9 +243,9 @@ Array Roads::curve_mesh(PoolVector<Vector3> points, float width, int flags, floa
new_index.resize(idx + index.size());
for (k = 0; k < index.size(); k++)
new_index.write()[idx + k] = index[k] + b;
tx += offsets[parts_list[part]].x;
tx += offsets[parts_list2[part]].x;
part += 1;
if (part >= parts_list.size())
if (part >= parts_list2.size())
break;
}
t += 2.0f;
@@ -258,6 +261,7 @@ Array Roads::curve_mesh(PoolVector<Vector3> points, float width, int flags, floa
static Ref<ConcavePolygonShape> create_concave_polygon_shape(Vector<Array> surfaces) {
PoolVector<Vector3> face_points;
int face_points_size = 0;
assert(surfaces.size() > 0);
//find the correct size for face_points
for (int i = 0; i < surfaces.size(); i++) {
@@ -268,12 +272,14 @@ static Ref<ConcavePolygonShape> create_concave_polygon_shape(Vector<Array> surfa
}
// If the surface is not empty then it must have an expected amount of data arrays
ERR_CONTINUE(surface_arrays.size() != Mesh::ARRAY_MAX);
assert(surface_arrays.size() == Mesh::ARRAY_MAX);
PoolVector<int> indices = surface_arrays[Mesh::ARRAY_INDEX];
face_points_size += indices.size();
}
face_points.resize(face_points_size);
if (face_points_size < 3) {
assert(0);
return Ref<ConcavePolygonShape>();
}
@@ -287,6 +293,10 @@ static Ref<ConcavePolygonShape> create_concave_polygon_shape(Vector<Array> surfa
PoolVector<Vector3> positions = surface_arrays[Mesh::ARRAY_VERTEX];
PoolVector<int> indices = surface_arrays[Mesh::ARRAY_INDEX];
assert(positions.size() >= 3);
assert(indices.size() >= 3);
assert(indices.size() % 3 == 0);
ERR_FAIL_COND_V(positions.size() < 3, Ref<ConcavePolygonShape>());
ERR_FAIL_COND_V(indices.size() < 3, Ref<ConcavePolygonShape>());
ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ConcavePolygonShape>());
@@ -312,7 +322,7 @@ static Ref<ConcavePolygonShape> create_concave_polygon_shape(Vector<Array> surfa
}
int Roads::make_vmesh(Node *root, Ref<Material> mat, Ref<ArrayMesh> mesh, MeshInstance *xmi, Vector3 p1, Vector3 p2,
Vector3 p3, float width, int flags, float sidewalk_width)
Vector3 p3, float width1, float width2, int flags, float sidewalk_width)
{
Vector3 m1 = p1 - p2;
Vector3 m2 = Vector3();
@@ -329,7 +339,7 @@ int Roads::make_vmesh(Node *root, Ref<Material> mat, Ref<ArrayMesh> mesh, MeshIn
points.resize(3);
for (i = 0; i < 3; i++)
points.write()[i] = pts[i].snapped(Vector3(4.0f, 0.1f, 4.0f));
Array rdata = curve_mesh(points, width, flags, sidewalk_width);
Array rdata = curve_mesh(points, width1, width2, flags, sidewalk_width);
Ref<ArrayMesh> mdata = mesh;
assert(mdata.is_valid());
mdata->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, rdata);
@@ -352,9 +362,25 @@ void Roads::add_scene_element(Node *root, Node *xnode, const Vector3 &p2, Ref<Co
Transform xform(Basis(), p2);
assert(xmi->get_mesh().is_valid() && xmi->get_mesh()->get_surface_count() > 0);
xmi->set_global_transform(xform);
printf("trimesh collision\n");
#if 0
Node *c = xmi->create_trimesh_collision();
assert(c);
add_child(c);
#if 0
CollisionShape *cs = memnew(CollisionShape);
cs->set_shape(shape);
body->add_child(cs);
#endif
#endif
StaticBody *sb = memnew(StaticBody);
CollisionShape *cs = memnew(CollisionShape);
assert(sb);
assert(cs);
sb->add_child(cs);
assert(!shape.is_null());
cs->set_shape(shape);
xmi->call_deferred("add_child", sb);
}
void Roads::process_vshapes()
@@ -368,7 +394,6 @@ void Roads::process_vshapes()
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)
break;
@@ -387,7 +412,6 @@ void Roads::process_vshapes()
active_vshapes.push_back(i);
#endif
}
printf("active vshapes %d\n", active_vshapes.size());
List<int>::Element *e;
for (e = active_vshapes.front(); e; e = e->next()) {
i = e->get();
@@ -438,8 +462,10 @@ void Roads::generate_threaded(void *p_userdata)
Vector3 p1 = vshapes[data->vshape].p1;
Vector3 p2 = vshapes[data->vshape].p2;
Vector3 p3 = vshapes[data->vshape].p3;
float width1 = vshapes[data->vshape].depth1;
float width2 = vshapes[data->vshape].depth2;
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);
int instance = obj->make_vmesh(obj, data->mat, data->mesh, data->xmi, p1, p2, p3, width1, width2, data->flags, data->sidewalk_width);
assert(instance >= 0);
obj->mutex.lock();
rg->get_vshapes().write()[data->vshape].instance = instance;
@@ -484,6 +510,7 @@ void RoadsData::_bind_methods()
ClassDB::bind_method(D_METHOD("get_site_polygon_3d", "site"), &RoadsData::get_site_polygon_3d);
ClassDB::bind_method(D_METHOD("get_here_sites", "site"), &RoadsData::get_here_sites);
ClassDB::bind_method(D_METHOD("get_site_avg_height", "site"), &RoadsData::get_site_avg_height);
ClassDB::bind_method(D_METHOD("save_json", "path"), &RoadsData::save_json);
}
void RoadsData::set_noise(Ref<FastNoiseLite> noise)
{