csg_shape.cpp 86 KB

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