519 lines
15 KiB
C++
519 lines
15 KiB
C++
#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>
|
|
#include <scene/3d/camera.h>
|
|
#include <scene/3d/physics_body.h>
|
|
#include <scene/3d/collision_shape.h>
|
|
#include "roads.h"
|
|
|
|
void Roads::_bind_methods()
|
|
{
|
|
ClassDB::bind_method(D_METHOD("curve_mesh", "points", "width", "flags", "sidewalk_width"), &Roads::curve_mesh);
|
|
ClassDB::bind_method(D_METHOD("add_scene_element", "root", "surface", "p2", "shape"), &Roads::add_scene_element);
|
|
}
|
|
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, "FastNoiseLite"));
|
|
}
|
|
bool Roads::_get(const StringName &p_name, Variant &r_ret) const
|
|
{
|
|
if (p_name == "road_data") {
|
|
r_ret = road_data;
|
|
return true;
|
|
} else if (p_name == "curve") {
|
|
r_ret = curve;
|
|
return true;
|
|
} else if (p_name == "noise") {
|
|
r_ret = noise;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool Roads::_set(const StringName &p_name, const Variant &p_value)
|
|
{
|
|
bool update = false;
|
|
if (p_name == "road_data") {
|
|
road_data = p_value;
|
|
update = true;
|
|
} else if (p_name == "curve") {
|
|
curve = p_value;
|
|
update = true;
|
|
} else if (p_name == "noise") {
|
|
noise = p_value;
|
|
update = true;
|
|
}
|
|
if (update) {
|
|
update_all();
|
|
_change_notify();
|
|
}
|
|
|
|
return update;
|
|
}
|
|
void Roads::update_all()
|
|
{
|
|
int i;
|
|
RoadGrid *rg = RoadsData::get_singleton()->get_road_grid();
|
|
if (road_data.is_valid()) {
|
|
Node *tmp = road_data->instance();
|
|
for (i = 0; i < tmp->get_child_count(); i++) {
|
|
Node *c = tmp->get_child(i);
|
|
MeshInstance *mi = Object::cast_to<MeshInstance>(c);
|
|
if (mi) {
|
|
String name = mi->get_name();
|
|
Ref<ArrayMesh> mesh = mi->get_mesh();
|
|
Array data = mesh->surface_get_arrays(0);
|
|
mesh_data[name] = data;
|
|
if (name == "road_main")
|
|
mat = mesh->surface_get_material(0);
|
|
}
|
|
}
|
|
if (curve.is_valid() && noise.is_valid()) {
|
|
curve->bake();
|
|
RoadsData::get_singleton()->set_noise(noise);
|
|
RoadsData::get_singleton()->set_curve(curve);
|
|
rg->build(curve, noise);
|
|
}
|
|
printf("vertices: %d\n", rg->get_diagram_vertex_count());
|
|
printf("edges: %d\n", rg->get_map_hedges_count());
|
|
rg->setup_vshapes();
|
|
}
|
|
}
|
|
|
|
Roads::Roads()
|
|
{
|
|
body = memnew(StaticBody);
|
|
}
|
|
Roads::~Roads()
|
|
{
|
|
memdelete(body);
|
|
}
|
|
Vector3 Roads::calculate_offsets(const Array &data) const
|
|
{
|
|
float minx = 0.0f, maxx = 0.0f, minz = 0.0f, maxz = 0.0f;
|
|
PoolVector<Vector3> verts = data[Mesh::ARRAY_VERTEX];
|
|
int i;
|
|
Vector3 ret;
|
|
for (i = 0; i < verts.size(); i++) {
|
|
if (minx > verts.read()[i].x)
|
|
minx = verts.read()[i].x;
|
|
if (maxx < verts.read()[i].x)
|
|
maxx = verts.read()[i].x;
|
|
if (minz > verts.read()[i].z)
|
|
minz = verts.read()[i].z;
|
|
if (maxz < verts.read()[i].z)
|
|
maxz = verts.read()[i].z;
|
|
}
|
|
ret.x = fabsf(maxx - minx);
|
|
ret.z = fabsf(maxz - minz);
|
|
return ret;
|
|
}
|
|
Vector3 Roads::quadratic_bezier(const Vector3 &p0, const Vector3 &p1,
|
|
const Vector3 &p2, float t) const
|
|
{
|
|
Vector3 q0 = p0.linear_interpolate(p1, t);
|
|
Vector3 q1 = p1.linear_interpolate(p2, t);
|
|
Vector3 r = q0.linear_interpolate(q1, t);
|
|
return r;
|
|
}
|
|
void Roads::all_offsets()
|
|
{
|
|
const String *k;
|
|
for (k = mesh_data.next(NULL); k; k = mesh_data.next(k))
|
|
offsets[*k] = calculate_offsets(mesh_data[*k]);
|
|
}
|
|
enum {
|
|
FLAGS_SIDEWALK = (1 << 0),
|
|
FLAGS_INTERSECTION = (1 << 1),
|
|
FLAGS_WALL = (1 << 2),
|
|
};
|
|
|
|
PoolVector<String> Roads::build_item_list(float width, int flags, float sidewalk_width) const
|
|
{
|
|
PoolVector<String> ret;
|
|
float tx = 0.0f;
|
|
while (tx < width) {
|
|
ret.push_back("road_main");
|
|
tx += offsets["road_main"].x;
|
|
}
|
|
if (flags & FLAGS_SIDEWALK) {
|
|
ret.push_back("sidewalk_start");
|
|
tx = 0.0f;
|
|
while (tx < sidewalk_width) {
|
|
ret.push_back("sidewalk");
|
|
tx += offsets["sidewalk"].x;
|
|
}
|
|
ret.push_back("sidewalk_end");
|
|
}
|
|
if (flags & FLAGS_WALL)
|
|
ret.push_back("wall");
|
|
return ret;
|
|
}
|
|
Ref<Curve3D> Roads::build_curve(Vector3 p1, Vector3 p2, Vector3 p3, float total_width) const
|
|
{
|
|
Ref<Curve3D> curve3;
|
|
curve3.instance();
|
|
Vector3 dir1 = (p1 - p2).normalized();
|
|
Vector3 dir2 = (p3 - p2).normalized();
|
|
float qt = 0.0f;
|
|
curve3->add_point(p1);
|
|
while (qt <= 1.0f) {
|
|
Vector3 p = quadratic_bezier(p2 + dir1 * total_width * 1.8f, p2, p2 + dir2 * total_width * 1.8f, qt);
|
|
curve3->add_point(p.snapped(Vector3(0.1, 0.1, 0.1)));
|
|
qt += 0.1f;
|
|
}
|
|
curve3->add_point(p3);
|
|
curve3->set_bake_interval(4.0f);
|
|
assert(curve3->get_baked_length() > 0);
|
|
return curve3;
|
|
}
|
|
|
|
Array Roads::curve_mesh(PoolVector<Vector3> points, float width, int flags, float sidewalk_sidth)
|
|
{
|
|
float tx = 0.0f, total_width = 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);
|
|
l = curve3->get_baked_length();
|
|
assert(l > 0.0f);
|
|
PoolVector<Vector3> new_verts, new_normals;
|
|
PoolVector<Vector2> new_uvs;
|
|
PoolVector<int> new_index;
|
|
ret.resize(Mesh::ARRAY_MAX);
|
|
|
|
while (t <= l) {
|
|
tx = 0.0f;
|
|
int part = 0;
|
|
while (tx < total_width) {
|
|
int k;
|
|
Array data = mesh_data[parts_list[part]];
|
|
int b = new_verts.size();
|
|
PoolVector<Vector3> verts = data[Mesh::ARRAY_VERTEX];
|
|
PoolVector<Vector3> normals = data[Mesh::ARRAY_NORMAL];
|
|
new_verts.resize(b + verts.size());
|
|
new_normals.resize(b + normals.size());
|
|
Transform xform;
|
|
Vector3 offt1, offt2;
|
|
for (k = 0; k < verts.size(); k++) {
|
|
Vector3 base = verts[k];
|
|
float point = t + 2.0 + base.z;
|
|
float right = verts[k].x + tx;
|
|
if (t <= l - 2.0) {
|
|
offt1 = curve3->interpolate_baked(point, true);
|
|
offt2 = curve3->interpolate_baked(point + 2.0, true);
|
|
assert(offt1.distance_squared_to(offt2) > 0.0f);
|
|
xform = Transform(Basis(), offt1).looking_at(offt2, Vector3(0.0f, 1.0f, 0.0f));
|
|
} else {
|
|
offt1 = curve3->interpolate_baked(point - 2.0, true);
|
|
offt2 = curve3->interpolate_baked(point, true);
|
|
assert(offt1.distance_squared_to(offt2) > 0.0f);
|
|
xform = Transform(Basis(), offt1).looking_at(offt2, Vector3(0.0f, 1.0f, 0.0f));
|
|
offt1 = offt2;
|
|
}
|
|
xform.origin = Vector3();
|
|
if (right < 0.25f)
|
|
right -= 0.15f;
|
|
Vector3 nvert = offt1 + xform.xform(Vector3(right, verts[k].y, 0.0));
|
|
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) {
|
|
new_verts.write()[b + k] = points[1];
|
|
new_normals.write()[b + k] = Vector3(0.0f, 1.0f, 0.0f);
|
|
} else {
|
|
new_verts.write()[b + k] = nvert;
|
|
new_normals.write()[b + k] = n;
|
|
}
|
|
}
|
|
new_uvs.append_array(data[Mesh::ARRAY_TEX_UV]);
|
|
int idx = new_index.size();
|
|
PoolVector<int> index = data[Mesh::ARRAY_INDEX];
|
|
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;
|
|
part += 1;
|
|
if (part >= parts_list.size())
|
|
break;
|
|
}
|
|
t += 2.0f;
|
|
}
|
|
ret[Mesh::ARRAY_VERTEX] = new_verts;
|
|
ret[Mesh::ARRAY_NORMAL] = new_normals;
|
|
ret[Mesh::ARRAY_TEX_UV] = new_uvs;
|
|
ret[Mesh::ARRAY_INDEX] = new_index;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static Ref<ConcavePolygonShape> create_concave_polygon_shape(Vector<Array> surfaces) {
|
|
PoolVector<Vector3> face_points;
|
|
int face_points_size = 0;
|
|
|
|
//find the correct size for face_points
|
|
for (int i = 0; i < surfaces.size(); i++) {
|
|
const Array &surface_arrays = surfaces[i];
|
|
if (surface_arrays.size() == 0) {
|
|
// That surface is empty
|
|
continue;
|
|
}
|
|
// If the surface is not empty then it must have an expected amount of data arrays
|
|
ERR_CONTINUE(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) {
|
|
return Ref<ConcavePolygonShape>();
|
|
}
|
|
|
|
//copy the points into it
|
|
int face_points_offset = 0;
|
|
for (int i = 0; i < surfaces.size(); i++) {
|
|
const Array &surface_arrays = surfaces[i];
|
|
if (surface_arrays.size() == 0) {
|
|
continue;
|
|
}
|
|
PoolVector<Vector3> positions = surface_arrays[Mesh::ARRAY_VERTEX];
|
|
PoolVector<int> indices = surface_arrays[Mesh::ARRAY_INDEX];
|
|
|
|
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>());
|
|
|
|
int face_points_count = face_points_offset + indices.size();
|
|
|
|
{
|
|
PoolVector<Vector3>::Write w = face_points.write();
|
|
PoolVector<int>::Read index_r = indices.read();
|
|
PoolVector<Vector3>::Read position_r = positions.read();
|
|
|
|
for (int p = face_points_offset; p < face_points_count; ++p) {
|
|
w[p] = position_r[index_r[p - face_points_offset]];
|
|
}
|
|
}
|
|
|
|
face_points_offset += indices.size();
|
|
}
|
|
|
|
Ref<ConcavePolygonShape> shape = memnew(ConcavePolygonShape);
|
|
shape->set_faces(face_points);
|
|
return shape;
|
|
}
|
|
|
|
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 m1 = p1 - p2;
|
|
Vector3 m2 = Vector3();
|
|
Vector3 m3 = p3 - p2;
|
|
Vector3 pts[] = {m1, m2, m3};
|
|
int i;
|
|
PoolVector<Vector3> points;
|
|
assert(p1.distance_squared_to(p2) > 2.0f);
|
|
assert(p2.distance_squared_to(p3) > 2.0f);
|
|
assert(p1.distance_squared_to(p3) > 2.0f);
|
|
assert(m1.distance_squared_to(m2) > 2.0f);
|
|
assert(m2.distance_squared_to(m3) > 2.0f);
|
|
assert(m1.distance_squared_to(m3) > 2.0f);
|
|
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);
|
|
Ref<ArrayMesh> mdata = mesh;
|
|
assert(mdata.is_valid());
|
|
mdata->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, rdata);
|
|
assert(mdata->get_surface_count() > 0);
|
|
Vector<Array> surfaces;
|
|
surfaces.push_back(rdata);
|
|
Ref<ConcavePolygonShape> shape = create_concave_polygon_shape(surfaces);
|
|
mdata->surface_set_material(0, mat);
|
|
xmi->set_mesh(mdata);
|
|
call_deferred("add_scene_element", root, xmi, p2, shape);
|
|
return xmi->get_instance_id();
|
|
}
|
|
|
|
void Roads::add_scene_element(Node *root, Node *xnode, const Vector3 &p2, Ref<ConcavePolygonShape> shape)
|
|
{
|
|
MeshInstance *xmi = Object::cast_to<MeshInstance>(xnode);
|
|
if (!xmi)
|
|
return;
|
|
root->add_child(xmi);
|
|
Transform xform(Basis(), p2);
|
|
assert(xmi->get_mesh().is_valid() && xmi->get_mesh()->get_surface_count() > 0);
|
|
xmi->set_global_transform(xform);
|
|
CollisionShape *cs = memnew(CollisionShape);
|
|
cs->set_shape(shape);
|
|
body->add_child(cs);
|
|
}
|
|
|
|
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)
|
|
break;
|
|
if (vshapes[i].instance >= 0)
|
|
continue;
|
|
#if 0
|
|
active_vshapes.push_back(i);
|
|
#else
|
|
if (vshapes[i].area.intersects(camarea))
|
|
active_vshapes.push_back(i);
|
|
else if (vshapes[i].area.encloses(camarea))
|
|
active_vshapes.push_back(i);
|
|
else if (camarea.encloses(vshapes[i].area))
|
|
active_vshapes.push_back(i);
|
|
else if (camarea.intersects(vshapes[i].area))
|
|
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();
|
|
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);
|
|
if (vshapes[i].instance < 0) {
|
|
if (thread.thread.is_started())
|
|
thread.thread.wait_to_finish();
|
|
thread.mat = mat;
|
|
thread.vshape = i;
|
|
thread.width = 6.0;
|
|
thread.sidewalk_width = 3.0;
|
|
thread.flags = FLAGS_SIDEWALK|FLAGS_INTERSECTION;
|
|
thread.root = this;
|
|
Ref<ArrayMesh> mesh;
|
|
mesh.instance();
|
|
thread.mesh = mesh;
|
|
thread.xmi = memnew(MeshInstance);
|
|
thread.thread.start(generate_threaded, &thread);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Roads::_notification(int p_what)
|
|
{
|
|
switch(p_what) {
|
|
case NOTIFICATION_PROCESS:
|
|
if ((counter % 60) == 0)
|
|
process_vshapes();
|
|
counter++;
|
|
break;
|
|
case NOTIFICATION_READY:
|
|
counter = 0;
|
|
set_process(true);
|
|
add_child(body);
|
|
break;
|
|
}
|
|
}
|
|
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();
|
|
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();
|
|
rg->get_vshapes().write()[data->vshape].instance = instance;
|
|
obj->mutex.unlock();
|
|
}
|
|
|
|
|
|
RoadsData::RoadsData()
|
|
{
|
|
rg = memnew(RoadGrid);
|
|
}
|
|
RoadsData::~RoadsData()
|
|
{
|
|
memdelete(rg);
|
|
rg = NULL;
|
|
}
|
|
static RoadsData *g_roads_data = NULL;
|
|
RoadsData* RoadsData::get_singleton()
|
|
{
|
|
return g_roads_data;
|
|
}
|
|
|
|
void RoadsData::create_singleton()
|
|
{
|
|
g_roads_data = memnew(RoadsData);
|
|
}
|
|
void RoadsData::destroy_singleton()
|
|
{
|
|
memdelete(g_roads_data);
|
|
g_roads_data = NULL;
|
|
}
|
|
RoadGrid *RoadsData::get_road_grid() const
|
|
{
|
|
return rg;
|
|
}
|
|
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<FastNoiseLite> noise)
|
|
{
|
|
this->noise = noise;
|
|
}
|
|
void RoadsData::set_curve(Ref<Curve> curve)
|
|
{
|
|
this->curve = curve;
|
|
}
|
|
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) {
|
|
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;
|
|
}
|
|
ret = y - n;
|
|
out:
|
|
return ret;
|
|
}
|
|
|