csg_shape.cpp 72 KB

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