csg_shape.cpp 72 KB

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