csg_shape.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722
  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_VISIBILITY_CHANGED: {
  727. if (!is_root_shape() && last_visible != is_visible()) {
  728. // Update this node's parent only if its own visibility has changed, not the visibility of parent nodes
  729. parent_shape->_make_dirty();
  730. }
  731. last_visible = is_visible();
  732. } break;
  733. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  734. if (!is_root_shape()) {
  735. // Update this node's parent only if its own transformation has changed, not the transformation of parent nodes
  736. parent_shape->_make_dirty();
  737. }
  738. } break;
  739. case NOTIFICATION_ENTER_TREE: {
  740. if (use_collision && is_root_shape()) {
  741. root_collision_shape.instantiate();
  742. root_collision_instance = PhysicsServer3D::get_singleton()->body_create();
  743. PhysicsServer3D::get_singleton()->body_set_mode(root_collision_instance, PhysicsServer3D::BODY_MODE_STATIC);
  744. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  745. PhysicsServer3D::get_singleton()->body_add_shape(root_collision_instance, root_collision_shape->get_rid());
  746. PhysicsServer3D::get_singleton()->body_set_space(root_collision_instance, get_world_3d()->get_space());
  747. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(root_collision_instance, get_instance_id());
  748. set_collision_layer(collision_layer);
  749. set_collision_mask(collision_mask);
  750. set_collision_priority(collision_priority);
  751. debug_shape_old_transform = get_global_transform();
  752. _make_dirty();
  753. }
  754. } break;
  755. case NOTIFICATION_EXIT_TREE: {
  756. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  757. PhysicsServer3D::get_singleton()->free(root_collision_instance);
  758. root_collision_instance = RID();
  759. root_collision_shape.unref();
  760. _clear_debug_collision_shape();
  761. }
  762. } break;
  763. case NOTIFICATION_TRANSFORM_CHANGED: {
  764. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  765. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  766. }
  767. _on_transform_changed();
  768. } break;
  769. }
  770. }
  771. void CSGShape3D::set_operation(Operation p_operation) {
  772. operation = p_operation;
  773. _make_dirty();
  774. update_gizmos();
  775. }
  776. CSGShape3D::Operation CSGShape3D::get_operation() const {
  777. return operation;
  778. }
  779. void CSGShape3D::set_calculate_tangents(bool p_calculate_tangents) {
  780. calculate_tangents = p_calculate_tangents;
  781. _make_dirty();
  782. }
  783. bool CSGShape3D::is_calculating_tangents() const {
  784. return calculate_tangents;
  785. }
  786. void CSGShape3D::_validate_property(PropertyInfo &p_property) const {
  787. bool is_collision_prefixed = p_property.name.begins_with("collision_");
  788. if ((is_collision_prefixed || p_property.name.begins_with("use_collision")) && is_inside_tree() && !is_root_shape()) {
  789. //hide collision if not root
  790. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  791. } else if (is_collision_prefixed && !bool(get("use_collision"))) {
  792. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  793. }
  794. }
  795. Array CSGShape3D::get_meshes() const {
  796. if (root_mesh.is_valid()) {
  797. Array arr;
  798. arr.resize(2);
  799. arr[0] = Transform3D();
  800. arr[1] = root_mesh;
  801. return arr;
  802. }
  803. return Array();
  804. }
  805. void CSGShape3D::_bind_methods() {
  806. ClassDB::bind_method(D_METHOD("_update_shape"), &CSGShape3D::_update_shape);
  807. ClassDB::bind_method(D_METHOD("is_root_shape"), &CSGShape3D::is_root_shape);
  808. ClassDB::bind_method(D_METHOD("set_operation", "operation"), &CSGShape3D::set_operation);
  809. ClassDB::bind_method(D_METHOD("get_operation"), &CSGShape3D::get_operation);
  810. #ifndef DISABLE_DEPRECATED
  811. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &CSGShape3D::set_snap);
  812. ClassDB::bind_method(D_METHOD("get_snap"), &CSGShape3D::get_snap);
  813. #endif // DISABLE_DEPRECATED
  814. ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape3D::set_use_collision);
  815. ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape3D::is_using_collision);
  816. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CSGShape3D::set_collision_layer);
  817. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CSGShape3D::get_collision_layer);
  818. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CSGShape3D::set_collision_mask);
  819. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CSGShape3D::get_collision_mask);
  820. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CSGShape3D::set_collision_mask_value);
  821. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CSGShape3D::get_collision_mask_value);
  822. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CSGShape3D::set_collision_layer_value);
  823. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CSGShape3D::get_collision_layer_value);
  824. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CSGShape3D::set_collision_priority);
  825. ClassDB::bind_method(D_METHOD("get_collision_priority"), &CSGShape3D::get_collision_priority);
  826. ClassDB::bind_method(D_METHOD("set_calculate_tangents", "enabled"), &CSGShape3D::set_calculate_tangents);
  827. ClassDB::bind_method(D_METHOD("is_calculating_tangents"), &CSGShape3D::is_calculating_tangents);
  828. ClassDB::bind_method(D_METHOD("get_meshes"), &CSGShape3D::get_meshes);
  829. ClassDB::bind_method(D_METHOD("bake_static_mesh"), &CSGShape3D::bake_static_mesh);
  830. ClassDB::bind_method(D_METHOD("bake_collision_shape"), &CSGShape3D::bake_collision_shape);
  831. ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "Union,Intersection,Subtraction"), "set_operation", "get_operation");
  832. #ifndef DISABLE_DEPRECATED
  833. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_RANGE, "0.000001,1,0.000001,suffix:m", PROPERTY_USAGE_NONE), "set_snap", "get_snap");
  834. #endif // DISABLE_DEPRECATED
  835. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "calculate_tangents"), "set_calculate_tangents", "is_calculating_tangents");
  836. ADD_GROUP("Collision", "collision_");
  837. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_collision"), "set_use_collision", "is_using_collision");
  838. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  839. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  840. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  841. BIND_ENUM_CONSTANT(OPERATION_UNION);
  842. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  843. BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
  844. }
  845. CSGShape3D::CSGShape3D() {
  846. set_notify_local_transform(true);
  847. }
  848. CSGShape3D::~CSGShape3D() {
  849. if (brush) {
  850. memdelete(brush);
  851. brush = nullptr;
  852. }
  853. }
  854. //////////////////////////////////
  855. CSGBrush *CSGCombiner3D::_build_brush() {
  856. return memnew(CSGBrush); //does not build anything
  857. }
  858. CSGCombiner3D::CSGCombiner3D() {
  859. }
  860. /////////////////////
  861. 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) {
  862. CSGBrush *new_brush = memnew(CSGBrush);
  863. Vector<bool> invert;
  864. invert.resize(p_vertices.size() / 3);
  865. {
  866. int ic = invert.size();
  867. bool *w = invert.ptrw();
  868. for (int i = 0; i < ic; i++) {
  869. w[i] = flip_faces;
  870. }
  871. }
  872. new_brush->build_from_faces(p_vertices, p_uv, p_smooth, p_materials, invert);
  873. return new_brush;
  874. }
  875. void CSGPrimitive3D::_bind_methods() {
  876. ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &CSGPrimitive3D::set_flip_faces);
  877. ClassDB::bind_method(D_METHOD("get_flip_faces"), &CSGPrimitive3D::get_flip_faces);
  878. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
  879. }
  880. void CSGPrimitive3D::set_flip_faces(bool p_invert) {
  881. if (flip_faces == p_invert) {
  882. return;
  883. }
  884. flip_faces = p_invert;
  885. _make_dirty();
  886. }
  887. bool CSGPrimitive3D::get_flip_faces() {
  888. return flip_faces;
  889. }
  890. CSGPrimitive3D::CSGPrimitive3D() {
  891. flip_faces = false;
  892. }
  893. /////////////////////
  894. CSGBrush *CSGMesh3D::_build_brush() {
  895. if (!mesh.is_valid()) {
  896. return memnew(CSGBrush);
  897. }
  898. Vector<Vector3> vertices;
  899. Vector<bool> smooth;
  900. Vector<Ref<Material>> materials;
  901. Vector<Vector2> uvs;
  902. Ref<Material> base_material = get_material();
  903. for (int i = 0; i < mesh->get_surface_count(); i++) {
  904. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  905. continue;
  906. }
  907. Array arrays = mesh->surface_get_arrays(i);
  908. if (arrays.size() == 0) {
  909. _make_dirty();
  910. ERR_FAIL_COND_V(arrays.is_empty(), memnew(CSGBrush));
  911. }
  912. Vector<Vector3> avertices = arrays[Mesh::ARRAY_VERTEX];
  913. if (avertices.size() == 0) {
  914. continue;
  915. }
  916. const Vector3 *vr = avertices.ptr();
  917. Vector<Vector3> anormals = arrays[Mesh::ARRAY_NORMAL];
  918. const Vector3 *nr = nullptr;
  919. if (anormals.size()) {
  920. nr = anormals.ptr();
  921. }
  922. Vector<Vector2> auvs = arrays[Mesh::ARRAY_TEX_UV];
  923. const Vector2 *uvr = nullptr;
  924. if (auvs.size()) {
  925. uvr = auvs.ptr();
  926. }
  927. Ref<Material> mat;
  928. if (base_material.is_valid()) {
  929. mat = base_material;
  930. } else {
  931. mat = mesh->surface_get_material(i);
  932. }
  933. Vector<int> aindices = arrays[Mesh::ARRAY_INDEX];
  934. if (aindices.size()) {
  935. int as = vertices.size();
  936. int is = aindices.size();
  937. vertices.resize(as + is);
  938. smooth.resize((as + is) / 3);
  939. materials.resize((as + is) / 3);
  940. uvs.resize(as + is);
  941. Vector3 *vw = vertices.ptrw();
  942. bool *sw = smooth.ptrw();
  943. Vector2 *uvw = uvs.ptrw();
  944. Ref<Material> *mw = materials.ptrw();
  945. const int *ir = aindices.ptr();
  946. for (int j = 0; j < is; j += 3) {
  947. Vector3 vertex[3];
  948. Vector3 normal[3];
  949. Vector2 uv[3];
  950. for (int k = 0; k < 3; k++) {
  951. int idx = ir[j + k];
  952. vertex[k] = vr[idx];
  953. if (nr) {
  954. normal[k] = nr[idx];
  955. }
  956. if (uvr) {
  957. uv[k] = uvr[idx];
  958. }
  959. }
  960. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  961. vw[as + j + 0] = vertex[0];
  962. vw[as + j + 1] = vertex[1];
  963. vw[as + j + 2] = vertex[2];
  964. uvw[as + j + 0] = uv[0];
  965. uvw[as + j + 1] = uv[1];
  966. uvw[as + j + 2] = uv[2];
  967. sw[(as + j) / 3] = !flat;
  968. mw[(as + j) / 3] = mat;
  969. }
  970. } else {
  971. int as = vertices.size();
  972. int is = avertices.size();
  973. vertices.resize(as + is);
  974. smooth.resize((as + is) / 3);
  975. uvs.resize(as + is);
  976. materials.resize((as + is) / 3);
  977. Vector3 *vw = vertices.ptrw();
  978. bool *sw = smooth.ptrw();
  979. Vector2 *uvw = uvs.ptrw();
  980. Ref<Material> *mw = materials.ptrw();
  981. for (int j = 0; j < is; j += 3) {
  982. Vector3 vertex[3];
  983. Vector3 normal[3];
  984. Vector2 uv[3];
  985. for (int k = 0; k < 3; k++) {
  986. vertex[k] = vr[j + k];
  987. if (nr) {
  988. normal[k] = nr[j + k];
  989. }
  990. if (uvr) {
  991. uv[k] = uvr[j + k];
  992. }
  993. }
  994. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  995. vw[as + j + 0] = vertex[0];
  996. vw[as + j + 1] = vertex[1];
  997. vw[as + j + 2] = vertex[2];
  998. uvw[as + j + 0] = uv[0];
  999. uvw[as + j + 1] = uv[1];
  1000. uvw[as + j + 2] = uv[2];
  1001. sw[(as + j) / 3] = !flat;
  1002. mw[(as + j) / 3] = mat;
  1003. }
  1004. }
  1005. }
  1006. if (vertices.size() == 0) {
  1007. return memnew(CSGBrush);
  1008. }
  1009. return _create_brush_from_arrays(vertices, uvs, smooth, materials);
  1010. }
  1011. void CSGMesh3D::_mesh_changed() {
  1012. _make_dirty();
  1013. callable_mp((Node3D *)this, &Node3D::update_gizmos).call_deferred();
  1014. }
  1015. void CSGMesh3D::set_material(const Ref<Material> &p_material) {
  1016. if (material == p_material) {
  1017. return;
  1018. }
  1019. material = p_material;
  1020. _make_dirty();
  1021. }
  1022. Ref<Material> CSGMesh3D::get_material() const {
  1023. return material;
  1024. }
  1025. void CSGMesh3D::_bind_methods() {
  1026. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CSGMesh3D::set_mesh);
  1027. ClassDB::bind_method(D_METHOD("get_mesh"), &CSGMesh3D::get_mesh);
  1028. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGMesh3D::set_material);
  1029. ClassDB::bind_method(D_METHOD("get_material"), &CSGMesh3D::get_material);
  1030. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  1031. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1032. }
  1033. void CSGMesh3D::set_mesh(const Ref<Mesh> &p_mesh) {
  1034. if (mesh == p_mesh) {
  1035. return;
  1036. }
  1037. if (mesh.is_valid()) {
  1038. mesh->disconnect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  1039. }
  1040. mesh = p_mesh;
  1041. if (mesh.is_valid()) {
  1042. mesh->connect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  1043. }
  1044. _mesh_changed();
  1045. }
  1046. Ref<Mesh> CSGMesh3D::get_mesh() {
  1047. return mesh;
  1048. }
  1049. ////////////////////////////////
  1050. CSGBrush *CSGSphere3D::_build_brush() {
  1051. // set our bounding box
  1052. CSGBrush *new_brush = memnew(CSGBrush);
  1053. int face_count = rings * radial_segments * 2 - radial_segments * 2;
  1054. bool invert_val = get_flip_faces();
  1055. Ref<Material> base_material = get_material();
  1056. Vector<Vector3> faces;
  1057. Vector<Vector2> uvs;
  1058. Vector<bool> smooth;
  1059. Vector<Ref<Material>> materials;
  1060. Vector<bool> invert;
  1061. faces.resize(face_count * 3);
  1062. uvs.resize(face_count * 3);
  1063. smooth.resize(face_count);
  1064. materials.resize(face_count);
  1065. invert.resize(face_count);
  1066. {
  1067. Vector3 *facesw = faces.ptrw();
  1068. Vector2 *uvsw = uvs.ptrw();
  1069. bool *smoothw = smooth.ptrw();
  1070. Ref<Material> *materialsw = materials.ptrw();
  1071. bool *invertw = invert.ptrw();
  1072. // We want to follow an order that's convenient for UVs.
  1073. // For latitude step we start at the top and move down like in an image.
  1074. const double latitude_step = -Math_PI / rings;
  1075. const double longitude_step = Math_TAU / radial_segments;
  1076. int face = 0;
  1077. for (int i = 0; i < rings; i++) {
  1078. double latitude0 = latitude_step * i + Math_TAU / 4;
  1079. double cos0 = Math::cos(latitude0);
  1080. double sin0 = Math::sin(latitude0);
  1081. double v0 = double(i) / rings;
  1082. double latitude1 = latitude_step * (i + 1) + Math_TAU / 4;
  1083. double cos1 = Math::cos(latitude1);
  1084. double sin1 = Math::sin(latitude1);
  1085. double v1 = double(i + 1) / rings;
  1086. for (int j = 0; j < radial_segments; j++) {
  1087. double longitude0 = longitude_step * j;
  1088. // We give sin to X and cos to Z on purpose.
  1089. // This allows UVs to be CCW on +X so it maps to images well.
  1090. double x0 = Math::sin(longitude0);
  1091. double z0 = Math::cos(longitude0);
  1092. double u0 = double(j) / radial_segments;
  1093. double longitude1 = longitude_step * (j + 1);
  1094. if (j == radial_segments - 1) {
  1095. longitude1 = 0;
  1096. }
  1097. double x1 = Math::sin(longitude1);
  1098. double z1 = Math::cos(longitude1);
  1099. double u1 = double(j + 1) / radial_segments;
  1100. Vector3 v[4] = {
  1101. Vector3(x0 * cos0, sin0, z0 * cos0) * radius,
  1102. Vector3(x1 * cos0, sin0, z1 * cos0) * radius,
  1103. Vector3(x1 * cos1, sin1, z1 * cos1) * radius,
  1104. Vector3(x0 * cos1, sin1, z0 * cos1) * radius,
  1105. };
  1106. Vector2 u[4] = {
  1107. Vector2(u0, v0),
  1108. Vector2(u1, v0),
  1109. Vector2(u1, v1),
  1110. Vector2(u0, v1),
  1111. };
  1112. // Draw the first face, but skip this at the north pole (i == 0).
  1113. if (i > 0) {
  1114. facesw[face * 3 + 0] = v[0];
  1115. facesw[face * 3 + 1] = v[1];
  1116. facesw[face * 3 + 2] = v[2];
  1117. uvsw[face * 3 + 0] = u[0];
  1118. uvsw[face * 3 + 1] = u[1];
  1119. uvsw[face * 3 + 2] = u[2];
  1120. smoothw[face] = smooth_faces;
  1121. invertw[face] = invert_val;
  1122. materialsw[face] = base_material;
  1123. face++;
  1124. }
  1125. // Draw the second face, but skip this at the south pole (i == rings - 1).
  1126. if (i < rings - 1) {
  1127. facesw[face * 3 + 0] = v[2];
  1128. facesw[face * 3 + 1] = v[3];
  1129. facesw[face * 3 + 2] = v[0];
  1130. uvsw[face * 3 + 0] = u[2];
  1131. uvsw[face * 3 + 1] = u[3];
  1132. uvsw[face * 3 + 2] = u[0];
  1133. smoothw[face] = smooth_faces;
  1134. invertw[face] = invert_val;
  1135. materialsw[face] = base_material;
  1136. face++;
  1137. }
  1138. }
  1139. }
  1140. if (face != face_count) {
  1141. ERR_PRINT("Face mismatch bug! fix code");
  1142. }
  1143. }
  1144. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1145. return new_brush;
  1146. }
  1147. void CSGSphere3D::_bind_methods() {
  1148. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGSphere3D::set_radius);
  1149. ClassDB::bind_method(D_METHOD("get_radius"), &CSGSphere3D::get_radius);
  1150. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGSphere3D::set_radial_segments);
  1151. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGSphere3D::get_radial_segments);
  1152. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CSGSphere3D::set_rings);
  1153. ClassDB::bind_method(D_METHOD("get_rings"), &CSGSphere3D::get_rings);
  1154. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGSphere3D::set_smooth_faces);
  1155. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGSphere3D::get_smooth_faces);
  1156. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGSphere3D::set_material);
  1157. ClassDB::bind_method(D_METHOD("get_material"), &CSGSphere3D::get_material);
  1158. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,suffix:m"), "set_radius", "get_radius");
  1159. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments");
  1160. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1"), "set_rings", "get_rings");
  1161. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1162. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1163. }
  1164. void CSGSphere3D::set_radius(const float p_radius) {
  1165. ERR_FAIL_COND(p_radius <= 0);
  1166. radius = p_radius;
  1167. _make_dirty();
  1168. update_gizmos();
  1169. }
  1170. float CSGSphere3D::get_radius() const {
  1171. return radius;
  1172. }
  1173. void CSGSphere3D::set_radial_segments(const int p_radial_segments) {
  1174. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  1175. _make_dirty();
  1176. update_gizmos();
  1177. }
  1178. int CSGSphere3D::get_radial_segments() const {
  1179. return radial_segments;
  1180. }
  1181. void CSGSphere3D::set_rings(const int p_rings) {
  1182. rings = p_rings > 1 ? p_rings : 1;
  1183. _make_dirty();
  1184. update_gizmos();
  1185. }
  1186. int CSGSphere3D::get_rings() const {
  1187. return rings;
  1188. }
  1189. void CSGSphere3D::set_smooth_faces(const bool p_smooth_faces) {
  1190. smooth_faces = p_smooth_faces;
  1191. _make_dirty();
  1192. }
  1193. bool CSGSphere3D::get_smooth_faces() const {
  1194. return smooth_faces;
  1195. }
  1196. void CSGSphere3D::set_material(const Ref<Material> &p_material) {
  1197. material = p_material;
  1198. _make_dirty();
  1199. }
  1200. Ref<Material> CSGSphere3D::get_material() const {
  1201. return material;
  1202. }
  1203. CSGSphere3D::CSGSphere3D() {
  1204. // defaults
  1205. radius = 0.5;
  1206. radial_segments = 12;
  1207. rings = 6;
  1208. smooth_faces = true;
  1209. }
  1210. ///////////////
  1211. CSGBrush *CSGBox3D::_build_brush() {
  1212. // set our bounding box
  1213. CSGBrush *new_brush = memnew(CSGBrush);
  1214. int face_count = 12; //it's a cube..
  1215. bool invert_val = get_flip_faces();
  1216. Ref<Material> base_material = get_material();
  1217. Vector<Vector3> faces;
  1218. Vector<Vector2> uvs;
  1219. Vector<bool> smooth;
  1220. Vector<Ref<Material>> materials;
  1221. Vector<bool> invert;
  1222. faces.resize(face_count * 3);
  1223. uvs.resize(face_count * 3);
  1224. smooth.resize(face_count);
  1225. materials.resize(face_count);
  1226. invert.resize(face_count);
  1227. {
  1228. Vector3 *facesw = faces.ptrw();
  1229. Vector2 *uvsw = uvs.ptrw();
  1230. bool *smoothw = smooth.ptrw();
  1231. Ref<Material> *materialsw = materials.ptrw();
  1232. bool *invertw = invert.ptrw();
  1233. int face = 0;
  1234. Vector3 vertex_mul = size / 2;
  1235. {
  1236. for (int i = 0; i < 6; i++) {
  1237. Vector3 face_points[4];
  1238. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  1239. for (int j = 0; j < 4; j++) {
  1240. float v[3];
  1241. v[0] = 1.0;
  1242. v[1] = 1 - 2 * ((j >> 1) & 1);
  1243. v[2] = v[1] * (1 - 2 * (j & 1));
  1244. for (int k = 0; k < 3; k++) {
  1245. if (i < 3) {
  1246. face_points[j][(i + k) % 3] = v[k];
  1247. } else {
  1248. face_points[3 - j][(i + k) % 3] = -v[k];
  1249. }
  1250. }
  1251. }
  1252. Vector2 u[4];
  1253. for (int j = 0; j < 4; j++) {
  1254. u[j] = Vector2(uv_points[j * 2 + 0], uv_points[j * 2 + 1]);
  1255. }
  1256. //face 1
  1257. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1258. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1259. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1260. uvsw[face * 3 + 0] = u[0];
  1261. uvsw[face * 3 + 1] = u[1];
  1262. uvsw[face * 3 + 2] = u[2];
  1263. smoothw[face] = false;
  1264. invertw[face] = invert_val;
  1265. materialsw[face] = base_material;
  1266. face++;
  1267. //face 2
  1268. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1269. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1270. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1271. uvsw[face * 3 + 0] = u[2];
  1272. uvsw[face * 3 + 1] = u[3];
  1273. uvsw[face * 3 + 2] = u[0];
  1274. smoothw[face] = false;
  1275. invertw[face] = invert_val;
  1276. materialsw[face] = base_material;
  1277. face++;
  1278. }
  1279. }
  1280. if (face != face_count) {
  1281. ERR_PRINT("Face mismatch bug! fix code");
  1282. }
  1283. }
  1284. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1285. return new_brush;
  1286. }
  1287. void CSGBox3D::_bind_methods() {
  1288. ClassDB::bind_method(D_METHOD("set_size", "size"), &CSGBox3D::set_size);
  1289. ClassDB::bind_method(D_METHOD("get_size"), &CSGBox3D::get_size);
  1290. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox3D::set_material);
  1291. ClassDB::bind_method(D_METHOD("get_material"), &CSGBox3D::get_material);
  1292. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1293. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1294. }
  1295. void CSGBox3D::set_size(const Vector3 &p_size) {
  1296. size = p_size;
  1297. _make_dirty();
  1298. update_gizmos();
  1299. }
  1300. Vector3 CSGBox3D::get_size() const {
  1301. return size;
  1302. }
  1303. #ifndef DISABLE_DEPRECATED
  1304. // Kept for compatibility from 3.x to 4.0.
  1305. bool CSGBox3D::_set(const StringName &p_name, const Variant &p_value) {
  1306. if (p_name == "width") {
  1307. size.x = p_value;
  1308. _make_dirty();
  1309. update_gizmos();
  1310. return true;
  1311. } else if (p_name == "height") {
  1312. size.y = p_value;
  1313. _make_dirty();
  1314. update_gizmos();
  1315. return true;
  1316. } else if (p_name == "depth") {
  1317. size.z = p_value;
  1318. _make_dirty();
  1319. update_gizmos();
  1320. return true;
  1321. } else {
  1322. return false;
  1323. }
  1324. }
  1325. #endif
  1326. void CSGBox3D::set_material(const Ref<Material> &p_material) {
  1327. material = p_material;
  1328. _make_dirty();
  1329. update_gizmos();
  1330. }
  1331. Ref<Material> CSGBox3D::get_material() const {
  1332. return material;
  1333. }
  1334. ///////////////
  1335. CSGBrush *CSGCylinder3D::_build_brush() {
  1336. // set our bounding box
  1337. CSGBrush *new_brush = memnew(CSGBrush);
  1338. int face_count = sides * (cone ? 1 : 2) + sides + (cone ? 0 : sides);
  1339. bool invert_val = get_flip_faces();
  1340. Ref<Material> base_material = get_material();
  1341. Vector<Vector3> faces;
  1342. Vector<Vector2> uvs;
  1343. Vector<bool> smooth;
  1344. Vector<Ref<Material>> materials;
  1345. Vector<bool> invert;
  1346. faces.resize(face_count * 3);
  1347. uvs.resize(face_count * 3);
  1348. smooth.resize(face_count);
  1349. materials.resize(face_count);
  1350. invert.resize(face_count);
  1351. {
  1352. Vector3 *facesw = faces.ptrw();
  1353. Vector2 *uvsw = uvs.ptrw();
  1354. bool *smoothw = smooth.ptrw();
  1355. Ref<Material> *materialsw = materials.ptrw();
  1356. bool *invertw = invert.ptrw();
  1357. int face = 0;
  1358. Vector3 vertex_mul(radius, height * 0.5, radius);
  1359. {
  1360. for (int i = 0; i < sides; i++) {
  1361. float inc = float(i) / sides;
  1362. float inc_n = float((i + 1)) / sides;
  1363. if (i == sides - 1) {
  1364. inc_n = 0;
  1365. }
  1366. float ang = inc * Math_TAU;
  1367. float ang_n = inc_n * Math_TAU;
  1368. Vector3 face_base(Math::cos(ang), 0, Math::sin(ang));
  1369. Vector3 face_base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
  1370. Vector3 face_points[4] = {
  1371. face_base + Vector3(0, -1, 0),
  1372. face_base_n + Vector3(0, -1, 0),
  1373. face_base_n * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1374. face_base * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1375. };
  1376. Vector2 u[4] = {
  1377. Vector2(inc, 0),
  1378. Vector2(inc_n, 0),
  1379. Vector2(inc_n, 1),
  1380. Vector2(inc, 1),
  1381. };
  1382. //side face 1
  1383. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1384. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1385. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1386. uvsw[face * 3 + 0] = u[0];
  1387. uvsw[face * 3 + 1] = u[1];
  1388. uvsw[face * 3 + 2] = u[2];
  1389. smoothw[face] = smooth_faces;
  1390. invertw[face] = invert_val;
  1391. materialsw[face] = base_material;
  1392. face++;
  1393. if (!cone) {
  1394. //side face 2
  1395. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1396. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1397. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1398. uvsw[face * 3 + 0] = u[2];
  1399. uvsw[face * 3 + 1] = u[3];
  1400. uvsw[face * 3 + 2] = u[0];
  1401. smoothw[face] = smooth_faces;
  1402. invertw[face] = invert_val;
  1403. materialsw[face] = base_material;
  1404. face++;
  1405. }
  1406. //bottom face 1
  1407. facesw[face * 3 + 0] = face_points[1] * vertex_mul;
  1408. facesw[face * 3 + 1] = face_points[0] * vertex_mul;
  1409. facesw[face * 3 + 2] = Vector3(0, -1, 0) * vertex_mul;
  1410. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1411. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1412. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1413. smoothw[face] = false;
  1414. invertw[face] = invert_val;
  1415. materialsw[face] = base_material;
  1416. face++;
  1417. if (!cone) {
  1418. //top face 1
  1419. facesw[face * 3 + 0] = face_points[3] * vertex_mul;
  1420. facesw[face * 3 + 1] = face_points[2] * vertex_mul;
  1421. facesw[face * 3 + 2] = Vector3(0, 1, 0) * vertex_mul;
  1422. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1423. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1424. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1425. smoothw[face] = false;
  1426. invertw[face] = invert_val;
  1427. materialsw[face] = base_material;
  1428. face++;
  1429. }
  1430. }
  1431. }
  1432. if (face != face_count) {
  1433. ERR_PRINT("Face mismatch bug! fix code");
  1434. }
  1435. }
  1436. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1437. return new_brush;
  1438. }
  1439. void CSGCylinder3D::_bind_methods() {
  1440. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCylinder3D::set_radius);
  1441. ClassDB::bind_method(D_METHOD("get_radius"), &CSGCylinder3D::get_radius);
  1442. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCylinder3D::set_height);
  1443. ClassDB::bind_method(D_METHOD("get_height"), &CSGCylinder3D::get_height);
  1444. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGCylinder3D::set_sides);
  1445. ClassDB::bind_method(D_METHOD("get_sides"), &CSGCylinder3D::get_sides);
  1446. ClassDB::bind_method(D_METHOD("set_cone", "cone"), &CSGCylinder3D::set_cone);
  1447. ClassDB::bind_method(D_METHOD("is_cone"), &CSGCylinder3D::is_cone);
  1448. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGCylinder3D::set_material);
  1449. ClassDB::bind_method(D_METHOD("get_material"), &CSGCylinder3D::get_material);
  1450. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGCylinder3D::set_smooth_faces);
  1451. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGCylinder3D::get_smooth_faces);
  1452. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_radius", "get_radius");
  1453. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_height", "get_height");
  1454. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1455. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cone"), "set_cone", "is_cone");
  1456. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1457. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1458. }
  1459. void CSGCylinder3D::set_radius(const float p_radius) {
  1460. radius = p_radius;
  1461. _make_dirty();
  1462. update_gizmos();
  1463. }
  1464. float CSGCylinder3D::get_radius() const {
  1465. return radius;
  1466. }
  1467. void CSGCylinder3D::set_height(const float p_height) {
  1468. height = p_height;
  1469. _make_dirty();
  1470. update_gizmos();
  1471. }
  1472. float CSGCylinder3D::get_height() const {
  1473. return height;
  1474. }
  1475. void CSGCylinder3D::set_sides(const int p_sides) {
  1476. ERR_FAIL_COND(p_sides < 3);
  1477. sides = p_sides;
  1478. _make_dirty();
  1479. update_gizmos();
  1480. }
  1481. int CSGCylinder3D::get_sides() const {
  1482. return sides;
  1483. }
  1484. void CSGCylinder3D::set_cone(const bool p_cone) {
  1485. cone = p_cone;
  1486. _make_dirty();
  1487. update_gizmos();
  1488. }
  1489. bool CSGCylinder3D::is_cone() const {
  1490. return cone;
  1491. }
  1492. void CSGCylinder3D::set_smooth_faces(const bool p_smooth_faces) {
  1493. smooth_faces = p_smooth_faces;
  1494. _make_dirty();
  1495. }
  1496. bool CSGCylinder3D::get_smooth_faces() const {
  1497. return smooth_faces;
  1498. }
  1499. void CSGCylinder3D::set_material(const Ref<Material> &p_material) {
  1500. material = p_material;
  1501. _make_dirty();
  1502. }
  1503. Ref<Material> CSGCylinder3D::get_material() const {
  1504. return material;
  1505. }
  1506. CSGCylinder3D::CSGCylinder3D() {
  1507. // defaults
  1508. radius = 0.5;
  1509. height = 2.0;
  1510. sides = 8;
  1511. cone = false;
  1512. smooth_faces = true;
  1513. }
  1514. ///////////////
  1515. CSGBrush *CSGTorus3D::_build_brush() {
  1516. // set our bounding box
  1517. float min_radius = inner_radius;
  1518. float max_radius = outer_radius;
  1519. if (min_radius == max_radius) {
  1520. return memnew(CSGBrush); //sorry, can't
  1521. }
  1522. if (min_radius > max_radius) {
  1523. SWAP(min_radius, max_radius);
  1524. }
  1525. float radius = (max_radius - min_radius) * 0.5;
  1526. CSGBrush *new_brush = memnew(CSGBrush);
  1527. int face_count = ring_sides * sides * 2;
  1528. bool invert_val = get_flip_faces();
  1529. Ref<Material> base_material = get_material();
  1530. Vector<Vector3> faces;
  1531. Vector<Vector2> uvs;
  1532. Vector<bool> smooth;
  1533. Vector<Ref<Material>> materials;
  1534. Vector<bool> invert;
  1535. faces.resize(face_count * 3);
  1536. uvs.resize(face_count * 3);
  1537. smooth.resize(face_count);
  1538. materials.resize(face_count);
  1539. invert.resize(face_count);
  1540. {
  1541. Vector3 *facesw = faces.ptrw();
  1542. Vector2 *uvsw = uvs.ptrw();
  1543. bool *smoothw = smooth.ptrw();
  1544. Ref<Material> *materialsw = materials.ptrw();
  1545. bool *invertw = invert.ptrw();
  1546. int face = 0;
  1547. {
  1548. for (int i = 0; i < sides; i++) {
  1549. float inci = float(i) / sides;
  1550. float inci_n = float((i + 1)) / sides;
  1551. if (i == sides - 1) {
  1552. inci_n = 0;
  1553. }
  1554. float angi = inci * Math_TAU;
  1555. float angi_n = inci_n * Math_TAU;
  1556. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1557. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1558. for (int j = 0; j < ring_sides; j++) {
  1559. float incj = float(j) / ring_sides;
  1560. float incj_n = float((j + 1)) / ring_sides;
  1561. if (j == ring_sides - 1) {
  1562. incj_n = 0;
  1563. }
  1564. float angj = incj * Math_TAU;
  1565. float angj_n = incj_n * Math_TAU;
  1566. Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
  1567. Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
  1568. Vector3 face_points[4] = {
  1569. Vector3(normali.x * normalj.x, normalj.y, normali.z * normalj.x),
  1570. Vector3(normali.x * normalj_n.x, normalj_n.y, normali.z * normalj_n.x),
  1571. Vector3(normali_n.x * normalj_n.x, normalj_n.y, normali_n.z * normalj_n.x),
  1572. Vector3(normali_n.x * normalj.x, normalj.y, normali_n.z * normalj.x)
  1573. };
  1574. Vector2 u[4] = {
  1575. Vector2(inci, incj),
  1576. Vector2(inci, incj_n),
  1577. Vector2(inci_n, incj_n),
  1578. Vector2(inci_n, incj),
  1579. };
  1580. // face 1
  1581. facesw[face * 3 + 0] = face_points[0];
  1582. facesw[face * 3 + 1] = face_points[2];
  1583. facesw[face * 3 + 2] = face_points[1];
  1584. uvsw[face * 3 + 0] = u[0];
  1585. uvsw[face * 3 + 1] = u[2];
  1586. uvsw[face * 3 + 2] = u[1];
  1587. smoothw[face] = smooth_faces;
  1588. invertw[face] = invert_val;
  1589. materialsw[face] = base_material;
  1590. face++;
  1591. //face 2
  1592. facesw[face * 3 + 0] = face_points[3];
  1593. facesw[face * 3 + 1] = face_points[2];
  1594. facesw[face * 3 + 2] = face_points[0];
  1595. uvsw[face * 3 + 0] = u[3];
  1596. uvsw[face * 3 + 1] = u[2];
  1597. uvsw[face * 3 + 2] = u[0];
  1598. smoothw[face] = smooth_faces;
  1599. invertw[face] = invert_val;
  1600. materialsw[face] = base_material;
  1601. face++;
  1602. }
  1603. }
  1604. }
  1605. if (face != face_count) {
  1606. ERR_PRINT("Face mismatch bug! fix code");
  1607. }
  1608. }
  1609. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1610. return new_brush;
  1611. }
  1612. void CSGTorus3D::_bind_methods() {
  1613. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &CSGTorus3D::set_inner_radius);
  1614. ClassDB::bind_method(D_METHOD("get_inner_radius"), &CSGTorus3D::get_inner_radius);
  1615. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &CSGTorus3D::set_outer_radius);
  1616. ClassDB::bind_method(D_METHOD("get_outer_radius"), &CSGTorus3D::get_outer_radius);
  1617. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGTorus3D::set_sides);
  1618. ClassDB::bind_method(D_METHOD("get_sides"), &CSGTorus3D::get_sides);
  1619. ClassDB::bind_method(D_METHOD("set_ring_sides", "sides"), &CSGTorus3D::set_ring_sides);
  1620. ClassDB::bind_method(D_METHOD("get_ring_sides"), &CSGTorus3D::get_ring_sides);
  1621. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGTorus3D::set_material);
  1622. ClassDB::bind_method(D_METHOD("get_material"), &CSGTorus3D::get_material);
  1623. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGTorus3D::set_smooth_faces);
  1624. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGTorus3D::get_smooth_faces);
  1625. 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");
  1626. 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");
  1627. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1628. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_ring_sides", "get_ring_sides");
  1629. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1630. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1631. }
  1632. void CSGTorus3D::set_inner_radius(const float p_inner_radius) {
  1633. inner_radius = p_inner_radius;
  1634. _make_dirty();
  1635. update_gizmos();
  1636. }
  1637. float CSGTorus3D::get_inner_radius() const {
  1638. return inner_radius;
  1639. }
  1640. void CSGTorus3D::set_outer_radius(const float p_outer_radius) {
  1641. outer_radius = p_outer_radius;
  1642. _make_dirty();
  1643. update_gizmos();
  1644. }
  1645. float CSGTorus3D::get_outer_radius() const {
  1646. return outer_radius;
  1647. }
  1648. void CSGTorus3D::set_sides(const int p_sides) {
  1649. ERR_FAIL_COND(p_sides < 3);
  1650. sides = p_sides;
  1651. _make_dirty();
  1652. update_gizmos();
  1653. }
  1654. int CSGTorus3D::get_sides() const {
  1655. return sides;
  1656. }
  1657. void CSGTorus3D::set_ring_sides(const int p_ring_sides) {
  1658. ERR_FAIL_COND(p_ring_sides < 3);
  1659. ring_sides = p_ring_sides;
  1660. _make_dirty();
  1661. update_gizmos();
  1662. }
  1663. int CSGTorus3D::get_ring_sides() const {
  1664. return ring_sides;
  1665. }
  1666. void CSGTorus3D::set_smooth_faces(const bool p_smooth_faces) {
  1667. smooth_faces = p_smooth_faces;
  1668. _make_dirty();
  1669. }
  1670. bool CSGTorus3D::get_smooth_faces() const {
  1671. return smooth_faces;
  1672. }
  1673. void CSGTorus3D::set_material(const Ref<Material> &p_material) {
  1674. material = p_material;
  1675. _make_dirty();
  1676. }
  1677. Ref<Material> CSGTorus3D::get_material() const {
  1678. return material;
  1679. }
  1680. CSGTorus3D::CSGTorus3D() {
  1681. // defaults
  1682. inner_radius = 0.5;
  1683. outer_radius = 1.0;
  1684. sides = 8;
  1685. ring_sides = 6;
  1686. smooth_faces = true;
  1687. }
  1688. ///////////////
  1689. CSGBrush *CSGPolygon3D::_build_brush() {
  1690. CSGBrush *new_brush = memnew(CSGBrush);
  1691. if (polygon.size() < 3) {
  1692. return new_brush;
  1693. }
  1694. // Triangulate polygon shape.
  1695. Vector<Point2> shape_polygon = polygon;
  1696. if (Triangulate::get_area(shape_polygon) > 0) {
  1697. shape_polygon.reverse();
  1698. }
  1699. int shape_sides = shape_polygon.size();
  1700. Vector<int> shape_faces = Geometry2D::triangulate_polygon(shape_polygon);
  1701. 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.");
  1702. // Get polygon enclosing Rect2.
  1703. Rect2 shape_rect(shape_polygon[0], Vector2());
  1704. for (int i = 1; i < shape_sides; i++) {
  1705. shape_rect.expand_to(shape_polygon[i]);
  1706. }
  1707. // If MODE_PATH, check if curve has changed.
  1708. Ref<Curve3D> curve;
  1709. if (mode == MODE_PATH) {
  1710. Path3D *current_path = Object::cast_to<Path3D>(get_node_or_null(path_node));
  1711. if (path != current_path) {
  1712. if (path) {
  1713. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1714. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1715. path->set_update_callback(Callable());
  1716. }
  1717. path = current_path;
  1718. if (path) {
  1719. path->connect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1720. path->connect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1721. path->set_update_callback(callable_mp(this, &CSGPolygon3D::_path_changed));
  1722. }
  1723. }
  1724. if (!path) {
  1725. return new_brush;
  1726. }
  1727. curve = path->get_curve();
  1728. if (curve.is_null() || curve->get_point_count() < 2) {
  1729. return new_brush;
  1730. }
  1731. }
  1732. // Calculate the number extrusions, ends and faces.
  1733. int extrusions = 0;
  1734. int extrusion_face_count = shape_sides * 2;
  1735. int end_count = 0;
  1736. int shape_face_count = shape_faces.size() / 3;
  1737. real_t curve_length = 1.0;
  1738. switch (mode) {
  1739. case MODE_DEPTH:
  1740. extrusions = 1;
  1741. end_count = 2;
  1742. break;
  1743. case MODE_SPIN:
  1744. extrusions = spin_sides;
  1745. if (spin_degrees < 360) {
  1746. end_count = 2;
  1747. }
  1748. break;
  1749. case MODE_PATH: {
  1750. curve_length = curve->get_baked_length();
  1751. if (path_interval_type == PATH_INTERVAL_DISTANCE) {
  1752. extrusions = MAX(1, Math::ceil(curve_length / path_interval)) + 1;
  1753. } else {
  1754. extrusions = Math::ceil(1.0 * curve->get_point_count() / path_interval);
  1755. }
  1756. if (!path_joined) {
  1757. end_count = 2;
  1758. extrusions -= 1;
  1759. }
  1760. } break;
  1761. }
  1762. int face_count = extrusions * extrusion_face_count + end_count * shape_face_count;
  1763. // Initialize variables used to create the mesh.
  1764. Ref<Material> base_material = get_material();
  1765. Vector<Vector3> faces;
  1766. Vector<Vector2> uvs;
  1767. Vector<bool> smooth;
  1768. Vector<Ref<Material>> materials;
  1769. Vector<bool> invert;
  1770. faces.resize(face_count * 3);
  1771. uvs.resize(face_count * 3);
  1772. smooth.resize(face_count);
  1773. materials.resize(face_count);
  1774. invert.resize(face_count);
  1775. int faces_removed = 0;
  1776. {
  1777. Vector3 *facesw = faces.ptrw();
  1778. Vector2 *uvsw = uvs.ptrw();
  1779. bool *smoothw = smooth.ptrw();
  1780. Ref<Material> *materialsw = materials.ptrw();
  1781. bool *invertw = invert.ptrw();
  1782. int face = 0;
  1783. Transform3D base_xform;
  1784. Transform3D current_xform;
  1785. Transform3D previous_xform;
  1786. Transform3D previous_previous_xform;
  1787. double u_step = 1.0 / extrusions;
  1788. if (path_u_distance > 0.0) {
  1789. u_step *= curve_length / path_u_distance;
  1790. }
  1791. double v_step = 1.0 / shape_sides;
  1792. double spin_step = Math::deg_to_rad(spin_degrees / spin_sides);
  1793. double extrusion_step = 1.0 / extrusions;
  1794. if (mode == MODE_PATH) {
  1795. if (path_joined) {
  1796. extrusion_step = 1.0 / (extrusions - 1);
  1797. }
  1798. extrusion_step *= curve_length;
  1799. }
  1800. if (mode == MODE_PATH) {
  1801. if (!path_local) {
  1802. base_xform = path->get_global_transform();
  1803. }
  1804. Vector3 current_point = curve->sample_baked(0);
  1805. Vector3 next_point = curve->sample_baked(extrusion_step);
  1806. Vector3 current_up = Vector3(0, 1, 0);
  1807. Vector3 direction = next_point - current_point;
  1808. if (path_joined) {
  1809. Vector3 last_point = curve->sample_baked(curve->get_baked_length());
  1810. direction = next_point - last_point;
  1811. }
  1812. switch (path_rotation) {
  1813. case PATH_ROTATION_POLYGON:
  1814. direction = Vector3(0, 0, -1);
  1815. break;
  1816. case PATH_ROTATION_PATH:
  1817. break;
  1818. case PATH_ROTATION_PATH_FOLLOW:
  1819. current_up = curve->sample_baked_up_vector(0, true);
  1820. break;
  1821. }
  1822. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1823. current_xform = base_xform.translated_local(current_point) * facing;
  1824. }
  1825. // Create the mesh.
  1826. if (end_count > 0) {
  1827. // Add front end face.
  1828. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  1829. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  1830. // We need to reverse the rotation of the shape face vertices.
  1831. int index = shape_faces[face_idx * 3 + 2 - face_vertex_idx];
  1832. Point2 p = shape_polygon[index];
  1833. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  1834. // Use the left side of the bottom half of the y-inverted texture.
  1835. uv.x = uv.x / 2;
  1836. uv.y = 1 - (uv.y / 2);
  1837. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  1838. uvsw[face * 3 + face_vertex_idx] = uv;
  1839. }
  1840. smoothw[face] = false;
  1841. materialsw[face] = base_material;
  1842. invertw[face] = flip_faces;
  1843. face++;
  1844. }
  1845. }
  1846. real_t angle_simplify_dot = Math::cos(Math::deg_to_rad(path_simplify_angle));
  1847. Vector3 previous_simplify_dir = Vector3(0, 0, 0);
  1848. int faces_combined = 0;
  1849. // Add extrusion faces.
  1850. for (int x0 = 0; x0 < extrusions; x0++) {
  1851. previous_previous_xform = previous_xform;
  1852. previous_xform = current_xform;
  1853. switch (mode) {
  1854. case MODE_DEPTH: {
  1855. current_xform.translate_local(Vector3(0, 0, -depth));
  1856. } break;
  1857. case MODE_SPIN: {
  1858. current_xform.rotate(Vector3(0, 1, 0), spin_step);
  1859. } break;
  1860. case MODE_PATH: {
  1861. double previous_offset = x0 * extrusion_step;
  1862. double current_offset = (x0 + 1) * extrusion_step;
  1863. double next_offset = (x0 + 2) * extrusion_step;
  1864. if (x0 == extrusions - 1) {
  1865. if (path_joined) {
  1866. current_offset = 0;
  1867. next_offset = extrusion_step;
  1868. } else {
  1869. next_offset = current_offset;
  1870. }
  1871. }
  1872. Vector3 previous_point = curve->sample_baked(previous_offset);
  1873. Vector3 current_point = curve->sample_baked(current_offset);
  1874. Vector3 next_point = curve->sample_baked(next_offset);
  1875. Vector3 current_up = Vector3(0, 1, 0);
  1876. Vector3 direction = next_point - previous_point;
  1877. Vector3 current_dir = (current_point - previous_point).normalized();
  1878. // If the angles are similar, remove the previous face and replace it with this one.
  1879. if (path_simplify_angle > 0.0 && x0 > 0 && previous_simplify_dir.dot(current_dir) > angle_simplify_dot) {
  1880. faces_combined += 1;
  1881. previous_xform = previous_previous_xform;
  1882. face -= extrusion_face_count;
  1883. faces_removed += extrusion_face_count;
  1884. } else {
  1885. faces_combined = 0;
  1886. previous_simplify_dir = current_dir;
  1887. }
  1888. switch (path_rotation) {
  1889. case PATH_ROTATION_POLYGON:
  1890. direction = Vector3(0, 0, -1);
  1891. break;
  1892. case PATH_ROTATION_PATH:
  1893. break;
  1894. case PATH_ROTATION_PATH_FOLLOW:
  1895. current_up = curve->sample_baked_up_vector(current_offset, true);
  1896. break;
  1897. }
  1898. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1899. current_xform = base_xform.translated_local(current_point) * facing;
  1900. } break;
  1901. }
  1902. double u0 = (x0 - faces_combined) * u_step;
  1903. double u1 = ((x0 + 1) * u_step);
  1904. if (mode == MODE_PATH && !path_continuous_u) {
  1905. u0 = 0.0;
  1906. u1 = 1.0;
  1907. }
  1908. for (int y0 = 0; y0 < shape_sides; y0++) {
  1909. int y1 = (y0 + 1) % shape_sides;
  1910. // Use the top half of the texture.
  1911. double v0 = (y0 * v_step) / 2;
  1912. double v1 = ((y0 + 1) * v_step) / 2;
  1913. Vector3 v[4] = {
  1914. previous_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1915. current_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1916. current_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1917. previous_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1918. };
  1919. Vector2 u[4] = {
  1920. Vector2(u0, v0),
  1921. Vector2(u1, v0),
  1922. Vector2(u1, v1),
  1923. Vector2(u0, v1),
  1924. };
  1925. // Face 1
  1926. facesw[face * 3 + 0] = v[0];
  1927. facesw[face * 3 + 1] = v[1];
  1928. facesw[face * 3 + 2] = v[2];
  1929. uvsw[face * 3 + 0] = u[0];
  1930. uvsw[face * 3 + 1] = u[1];
  1931. uvsw[face * 3 + 2] = u[2];
  1932. smoothw[face] = smooth_faces;
  1933. invertw[face] = flip_faces;
  1934. materialsw[face] = base_material;
  1935. face++;
  1936. // Face 2
  1937. facesw[face * 3 + 0] = v[2];
  1938. facesw[face * 3 + 1] = v[3];
  1939. facesw[face * 3 + 2] = v[0];
  1940. uvsw[face * 3 + 0] = u[2];
  1941. uvsw[face * 3 + 1] = u[3];
  1942. uvsw[face * 3 + 2] = u[0];
  1943. smoothw[face] = smooth_faces;
  1944. invertw[face] = flip_faces;
  1945. materialsw[face] = base_material;
  1946. face++;
  1947. }
  1948. }
  1949. if (end_count > 1) {
  1950. // Add back end face.
  1951. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  1952. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  1953. int index = shape_faces[face_idx * 3 + face_vertex_idx];
  1954. Point2 p = shape_polygon[index];
  1955. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  1956. // Use the x-inverted ride side of the bottom half of the y-inverted texture.
  1957. uv.x = 1 - uv.x / 2;
  1958. uv.y = 1 - (uv.y / 2);
  1959. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  1960. uvsw[face * 3 + face_vertex_idx] = uv;
  1961. }
  1962. smoothw[face] = false;
  1963. materialsw[face] = base_material;
  1964. invertw[face] = flip_faces;
  1965. face++;
  1966. }
  1967. }
  1968. face_count -= faces_removed;
  1969. ERR_FAIL_COND_V_MSG(face != face_count, new_brush, "Bug: Failed to create the CSGPolygon mesh correctly.");
  1970. }
  1971. if (faces_removed > 0) {
  1972. faces.resize(face_count * 3);
  1973. uvs.resize(face_count * 3);
  1974. smooth.resize(face_count);
  1975. materials.resize(face_count);
  1976. invert.resize(face_count);
  1977. }
  1978. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1979. return new_brush;
  1980. }
  1981. void CSGPolygon3D::_notification(int p_what) {
  1982. if (p_what == NOTIFICATION_EXIT_TREE) {
  1983. if (path) {
  1984. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1985. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1986. path = nullptr;
  1987. }
  1988. }
  1989. }
  1990. void CSGPolygon3D::_validate_property(PropertyInfo &p_property) const {
  1991. if (p_property.name.begins_with("spin") && mode != MODE_SPIN) {
  1992. p_property.usage = PROPERTY_USAGE_NONE;
  1993. }
  1994. if (p_property.name.begins_with("path") && mode != MODE_PATH) {
  1995. p_property.usage = PROPERTY_USAGE_NONE;
  1996. }
  1997. if (p_property.name == "depth" && mode != MODE_DEPTH) {
  1998. p_property.usage = PROPERTY_USAGE_NONE;
  1999. }
  2000. }
  2001. void CSGPolygon3D::_path_changed() {
  2002. _make_dirty();
  2003. update_gizmos();
  2004. }
  2005. void CSGPolygon3D::_path_exited() {
  2006. path = nullptr;
  2007. }
  2008. void CSGPolygon3D::_bind_methods() {
  2009. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CSGPolygon3D::set_polygon);
  2010. ClassDB::bind_method(D_METHOD("get_polygon"), &CSGPolygon3D::get_polygon);
  2011. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSGPolygon3D::set_mode);
  2012. ClassDB::bind_method(D_METHOD("get_mode"), &CSGPolygon3D::get_mode);
  2013. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGPolygon3D::set_depth);
  2014. ClassDB::bind_method(D_METHOD("get_depth"), &CSGPolygon3D::get_depth);
  2015. ClassDB::bind_method(D_METHOD("set_spin_degrees", "degrees"), &CSGPolygon3D::set_spin_degrees);
  2016. ClassDB::bind_method(D_METHOD("get_spin_degrees"), &CSGPolygon3D::get_spin_degrees);
  2017. ClassDB::bind_method(D_METHOD("set_spin_sides", "spin_sides"), &CSGPolygon3D::set_spin_sides);
  2018. ClassDB::bind_method(D_METHOD("get_spin_sides"), &CSGPolygon3D::get_spin_sides);
  2019. ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon3D::set_path_node);
  2020. ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon3D::get_path_node);
  2021. ClassDB::bind_method(D_METHOD("set_path_interval_type", "interval_type"), &CSGPolygon3D::set_path_interval_type);
  2022. ClassDB::bind_method(D_METHOD("get_path_interval_type"), &CSGPolygon3D::get_path_interval_type);
  2023. ClassDB::bind_method(D_METHOD("set_path_interval", "interval"), &CSGPolygon3D::set_path_interval);
  2024. ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon3D::get_path_interval);
  2025. ClassDB::bind_method(D_METHOD("set_path_simplify_angle", "degrees"), &CSGPolygon3D::set_path_simplify_angle);
  2026. ClassDB::bind_method(D_METHOD("get_path_simplify_angle"), &CSGPolygon3D::get_path_simplify_angle);
  2027. ClassDB::bind_method(D_METHOD("set_path_rotation", "path_rotation"), &CSGPolygon3D::set_path_rotation);
  2028. ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon3D::get_path_rotation);
  2029. ClassDB::bind_method(D_METHOD("set_path_local", "enable"), &CSGPolygon3D::set_path_local);
  2030. ClassDB::bind_method(D_METHOD("is_path_local"), &CSGPolygon3D::is_path_local);
  2031. ClassDB::bind_method(D_METHOD("set_path_continuous_u", "enable"), &CSGPolygon3D::set_path_continuous_u);
  2032. ClassDB::bind_method(D_METHOD("is_path_continuous_u"), &CSGPolygon3D::is_path_continuous_u);
  2033. ClassDB::bind_method(D_METHOD("set_path_u_distance", "distance"), &CSGPolygon3D::set_path_u_distance);
  2034. ClassDB::bind_method(D_METHOD("get_path_u_distance"), &CSGPolygon3D::get_path_u_distance);
  2035. ClassDB::bind_method(D_METHOD("set_path_joined", "enable"), &CSGPolygon3D::set_path_joined);
  2036. ClassDB::bind_method(D_METHOD("is_path_joined"), &CSGPolygon3D::is_path_joined);
  2037. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGPolygon3D::set_material);
  2038. ClassDB::bind_method(D_METHOD("get_material"), &CSGPolygon3D::get_material);
  2039. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGPolygon3D::set_smooth_faces);
  2040. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGPolygon3D::get_smooth_faces);
  2041. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CSGPolygon3D::_is_editable_3d_polygon);
  2042. ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &CSGPolygon3D::_has_editable_3d_polygon_no_depth);
  2043. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  2044. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Depth,Spin,Path"), "set_mode", "get_mode");
  2045. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.01,100.0,0.01,or_greater,exp,suffix:m"), "set_depth", "get_depth");
  2046. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
  2047. ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
  2048. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path3D"), "set_path_node", "get_path_node");
  2049. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_interval_type", PROPERTY_HINT_ENUM, "Distance,Subdivide"), "set_path_interval_type", "get_path_interval_type");
  2050. 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");
  2051. 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");
  2052. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
  2053. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
  2054. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
  2055. 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");
  2056. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_joined"), "set_path_joined", "is_path_joined");
  2057. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  2058. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  2059. BIND_ENUM_CONSTANT(MODE_DEPTH);
  2060. BIND_ENUM_CONSTANT(MODE_SPIN);
  2061. BIND_ENUM_CONSTANT(MODE_PATH);
  2062. BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
  2063. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
  2064. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
  2065. BIND_ENUM_CONSTANT(PATH_INTERVAL_DISTANCE);
  2066. BIND_ENUM_CONSTANT(PATH_INTERVAL_SUBDIVIDE);
  2067. }
  2068. void CSGPolygon3D::set_polygon(const Vector<Vector2> &p_polygon) {
  2069. polygon = p_polygon;
  2070. _make_dirty();
  2071. update_gizmos();
  2072. }
  2073. Vector<Vector2> CSGPolygon3D::get_polygon() const {
  2074. return polygon;
  2075. }
  2076. void CSGPolygon3D::set_mode(Mode p_mode) {
  2077. mode = p_mode;
  2078. _make_dirty();
  2079. update_gizmos();
  2080. notify_property_list_changed();
  2081. }
  2082. CSGPolygon3D::Mode CSGPolygon3D::get_mode() const {
  2083. return mode;
  2084. }
  2085. void CSGPolygon3D::set_depth(const float p_depth) {
  2086. ERR_FAIL_COND(p_depth < 0.001);
  2087. depth = p_depth;
  2088. _make_dirty();
  2089. update_gizmos();
  2090. }
  2091. float CSGPolygon3D::get_depth() const {
  2092. return depth;
  2093. }
  2094. void CSGPolygon3D::set_path_continuous_u(bool p_enable) {
  2095. path_continuous_u = p_enable;
  2096. _make_dirty();
  2097. }
  2098. bool CSGPolygon3D::is_path_continuous_u() const {
  2099. return path_continuous_u;
  2100. }
  2101. void CSGPolygon3D::set_path_u_distance(real_t p_path_u_distance) {
  2102. path_u_distance = p_path_u_distance;
  2103. _make_dirty();
  2104. update_gizmos();
  2105. }
  2106. real_t CSGPolygon3D::get_path_u_distance() const {
  2107. return path_u_distance;
  2108. }
  2109. void CSGPolygon3D::set_spin_degrees(const float p_spin_degrees) {
  2110. ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
  2111. spin_degrees = p_spin_degrees;
  2112. _make_dirty();
  2113. update_gizmos();
  2114. }
  2115. float CSGPolygon3D::get_spin_degrees() const {
  2116. return spin_degrees;
  2117. }
  2118. void CSGPolygon3D::set_spin_sides(int p_spin_sides) {
  2119. ERR_FAIL_COND(p_spin_sides < 3);
  2120. spin_sides = p_spin_sides;
  2121. _make_dirty();
  2122. update_gizmos();
  2123. }
  2124. int CSGPolygon3D::get_spin_sides() const {
  2125. return spin_sides;
  2126. }
  2127. void CSGPolygon3D::set_path_node(const NodePath &p_path) {
  2128. path_node = p_path;
  2129. _make_dirty();
  2130. update_gizmos();
  2131. }
  2132. NodePath CSGPolygon3D::get_path_node() const {
  2133. return path_node;
  2134. }
  2135. void CSGPolygon3D::set_path_interval_type(PathIntervalType p_interval_type) {
  2136. path_interval_type = p_interval_type;
  2137. _make_dirty();
  2138. update_gizmos();
  2139. }
  2140. CSGPolygon3D::PathIntervalType CSGPolygon3D::get_path_interval_type() const {
  2141. return path_interval_type;
  2142. }
  2143. void CSGPolygon3D::set_path_interval(float p_interval) {
  2144. path_interval = p_interval;
  2145. _make_dirty();
  2146. update_gizmos();
  2147. }
  2148. float CSGPolygon3D::get_path_interval() const {
  2149. return path_interval;
  2150. }
  2151. void CSGPolygon3D::set_path_simplify_angle(float p_angle) {
  2152. path_simplify_angle = p_angle;
  2153. _make_dirty();
  2154. update_gizmos();
  2155. }
  2156. float CSGPolygon3D::get_path_simplify_angle() const {
  2157. return path_simplify_angle;
  2158. }
  2159. void CSGPolygon3D::set_path_rotation(PathRotation p_rotation) {
  2160. path_rotation = p_rotation;
  2161. _make_dirty();
  2162. update_gizmos();
  2163. }
  2164. CSGPolygon3D::PathRotation CSGPolygon3D::get_path_rotation() const {
  2165. return path_rotation;
  2166. }
  2167. void CSGPolygon3D::set_path_local(bool p_enable) {
  2168. path_local = p_enable;
  2169. _make_dirty();
  2170. update_gizmos();
  2171. }
  2172. bool CSGPolygon3D::is_path_local() const {
  2173. return path_local;
  2174. }
  2175. void CSGPolygon3D::set_path_joined(bool p_enable) {
  2176. path_joined = p_enable;
  2177. _make_dirty();
  2178. update_gizmos();
  2179. }
  2180. bool CSGPolygon3D::is_path_joined() const {
  2181. return path_joined;
  2182. }
  2183. void CSGPolygon3D::set_smooth_faces(const bool p_smooth_faces) {
  2184. smooth_faces = p_smooth_faces;
  2185. _make_dirty();
  2186. }
  2187. bool CSGPolygon3D::get_smooth_faces() const {
  2188. return smooth_faces;
  2189. }
  2190. void CSGPolygon3D::set_material(const Ref<Material> &p_material) {
  2191. material = p_material;
  2192. _make_dirty();
  2193. }
  2194. Ref<Material> CSGPolygon3D::get_material() const {
  2195. return material;
  2196. }
  2197. bool CSGPolygon3D::_is_editable_3d_polygon() const {
  2198. return true;
  2199. }
  2200. bool CSGPolygon3D::_has_editable_3d_polygon_no_depth() const {
  2201. return true;
  2202. }
  2203. CSGPolygon3D::CSGPolygon3D() {
  2204. // defaults
  2205. mode = MODE_DEPTH;
  2206. polygon.push_back(Vector2(0, 0));
  2207. polygon.push_back(Vector2(0, 1));
  2208. polygon.push_back(Vector2(1, 1));
  2209. polygon.push_back(Vector2(1, 0));
  2210. depth = 1.0;
  2211. spin_degrees = 360;
  2212. spin_sides = 8;
  2213. smooth_faces = false;
  2214. path_interval_type = PATH_INTERVAL_DISTANCE;
  2215. path_interval = 1.0;
  2216. path_simplify_angle = 0.0;
  2217. path_rotation = PATH_ROTATION_PATH_FOLLOW;
  2218. path_local = false;
  2219. path_continuous_u = true;
  2220. path_u_distance = 1.0;
  2221. path_joined = false;
  2222. path = nullptr;
  2223. }