| 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 | |
| 31 | #include "csg.h" |
| 32 | |
| 33 | // CSGBrush |
| 34 | |
| 35 | 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) { |
| 36 | faces.clear(); |
| 37 | |
| 38 | int vc = p_vertices.size(); |
| 39 | |
| 40 | ERR_FAIL_COND((vc % 3) != 0); |
| 41 | |
| 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 | |
| 52 | HashMap<Ref<Material>, int> material_map; |
| 53 | |
| 54 | faces.resize(p_vertices.size() / 3); |
| 55 | |
| 56 | for (int i = 0; i < faces.size(); i++) { |
| 57 | Face &f = faces.write[i]; |
| 58 | f.vertices[0] = rv[i * 3 + 0]; |
| 59 | f.vertices[1] = rv[i * 3 + 1]; |
| 60 | f.vertices[2] = rv[i * 3 + 2]; |
| 61 | |
| 62 | if (uvc == vc) { |
| 63 | f.uvs[0] = ruv[i * 3 + 0]; |
| 64 | f.uvs[1] = ruv[i * 3 + 1]; |
| 65 | f.uvs[2] = ruv[i * 3 + 2]; |
| 66 | } |
| 67 | |
| 68 | if (sc == vc / 3) { |
| 69 | f.smooth = rs[i]; |
| 70 | } else { |
| 71 | f.smooth = false; |
| 72 | } |
| 73 | |
| 74 | if (ic == vc / 3) { |
| 75 | f.invert = ri[i]; |
| 76 | } else { |
| 77 | f.invert = false; |
| 78 | } |
| 79 | |
| 80 | if (mc == vc / 3) { |
| 81 | Ref<Material> mat = rm[i]; |
| 82 | if (mat.is_valid()) { |
| 83 | HashMap<Ref<Material>, int>::ConstIterator E = material_map.find(mat); |
| 84 | |
| 85 | if (E) { |
| 86 | f.material = E->value; |
| 87 | } else { |
| 88 | f.material = material_map.size(); |
| 89 | material_map[mat] = f.material; |
| 90 | } |
| 91 | |
| 92 | } else { |
| 93 | f.material = -1; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | materials.resize(material_map.size()); |
| 99 | for (const KeyValue<Ref<Material>, int> &E : material_map) { |
| 100 | materials.write[E.value] = E.key; |
| 101 | } |
| 102 | |
| 103 | _regen_face_aabbs(); |
| 104 | } |
| 105 | |
| 106 | void CSGBrush::copy_from(const CSGBrush &p_brush, const Transform3D &p_xform) { |
| 107 | faces = p_brush.faces; |
| 108 | materials = p_brush.materials; |
| 109 | |
| 110 | for (int i = 0; i < faces.size(); i++) { |
| 111 | for (int j = 0; j < 3; j++) { |
| 112 | faces.write[i].vertices[j] = p_xform.xform(p_brush.faces[i].vertices[j]); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | _regen_face_aabbs(); |
| 117 | } |