| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/face3.h" |
| 34 |
#include "core/object/ref_counted.h" |
| 35 |
|
| 36 |
class TriangleMesh : public RefCounted { |
| 37 |
GDCLASS(TriangleMesh, RefCounted); |
| 38 |
|
| 39 |
public: |
| 40 |
struct Triangle { |
| 41 |
Vector3 normal; |
| 42 |
int indices[3]; |
| 43 |
int32_t surface_index = 0; |
| 44 |
}; |
| 45 |
|
| 46 |
protected: |
| 47 |
static void _bind_methods(); |
| 48 |
|
| 49 |
private: |
| 50 |
Vector<Triangle> triangles; |
| 51 |
Vector<Vector3> vertices; |
| 52 |
|
| 53 |
struct BVH { |
| 54 |
AABB aabb; |
| 55 |
Vector3 center; |
| 56 |
int left = -1; |
| 57 |
int right = -1; |
| 58 |
|
| 59 |
int32_t face_index = -1; |
| 60 |
}; |
| 61 |
|
| 62 |
struct BVHCmpX { |
| 63 |
bool operator()(const BVH *p_left, const BVH *p_right) const { |
| 64 |
return p_left->center.x < p_right->center.x; |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
struct BVHCmpY { |
| 69 |
bool operator()(const BVH *p_left, const BVH *p_right) const { |
| 70 |
return p_left->center.y < p_right->center.y; |
| 71 |
} |
| 72 |
}; |
| 73 |
struct BVHCmpZ { |
| 74 |
bool operator()(const BVH *p_left, const BVH *p_right) const { |
| 75 |
return p_left->center.z < p_right->center.z; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
int _create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc); |
| 80 |
|
| 81 |
Vector<BVH> bvh; |
| 82 |
int max_depth = 0; |
| 83 |
bool valid = false; |
| 84 |
|
| 85 |
public: |
| 86 |
bool is_valid() const; |
| 87 |
bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr, int32_t *r_face_index = nullptr) const; |
| 88 |
bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr, int32_t *r_face_index = nullptr) const; |
| 89 |
bool inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale = Vector3(1, 1, 1)) const; |
| 90 |
Vector<Face3> get_faces() const; |
| 91 |
|
| 92 |
const Vector<Triangle> &get_triangles() const { return triangles; } |
| 93 |
const Vector<Vector3> &get_vertices() const { return vertices; } |
| 94 |
void get_indices(Vector<int> *r_triangles_indices) const; |
| 95 |
|
| 96 |
void create(const Vector<Vector3> &p_faces, const Vector<int32_t> &p_surface_indices = Vector<int32_t>()); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
bool create_from_faces(const Vector<Vector3> &p_faces); |
| 101 |
Dictionary intersect_segment_scriptwrap(const Vector3 &p_begin, const Vector3 &p_end) const; |
| 102 |
Dictionary intersect_ray_scriptwrap(const Vector3 &p_begin, const Vector3 &p_dir) const; |
| 103 |
Vector<Vector3> get_faces_scriptwrap() const; |
| 104 |
|
| 105 |
TriangleMesh(); |
| 106 |
}; |