csg.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/list.h"
  33. #include "core/map.h"
  34. #include "core/math/aabb.h"
  35. #include "core/math/plane.h"
  36. #include "core/math/transform.h"
  37. #include "core/math/vector2.h"
  38. #include "core/math/vector3.h"
  39. #include "core/oa_hash_map.h"
  40. #include "core/pool_vector.h"
  41. #include "core/reference.h"
  42. #include "core/vector.h"
  43. #include "scene/resources/material.h"
  44. struct CSGBrush {
  45. struct Face {
  46. Vector3 vertices[3];
  47. Vector2 uvs[3];
  48. AABB aabb;
  49. bool smooth;
  50. bool invert;
  51. int material;
  52. };
  53. Vector<Face> faces;
  54. Vector<Ref<Material>> materials;
  55. inline void _regen_face_aabbs();
  56. // Create a brush from faces.
  57. 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);
  58. void copy_from(const CSGBrush &p_brush, const Transform &p_xform);
  59. };
  60. struct CSGBrushOperation {
  61. enum Operation {
  62. OPERATION_UNION,
  63. OPERATION_INTERSECTION,
  64. OPERATION_SUBTRACTION,
  65. };
  66. void merge_brushes(Operation p_operation, const CSGBrush &p_brush_a, const CSGBrush &p_brush_b, CSGBrush &r_merged_brush, float p_vertex_snap);
  67. struct MeshMerge {
  68. struct Face {
  69. bool from_b;
  70. bool inside;
  71. int points[3];
  72. Vector2 uvs[3];
  73. bool smooth;
  74. bool invert;
  75. int material_idx;
  76. };
  77. struct FaceBVH {
  78. int face;
  79. int left;
  80. int right;
  81. int next;
  82. Vector3 center;
  83. AABB aabb;
  84. };
  85. struct FaceBVHCmpX {
  86. _FORCE_INLINE_ bool operator()(const FaceBVH *p_left, const FaceBVH *p_right) const {
  87. return p_left->center.x < p_right->center.x;
  88. }
  89. };
  90. struct FaceBVHCmpY {
  91. _FORCE_INLINE_ bool operator()(const FaceBVH *p_left, const FaceBVH *p_right) const {
  92. return p_left->center.y < p_right->center.y;
  93. }
  94. };
  95. struct FaceBVHCmpZ {
  96. _FORCE_INLINE_ bool operator()(const FaceBVH *p_left, const FaceBVH *p_right) const {
  97. return p_left->center.z < p_right->center.z;
  98. }
  99. };
  100. struct VertexKey {
  101. int32_t x, y, z;
  102. _FORCE_INLINE_ bool operator<(const VertexKey &p_key) const {
  103. if (x == p_key.x) {
  104. if (y == p_key.y) {
  105. return z < p_key.z;
  106. } else {
  107. return y < p_key.y;
  108. }
  109. } else {
  110. return x < p_key.x;
  111. }
  112. }
  113. _FORCE_INLINE_ bool operator==(const VertexKey &p_key) const {
  114. return (x == p_key.x && y == p_key.y && z == p_key.z);
  115. }
  116. };
  117. struct VertexKeyHash {
  118. static _FORCE_INLINE_ uint32_t hash(const VertexKey &p_vk) {
  119. uint32_t h = hash_djb2_one_32(p_vk.x);
  120. h = hash_djb2_one_32(p_vk.y, h);
  121. h = hash_djb2_one_32(p_vk.z, h);
  122. return h;
  123. }
  124. };
  125. Vector<Vector3> points;
  126. Vector<Face> faces;
  127. Map<Ref<Material>, int> materials;
  128. Map<Vector3, int> vertex_map;
  129. OAHashMap<VertexKey, int, VertexKeyHash> snap_cache;
  130. float vertex_snap;
  131. inline void _add_distance(List<real_t> &r_intersectionsA, List<real_t> &r_intersectionsB, bool p_from_B, real_t p_distance) const;
  132. inline bool _bvh_inside(FaceBVH *facebvhptr, int p_max_depth, int p_bvh_first, int p_face_idx) const;
  133. inline int _create_bvh(FaceBVH *facebvhptr, FaceBVH **facebvhptrptr, int p_from, int p_size, int p_depth, int &r_max_depth, int &r_max_alloc);
  134. 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);
  135. void mark_inside_faces();
  136. };
  137. struct Build2DFaces {
  138. struct Vertex2D {
  139. Vector2 point;
  140. Vector2 uv;
  141. };
  142. struct Face2D {
  143. int vertex_idx[3] = {};
  144. };
  145. Vector<Vertex2D> vertices;
  146. Vector<Face2D> faces;
  147. Plane plane;
  148. Transform to_2D;
  149. Transform to_3D;
  150. float vertex_snap2 = 0.0;
  151. inline int _get_point_idx(const Vector2 &p_point);
  152. inline int _add_vertex(const Vertex2D &p_vertex);
  153. inline void _add_vertex_idx_sorted(Vector<int> &r_vertex_indices, int p_new_vertex_index);
  154. inline void _merge_faces(const Vector<int> &p_segment_indices);
  155. inline void _find_edge_intersections(const Vector2 p_segment_points[2], Vector<int> &r_segment_indices);
  156. inline int _insert_point(const Vector2 &p_point);
  157. void insert(const CSGBrush &p_brush, int p_brush_face);
  158. void addFacesToMesh(MeshMerge &r_mesh_merge, bool p_smooth, bool p_invert, const Ref<Material> &p_material, bool p_from_b);
  159. Build2DFaces() {}
  160. Build2DFaces(const CSGBrush &p_brush, int p_brush_face, float p_vertex_snap2);
  161. };
  162. struct Build2DFaceCollection {
  163. Map<int, Build2DFaces> build2DFacesA;
  164. Map<int, Build2DFaces> build2DFacesB;
  165. };
  166. 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);
  167. };
  168. #endif // CSG_H