csg_shape.cpp 73 KB

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