csg_shape.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  1. /**************************************************************************/
  2. /* csg_shape.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_shape.h"
  31. #include "core/math/geometry_2d.h"
  32. #include <manifold/manifold.h>
  33. void CSGShape3D::set_use_collision(bool p_enable) {
  34. if (use_collision == p_enable) {
  35. return;
  36. }
  37. use_collision = p_enable;
  38. if (!is_inside_tree() || !is_root_shape()) {
  39. return;
  40. }
  41. if (use_collision) {
  42. root_collision_shape.instantiate();
  43. root_collision_instance = PhysicsServer3D::get_singleton()->body_create();
  44. PhysicsServer3D::get_singleton()->body_set_mode(root_collision_instance, PhysicsServer3D::BODY_MODE_STATIC);
  45. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  46. PhysicsServer3D::get_singleton()->body_add_shape(root_collision_instance, root_collision_shape->get_rid());
  47. PhysicsServer3D::get_singleton()->body_set_space(root_collision_instance, get_world_3d()->get_space());
  48. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(root_collision_instance, get_instance_id());
  49. set_collision_layer(collision_layer);
  50. set_collision_mask(collision_mask);
  51. set_collision_priority(collision_priority);
  52. _make_dirty(); //force update
  53. } else {
  54. PhysicsServer3D::get_singleton()->free(root_collision_instance);
  55. root_collision_instance = RID();
  56. root_collision_shape.unref();
  57. }
  58. notify_property_list_changed();
  59. }
  60. bool CSGShape3D::is_using_collision() const {
  61. return use_collision;
  62. }
  63. void CSGShape3D::set_collision_layer(uint32_t p_layer) {
  64. collision_layer = p_layer;
  65. if (root_collision_instance.is_valid()) {
  66. PhysicsServer3D::get_singleton()->body_set_collision_layer(root_collision_instance, p_layer);
  67. }
  68. }
  69. uint32_t CSGShape3D::get_collision_layer() const {
  70. return collision_layer;
  71. }
  72. void CSGShape3D::set_collision_mask(uint32_t p_mask) {
  73. collision_mask = p_mask;
  74. if (root_collision_instance.is_valid()) {
  75. PhysicsServer3D::get_singleton()->body_set_collision_mask(root_collision_instance, p_mask);
  76. }
  77. }
  78. uint32_t CSGShape3D::get_collision_mask() const {
  79. return collision_mask;
  80. }
  81. void CSGShape3D::set_collision_layer_value(int p_layer_number, bool p_value) {
  82. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  83. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  84. uint32_t layer = get_collision_layer();
  85. if (p_value) {
  86. layer |= 1 << (p_layer_number - 1);
  87. } else {
  88. layer &= ~(1 << (p_layer_number - 1));
  89. }
  90. set_collision_layer(layer);
  91. }
  92. bool CSGShape3D::get_collision_layer_value(int p_layer_number) const {
  93. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  94. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  95. return get_collision_layer() & (1 << (p_layer_number - 1));
  96. }
  97. void CSGShape3D::set_collision_mask_value(int p_layer_number, bool p_value) {
  98. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  99. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  100. uint32_t mask = get_collision_mask();
  101. if (p_value) {
  102. mask |= 1 << (p_layer_number - 1);
  103. } else {
  104. mask &= ~(1 << (p_layer_number - 1));
  105. }
  106. set_collision_mask(mask);
  107. }
  108. bool CSGShape3D::get_collision_mask_value(int p_layer_number) const {
  109. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  110. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  111. return get_collision_mask() & (1 << (p_layer_number - 1));
  112. }
  113. void CSGShape3D::set_collision_priority(real_t p_priority) {
  114. collision_priority = p_priority;
  115. if (root_collision_instance.is_valid()) {
  116. PhysicsServer3D::get_singleton()->body_set_collision_priority(root_collision_instance, p_priority);
  117. }
  118. }
  119. real_t CSGShape3D::get_collision_priority() const {
  120. return collision_priority;
  121. }
  122. bool CSGShape3D::is_root_shape() const {
  123. return !parent_shape;
  124. }
  125. void CSGShape3D::set_snap(float p_snap) {
  126. if (snap == p_snap) {
  127. return;
  128. }
  129. snap = p_snap;
  130. _make_dirty();
  131. }
  132. float CSGShape3D::get_snap() const {
  133. return snap;
  134. }
  135. void CSGShape3D::_make_dirty(bool p_parent_removing) {
  136. if ((p_parent_removing || is_root_shape()) && !dirty) {
  137. callable_mp(this, &CSGShape3D::_update_shape).call_deferred(); // Must be deferred; otherwise, is_root_shape() will use the previous parent.
  138. }
  139. if (!is_root_shape()) {
  140. parent_shape->_make_dirty();
  141. } else if (!dirty) {
  142. callable_mp(this, &CSGShape3D::_update_shape).call_deferred();
  143. }
  144. dirty = true;
  145. }
  146. enum ManifoldProperty {
  147. MANIFOLD_PROPERTY_POSITION_X = 0,
  148. MANIFOLD_PROPERTY_POSITION_Y,
  149. MANIFOLD_PROPERTY_POSITION_Z,
  150. MANIFOLD_PROPERTY_INVERT,
  151. MANIFOLD_PROPERTY_SMOOTH_GROUP,
  152. MANIFOLD_PROPERTY_UV_X_0,
  153. MANIFOLD_PROPERTY_UV_Y_0,
  154. MANIFOLD_PROPERTY_MAX
  155. };
  156. static void _unpack_manifold(
  157. const manifold::Manifold &p_manifold,
  158. const HashMap<int32_t, Ref<Material>> &p_mesh_materials,
  159. CSGBrush *r_mesh_merge) {
  160. manifold::MeshGL64 mesh = p_manifold.GetMeshGL64();
  161. constexpr int32_t order[3] = { 0, 2, 1 };
  162. for (size_t run_i = 0; run_i < mesh.runIndex.size() - 1; run_i++) {
  163. uint32_t original_id = -1;
  164. if (run_i < mesh.runOriginalID.size()) {
  165. original_id = mesh.runOriginalID[run_i];
  166. }
  167. Ref<Material> material;
  168. if (p_mesh_materials.has(original_id)) {
  169. material = p_mesh_materials[original_id];
  170. }
  171. // Find or reserve a material ID in the brush.
  172. int32_t material_id = r_mesh_merge->materials.find(material);
  173. if (material_id == -1) {
  174. material_id = r_mesh_merge->materials.size();
  175. r_mesh_merge->materials.push_back(material);
  176. }
  177. size_t begin = mesh.runIndex[run_i];
  178. size_t end = mesh.runIndex[run_i + 1];
  179. for (size_t vert_i = begin; vert_i < end; vert_i += 3) {
  180. CSGBrush::Face face;
  181. face.material = material_id;
  182. int32_t first_property_index = mesh.triVerts[vert_i + order[0]];
  183. face.smooth = mesh.vertProperties[first_property_index * mesh.numProp + MANIFOLD_PROPERTY_SMOOTH_GROUP] > 0.5f;
  184. face.invert = mesh.vertProperties[first_property_index * mesh.numProp + MANIFOLD_PROPERTY_INVERT] > 0.5f;
  185. for (int32_t tri_order_i = 0; tri_order_i < 3; tri_order_i++) {
  186. int32_t property_i = mesh.triVerts[vert_i + order[tri_order_i]];
  187. ERR_FAIL_COND_MSG(property_i * mesh.numProp >= mesh.vertProperties.size(), "Invalid index into vertex properties");
  188. face.vertices[tri_order_i] = Vector3(
  189. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_POSITION_X],
  190. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_POSITION_Y],
  191. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_POSITION_Z]);
  192. face.uvs[tri_order_i] = Vector2(
  193. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_UV_X_0],
  194. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_UV_Y_0]);
  195. }
  196. r_mesh_merge->faces.push_back(face);
  197. }
  198. }
  199. r_mesh_merge->_regen_face_aabbs();
  200. }
  201. static void _pack_manifold(
  202. const CSGBrush *const p_mesh_merge,
  203. manifold::Manifold &r_manifold,
  204. HashMap<int32_t, Ref<Material>> &p_mesh_materials,
  205. float p_snap) {
  206. ERR_FAIL_NULL_MSG(p_mesh_merge, "p_mesh_merge is null");
  207. HashMap<uint32_t, Vector<CSGBrush::Face>> faces_by_material;
  208. for (int face_i = 0; face_i < p_mesh_merge->faces.size(); face_i++) {
  209. const CSGBrush::Face &face = p_mesh_merge->faces[face_i];
  210. faces_by_material[face.material].push_back(face);
  211. }
  212. manifold::MeshGL64 mesh;
  213. mesh.tolerance = p_snap;
  214. mesh.numProp = MANIFOLD_PROPERTY_MAX;
  215. mesh.runOriginalID.reserve(faces_by_material.size());
  216. mesh.runIndex.reserve(faces_by_material.size() + 1);
  217. mesh.vertProperties.reserve(p_mesh_merge->faces.size() * 3 * MANIFOLD_PROPERTY_MAX);
  218. // Make a run of triangles for each material.
  219. for (const KeyValue<uint32_t, Vector<CSGBrush::Face>> &E : faces_by_material) {
  220. const uint32_t material_id = E.key;
  221. const Vector<CSGBrush::Face> &faces = E.value;
  222. mesh.runIndex.push_back(mesh.triVerts.size());
  223. // Associate the material with an ID.
  224. uint32_t reserved_id = r_manifold.ReserveIDs(1);
  225. mesh.runOriginalID.push_back(reserved_id);
  226. Ref<Material> material;
  227. if (material_id < p_mesh_merge->materials.size()) {
  228. material = p_mesh_merge->materials[material_id];
  229. }
  230. p_mesh_materials.insert(reserved_id, material);
  231. for (const CSGBrush::Face &face : faces) {
  232. for (int32_t tri_order_i = 0; tri_order_i < 3; tri_order_i++) {
  233. constexpr int32_t order[3] = { 0, 2, 1 };
  234. int i = order[tri_order_i];
  235. mesh.triVerts.push_back(mesh.vertProperties.size() / MANIFOLD_PROPERTY_MAX);
  236. size_t begin = mesh.vertProperties.size();
  237. mesh.vertProperties.resize(mesh.vertProperties.size() + MANIFOLD_PROPERTY_MAX);
  238. // Add the vertex properties.
  239. // Use CSGBrush constants rather than push_back for clarity.
  240. double *vert = &mesh.vertProperties[begin];
  241. vert[MANIFOLD_PROPERTY_POSITION_X] = face.vertices[i].x;
  242. vert[MANIFOLD_PROPERTY_POSITION_Y] = face.vertices[i].y;
  243. vert[MANIFOLD_PROPERTY_POSITION_Z] = face.vertices[i].z;
  244. vert[MANIFOLD_PROPERTY_UV_X_0] = face.uvs[i].x;
  245. vert[MANIFOLD_PROPERTY_UV_Y_0] = face.uvs[i].y;
  246. vert[MANIFOLD_PROPERTY_SMOOTH_GROUP] = face.smooth ? 1.0f : 0.0f;
  247. vert[MANIFOLD_PROPERTY_INVERT] = face.invert ? 1.0f : 0.0f;
  248. }
  249. }
  250. }
  251. // runIndex needs an explicit end value.
  252. mesh.runIndex.push_back(mesh.triVerts.size());
  253. ERR_FAIL_COND_MSG(mesh.vertProperties.size() % mesh.numProp != 0, "Invalid vertex properties size.");
  254. mesh.Merge();
  255. r_manifold = manifold::Manifold(mesh);
  256. manifold::Manifold::Error err = r_manifold.Status();
  257. if (err != manifold::Manifold::Error::NoError) {
  258. print_error(String("Manifold creation from mesh failed:" + itos((int)err)));
  259. }
  260. }
  261. struct ManifoldOperation {
  262. manifold::Manifold manifold;
  263. manifold::OpType operation;
  264. static manifold::OpType convert_csg_op(CSGShape3D::Operation op) {
  265. switch (op) {
  266. case CSGShape3D::OPERATION_SUBTRACTION:
  267. return manifold::OpType::Subtract;
  268. case CSGShape3D::OPERATION_INTERSECTION:
  269. return manifold::OpType::Intersect;
  270. default:
  271. return manifold::OpType::Add;
  272. }
  273. }
  274. ManifoldOperation() :
  275. operation(manifold::OpType::Add) {}
  276. ManifoldOperation(const manifold::Manifold &m, manifold::OpType op) :
  277. manifold(m), operation(op) {}
  278. };
  279. CSGBrush *CSGShape3D::_get_brush() {
  280. if (!dirty) {
  281. return brush;
  282. }
  283. if (brush) {
  284. memdelete(brush);
  285. }
  286. brush = nullptr;
  287. CSGBrush *n = _build_brush();
  288. HashMap<int32_t, Ref<Material>> mesh_materials;
  289. manifold::Manifold root_manifold;
  290. _pack_manifold(n, root_manifold, mesh_materials, get_snap());
  291. manifold::OpType current_op = ManifoldOperation::convert_csg_op(get_operation());
  292. std::vector<manifold::Manifold> manifolds;
  293. manifolds.push_back(root_manifold);
  294. for (int i = 0; i < get_child_count(); i++) {
  295. CSGShape3D *child = Object::cast_to<CSGShape3D>(get_child(i));
  296. if (!child || !child->is_visible()) {
  297. continue;
  298. }
  299. CSGBrush *child_brush = child->_get_brush();
  300. if (!child_brush) {
  301. continue;
  302. }
  303. CSGBrush transformed_brush;
  304. transformed_brush.copy_from(*child_brush, child->get_transform());
  305. manifold::Manifold child_manifold;
  306. _pack_manifold(&transformed_brush, child_manifold, mesh_materials, get_snap());
  307. manifold::OpType child_operation = ManifoldOperation::convert_csg_op(child->get_operation());
  308. if (child_operation != current_op) {
  309. manifold::Manifold result = manifold::Manifold::BatchBoolean(manifolds, current_op);
  310. manifolds.clear();
  311. manifolds.push_back(result);
  312. current_op = child_operation;
  313. }
  314. manifolds.push_back(child_manifold);
  315. }
  316. if (!manifolds.empty()) {
  317. manifold::Manifold manifold_result = manifold::Manifold::BatchBoolean(manifolds, current_op);
  318. if (n) {
  319. memdelete(n);
  320. }
  321. n = memnew(CSGBrush);
  322. _unpack_manifold(manifold_result, mesh_materials, n);
  323. }
  324. AABB aabb;
  325. if (n && !n->faces.is_empty()) {
  326. aabb.position = n->faces[0].vertices[0];
  327. for (const CSGBrush::Face &face : n->faces) {
  328. for (int i = 0; i < 3; ++i) {
  329. aabb.expand_to(face.vertices[i]);
  330. }
  331. }
  332. }
  333. node_aabb = aabb;
  334. brush = n;
  335. dirty = false;
  336. return brush;
  337. }
  338. int CSGShape3D::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
  339. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  340. return surface.vertices.size() / 3;
  341. }
  342. int CSGShape3D::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
  343. // always 3
  344. return 3;
  345. }
  346. void CSGShape3D::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
  347. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  348. Vector3 v = surface.verticesw[iFace * 3 + iVert];
  349. fvPosOut[0] = v.x;
  350. fvPosOut[1] = v.y;
  351. fvPosOut[2] = v.z;
  352. }
  353. void CSGShape3D::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert) {
  354. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  355. Vector3 n = surface.normalsw[iFace * 3 + iVert];
  356. fvNormOut[0] = n.x;
  357. fvNormOut[1] = n.y;
  358. fvNormOut[2] = n.z;
  359. }
  360. void CSGShape3D::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
  361. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  362. Vector2 t = surface.uvsw[iFace * 3 + iVert];
  363. fvTexcOut[0] = t.x;
  364. fvTexcOut[1] = t.y;
  365. }
  366. void CSGShape3D::mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  367. const tbool bIsOrientationPreserving, const int iFace, const int iVert) {
  368. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  369. int i = iFace * 3 + iVert;
  370. Vector3 normal = surface.normalsw[i];
  371. Vector3 tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
  372. Vector3 bitangent = Vector3(-fvBiTangent[0], -fvBiTangent[1], -fvBiTangent[2]); // for some reason these are reversed, something with the coordinate system in Godot
  373. float d = bitangent.dot(normal.cross(tangent));
  374. i *= 4;
  375. surface.tansw[i++] = tangent.x;
  376. surface.tansw[i++] = tangent.y;
  377. surface.tansw[i++] = tangent.z;
  378. surface.tansw[i++] = d < 0 ? -1 : 1;
  379. }
  380. void CSGShape3D::_update_shape() {
  381. if (!is_root_shape()) {
  382. return;
  383. }
  384. set_base(RID());
  385. root_mesh.unref(); //byebye root mesh
  386. CSGBrush *n = _get_brush();
  387. ERR_FAIL_NULL_MSG(n, "Cannot get CSGBrush.");
  388. OAHashMap<Vector3, Vector3> vec_map;
  389. Vector<int> face_count;
  390. face_count.resize(n->materials.size() + 1);
  391. for (int i = 0; i < face_count.size(); i++) {
  392. face_count.write[i] = 0;
  393. }
  394. for (int i = 0; i < n->faces.size(); i++) {
  395. int mat = n->faces[i].material;
  396. ERR_CONTINUE(mat < -1 || mat >= face_count.size());
  397. int idx = mat == -1 ? face_count.size() - 1 : mat;
  398. if (n->faces[i].smooth) {
  399. Plane p(n->faces[i].vertices[0], n->faces[i].vertices[1], n->faces[i].vertices[2]);
  400. for (int j = 0; j < 3; j++) {
  401. Vector3 v = n->faces[i].vertices[j];
  402. Vector3 add;
  403. if (vec_map.lookup(v, add)) {
  404. add += p.normal;
  405. } else {
  406. add = p.normal;
  407. }
  408. vec_map.set(v, add);
  409. }
  410. }
  411. face_count.write[idx]++;
  412. }
  413. Vector<ShapeUpdateSurface> surfaces;
  414. surfaces.resize(face_count.size());
  415. //create arrays
  416. for (int i = 0; i < surfaces.size(); i++) {
  417. surfaces.write[i].vertices.resize(face_count[i] * 3);
  418. surfaces.write[i].normals.resize(face_count[i] * 3);
  419. surfaces.write[i].uvs.resize(face_count[i] * 3);
  420. if (calculate_tangents) {
  421. surfaces.write[i].tans.resize(face_count[i] * 3 * 4);
  422. }
  423. surfaces.write[i].last_added = 0;
  424. if (i != surfaces.size() - 1) {
  425. surfaces.write[i].material = n->materials[i];
  426. }
  427. surfaces.write[i].verticesw = surfaces.write[i].vertices.ptrw();
  428. surfaces.write[i].normalsw = surfaces.write[i].normals.ptrw();
  429. surfaces.write[i].uvsw = surfaces.write[i].uvs.ptrw();
  430. if (calculate_tangents) {
  431. surfaces.write[i].tansw = surfaces.write[i].tans.ptrw();
  432. }
  433. }
  434. //fill arrays
  435. {
  436. for (int i = 0; i < n->faces.size(); i++) {
  437. int order[3] = { 0, 1, 2 };
  438. if (n->faces[i].invert) {
  439. SWAP(order[1], order[2]);
  440. }
  441. int mat = n->faces[i].material;
  442. ERR_CONTINUE(mat < -1 || mat >= face_count.size());
  443. int idx = mat == -1 ? face_count.size() - 1 : mat;
  444. int last = surfaces[idx].last_added;
  445. Plane p(n->faces[i].vertices[0], n->faces[i].vertices[1], n->faces[i].vertices[2]);
  446. for (int j = 0; j < 3; j++) {
  447. Vector3 v = n->faces[i].vertices[j];
  448. Vector3 normal = p.normal;
  449. if (n->faces[i].smooth && vec_map.lookup(v, normal)) {
  450. normal.normalize();
  451. }
  452. if (n->faces[i].invert) {
  453. normal = -normal;
  454. }
  455. int k = last + order[j];
  456. surfaces[idx].verticesw[k] = v;
  457. surfaces[idx].uvsw[k] = n->faces[i].uvs[j];
  458. surfaces[idx].normalsw[k] = normal;
  459. if (calculate_tangents) {
  460. // zero out our tangents for now
  461. k *= 4;
  462. surfaces[idx].tansw[k++] = 0.0;
  463. surfaces[idx].tansw[k++] = 0.0;
  464. surfaces[idx].tansw[k++] = 0.0;
  465. surfaces[idx].tansw[k++] = 0.0;
  466. }
  467. }
  468. surfaces.write[idx].last_added += 3;
  469. }
  470. }
  471. root_mesh.instantiate();
  472. //create surfaces
  473. for (int i = 0; i < surfaces.size(); i++) {
  474. // calculate tangents for this surface
  475. bool have_tangents = calculate_tangents;
  476. if (have_tangents) {
  477. SMikkTSpaceInterface mkif;
  478. mkif.m_getNormal = mikktGetNormal;
  479. mkif.m_getNumFaces = mikktGetNumFaces;
  480. mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
  481. mkif.m_getPosition = mikktGetPosition;
  482. mkif.m_getTexCoord = mikktGetTexCoord;
  483. mkif.m_setTSpace = mikktSetTSpaceDefault;
  484. mkif.m_setTSpaceBasic = nullptr;
  485. SMikkTSpaceContext msc;
  486. msc.m_pInterface = &mkif;
  487. msc.m_pUserData = &surfaces.write[i];
  488. have_tangents = genTangSpaceDefault(&msc);
  489. }
  490. if (surfaces[i].last_added == 0) {
  491. continue;
  492. }
  493. // and convert to surface array
  494. Array array;
  495. array.resize(Mesh::ARRAY_MAX);
  496. array[Mesh::ARRAY_VERTEX] = surfaces[i].vertices;
  497. array[Mesh::ARRAY_NORMAL] = surfaces[i].normals;
  498. array[Mesh::ARRAY_TEX_UV] = surfaces[i].uvs;
  499. if (have_tangents) {
  500. array[Mesh::ARRAY_TANGENT] = surfaces[i].tans;
  501. }
  502. int idx = root_mesh->get_surface_count();
  503. root_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array);
  504. root_mesh->surface_set_material(idx, surfaces[i].material);
  505. }
  506. set_base(root_mesh->get_rid());
  507. _update_collision_faces();
  508. }
  509. Vector<Vector3> CSGShape3D::_get_brush_collision_faces() {
  510. Vector<Vector3> collision_faces;
  511. CSGBrush *n = _get_brush();
  512. ERR_FAIL_NULL_V_MSG(n, collision_faces, "Cannot get CSGBrush.");
  513. collision_faces.resize(n->faces.size() * 3);
  514. Vector3 *collision_faces_ptrw = collision_faces.ptrw();
  515. for (int i = 0; i < n->faces.size(); i++) {
  516. int order[3] = { 0, 1, 2 };
  517. if (n->faces[i].invert) {
  518. SWAP(order[1], order[2]);
  519. }
  520. collision_faces_ptrw[i * 3 + 0] = n->faces[i].vertices[order[0]];
  521. collision_faces_ptrw[i * 3 + 1] = n->faces[i].vertices[order[1]];
  522. collision_faces_ptrw[i * 3 + 2] = n->faces[i].vertices[order[2]];
  523. }
  524. return collision_faces;
  525. }
  526. void CSGShape3D::_update_collision_faces() {
  527. if (use_collision && is_root_shape() && root_collision_shape.is_valid()) {
  528. root_collision_shape->set_faces(_get_brush_collision_faces());
  529. if (_is_debug_collision_shape_visible()) {
  530. _update_debug_collision_shape();
  531. }
  532. }
  533. }
  534. Ref<ArrayMesh> CSGShape3D::bake_static_mesh() {
  535. Ref<ArrayMesh> baked_mesh;
  536. if (is_root_shape() && root_mesh.is_valid()) {
  537. baked_mesh = root_mesh;
  538. }
  539. return baked_mesh;
  540. }
  541. Ref<ConcavePolygonShape3D> CSGShape3D::bake_collision_shape() {
  542. Ref<ConcavePolygonShape3D> baked_collision_shape;
  543. if (is_root_shape() && root_collision_shape.is_valid()) {
  544. baked_collision_shape.instantiate();
  545. baked_collision_shape->set_faces(root_collision_shape->get_faces());
  546. } else if (is_root_shape()) {
  547. baked_collision_shape.instantiate();
  548. baked_collision_shape->set_faces(_get_brush_collision_faces());
  549. }
  550. return baked_collision_shape;
  551. }
  552. bool CSGShape3D::_is_debug_collision_shape_visible() {
  553. return !Engine::get_singleton()->is_editor_hint() && is_inside_tree() && get_tree()->is_debugging_collisions_hint();
  554. }
  555. void CSGShape3D::_update_debug_collision_shape() {
  556. if (!use_collision || !is_root_shape() || !root_collision_shape.is_valid() || !_is_debug_collision_shape_visible()) {
  557. return;
  558. }
  559. ERR_FAIL_NULL(RenderingServer::get_singleton());
  560. if (root_collision_debug_instance.is_null()) {
  561. root_collision_debug_instance = RS::get_singleton()->instance_create();
  562. }
  563. Ref<Mesh> debug_mesh = root_collision_shape->get_debug_mesh();
  564. RS::get_singleton()->instance_set_scenario(root_collision_debug_instance, get_world_3d()->get_scenario());
  565. RS::get_singleton()->instance_set_base(root_collision_debug_instance, debug_mesh->get_rid());
  566. RS::get_singleton()->instance_set_transform(root_collision_debug_instance, get_global_transform());
  567. }
  568. void CSGShape3D::_clear_debug_collision_shape() {
  569. if (root_collision_debug_instance.is_valid()) {
  570. RS::get_singleton()->free(root_collision_debug_instance);
  571. root_collision_debug_instance = RID();
  572. }
  573. }
  574. void CSGShape3D::_on_transform_changed() {
  575. if (root_collision_debug_instance.is_valid() && !debug_shape_old_transform.is_equal_approx(get_global_transform())) {
  576. debug_shape_old_transform = get_global_transform();
  577. RS::get_singleton()->instance_set_transform(root_collision_debug_instance, debug_shape_old_transform);
  578. }
  579. }
  580. AABB CSGShape3D::get_aabb() const {
  581. return node_aabb;
  582. }
  583. Vector<Vector3> CSGShape3D::get_brush_faces() {
  584. ERR_FAIL_COND_V(!is_inside_tree(), Vector<Vector3>());
  585. CSGBrush *b = _get_brush();
  586. if (!b) {
  587. return Vector<Vector3>();
  588. }
  589. Vector<Vector3> faces;
  590. int fc = b->faces.size();
  591. faces.resize(fc * 3);
  592. {
  593. Vector3 *w = faces.ptrw();
  594. for (int i = 0; i < fc; i++) {
  595. w[i * 3 + 0] = b->faces[i].vertices[0];
  596. w[i * 3 + 1] = b->faces[i].vertices[1];
  597. w[i * 3 + 2] = b->faces[i].vertices[2];
  598. }
  599. }
  600. return faces;
  601. }
  602. void CSGShape3D::_notification(int p_what) {
  603. switch (p_what) {
  604. case NOTIFICATION_PARENTED: {
  605. Node *parentn = get_parent();
  606. if (parentn) {
  607. parent_shape = Object::cast_to<CSGShape3D>(parentn);
  608. if (parent_shape) {
  609. set_base(RID());
  610. root_mesh.unref();
  611. }
  612. }
  613. if (!brush || parent_shape) {
  614. // Update this node if uninitialized, or both this node and its new parent if it gets added to another CSG shape
  615. _make_dirty();
  616. }
  617. last_visible = is_visible();
  618. } break;
  619. case NOTIFICATION_UNPARENTED: {
  620. if (!is_root_shape()) {
  621. // Update this node and its previous parent only if it's currently being removed from another CSG shape
  622. _make_dirty(true); // Must be forced since is_root_shape() uses the previous parent
  623. }
  624. parent_shape = nullptr;
  625. } break;
  626. case NOTIFICATION_VISIBILITY_CHANGED: {
  627. if (!is_root_shape() && last_visible != is_visible()) {
  628. // Update this node's parent only if its own visibility has changed, not the visibility of parent nodes
  629. parent_shape->_make_dirty();
  630. }
  631. last_visible = is_visible();
  632. } break;
  633. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  634. if (!is_root_shape()) {
  635. // Update this node's parent only if its own transformation has changed, not the transformation of parent nodes
  636. parent_shape->_make_dirty();
  637. }
  638. } break;
  639. case NOTIFICATION_ENTER_TREE: {
  640. if (use_collision && is_root_shape()) {
  641. root_collision_shape.instantiate();
  642. root_collision_instance = PhysicsServer3D::get_singleton()->body_create();
  643. PhysicsServer3D::get_singleton()->body_set_mode(root_collision_instance, PhysicsServer3D::BODY_MODE_STATIC);
  644. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  645. PhysicsServer3D::get_singleton()->body_add_shape(root_collision_instance, root_collision_shape->get_rid());
  646. PhysicsServer3D::get_singleton()->body_set_space(root_collision_instance, get_world_3d()->get_space());
  647. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(root_collision_instance, get_instance_id());
  648. set_collision_layer(collision_layer);
  649. set_collision_mask(collision_mask);
  650. set_collision_priority(collision_priority);
  651. debug_shape_old_transform = get_global_transform();
  652. _make_dirty();
  653. }
  654. } break;
  655. case NOTIFICATION_EXIT_TREE: {
  656. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  657. PhysicsServer3D::get_singleton()->free(root_collision_instance);
  658. root_collision_instance = RID();
  659. root_collision_shape.unref();
  660. _clear_debug_collision_shape();
  661. }
  662. } break;
  663. case NOTIFICATION_TRANSFORM_CHANGED: {
  664. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  665. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  666. }
  667. _on_transform_changed();
  668. } break;
  669. }
  670. }
  671. void CSGShape3D::set_operation(Operation p_operation) {
  672. operation = p_operation;
  673. _make_dirty();
  674. update_gizmos();
  675. }
  676. CSGShape3D::Operation CSGShape3D::get_operation() const {
  677. return operation;
  678. }
  679. void CSGShape3D::set_calculate_tangents(bool p_calculate_tangents) {
  680. calculate_tangents = p_calculate_tangents;
  681. _make_dirty();
  682. }
  683. bool CSGShape3D::is_calculating_tangents() const {
  684. return calculate_tangents;
  685. }
  686. void CSGShape3D::_validate_property(PropertyInfo &p_property) const {
  687. bool is_collision_prefixed = p_property.name.begins_with("collision_");
  688. if ((is_collision_prefixed || p_property.name.begins_with("use_collision")) && is_inside_tree() && !is_root_shape()) {
  689. //hide collision if not root
  690. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  691. } else if (is_collision_prefixed && !bool(get("use_collision"))) {
  692. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  693. }
  694. }
  695. Array CSGShape3D::get_meshes() const {
  696. if (root_mesh.is_valid()) {
  697. Array arr;
  698. arr.resize(2);
  699. arr[0] = Transform3D();
  700. arr[1] = root_mesh;
  701. return arr;
  702. }
  703. return Array();
  704. }
  705. void CSGShape3D::_bind_methods() {
  706. ClassDB::bind_method(D_METHOD("_update_shape"), &CSGShape3D::_update_shape);
  707. ClassDB::bind_method(D_METHOD("is_root_shape"), &CSGShape3D::is_root_shape);
  708. ClassDB::bind_method(D_METHOD("set_operation", "operation"), &CSGShape3D::set_operation);
  709. ClassDB::bind_method(D_METHOD("get_operation"), &CSGShape3D::get_operation);
  710. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &CSGShape3D::set_snap);
  711. ClassDB::bind_method(D_METHOD("get_snap"), &CSGShape3D::get_snap);
  712. ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape3D::set_use_collision);
  713. ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape3D::is_using_collision);
  714. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CSGShape3D::set_collision_layer);
  715. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CSGShape3D::get_collision_layer);
  716. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CSGShape3D::set_collision_mask);
  717. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CSGShape3D::get_collision_mask);
  718. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CSGShape3D::set_collision_mask_value);
  719. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CSGShape3D::get_collision_mask_value);
  720. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CSGShape3D::set_collision_layer_value);
  721. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CSGShape3D::get_collision_layer_value);
  722. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CSGShape3D::set_collision_priority);
  723. ClassDB::bind_method(D_METHOD("get_collision_priority"), &CSGShape3D::get_collision_priority);
  724. ClassDB::bind_method(D_METHOD("set_calculate_tangents", "enabled"), &CSGShape3D::set_calculate_tangents);
  725. ClassDB::bind_method(D_METHOD("is_calculating_tangents"), &CSGShape3D::is_calculating_tangents);
  726. ClassDB::bind_method(D_METHOD("get_meshes"), &CSGShape3D::get_meshes);
  727. ClassDB::bind_method(D_METHOD("bake_static_mesh"), &CSGShape3D::bake_static_mesh);
  728. ClassDB::bind_method(D_METHOD("bake_collision_shape"), &CSGShape3D::bake_collision_shape);
  729. ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "Union,Intersection,Subtraction"), "set_operation", "get_operation");
  730. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_RANGE, "0.000001,1,0.000001,suffix:m"), "set_snap", "get_snap");
  731. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "calculate_tangents"), "set_calculate_tangents", "is_calculating_tangents");
  732. ADD_GROUP("Collision", "collision_");
  733. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_collision"), "set_use_collision", "is_using_collision");
  734. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  735. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  736. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  737. BIND_ENUM_CONSTANT(OPERATION_UNION);
  738. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  739. BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
  740. }
  741. CSGShape3D::CSGShape3D() {
  742. set_notify_local_transform(true);
  743. }
  744. CSGShape3D::~CSGShape3D() {
  745. if (brush) {
  746. memdelete(brush);
  747. brush = nullptr;
  748. }
  749. }
  750. //////////////////////////////////
  751. CSGBrush *CSGCombiner3D::_build_brush() {
  752. return memnew(CSGBrush); //does not build anything
  753. }
  754. CSGCombiner3D::CSGCombiner3D() {
  755. }
  756. /////////////////////
  757. CSGBrush *CSGPrimitive3D::_create_brush_from_arrays(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uv, const Vector<bool> &p_smooth, const Vector<Ref<Material>> &p_materials) {
  758. CSGBrush *new_brush = memnew(CSGBrush);
  759. Vector<bool> invert;
  760. invert.resize(p_vertices.size() / 3);
  761. {
  762. int ic = invert.size();
  763. bool *w = invert.ptrw();
  764. for (int i = 0; i < ic; i++) {
  765. w[i] = flip_faces;
  766. }
  767. }
  768. new_brush->build_from_faces(p_vertices, p_uv, p_smooth, p_materials, invert);
  769. return new_brush;
  770. }
  771. void CSGPrimitive3D::_bind_methods() {
  772. ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &CSGPrimitive3D::set_flip_faces);
  773. ClassDB::bind_method(D_METHOD("get_flip_faces"), &CSGPrimitive3D::get_flip_faces);
  774. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
  775. }
  776. void CSGPrimitive3D::set_flip_faces(bool p_invert) {
  777. if (flip_faces == p_invert) {
  778. return;
  779. }
  780. flip_faces = p_invert;
  781. _make_dirty();
  782. }
  783. bool CSGPrimitive3D::get_flip_faces() {
  784. return flip_faces;
  785. }
  786. CSGPrimitive3D::CSGPrimitive3D() {
  787. flip_faces = false;
  788. }
  789. /////////////////////
  790. CSGBrush *CSGMesh3D::_build_brush() {
  791. if (!mesh.is_valid()) {
  792. return memnew(CSGBrush);
  793. }
  794. Vector<Vector3> vertices;
  795. Vector<bool> smooth;
  796. Vector<Ref<Material>> materials;
  797. Vector<Vector2> uvs;
  798. Ref<Material> base_material = get_material();
  799. for (int i = 0; i < mesh->get_surface_count(); i++) {
  800. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  801. continue;
  802. }
  803. Array arrays = mesh->surface_get_arrays(i);
  804. if (arrays.size() == 0) {
  805. _make_dirty();
  806. ERR_FAIL_COND_V(arrays.is_empty(), memnew(CSGBrush));
  807. }
  808. Vector<Vector3> avertices = arrays[Mesh::ARRAY_VERTEX];
  809. if (avertices.size() == 0) {
  810. continue;
  811. }
  812. const Vector3 *vr = avertices.ptr();
  813. Vector<Vector3> anormals = arrays[Mesh::ARRAY_NORMAL];
  814. const Vector3 *nr = nullptr;
  815. if (anormals.size()) {
  816. nr = anormals.ptr();
  817. }
  818. Vector<Vector2> auvs = arrays[Mesh::ARRAY_TEX_UV];
  819. const Vector2 *uvr = nullptr;
  820. if (auvs.size()) {
  821. uvr = auvs.ptr();
  822. }
  823. Ref<Material> mat;
  824. if (base_material.is_valid()) {
  825. mat = base_material;
  826. } else {
  827. mat = mesh->surface_get_material(i);
  828. }
  829. Vector<int> aindices = arrays[Mesh::ARRAY_INDEX];
  830. if (aindices.size()) {
  831. int as = vertices.size();
  832. int is = aindices.size();
  833. vertices.resize(as + is);
  834. smooth.resize((as + is) / 3);
  835. materials.resize((as + is) / 3);
  836. uvs.resize(as + is);
  837. Vector3 *vw = vertices.ptrw();
  838. bool *sw = smooth.ptrw();
  839. Vector2 *uvw = uvs.ptrw();
  840. Ref<Material> *mw = materials.ptrw();
  841. const int *ir = aindices.ptr();
  842. for (int j = 0; j < is; j += 3) {
  843. Vector3 vertex[3];
  844. Vector3 normal[3];
  845. Vector2 uv[3];
  846. for (int k = 0; k < 3; k++) {
  847. int idx = ir[j + k];
  848. vertex[k] = vr[idx];
  849. if (nr) {
  850. normal[k] = nr[idx];
  851. }
  852. if (uvr) {
  853. uv[k] = uvr[idx];
  854. }
  855. }
  856. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  857. vw[as + j + 0] = vertex[0];
  858. vw[as + j + 1] = vertex[1];
  859. vw[as + j + 2] = vertex[2];
  860. uvw[as + j + 0] = uv[0];
  861. uvw[as + j + 1] = uv[1];
  862. uvw[as + j + 2] = uv[2];
  863. sw[(as + j) / 3] = !flat;
  864. mw[(as + j) / 3] = mat;
  865. }
  866. } else {
  867. int as = vertices.size();
  868. int is = avertices.size();
  869. vertices.resize(as + is);
  870. smooth.resize((as + is) / 3);
  871. uvs.resize(as + is);
  872. materials.resize((as + is) / 3);
  873. Vector3 *vw = vertices.ptrw();
  874. bool *sw = smooth.ptrw();
  875. Vector2 *uvw = uvs.ptrw();
  876. Ref<Material> *mw = materials.ptrw();
  877. for (int j = 0; j < is; j += 3) {
  878. Vector3 vertex[3];
  879. Vector3 normal[3];
  880. Vector2 uv[3];
  881. for (int k = 0; k < 3; k++) {
  882. vertex[k] = vr[j + k];
  883. if (nr) {
  884. normal[k] = nr[j + k];
  885. }
  886. if (uvr) {
  887. uv[k] = uvr[j + k];
  888. }
  889. }
  890. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  891. vw[as + j + 0] = vertex[0];
  892. vw[as + j + 1] = vertex[1];
  893. vw[as + j + 2] = vertex[2];
  894. uvw[as + j + 0] = uv[0];
  895. uvw[as + j + 1] = uv[1];
  896. uvw[as + j + 2] = uv[2];
  897. sw[(as + j) / 3] = !flat;
  898. mw[(as + j) / 3] = mat;
  899. }
  900. }
  901. }
  902. if (vertices.size() == 0) {
  903. return memnew(CSGBrush);
  904. }
  905. return _create_brush_from_arrays(vertices, uvs, smooth, materials);
  906. }
  907. void CSGMesh3D::_mesh_changed() {
  908. _make_dirty();
  909. callable_mp((Node3D *)this, &Node3D::update_gizmos).call_deferred();
  910. }
  911. void CSGMesh3D::set_material(const Ref<Material> &p_material) {
  912. if (material == p_material) {
  913. return;
  914. }
  915. material = p_material;
  916. _make_dirty();
  917. }
  918. Ref<Material> CSGMesh3D::get_material() const {
  919. return material;
  920. }
  921. void CSGMesh3D::_bind_methods() {
  922. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CSGMesh3D::set_mesh);
  923. ClassDB::bind_method(D_METHOD("get_mesh"), &CSGMesh3D::get_mesh);
  924. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGMesh3D::set_material);
  925. ClassDB::bind_method(D_METHOD("get_material"), &CSGMesh3D::get_material);
  926. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  927. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  928. }
  929. void CSGMesh3D::set_mesh(const Ref<Mesh> &p_mesh) {
  930. if (mesh == p_mesh) {
  931. return;
  932. }
  933. if (mesh.is_valid()) {
  934. mesh->disconnect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  935. }
  936. mesh = p_mesh;
  937. if (mesh.is_valid()) {
  938. mesh->connect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  939. }
  940. _mesh_changed();
  941. }
  942. Ref<Mesh> CSGMesh3D::get_mesh() {
  943. return mesh;
  944. }
  945. ////////////////////////////////
  946. CSGBrush *CSGSphere3D::_build_brush() {
  947. // set our bounding box
  948. CSGBrush *new_brush = memnew(CSGBrush);
  949. int face_count = rings * radial_segments * 2 - radial_segments * 2;
  950. bool invert_val = get_flip_faces();
  951. Ref<Material> base_material = get_material();
  952. Vector<Vector3> faces;
  953. Vector<Vector2> uvs;
  954. Vector<bool> smooth;
  955. Vector<Ref<Material>> materials;
  956. Vector<bool> invert;
  957. faces.resize(face_count * 3);
  958. uvs.resize(face_count * 3);
  959. smooth.resize(face_count);
  960. materials.resize(face_count);
  961. invert.resize(face_count);
  962. {
  963. Vector3 *facesw = faces.ptrw();
  964. Vector2 *uvsw = uvs.ptrw();
  965. bool *smoothw = smooth.ptrw();
  966. Ref<Material> *materialsw = materials.ptrw();
  967. bool *invertw = invert.ptrw();
  968. // We want to follow an order that's convenient for UVs.
  969. // For latitude step we start at the top and move down like in an image.
  970. const double latitude_step = -Math_PI / rings;
  971. const double longitude_step = Math_TAU / radial_segments;
  972. int face = 0;
  973. for (int i = 0; i < rings; i++) {
  974. double latitude0 = latitude_step * i + Math_TAU / 4;
  975. double cos0 = Math::cos(latitude0);
  976. double sin0 = Math::sin(latitude0);
  977. double v0 = double(i) / rings;
  978. double latitude1 = latitude_step * (i + 1) + Math_TAU / 4;
  979. double cos1 = Math::cos(latitude1);
  980. double sin1 = Math::sin(latitude1);
  981. double v1 = double(i + 1) / rings;
  982. for (int j = 0; j < radial_segments; j++) {
  983. double longitude0 = longitude_step * j;
  984. // We give sin to X and cos to Z on purpose.
  985. // This allows UVs to be CCW on +X so it maps to images well.
  986. double x0 = Math::sin(longitude0);
  987. double z0 = Math::cos(longitude0);
  988. double u0 = double(j) / radial_segments;
  989. double longitude1 = longitude_step * (j + 1);
  990. if (j == radial_segments - 1) {
  991. longitude1 = 0;
  992. }
  993. double x1 = Math::sin(longitude1);
  994. double z1 = Math::cos(longitude1);
  995. double u1 = double(j + 1) / radial_segments;
  996. Vector3 v[4] = {
  997. Vector3(x0 * cos0, sin0, z0 * cos0) * radius,
  998. Vector3(x1 * cos0, sin0, z1 * cos0) * radius,
  999. Vector3(x1 * cos1, sin1, z1 * cos1) * radius,
  1000. Vector3(x0 * cos1, sin1, z0 * cos1) * radius,
  1001. };
  1002. Vector2 u[4] = {
  1003. Vector2(u0, v0),
  1004. Vector2(u1, v0),
  1005. Vector2(u1, v1),
  1006. Vector2(u0, v1),
  1007. };
  1008. // Draw the first face, but skip this at the north pole (i == 0).
  1009. if (i > 0) {
  1010. facesw[face * 3 + 0] = v[0];
  1011. facesw[face * 3 + 1] = v[1];
  1012. facesw[face * 3 + 2] = v[2];
  1013. uvsw[face * 3 + 0] = u[0];
  1014. uvsw[face * 3 + 1] = u[1];
  1015. uvsw[face * 3 + 2] = u[2];
  1016. smoothw[face] = smooth_faces;
  1017. invertw[face] = invert_val;
  1018. materialsw[face] = base_material;
  1019. face++;
  1020. }
  1021. // Draw the second face, but skip this at the south pole (i == rings - 1).
  1022. if (i < rings - 1) {
  1023. facesw[face * 3 + 0] = v[2];
  1024. facesw[face * 3 + 1] = v[3];
  1025. facesw[face * 3 + 2] = v[0];
  1026. uvsw[face * 3 + 0] = u[2];
  1027. uvsw[face * 3 + 1] = u[3];
  1028. uvsw[face * 3 + 2] = u[0];
  1029. smoothw[face] = smooth_faces;
  1030. invertw[face] = invert_val;
  1031. materialsw[face] = base_material;
  1032. face++;
  1033. }
  1034. }
  1035. }
  1036. if (face != face_count) {
  1037. ERR_PRINT("Face mismatch bug! fix code");
  1038. }
  1039. }
  1040. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1041. return new_brush;
  1042. }
  1043. void CSGSphere3D::_bind_methods() {
  1044. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGSphere3D::set_radius);
  1045. ClassDB::bind_method(D_METHOD("get_radius"), &CSGSphere3D::get_radius);
  1046. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGSphere3D::set_radial_segments);
  1047. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGSphere3D::get_radial_segments);
  1048. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CSGSphere3D::set_rings);
  1049. ClassDB::bind_method(D_METHOD("get_rings"), &CSGSphere3D::get_rings);
  1050. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGSphere3D::set_smooth_faces);
  1051. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGSphere3D::get_smooth_faces);
  1052. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGSphere3D::set_material);
  1053. ClassDB::bind_method(D_METHOD("get_material"), &CSGSphere3D::get_material);
  1054. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,suffix:m"), "set_radius", "get_radius");
  1055. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments");
  1056. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1"), "set_rings", "get_rings");
  1057. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1058. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1059. }
  1060. void CSGSphere3D::set_radius(const float p_radius) {
  1061. ERR_FAIL_COND(p_radius <= 0);
  1062. radius = p_radius;
  1063. _make_dirty();
  1064. update_gizmos();
  1065. }
  1066. float CSGSphere3D::get_radius() const {
  1067. return radius;
  1068. }
  1069. void CSGSphere3D::set_radial_segments(const int p_radial_segments) {
  1070. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  1071. _make_dirty();
  1072. update_gizmos();
  1073. }
  1074. int CSGSphere3D::get_radial_segments() const {
  1075. return radial_segments;
  1076. }
  1077. void CSGSphere3D::set_rings(const int p_rings) {
  1078. rings = p_rings > 1 ? p_rings : 1;
  1079. _make_dirty();
  1080. update_gizmos();
  1081. }
  1082. int CSGSphere3D::get_rings() const {
  1083. return rings;
  1084. }
  1085. void CSGSphere3D::set_smooth_faces(const bool p_smooth_faces) {
  1086. smooth_faces = p_smooth_faces;
  1087. _make_dirty();
  1088. }
  1089. bool CSGSphere3D::get_smooth_faces() const {
  1090. return smooth_faces;
  1091. }
  1092. void CSGSphere3D::set_material(const Ref<Material> &p_material) {
  1093. material = p_material;
  1094. _make_dirty();
  1095. }
  1096. Ref<Material> CSGSphere3D::get_material() const {
  1097. return material;
  1098. }
  1099. CSGSphere3D::CSGSphere3D() {
  1100. // defaults
  1101. radius = 0.5;
  1102. radial_segments = 12;
  1103. rings = 6;
  1104. smooth_faces = true;
  1105. }
  1106. ///////////////
  1107. CSGBrush *CSGBox3D::_build_brush() {
  1108. // set our bounding box
  1109. CSGBrush *new_brush = memnew(CSGBrush);
  1110. int face_count = 12; //it's a cube..
  1111. bool invert_val = get_flip_faces();
  1112. Ref<Material> base_material = get_material();
  1113. Vector<Vector3> faces;
  1114. Vector<Vector2> uvs;
  1115. Vector<bool> smooth;
  1116. Vector<Ref<Material>> materials;
  1117. Vector<bool> invert;
  1118. faces.resize(face_count * 3);
  1119. uvs.resize(face_count * 3);
  1120. smooth.resize(face_count);
  1121. materials.resize(face_count);
  1122. invert.resize(face_count);
  1123. {
  1124. Vector3 *facesw = faces.ptrw();
  1125. Vector2 *uvsw = uvs.ptrw();
  1126. bool *smoothw = smooth.ptrw();
  1127. Ref<Material> *materialsw = materials.ptrw();
  1128. bool *invertw = invert.ptrw();
  1129. int face = 0;
  1130. Vector3 vertex_mul = size / 2;
  1131. {
  1132. for (int i = 0; i < 6; i++) {
  1133. Vector3 face_points[4];
  1134. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  1135. for (int j = 0; j < 4; j++) {
  1136. float v[3];
  1137. v[0] = 1.0;
  1138. v[1] = 1 - 2 * ((j >> 1) & 1);
  1139. v[2] = v[1] * (1 - 2 * (j & 1));
  1140. for (int k = 0; k < 3; k++) {
  1141. if (i < 3) {
  1142. face_points[j][(i + k) % 3] = v[k];
  1143. } else {
  1144. face_points[3 - j][(i + k) % 3] = -v[k];
  1145. }
  1146. }
  1147. }
  1148. Vector2 u[4];
  1149. for (int j = 0; j < 4; j++) {
  1150. u[j] = Vector2(uv_points[j * 2 + 0], uv_points[j * 2 + 1]);
  1151. }
  1152. //face 1
  1153. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1154. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1155. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1156. uvsw[face * 3 + 0] = u[0];
  1157. uvsw[face * 3 + 1] = u[1];
  1158. uvsw[face * 3 + 2] = u[2];
  1159. smoothw[face] = false;
  1160. invertw[face] = invert_val;
  1161. materialsw[face] = base_material;
  1162. face++;
  1163. //face 2
  1164. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1165. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1166. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1167. uvsw[face * 3 + 0] = u[2];
  1168. uvsw[face * 3 + 1] = u[3];
  1169. uvsw[face * 3 + 2] = u[0];
  1170. smoothw[face] = false;
  1171. invertw[face] = invert_val;
  1172. materialsw[face] = base_material;
  1173. face++;
  1174. }
  1175. }
  1176. if (face != face_count) {
  1177. ERR_PRINT("Face mismatch bug! fix code");
  1178. }
  1179. }
  1180. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1181. return new_brush;
  1182. }
  1183. void CSGBox3D::_bind_methods() {
  1184. ClassDB::bind_method(D_METHOD("set_size", "size"), &CSGBox3D::set_size);
  1185. ClassDB::bind_method(D_METHOD("get_size"), &CSGBox3D::get_size);
  1186. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox3D::set_material);
  1187. ClassDB::bind_method(D_METHOD("get_material"), &CSGBox3D::get_material);
  1188. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1189. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1190. }
  1191. void CSGBox3D::set_size(const Vector3 &p_size) {
  1192. size = p_size;
  1193. _make_dirty();
  1194. update_gizmos();
  1195. }
  1196. Vector3 CSGBox3D::get_size() const {
  1197. return size;
  1198. }
  1199. #ifndef DISABLE_DEPRECATED
  1200. // Kept for compatibility from 3.x to 4.0.
  1201. bool CSGBox3D::_set(const StringName &p_name, const Variant &p_value) {
  1202. if (p_name == "width") {
  1203. size.x = p_value;
  1204. _make_dirty();
  1205. update_gizmos();
  1206. return true;
  1207. } else if (p_name == "height") {
  1208. size.y = p_value;
  1209. _make_dirty();
  1210. update_gizmos();
  1211. return true;
  1212. } else if (p_name == "depth") {
  1213. size.z = p_value;
  1214. _make_dirty();
  1215. update_gizmos();
  1216. return true;
  1217. } else {
  1218. return false;
  1219. }
  1220. }
  1221. #endif
  1222. void CSGBox3D::set_material(const Ref<Material> &p_material) {
  1223. material = p_material;
  1224. _make_dirty();
  1225. update_gizmos();
  1226. }
  1227. Ref<Material> CSGBox3D::get_material() const {
  1228. return material;
  1229. }
  1230. ///////////////
  1231. CSGBrush *CSGCylinder3D::_build_brush() {
  1232. // set our bounding box
  1233. CSGBrush *new_brush = memnew(CSGBrush);
  1234. int face_count = sides * (cone ? 1 : 2) + sides + (cone ? 0 : sides);
  1235. bool invert_val = get_flip_faces();
  1236. Ref<Material> base_material = get_material();
  1237. Vector<Vector3> faces;
  1238. Vector<Vector2> uvs;
  1239. Vector<bool> smooth;
  1240. Vector<Ref<Material>> materials;
  1241. Vector<bool> invert;
  1242. faces.resize(face_count * 3);
  1243. uvs.resize(face_count * 3);
  1244. smooth.resize(face_count);
  1245. materials.resize(face_count);
  1246. invert.resize(face_count);
  1247. {
  1248. Vector3 *facesw = faces.ptrw();
  1249. Vector2 *uvsw = uvs.ptrw();
  1250. bool *smoothw = smooth.ptrw();
  1251. Ref<Material> *materialsw = materials.ptrw();
  1252. bool *invertw = invert.ptrw();
  1253. int face = 0;
  1254. Vector3 vertex_mul(radius, height * 0.5, radius);
  1255. {
  1256. for (int i = 0; i < sides; i++) {
  1257. float inc = float(i) / sides;
  1258. float inc_n = float((i + 1)) / sides;
  1259. if (i == sides - 1) {
  1260. inc_n = 0;
  1261. }
  1262. float ang = inc * Math_TAU;
  1263. float ang_n = inc_n * Math_TAU;
  1264. Vector3 face_base(Math::cos(ang), 0, Math::sin(ang));
  1265. Vector3 face_base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
  1266. Vector3 face_points[4] = {
  1267. face_base + Vector3(0, -1, 0),
  1268. face_base_n + Vector3(0, -1, 0),
  1269. face_base_n * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1270. face_base * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1271. };
  1272. Vector2 u[4] = {
  1273. Vector2(inc, 0),
  1274. Vector2(inc_n, 0),
  1275. Vector2(inc_n, 1),
  1276. Vector2(inc, 1),
  1277. };
  1278. //side face 1
  1279. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1280. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1281. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1282. uvsw[face * 3 + 0] = u[0];
  1283. uvsw[face * 3 + 1] = u[1];
  1284. uvsw[face * 3 + 2] = u[2];
  1285. smoothw[face] = smooth_faces;
  1286. invertw[face] = invert_val;
  1287. materialsw[face] = base_material;
  1288. face++;
  1289. if (!cone) {
  1290. //side face 2
  1291. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1292. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1293. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1294. uvsw[face * 3 + 0] = u[2];
  1295. uvsw[face * 3 + 1] = u[3];
  1296. uvsw[face * 3 + 2] = u[0];
  1297. smoothw[face] = smooth_faces;
  1298. invertw[face] = invert_val;
  1299. materialsw[face] = base_material;
  1300. face++;
  1301. }
  1302. //bottom face 1
  1303. facesw[face * 3 + 0] = face_points[1] * vertex_mul;
  1304. facesw[face * 3 + 1] = face_points[0] * vertex_mul;
  1305. facesw[face * 3 + 2] = Vector3(0, -1, 0) * vertex_mul;
  1306. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1307. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1308. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1309. smoothw[face] = false;
  1310. invertw[face] = invert_val;
  1311. materialsw[face] = base_material;
  1312. face++;
  1313. if (!cone) {
  1314. //top face 1
  1315. facesw[face * 3 + 0] = face_points[3] * vertex_mul;
  1316. facesw[face * 3 + 1] = face_points[2] * vertex_mul;
  1317. facesw[face * 3 + 2] = Vector3(0, 1, 0) * vertex_mul;
  1318. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1319. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1320. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1321. smoothw[face] = false;
  1322. invertw[face] = invert_val;
  1323. materialsw[face] = base_material;
  1324. face++;
  1325. }
  1326. }
  1327. }
  1328. if (face != face_count) {
  1329. ERR_PRINT("Face mismatch bug! fix code");
  1330. }
  1331. }
  1332. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1333. return new_brush;
  1334. }
  1335. void CSGCylinder3D::_bind_methods() {
  1336. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCylinder3D::set_radius);
  1337. ClassDB::bind_method(D_METHOD("get_radius"), &CSGCylinder3D::get_radius);
  1338. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCylinder3D::set_height);
  1339. ClassDB::bind_method(D_METHOD("get_height"), &CSGCylinder3D::get_height);
  1340. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGCylinder3D::set_sides);
  1341. ClassDB::bind_method(D_METHOD("get_sides"), &CSGCylinder3D::get_sides);
  1342. ClassDB::bind_method(D_METHOD("set_cone", "cone"), &CSGCylinder3D::set_cone);
  1343. ClassDB::bind_method(D_METHOD("is_cone"), &CSGCylinder3D::is_cone);
  1344. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGCylinder3D::set_material);
  1345. ClassDB::bind_method(D_METHOD("get_material"), &CSGCylinder3D::get_material);
  1346. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGCylinder3D::set_smooth_faces);
  1347. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGCylinder3D::get_smooth_faces);
  1348. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_radius", "get_radius");
  1349. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_height", "get_height");
  1350. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1351. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cone"), "set_cone", "is_cone");
  1352. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1353. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1354. }
  1355. void CSGCylinder3D::set_radius(const float p_radius) {
  1356. radius = p_radius;
  1357. _make_dirty();
  1358. update_gizmos();
  1359. }
  1360. float CSGCylinder3D::get_radius() const {
  1361. return radius;
  1362. }
  1363. void CSGCylinder3D::set_height(const float p_height) {
  1364. height = p_height;
  1365. _make_dirty();
  1366. update_gizmos();
  1367. }
  1368. float CSGCylinder3D::get_height() const {
  1369. return height;
  1370. }
  1371. void CSGCylinder3D::set_sides(const int p_sides) {
  1372. ERR_FAIL_COND(p_sides < 3);
  1373. sides = p_sides;
  1374. _make_dirty();
  1375. update_gizmos();
  1376. }
  1377. int CSGCylinder3D::get_sides() const {
  1378. return sides;
  1379. }
  1380. void CSGCylinder3D::set_cone(const bool p_cone) {
  1381. cone = p_cone;
  1382. _make_dirty();
  1383. update_gizmos();
  1384. }
  1385. bool CSGCylinder3D::is_cone() const {
  1386. return cone;
  1387. }
  1388. void CSGCylinder3D::set_smooth_faces(const bool p_smooth_faces) {
  1389. smooth_faces = p_smooth_faces;
  1390. _make_dirty();
  1391. }
  1392. bool CSGCylinder3D::get_smooth_faces() const {
  1393. return smooth_faces;
  1394. }
  1395. void CSGCylinder3D::set_material(const Ref<Material> &p_material) {
  1396. material = p_material;
  1397. _make_dirty();
  1398. }
  1399. Ref<Material> CSGCylinder3D::get_material() const {
  1400. return material;
  1401. }
  1402. CSGCylinder3D::CSGCylinder3D() {
  1403. // defaults
  1404. radius = 0.5;
  1405. height = 2.0;
  1406. sides = 8;
  1407. cone = false;
  1408. smooth_faces = true;
  1409. }
  1410. ///////////////
  1411. CSGBrush *CSGTorus3D::_build_brush() {
  1412. // set our bounding box
  1413. float min_radius = inner_radius;
  1414. float max_radius = outer_radius;
  1415. if (min_radius == max_radius) {
  1416. return memnew(CSGBrush); //sorry, can't
  1417. }
  1418. if (min_radius > max_radius) {
  1419. SWAP(min_radius, max_radius);
  1420. }
  1421. float radius = (max_radius - min_radius) * 0.5;
  1422. CSGBrush *new_brush = memnew(CSGBrush);
  1423. int face_count = ring_sides * sides * 2;
  1424. bool invert_val = get_flip_faces();
  1425. Ref<Material> base_material = get_material();
  1426. Vector<Vector3> faces;
  1427. Vector<Vector2> uvs;
  1428. Vector<bool> smooth;
  1429. Vector<Ref<Material>> materials;
  1430. Vector<bool> invert;
  1431. faces.resize(face_count * 3);
  1432. uvs.resize(face_count * 3);
  1433. smooth.resize(face_count);
  1434. materials.resize(face_count);
  1435. invert.resize(face_count);
  1436. {
  1437. Vector3 *facesw = faces.ptrw();
  1438. Vector2 *uvsw = uvs.ptrw();
  1439. bool *smoothw = smooth.ptrw();
  1440. Ref<Material> *materialsw = materials.ptrw();
  1441. bool *invertw = invert.ptrw();
  1442. int face = 0;
  1443. {
  1444. for (int i = 0; i < sides; i++) {
  1445. float inci = float(i) / sides;
  1446. float inci_n = float((i + 1)) / sides;
  1447. if (i == sides - 1) {
  1448. inci_n = 0;
  1449. }
  1450. float angi = inci * Math_TAU;
  1451. float angi_n = inci_n * Math_TAU;
  1452. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1453. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1454. for (int j = 0; j < ring_sides; j++) {
  1455. float incj = float(j) / ring_sides;
  1456. float incj_n = float((j + 1)) / ring_sides;
  1457. if (j == ring_sides - 1) {
  1458. incj_n = 0;
  1459. }
  1460. float angj = incj * Math_TAU;
  1461. float angj_n = incj_n * Math_TAU;
  1462. Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
  1463. Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
  1464. Vector3 face_points[4] = {
  1465. Vector3(normali.x * normalj.x, normalj.y, normali.z * normalj.x),
  1466. Vector3(normali.x * normalj_n.x, normalj_n.y, normali.z * normalj_n.x),
  1467. Vector3(normali_n.x * normalj_n.x, normalj_n.y, normali_n.z * normalj_n.x),
  1468. Vector3(normali_n.x * normalj.x, normalj.y, normali_n.z * normalj.x)
  1469. };
  1470. Vector2 u[4] = {
  1471. Vector2(inci, incj),
  1472. Vector2(inci, incj_n),
  1473. Vector2(inci_n, incj_n),
  1474. Vector2(inci_n, incj),
  1475. };
  1476. // face 1
  1477. facesw[face * 3 + 0] = face_points[0];
  1478. facesw[face * 3 + 1] = face_points[2];
  1479. facesw[face * 3 + 2] = face_points[1];
  1480. uvsw[face * 3 + 0] = u[0];
  1481. uvsw[face * 3 + 1] = u[2];
  1482. uvsw[face * 3 + 2] = u[1];
  1483. smoothw[face] = smooth_faces;
  1484. invertw[face] = invert_val;
  1485. materialsw[face] = base_material;
  1486. face++;
  1487. //face 2
  1488. facesw[face * 3 + 0] = face_points[3];
  1489. facesw[face * 3 + 1] = face_points[2];
  1490. facesw[face * 3 + 2] = face_points[0];
  1491. uvsw[face * 3 + 0] = u[3];
  1492. uvsw[face * 3 + 1] = u[2];
  1493. uvsw[face * 3 + 2] = u[0];
  1494. smoothw[face] = smooth_faces;
  1495. invertw[face] = invert_val;
  1496. materialsw[face] = base_material;
  1497. face++;
  1498. }
  1499. }
  1500. }
  1501. if (face != face_count) {
  1502. ERR_PRINT("Face mismatch bug! fix code");
  1503. }
  1504. }
  1505. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1506. return new_brush;
  1507. }
  1508. void CSGTorus3D::_bind_methods() {
  1509. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &CSGTorus3D::set_inner_radius);
  1510. ClassDB::bind_method(D_METHOD("get_inner_radius"), &CSGTorus3D::get_inner_radius);
  1511. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &CSGTorus3D::set_outer_radius);
  1512. ClassDB::bind_method(D_METHOD("get_outer_radius"), &CSGTorus3D::get_outer_radius);
  1513. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGTorus3D::set_sides);
  1514. ClassDB::bind_method(D_METHOD("get_sides"), &CSGTorus3D::get_sides);
  1515. ClassDB::bind_method(D_METHOD("set_ring_sides", "sides"), &CSGTorus3D::set_ring_sides);
  1516. ClassDB::bind_method(D_METHOD("get_ring_sides"), &CSGTorus3D::get_ring_sides);
  1517. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGTorus3D::set_material);
  1518. ClassDB::bind_method(D_METHOD("get_material"), &CSGTorus3D::get_material);
  1519. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGTorus3D::set_smooth_faces);
  1520. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGTorus3D::get_smooth_faces);
  1521. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inner_radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_inner_radius", "get_inner_radius");
  1522. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "outer_radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_outer_radius", "get_outer_radius");
  1523. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1524. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_ring_sides", "get_ring_sides");
  1525. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1526. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1527. }
  1528. void CSGTorus3D::set_inner_radius(const float p_inner_radius) {
  1529. inner_radius = p_inner_radius;
  1530. _make_dirty();
  1531. update_gizmos();
  1532. }
  1533. float CSGTorus3D::get_inner_radius() const {
  1534. return inner_radius;
  1535. }
  1536. void CSGTorus3D::set_outer_radius(const float p_outer_radius) {
  1537. outer_radius = p_outer_radius;
  1538. _make_dirty();
  1539. update_gizmos();
  1540. }
  1541. float CSGTorus3D::get_outer_radius() const {
  1542. return outer_radius;
  1543. }
  1544. void CSGTorus3D::set_sides(const int p_sides) {
  1545. ERR_FAIL_COND(p_sides < 3);
  1546. sides = p_sides;
  1547. _make_dirty();
  1548. update_gizmos();
  1549. }
  1550. int CSGTorus3D::get_sides() const {
  1551. return sides;
  1552. }
  1553. void CSGTorus3D::set_ring_sides(const int p_ring_sides) {
  1554. ERR_FAIL_COND(p_ring_sides < 3);
  1555. ring_sides = p_ring_sides;
  1556. _make_dirty();
  1557. update_gizmos();
  1558. }
  1559. int CSGTorus3D::get_ring_sides() const {
  1560. return ring_sides;
  1561. }
  1562. void CSGTorus3D::set_smooth_faces(const bool p_smooth_faces) {
  1563. smooth_faces = p_smooth_faces;
  1564. _make_dirty();
  1565. }
  1566. bool CSGTorus3D::get_smooth_faces() const {
  1567. return smooth_faces;
  1568. }
  1569. void CSGTorus3D::set_material(const Ref<Material> &p_material) {
  1570. material = p_material;
  1571. _make_dirty();
  1572. }
  1573. Ref<Material> CSGTorus3D::get_material() const {
  1574. return material;
  1575. }
  1576. CSGTorus3D::CSGTorus3D() {
  1577. // defaults
  1578. inner_radius = 0.5;
  1579. outer_radius = 1.0;
  1580. sides = 8;
  1581. ring_sides = 6;
  1582. smooth_faces = true;
  1583. }
  1584. ///////////////
  1585. CSGBrush *CSGPolygon3D::_build_brush() {
  1586. CSGBrush *new_brush = memnew(CSGBrush);
  1587. if (polygon.size() < 3) {
  1588. return new_brush;
  1589. }
  1590. // Triangulate polygon shape.
  1591. Vector<Point2> shape_polygon = polygon;
  1592. if (Triangulate::get_area(shape_polygon) > 0) {
  1593. shape_polygon.reverse();
  1594. }
  1595. int shape_sides = shape_polygon.size();
  1596. Vector<int> shape_faces = Geometry2D::triangulate_polygon(shape_polygon);
  1597. ERR_FAIL_COND_V_MSG(shape_faces.size() < 3, new_brush, "Failed to triangulate CSGPolygon. Make sure the polygon doesn't have any intersecting edges.");
  1598. // Get polygon enclosing Rect2.
  1599. Rect2 shape_rect(shape_polygon[0], Vector2());
  1600. for (int i = 1; i < shape_sides; i++) {
  1601. shape_rect.expand_to(shape_polygon[i]);
  1602. }
  1603. // If MODE_PATH, check if curve has changed.
  1604. Ref<Curve3D> curve;
  1605. if (mode == MODE_PATH) {
  1606. Path3D *current_path = Object::cast_to<Path3D>(get_node_or_null(path_node));
  1607. if (path != current_path) {
  1608. if (path) {
  1609. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1610. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1611. path->set_update_callback(Callable());
  1612. }
  1613. path = current_path;
  1614. if (path) {
  1615. path->connect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1616. path->connect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1617. path->set_update_callback(callable_mp(this, &CSGPolygon3D::_path_changed));
  1618. }
  1619. }
  1620. if (!path) {
  1621. return new_brush;
  1622. }
  1623. curve = path->get_curve();
  1624. if (curve.is_null() || curve->get_point_count() < 2) {
  1625. return new_brush;
  1626. }
  1627. }
  1628. // Calculate the number extrusions, ends and faces.
  1629. int extrusions = 0;
  1630. int extrusion_face_count = shape_sides * 2;
  1631. int end_count = 0;
  1632. int shape_face_count = shape_faces.size() / 3;
  1633. real_t curve_length = 1.0;
  1634. switch (mode) {
  1635. case MODE_DEPTH:
  1636. extrusions = 1;
  1637. end_count = 2;
  1638. break;
  1639. case MODE_SPIN:
  1640. extrusions = spin_sides;
  1641. if (spin_degrees < 360) {
  1642. end_count = 2;
  1643. }
  1644. break;
  1645. case MODE_PATH: {
  1646. curve_length = curve->get_baked_length();
  1647. if (path_interval_type == PATH_INTERVAL_DISTANCE) {
  1648. extrusions = MAX(1, Math::ceil(curve_length / path_interval)) + 1;
  1649. } else {
  1650. extrusions = Math::ceil(1.0 * curve->get_point_count() / path_interval);
  1651. }
  1652. if (!path_joined) {
  1653. end_count = 2;
  1654. extrusions -= 1;
  1655. }
  1656. } break;
  1657. }
  1658. int face_count = extrusions * extrusion_face_count + end_count * shape_face_count;
  1659. // Initialize variables used to create the mesh.
  1660. Ref<Material> base_material = get_material();
  1661. Vector<Vector3> faces;
  1662. Vector<Vector2> uvs;
  1663. Vector<bool> smooth;
  1664. Vector<Ref<Material>> materials;
  1665. Vector<bool> invert;
  1666. faces.resize(face_count * 3);
  1667. uvs.resize(face_count * 3);
  1668. smooth.resize(face_count);
  1669. materials.resize(face_count);
  1670. invert.resize(face_count);
  1671. int faces_removed = 0;
  1672. {
  1673. Vector3 *facesw = faces.ptrw();
  1674. Vector2 *uvsw = uvs.ptrw();
  1675. bool *smoothw = smooth.ptrw();
  1676. Ref<Material> *materialsw = materials.ptrw();
  1677. bool *invertw = invert.ptrw();
  1678. int face = 0;
  1679. Transform3D base_xform;
  1680. Transform3D current_xform;
  1681. Transform3D previous_xform;
  1682. Transform3D previous_previous_xform;
  1683. double u_step = 1.0 / extrusions;
  1684. if (path_u_distance > 0.0) {
  1685. u_step *= curve_length / path_u_distance;
  1686. }
  1687. double v_step = 1.0 / shape_sides;
  1688. double spin_step = Math::deg_to_rad(spin_degrees / spin_sides);
  1689. double extrusion_step = 1.0 / extrusions;
  1690. if (mode == MODE_PATH) {
  1691. if (path_joined) {
  1692. extrusion_step = 1.0 / (extrusions - 1);
  1693. }
  1694. extrusion_step *= curve_length;
  1695. }
  1696. if (mode == MODE_PATH) {
  1697. if (!path_local) {
  1698. base_xform = path->get_global_transform();
  1699. }
  1700. Vector3 current_point = curve->sample_baked(0);
  1701. Vector3 next_point = curve->sample_baked(extrusion_step);
  1702. Vector3 current_up = Vector3(0, 1, 0);
  1703. Vector3 direction = next_point - current_point;
  1704. if (path_joined) {
  1705. Vector3 last_point = curve->sample_baked(curve->get_baked_length());
  1706. direction = next_point - last_point;
  1707. }
  1708. switch (path_rotation) {
  1709. case PATH_ROTATION_POLYGON:
  1710. direction = Vector3(0, 0, -1);
  1711. break;
  1712. case PATH_ROTATION_PATH:
  1713. break;
  1714. case PATH_ROTATION_PATH_FOLLOW:
  1715. current_up = curve->sample_baked_up_vector(0, true);
  1716. break;
  1717. }
  1718. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1719. current_xform = base_xform.translated_local(current_point) * facing;
  1720. }
  1721. // Create the mesh.
  1722. if (end_count > 0) {
  1723. // Add front end face.
  1724. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  1725. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  1726. // We need to reverse the rotation of the shape face vertices.
  1727. int index = shape_faces[face_idx * 3 + 2 - face_vertex_idx];
  1728. Point2 p = shape_polygon[index];
  1729. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  1730. // Use the left side of the bottom half of the y-inverted texture.
  1731. uv.x = uv.x / 2;
  1732. uv.y = 1 - (uv.y / 2);
  1733. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  1734. uvsw[face * 3 + face_vertex_idx] = uv;
  1735. }
  1736. smoothw[face] = false;
  1737. materialsw[face] = base_material;
  1738. invertw[face] = flip_faces;
  1739. face++;
  1740. }
  1741. }
  1742. real_t angle_simplify_dot = Math::cos(Math::deg_to_rad(path_simplify_angle));
  1743. Vector3 previous_simplify_dir = Vector3(0, 0, 0);
  1744. int faces_combined = 0;
  1745. // Add extrusion faces.
  1746. for (int x0 = 0; x0 < extrusions; x0++) {
  1747. previous_previous_xform = previous_xform;
  1748. previous_xform = current_xform;
  1749. switch (mode) {
  1750. case MODE_DEPTH: {
  1751. current_xform.translate_local(Vector3(0, 0, -depth));
  1752. } break;
  1753. case MODE_SPIN: {
  1754. current_xform.rotate(Vector3(0, 1, 0), spin_step);
  1755. } break;
  1756. case MODE_PATH: {
  1757. double previous_offset = x0 * extrusion_step;
  1758. double current_offset = (x0 + 1) * extrusion_step;
  1759. double next_offset = (x0 + 2) * extrusion_step;
  1760. if (x0 == extrusions - 1) {
  1761. if (path_joined) {
  1762. current_offset = 0;
  1763. next_offset = extrusion_step;
  1764. } else {
  1765. next_offset = current_offset;
  1766. }
  1767. }
  1768. Vector3 previous_point = curve->sample_baked(previous_offset);
  1769. Vector3 current_point = curve->sample_baked(current_offset);
  1770. Vector3 next_point = curve->sample_baked(next_offset);
  1771. Vector3 current_up = Vector3(0, 1, 0);
  1772. Vector3 direction = next_point - previous_point;
  1773. Vector3 current_dir = (current_point - previous_point).normalized();
  1774. // If the angles are similar, remove the previous face and replace it with this one.
  1775. if (path_simplify_angle > 0.0 && x0 > 0 && previous_simplify_dir.dot(current_dir) > angle_simplify_dot) {
  1776. faces_combined += 1;
  1777. previous_xform = previous_previous_xform;
  1778. face -= extrusion_face_count;
  1779. faces_removed += extrusion_face_count;
  1780. } else {
  1781. faces_combined = 0;
  1782. previous_simplify_dir = current_dir;
  1783. }
  1784. switch (path_rotation) {
  1785. case PATH_ROTATION_POLYGON:
  1786. direction = Vector3(0, 0, -1);
  1787. break;
  1788. case PATH_ROTATION_PATH:
  1789. break;
  1790. case PATH_ROTATION_PATH_FOLLOW:
  1791. current_up = curve->sample_baked_up_vector(current_offset, true);
  1792. break;
  1793. }
  1794. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1795. current_xform = base_xform.translated_local(current_point) * facing;
  1796. } break;
  1797. }
  1798. double u0 = (x0 - faces_combined) * u_step;
  1799. double u1 = ((x0 + 1) * u_step);
  1800. if (mode == MODE_PATH && !path_continuous_u) {
  1801. u0 = 0.0;
  1802. u1 = 1.0;
  1803. }
  1804. for (int y0 = 0; y0 < shape_sides; y0++) {
  1805. int y1 = (y0 + 1) % shape_sides;
  1806. // Use the top half of the texture.
  1807. double v0 = (y0 * v_step) / 2;
  1808. double v1 = ((y0 + 1) * v_step) / 2;
  1809. Vector3 v[4] = {
  1810. previous_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1811. current_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1812. current_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1813. previous_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1814. };
  1815. Vector2 u[4] = {
  1816. Vector2(u0, v0),
  1817. Vector2(u1, v0),
  1818. Vector2(u1, v1),
  1819. Vector2(u0, v1),
  1820. };
  1821. // Face 1
  1822. facesw[face * 3 + 0] = v[0];
  1823. facesw[face * 3 + 1] = v[1];
  1824. facesw[face * 3 + 2] = v[2];
  1825. uvsw[face * 3 + 0] = u[0];
  1826. uvsw[face * 3 + 1] = u[1];
  1827. uvsw[face * 3 + 2] = u[2];
  1828. smoothw[face] = smooth_faces;
  1829. invertw[face] = flip_faces;
  1830. materialsw[face] = base_material;
  1831. face++;
  1832. // Face 2
  1833. facesw[face * 3 + 0] = v[2];
  1834. facesw[face * 3 + 1] = v[3];
  1835. facesw[face * 3 + 2] = v[0];
  1836. uvsw[face * 3 + 0] = u[2];
  1837. uvsw[face * 3 + 1] = u[3];
  1838. uvsw[face * 3 + 2] = u[0];
  1839. smoothw[face] = smooth_faces;
  1840. invertw[face] = flip_faces;
  1841. materialsw[face] = base_material;
  1842. face++;
  1843. }
  1844. }
  1845. if (end_count > 1) {
  1846. // Add back end face.
  1847. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  1848. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  1849. int index = shape_faces[face_idx * 3 + face_vertex_idx];
  1850. Point2 p = shape_polygon[index];
  1851. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  1852. // Use the x-inverted ride side of the bottom half of the y-inverted texture.
  1853. uv.x = 1 - uv.x / 2;
  1854. uv.y = 1 - (uv.y / 2);
  1855. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  1856. uvsw[face * 3 + face_vertex_idx] = uv;
  1857. }
  1858. smoothw[face] = false;
  1859. materialsw[face] = base_material;
  1860. invertw[face] = flip_faces;
  1861. face++;
  1862. }
  1863. }
  1864. face_count -= faces_removed;
  1865. ERR_FAIL_COND_V_MSG(face != face_count, new_brush, "Bug: Failed to create the CSGPolygon mesh correctly.");
  1866. }
  1867. if (faces_removed > 0) {
  1868. faces.resize(face_count * 3);
  1869. uvs.resize(face_count * 3);
  1870. smooth.resize(face_count);
  1871. materials.resize(face_count);
  1872. invert.resize(face_count);
  1873. }
  1874. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1875. return new_brush;
  1876. }
  1877. void CSGPolygon3D::_notification(int p_what) {
  1878. if (p_what == NOTIFICATION_EXIT_TREE) {
  1879. if (path) {
  1880. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1881. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1882. path = nullptr;
  1883. }
  1884. }
  1885. }
  1886. void CSGPolygon3D::_validate_property(PropertyInfo &p_property) const {
  1887. if (p_property.name.begins_with("spin") && mode != MODE_SPIN) {
  1888. p_property.usage = PROPERTY_USAGE_NONE;
  1889. }
  1890. if (p_property.name.begins_with("path") && mode != MODE_PATH) {
  1891. p_property.usage = PROPERTY_USAGE_NONE;
  1892. }
  1893. if (p_property.name == "depth" && mode != MODE_DEPTH) {
  1894. p_property.usage = PROPERTY_USAGE_NONE;
  1895. }
  1896. }
  1897. void CSGPolygon3D::_path_changed() {
  1898. _make_dirty();
  1899. update_gizmos();
  1900. }
  1901. void CSGPolygon3D::_path_exited() {
  1902. path = nullptr;
  1903. }
  1904. void CSGPolygon3D::_bind_methods() {
  1905. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CSGPolygon3D::set_polygon);
  1906. ClassDB::bind_method(D_METHOD("get_polygon"), &CSGPolygon3D::get_polygon);
  1907. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSGPolygon3D::set_mode);
  1908. ClassDB::bind_method(D_METHOD("get_mode"), &CSGPolygon3D::get_mode);
  1909. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGPolygon3D::set_depth);
  1910. ClassDB::bind_method(D_METHOD("get_depth"), &CSGPolygon3D::get_depth);
  1911. ClassDB::bind_method(D_METHOD("set_spin_degrees", "degrees"), &CSGPolygon3D::set_spin_degrees);
  1912. ClassDB::bind_method(D_METHOD("get_spin_degrees"), &CSGPolygon3D::get_spin_degrees);
  1913. ClassDB::bind_method(D_METHOD("set_spin_sides", "spin_sides"), &CSGPolygon3D::set_spin_sides);
  1914. ClassDB::bind_method(D_METHOD("get_spin_sides"), &CSGPolygon3D::get_spin_sides);
  1915. ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon3D::set_path_node);
  1916. ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon3D::get_path_node);
  1917. ClassDB::bind_method(D_METHOD("set_path_interval_type", "interval_type"), &CSGPolygon3D::set_path_interval_type);
  1918. ClassDB::bind_method(D_METHOD("get_path_interval_type"), &CSGPolygon3D::get_path_interval_type);
  1919. ClassDB::bind_method(D_METHOD("set_path_interval", "interval"), &CSGPolygon3D::set_path_interval);
  1920. ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon3D::get_path_interval);
  1921. ClassDB::bind_method(D_METHOD("set_path_simplify_angle", "degrees"), &CSGPolygon3D::set_path_simplify_angle);
  1922. ClassDB::bind_method(D_METHOD("get_path_simplify_angle"), &CSGPolygon3D::get_path_simplify_angle);
  1923. ClassDB::bind_method(D_METHOD("set_path_rotation", "path_rotation"), &CSGPolygon3D::set_path_rotation);
  1924. ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon3D::get_path_rotation);
  1925. ClassDB::bind_method(D_METHOD("set_path_local", "enable"), &CSGPolygon3D::set_path_local);
  1926. ClassDB::bind_method(D_METHOD("is_path_local"), &CSGPolygon3D::is_path_local);
  1927. ClassDB::bind_method(D_METHOD("set_path_continuous_u", "enable"), &CSGPolygon3D::set_path_continuous_u);
  1928. ClassDB::bind_method(D_METHOD("is_path_continuous_u"), &CSGPolygon3D::is_path_continuous_u);
  1929. ClassDB::bind_method(D_METHOD("set_path_u_distance", "distance"), &CSGPolygon3D::set_path_u_distance);
  1930. ClassDB::bind_method(D_METHOD("get_path_u_distance"), &CSGPolygon3D::get_path_u_distance);
  1931. ClassDB::bind_method(D_METHOD("set_path_joined", "enable"), &CSGPolygon3D::set_path_joined);
  1932. ClassDB::bind_method(D_METHOD("is_path_joined"), &CSGPolygon3D::is_path_joined);
  1933. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGPolygon3D::set_material);
  1934. ClassDB::bind_method(D_METHOD("get_material"), &CSGPolygon3D::get_material);
  1935. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGPolygon3D::set_smooth_faces);
  1936. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGPolygon3D::get_smooth_faces);
  1937. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CSGPolygon3D::_is_editable_3d_polygon);
  1938. ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &CSGPolygon3D::_has_editable_3d_polygon_no_depth);
  1939. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  1940. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Depth,Spin,Path"), "set_mode", "get_mode");
  1941. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.01,100.0,0.01,or_greater,exp,suffix:m"), "set_depth", "get_depth");
  1942. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
  1943. ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
  1944. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path3D"), "set_path_node", "get_path_node");
  1945. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_interval_type", PROPERTY_HINT_ENUM, "Distance,Subdivide"), "set_path_interval_type", "get_path_interval_type");
  1946. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_interval", PROPERTY_HINT_RANGE, "0.01,1.0,0.01,exp,or_greater"), "set_path_interval", "get_path_interval");
  1947. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_simplify_angle", PROPERTY_HINT_RANGE, "0.0,180.0,0.1"), "set_path_simplify_angle", "get_path_simplify_angle");
  1948. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
  1949. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
  1950. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
  1951. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_u_distance", PROPERTY_HINT_RANGE, "0.0,10.0,0.01,or_greater,suffix:m"), "set_path_u_distance", "get_path_u_distance");
  1952. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_joined"), "set_path_joined", "is_path_joined");
  1953. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1954. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1955. BIND_ENUM_CONSTANT(MODE_DEPTH);
  1956. BIND_ENUM_CONSTANT(MODE_SPIN);
  1957. BIND_ENUM_CONSTANT(MODE_PATH);
  1958. BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
  1959. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
  1960. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
  1961. BIND_ENUM_CONSTANT(PATH_INTERVAL_DISTANCE);
  1962. BIND_ENUM_CONSTANT(PATH_INTERVAL_SUBDIVIDE);
  1963. }
  1964. void CSGPolygon3D::set_polygon(const Vector<Vector2> &p_polygon) {
  1965. polygon = p_polygon;
  1966. _make_dirty();
  1967. update_gizmos();
  1968. }
  1969. Vector<Vector2> CSGPolygon3D::get_polygon() const {
  1970. return polygon;
  1971. }
  1972. void CSGPolygon3D::set_mode(Mode p_mode) {
  1973. mode = p_mode;
  1974. _make_dirty();
  1975. update_gizmos();
  1976. notify_property_list_changed();
  1977. }
  1978. CSGPolygon3D::Mode CSGPolygon3D::get_mode() const {
  1979. return mode;
  1980. }
  1981. void CSGPolygon3D::set_depth(const float p_depth) {
  1982. ERR_FAIL_COND(p_depth < 0.001);
  1983. depth = p_depth;
  1984. _make_dirty();
  1985. update_gizmos();
  1986. }
  1987. float CSGPolygon3D::get_depth() const {
  1988. return depth;
  1989. }
  1990. void CSGPolygon3D::set_path_continuous_u(bool p_enable) {
  1991. path_continuous_u = p_enable;
  1992. _make_dirty();
  1993. }
  1994. bool CSGPolygon3D::is_path_continuous_u() const {
  1995. return path_continuous_u;
  1996. }
  1997. void CSGPolygon3D::set_path_u_distance(real_t p_path_u_distance) {
  1998. path_u_distance = p_path_u_distance;
  1999. _make_dirty();
  2000. update_gizmos();
  2001. }
  2002. real_t CSGPolygon3D::get_path_u_distance() const {
  2003. return path_u_distance;
  2004. }
  2005. void CSGPolygon3D::set_spin_degrees(const float p_spin_degrees) {
  2006. ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
  2007. spin_degrees = p_spin_degrees;
  2008. _make_dirty();
  2009. update_gizmos();
  2010. }
  2011. float CSGPolygon3D::get_spin_degrees() const {
  2012. return spin_degrees;
  2013. }
  2014. void CSGPolygon3D::set_spin_sides(int p_spin_sides) {
  2015. ERR_FAIL_COND(p_spin_sides < 3);
  2016. spin_sides = p_spin_sides;
  2017. _make_dirty();
  2018. update_gizmos();
  2019. }
  2020. int CSGPolygon3D::get_spin_sides() const {
  2021. return spin_sides;
  2022. }
  2023. void CSGPolygon3D::set_path_node(const NodePath &p_path) {
  2024. path_node = p_path;
  2025. _make_dirty();
  2026. update_gizmos();
  2027. }
  2028. NodePath CSGPolygon3D::get_path_node() const {
  2029. return path_node;
  2030. }
  2031. void CSGPolygon3D::set_path_interval_type(PathIntervalType p_interval_type) {
  2032. path_interval_type = p_interval_type;
  2033. _make_dirty();
  2034. update_gizmos();
  2035. }
  2036. CSGPolygon3D::PathIntervalType CSGPolygon3D::get_path_interval_type() const {
  2037. return path_interval_type;
  2038. }
  2039. void CSGPolygon3D::set_path_interval(float p_interval) {
  2040. path_interval = p_interval;
  2041. _make_dirty();
  2042. update_gizmos();
  2043. }
  2044. float CSGPolygon3D::get_path_interval() const {
  2045. return path_interval;
  2046. }
  2047. void CSGPolygon3D::set_path_simplify_angle(float p_angle) {
  2048. path_simplify_angle = p_angle;
  2049. _make_dirty();
  2050. update_gizmos();
  2051. }
  2052. float CSGPolygon3D::get_path_simplify_angle() const {
  2053. return path_simplify_angle;
  2054. }
  2055. void CSGPolygon3D::set_path_rotation(PathRotation p_rotation) {
  2056. path_rotation = p_rotation;
  2057. _make_dirty();
  2058. update_gizmos();
  2059. }
  2060. CSGPolygon3D::PathRotation CSGPolygon3D::get_path_rotation() const {
  2061. return path_rotation;
  2062. }
  2063. void CSGPolygon3D::set_path_local(bool p_enable) {
  2064. path_local = p_enable;
  2065. _make_dirty();
  2066. update_gizmos();
  2067. }
  2068. bool CSGPolygon3D::is_path_local() const {
  2069. return path_local;
  2070. }
  2071. void CSGPolygon3D::set_path_joined(bool p_enable) {
  2072. path_joined = p_enable;
  2073. _make_dirty();
  2074. update_gizmos();
  2075. }
  2076. bool CSGPolygon3D::is_path_joined() const {
  2077. return path_joined;
  2078. }
  2079. void CSGPolygon3D::set_smooth_faces(const bool p_smooth_faces) {
  2080. smooth_faces = p_smooth_faces;
  2081. _make_dirty();
  2082. }
  2083. bool CSGPolygon3D::get_smooth_faces() const {
  2084. return smooth_faces;
  2085. }
  2086. void CSGPolygon3D::set_material(const Ref<Material> &p_material) {
  2087. material = p_material;
  2088. _make_dirty();
  2089. }
  2090. Ref<Material> CSGPolygon3D::get_material() const {
  2091. return material;
  2092. }
  2093. bool CSGPolygon3D::_is_editable_3d_polygon() const {
  2094. return true;
  2095. }
  2096. bool CSGPolygon3D::_has_editable_3d_polygon_no_depth() const {
  2097. return true;
  2098. }
  2099. CSGPolygon3D::CSGPolygon3D() {
  2100. // defaults
  2101. mode = MODE_DEPTH;
  2102. polygon.push_back(Vector2(0, 0));
  2103. polygon.push_back(Vector2(0, 1));
  2104. polygon.push_back(Vector2(1, 1));
  2105. polygon.push_back(Vector2(1, 0));
  2106. depth = 1.0;
  2107. spin_degrees = 360;
  2108. spin_sides = 8;
  2109. smooth_faces = false;
  2110. path_interval_type = PATH_INTERVAL_DISTANCE;
  2111. path_interval = 1.0;
  2112. path_simplify_angle = 0.0;
  2113. path_rotation = PATH_ROTATION_PATH_FOLLOW;
  2114. path_local = false;
  2115. path_continuous_u = true;
  2116. path_u_distance = 1.0;
  2117. path_joined = false;
  2118. path = nullptr;
  2119. }