csg.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* csg.cpp */
  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. #include "csg.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/templates/sort_array.h"
  34. #include "scene/resources/mesh_data_tool.h"
  35. #include "scene/resources/surface_tool.h"
  36. #include "thirdparty/manifold/include/manifold/manifold.h"
  37. // CSGBrush
  38. void CSGBrush::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_flip_faces) {
  39. faces.clear();
  40. int vc = p_vertices.size();
  41. ERR_FAIL_COND((vc % 3) != 0);
  42. const Vector3 *rv = p_vertices.ptr();
  43. int uvc = p_uvs.size();
  44. const Vector2 *ruv = p_uvs.ptr();
  45. int sc = p_smooth.size();
  46. const bool *rs = p_smooth.ptr();
  47. int mc = p_materials.size();
  48. const Ref<Material> *rm = p_materials.ptr();
  49. int ic = p_flip_faces.size();
  50. const bool *ri = p_flip_faces.ptr();
  51. HashMap<Ref<Material>, int> material_map;
  52. faces.resize(p_vertices.size() / 3);
  53. for (int i = 0; i < faces.size(); i++) {
  54. Face &f = faces.write[i];
  55. f.vertices[0] = rv[i * 3 + 0];
  56. f.vertices[1] = rv[i * 3 + 1];
  57. f.vertices[2] = rv[i * 3 + 2];
  58. if (uvc == vc) {
  59. f.uvs[0] = ruv[i * 3 + 0];
  60. f.uvs[1] = ruv[i * 3 + 1];
  61. f.uvs[2] = ruv[i * 3 + 2];
  62. }
  63. if (sc == vc / 3) {
  64. f.smooth = rs[i];
  65. } else {
  66. f.smooth = false;
  67. }
  68. if (ic == vc / 3) {
  69. f.invert = ri[i];
  70. } else {
  71. f.invert = false;
  72. }
  73. if (mc == vc / 3) {
  74. Ref<Material> mat = rm[i];
  75. if (mat.is_valid()) {
  76. HashMap<Ref<Material>, int>::ConstIterator E = material_map.find(mat);
  77. if (E) {
  78. f.material = E->value;
  79. } else {
  80. f.material = material_map.size();
  81. material_map[mat] = f.material;
  82. }
  83. } else {
  84. f.material = -1;
  85. }
  86. }
  87. }
  88. materials.resize(material_map.size());
  89. for (const KeyValue<Ref<Material>, int> &E : material_map) {
  90. materials.write[E.value] = E.key;
  91. }
  92. _regen_face_aabbs();
  93. }
  94. void CSGBrush::copy_from(const CSGBrush &p_brush, const Transform3D &p_xform) {
  95. faces = p_brush.faces;
  96. materials = p_brush.materials;
  97. for (int i = 0; i < faces.size(); i++) {
  98. for (int j = 0; j < 3; j++) {
  99. faces.write[i].vertices[j] = p_xform.xform(p_brush.faces[i].vertices[j]);
  100. }
  101. }
  102. _regen_face_aabbs();
  103. }