csg_shape.cpp 83 KB

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