csg_shape.cpp 68 KB

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