csg.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* csg.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/math/aabb.h"
  33. #include "core/math/plane.h"
  34. #include "core/math/transform_3d.h"
  35. #include "core/math/vector2.h"
  36. #include "core/math/vector3.h"
  37. #include "core/object/ref_counted.h"
  38. #include "core/templates/list.h"
  39. #include "core/templates/oa_hash_map.h"
  40. #include "core/templates/vector.h"
  41. #include "scene/resources/material.h"
  42. struct CSGBrush {
  43. struct Face {
  44. Vector3 vertices[3];
  45. Vector2 uvs[3];
  46. AABB aabb;
  47. bool smooth = false;
  48. bool invert = false;
  49. int material = 0;
  50. };
  51. Vector<Face> faces;
  52. Vector<Ref<Material>> materials;
  53. inline void _regen_face_aabbs();
  54. // Create a brush from faces.
  55. void build_from_faces(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uvs, const Vector<bool> &p_smooth, const Vector<Ref<Material>> &p_materials, const Vector<bool> &p_invert_faces);
  56. void copy_from(const CSGBrush &p_brush, const Transform3D &p_xform);
  57. };
  58. struct CSGBrushOperation {
  59. enum Operation {
  60. OPERATION_UNION,
  61. OPERATION_INTERSECTION,
  62. OPERATION_SUBTRACTION,
  63. };
  64. void merge_brushes(Operation p_operation, const CSGBrush &p_brush_a, const CSGBrush &p_brush_b, CSGBrush &r_merged_brush, float p_vertex_snap);
  65. struct MeshMerge {
  66. struct Face {
  67. bool from_b = false;
  68. bool inside = false;
  69. int points[3] = {};
  70. Vector2 uvs[3];
  71. bool smooth = false;
  72. bool invert = false;
  73. int material_idx = 0;
  74. };
  75. struct FaceBVH {
  76. int face = 0;
  77. int left = 0;
  78. int right = 0;
  79. int next = 0;
  80. Vector3 center;
  81. AABB aabb;
  82. };
  83. struct FaceBVHCmpX {
  84. _FORCE_INLINE_ bool operator()(const FaceBVH *p_left, const FaceBVH *p_right) const {
  85. return p_left->center.x < p_right->center.x;
  86. }
  87. };
  88. struct FaceBVHCmpY {
  89. _FORCE_INLINE_ bool operator()(const FaceBVH *p_left, const FaceBVH *p_right) const {
  90. return p_left->center.y < p_right->center.y;
  91. }
  92. };
  93. struct FaceBVHCmpZ {
  94. _FORCE_INLINE_ bool operator()(const FaceBVH *p_left, const FaceBVH *p_right) const {
  95. return p_left->center.z < p_right->center.z;
  96. }
  97. };
  98. struct VertexKey {
  99. int32_t x, y, z;
  100. _FORCE_INLINE_ bool operator<(const VertexKey &p_key) const {
  101. if (x == p_key.x) {
  102. if (y == p_key.y) {
  103. return z < p_key.z;
  104. } else {
  105. return y < p_key.y;
  106. }
  107. } else {
  108. return x < p_key.x;
  109. }
  110. }
  111. _FORCE_INLINE_ bool operator==(const VertexKey &p_key) const {
  112. return (x == p_key.x && y == p_key.y && z == p_key.z);
  113. }
  114. };
  115. struct VertexKeyHash {
  116. static _FORCE_INLINE_ uint32_t hash(const VertexKey &p_vk) {
  117. uint32_t h = hash_murmur3_one_32(p_vk.x);
  118. h = hash_murmur3_one_32(p_vk.y, h);
  119. h = hash_murmur3_one_32(p_vk.z, h);
  120. return h;
  121. }
  122. };
  123. struct Intersection {
  124. bool found = false;
  125. real_t conormal = FLT_MAX;
  126. real_t distance_squared = FLT_MAX;
  127. real_t origin_angle = FLT_MAX;
  128. };
  129. struct IntersectionDistance {
  130. bool is_conormal;
  131. real_t distance_squared;
  132. };
  133. Vector<Vector3> points;
  134. Vector<Face> faces;
  135. HashMap<Ref<Material>, int> materials;
  136. HashMap<Vector3, int> vertex_map;
  137. OAHashMap<VertexKey, int, VertexKeyHash> snap_cache;
  138. float vertex_snap = 0.0;
  139. inline void _add_distance(List<IntersectionDistance> &r_intersectionsA, List<IntersectionDistance> &r_intersectionsB, bool p_from_B, real_t p_distance, bool p_is_conormal) const;
  140. inline bool _bvh_inside(FaceBVH *r_facebvhptr, int p_max_depth, int p_bvh_first, int p_face_idx) const;
  141. inline int _create_bvh(FaceBVH *r_facebvhptr, FaceBVH **r_facebvhptrptr, int p_from, int p_size, int p_depth, int &r_max_depth, int &r_max_alloc);
  142. void add_face(const Vector3 p_points[3], const Vector2 p_uvs[3], bool p_smooth, bool p_invert, const Ref<Material> &p_material, bool p_from_b);
  143. void mark_inside_faces();
  144. };
  145. struct Build2DFaces {
  146. struct Vertex2D {
  147. Vector2 point;
  148. Vector2 uv;
  149. };
  150. struct Face2D {
  151. int vertex_idx[3] = {};
  152. };
  153. Vector<Vertex2D> vertices;
  154. Vector<Face2D> faces;
  155. Plane plane;
  156. Transform3D to_2D;
  157. Transform3D to_3D;
  158. float vertex_snap2 = 0.0;
  159. inline int _get_point_idx(const Vector2 &p_point);
  160. inline int _add_vertex(const Vertex2D &p_vertex);
  161. inline void _add_vertex_idx_sorted(Vector<int> &r_vertex_indices, int p_new_vertex_index);
  162. inline void _merge_faces(const Vector<int> &p_segment_indices);
  163. inline void _find_edge_intersections(const Vector2 p_segment_points[2], Vector<int> &r_segment_indices);
  164. inline int _insert_point(const Vector2 &p_point);
  165. void insert(const CSGBrush &p_brush, int p_brush_face);
  166. void addFacesToMesh(MeshMerge &r_mesh_merge, bool p_smooth, bool p_invert, const Ref<Material> &p_material, bool p_from_b);
  167. Build2DFaces() {}
  168. Build2DFaces(const CSGBrush &p_brush, int p_brush_face, float p_vertex_snap2);
  169. };
  170. struct Build2DFaceCollection {
  171. HashMap<int, Build2DFaces> build2DFacesA;
  172. HashMap<int, Build2DFaces> build2DFacesB;
  173. };
  174. void update_faces(const CSGBrush &p_brush_a, const int p_face_idx_a, const CSGBrush &p_brush_b, const int p_face_idx_b, Build2DFaceCollection &p_collection, float p_vertex_snap);
  175. };
  176. #endif // CSG_H