csg_shape.cpp 87 KB

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