csg.cpp 4.1 KB

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