csg.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*************************************************************************/
  2. /* csg.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef CSG_H
  31. #define CSG_H
  32. #include "core/dvector.h"
  33. #include "core/map.h"
  34. #include "core/math/aabb.h"
  35. #include "core/math/plane.h"
  36. #include "core/math/rect2.h"
  37. #include "core/math/transform.h"
  38. #include "core/math/vector3.h"
  39. #include "core/oa_hash_map.h"
  40. #include "scene/resources/material.h"
  41. struct CSGBrush {
  42. struct Face {
  43. Vector3 vertices[3];
  44. Vector2 uvs[3];
  45. AABB aabb;
  46. bool smooth;
  47. bool invert;
  48. int material;
  49. };
  50. Vector<Face> faces;
  51. Vector<Ref<Material> > materials;
  52. void _regen_face_aabbs();
  53. //create a brush from faces
  54. void build_from_faces(const PoolVector<Vector3> &p_vertices, const PoolVector<Vector2> &p_uvs, const PoolVector<bool> &p_smooth, const PoolVector<Ref<Material> > &p_materials, const PoolVector<bool> &p_invert_faces);
  55. void copy_from(const CSGBrush &p_brush, const Transform &p_xform);
  56. void clear();
  57. };
  58. struct CSGBrushOperation {
  59. enum Operation {
  60. OPERATION_UNION,
  61. OPERATION_INTERSECTION,
  62. OPERATION_SUBSTRACTION,
  63. };
  64. struct MeshMerge {
  65. struct BVH {
  66. int face;
  67. int left;
  68. int right;
  69. int next;
  70. Vector3 center;
  71. AABB aabb;
  72. };
  73. struct BVHCmpX {
  74. bool operator()(const BVH *p_left, const BVH *p_right) const {
  75. return p_left->center.x < p_right->center.x;
  76. }
  77. };
  78. struct BVHCmpY {
  79. bool operator()(const BVH *p_left, const BVH *p_right) const {
  80. return p_left->center.y < p_right->center.y;
  81. }
  82. };
  83. struct BVHCmpZ {
  84. bool operator()(const BVH *p_left, const BVH *p_right) const {
  85. return p_left->center.z < p_right->center.z;
  86. }
  87. };
  88. int _bvh_count_intersections(BVH *bvhptr, int p_max_depth, int p_bvh_first, const Vector3 &p_begin, const Vector3 &p_end, int p_exclude) const;
  89. int _create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc);
  90. struct VertexKey {
  91. int32_t x, y, z;
  92. _FORCE_INLINE_ bool operator<(const VertexKey &p_key) const {
  93. if (x == p_key.x) {
  94. if (y == p_key.y) {
  95. return z < p_key.z;
  96. } else {
  97. return y < p_key.y;
  98. }
  99. } else {
  100. return x < p_key.x;
  101. }
  102. }
  103. _FORCE_INLINE_ bool operator==(const VertexKey &p_key) const {
  104. return (x == p_key.x && y == p_key.y && z == p_key.z);
  105. }
  106. };
  107. struct VertexKeyHash {
  108. static _FORCE_INLINE_ uint32_t hash(const VertexKey &p_vk) {
  109. uint32_t h = hash_djb2_one_32(p_vk.x);
  110. h = hash_djb2_one_32(p_vk.y, h);
  111. h = hash_djb2_one_32(p_vk.z, h);
  112. return h;
  113. }
  114. };
  115. OAHashMap<VertexKey, int, VertexKeyHash> snap_cache;
  116. Vector<Vector3> points;
  117. struct Face {
  118. bool from_b;
  119. bool inside;
  120. int points[3];
  121. Vector2 uvs[3];
  122. bool smooth;
  123. bool invert;
  124. int material_idx;
  125. };
  126. Vector<Face> faces;
  127. Map<Ref<Material>, int> materials;
  128. Map<Vector3, int> vertex_map;
  129. void add_face(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, const Vector2 &p_uv_a, const Vector2 &p_uv_b, const Vector2 &p_uv_c, bool p_smooth, bool p_invert, const Ref<Material> &p_material, bool p_from_b);
  130. // void add_face(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, bool p_from_b);
  131. float vertex_snap;
  132. void mark_inside_faces();
  133. };
  134. struct BuildPoly {
  135. Plane plane;
  136. Transform to_poly;
  137. Transform to_world;
  138. int face_index;
  139. struct Point {
  140. Vector2 point;
  141. Vector2 uv;
  142. };
  143. Vector<Point> points;
  144. struct Edge {
  145. bool outer;
  146. int points[2];
  147. Edge() {
  148. outer = false;
  149. }
  150. };
  151. Vector<Edge> edges;
  152. Ref<Material> material;
  153. bool smooth;
  154. bool invert;
  155. int base_edges; //edges from original triangle, even if split
  156. void _clip_segment(const CSGBrush *p_brush, int p_face, const Vector2 *segment, MeshMerge &mesh_merge, bool p_for_B);
  157. void create(const CSGBrush *p_brush, int p_face, MeshMerge &mesh_merge, bool p_for_B);
  158. void clip(const CSGBrush *p_brush, int p_face, MeshMerge &mesh_merge, bool p_for_B);
  159. };
  160. struct PolyPoints {
  161. Vector<int> points;
  162. Vector<Vector<int> > holes;
  163. };
  164. struct EdgeSort {
  165. int edge;
  166. int prev_point;
  167. int edge_point;
  168. float angle;
  169. bool operator<(const EdgeSort &p_edge) const { return angle < p_edge.angle; }
  170. };
  171. struct CallbackData {
  172. const CSGBrush *A;
  173. const CSGBrush *B;
  174. int face_a;
  175. CSGBrushOperation *self;
  176. Map<int, BuildPoly> build_polys_A;
  177. Map<int, BuildPoly> build_polys_B;
  178. };
  179. void _add_poly_points(const BuildPoly &p_poly, int p_edge, int p_from_point, int p_to_point, const Vector<Vector<int> > &vertex_process, Vector<bool> &edge_process, Vector<PolyPoints> &r_poly);
  180. void _add_poly_outline(const BuildPoly &p_poly, int p_from_point, int p_to_point, const Vector<Vector<int> > &vertex_process, Vector<int> &r_outline);
  181. void _merge_poly(MeshMerge &mesh, int p_face_idx, const BuildPoly &p_poly, bool p_from_b);
  182. void _collision_callback(const CSGBrush *A, int p_face_a, Map<int, BuildPoly> &build_polys_a, const CSGBrush *B, int p_face_b, Map<int, BuildPoly> &build_polys_b, MeshMerge &mesh_merge);
  183. static void _collision_callbacks(void *ud, int p_face_b);
  184. void merge_brushes(Operation p_operation, const CSGBrush &p_A, const CSGBrush &p_B, CSGBrush &result, float p_snap = 0.001);
  185. };
  186. #endif // CSG_H