csg_shape.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  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_TRANSFORM_CHANGED) {
  418. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  419. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  420. }
  421. }
  422. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  423. if (parent) {
  424. parent->_make_dirty();
  425. }
  426. }
  427. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  428. if (parent) {
  429. parent->_make_dirty();
  430. }
  431. }
  432. if (p_what == NOTIFICATION_EXIT_TREE) {
  433. if (parent) {
  434. parent->_make_dirty();
  435. }
  436. parent = nullptr;
  437. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  438. PhysicsServer3D::get_singleton()->free(root_collision_instance);
  439. root_collision_instance = RID();
  440. root_collision_shape.unref();
  441. }
  442. _make_dirty();
  443. }
  444. }
  445. void CSGShape3D::set_operation(Operation p_operation) {
  446. operation = p_operation;
  447. _make_dirty();
  448. update_gizmo();
  449. }
  450. CSGShape3D::Operation CSGShape3D::get_operation() const {
  451. return operation;
  452. }
  453. void CSGShape3D::set_calculate_tangents(bool p_calculate_tangents) {
  454. calculate_tangents = p_calculate_tangents;
  455. _make_dirty();
  456. }
  457. bool CSGShape3D::is_calculating_tangents() const {
  458. return calculate_tangents;
  459. }
  460. void CSGShape3D::_validate_property(PropertyInfo &property) const {
  461. bool is_collision_prefixed = property.name.begins_with("collision_");
  462. if ((is_collision_prefixed || property.name.begins_with("use_collision")) && is_inside_tree() && !is_root_shape()) {
  463. //hide collision if not root
  464. property.usage = PROPERTY_USAGE_NOEDITOR;
  465. } else if (is_collision_prefixed && !bool(get("use_collision"))) {
  466. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  467. }
  468. }
  469. Array CSGShape3D::get_meshes() const {
  470. if (root_mesh.is_valid()) {
  471. Array arr;
  472. arr.resize(2);
  473. arr[0] = Transform();
  474. arr[1] = root_mesh;
  475. return arr;
  476. }
  477. return Array();
  478. }
  479. void CSGShape3D::_bind_methods() {
  480. ClassDB::bind_method(D_METHOD("_update_shape"), &CSGShape3D::_update_shape);
  481. ClassDB::bind_method(D_METHOD("is_root_shape"), &CSGShape3D::is_root_shape);
  482. ClassDB::bind_method(D_METHOD("set_operation", "operation"), &CSGShape3D::set_operation);
  483. ClassDB::bind_method(D_METHOD("get_operation"), &CSGShape3D::get_operation);
  484. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &CSGShape3D::set_snap);
  485. ClassDB::bind_method(D_METHOD("get_snap"), &CSGShape3D::get_snap);
  486. ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape3D::set_use_collision);
  487. ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape3D::is_using_collision);
  488. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CSGShape3D::set_collision_layer);
  489. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CSGShape3D::get_collision_layer);
  490. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CSGShape3D::set_collision_mask);
  491. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CSGShape3D::get_collision_mask);
  492. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &CSGShape3D::set_collision_mask_bit);
  493. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &CSGShape3D::get_collision_mask_bit);
  494. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &CSGShape3D::set_collision_layer_bit);
  495. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &CSGShape3D::get_collision_layer_bit);
  496. ClassDB::bind_method(D_METHOD("set_calculate_tangents", "enabled"), &CSGShape3D::set_calculate_tangents);
  497. ClassDB::bind_method(D_METHOD("is_calculating_tangents"), &CSGShape3D::is_calculating_tangents);
  498. ClassDB::bind_method(D_METHOD("get_meshes"), &CSGShape3D::get_meshes);
  499. ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "Union,Intersection,Subtraction"), "set_operation", "get_operation");
  500. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_RANGE, "0.0001,1,0.001"), "set_snap", "get_snap");
  501. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "calculate_tangents"), "set_calculate_tangents", "is_calculating_tangents");
  502. ADD_GROUP("Collision", "collision_");
  503. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_collision"), "set_use_collision", "is_using_collision");
  504. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  505. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  506. BIND_ENUM_CONSTANT(OPERATION_UNION);
  507. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  508. BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
  509. }
  510. CSGShape3D::CSGShape3D() {
  511. operation = OPERATION_UNION;
  512. parent = nullptr;
  513. brush = nullptr;
  514. dirty = false;
  515. snap = 0.001;
  516. use_collision = false;
  517. collision_layer = 1;
  518. collision_mask = 1;
  519. calculate_tangents = true;
  520. set_notify_local_transform(true);
  521. }
  522. CSGShape3D::~CSGShape3D() {
  523. if (brush) {
  524. memdelete(brush);
  525. brush = nullptr;
  526. }
  527. }
  528. //////////////////////////////////
  529. CSGBrush *CSGCombiner3D::_build_brush() {
  530. return memnew(CSGBrush); //does not build anything
  531. }
  532. CSGCombiner3D::CSGCombiner3D() {
  533. }
  534. /////////////////////
  535. 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) {
  536. CSGBrush *brush = memnew(CSGBrush);
  537. Vector<bool> invert;
  538. invert.resize(p_vertices.size() / 3);
  539. {
  540. int ic = invert.size();
  541. bool *w = invert.ptrw();
  542. for (int i = 0; i < ic; i++) {
  543. w[i] = invert_faces;
  544. }
  545. }
  546. brush->build_from_faces(p_vertices, p_uv, p_smooth, p_materials, invert);
  547. return brush;
  548. }
  549. void CSGPrimitive3D::_bind_methods() {
  550. ClassDB::bind_method(D_METHOD("set_invert_faces", "invert_faces"), &CSGPrimitive3D::set_invert_faces);
  551. ClassDB::bind_method(D_METHOD("is_inverting_faces"), &CSGPrimitive3D::is_inverting_faces);
  552. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_faces"), "set_invert_faces", "is_inverting_faces");
  553. }
  554. void CSGPrimitive3D::set_invert_faces(bool p_invert) {
  555. if (invert_faces == p_invert) {
  556. return;
  557. }
  558. invert_faces = p_invert;
  559. _make_dirty();
  560. }
  561. bool CSGPrimitive3D::is_inverting_faces() {
  562. return invert_faces;
  563. }
  564. CSGPrimitive3D::CSGPrimitive3D() {
  565. invert_faces = false;
  566. }
  567. /////////////////////
  568. CSGBrush *CSGMesh3D::_build_brush() {
  569. if (!mesh.is_valid()) {
  570. return memnew(CSGBrush);
  571. }
  572. Vector<Vector3> vertices;
  573. Vector<bool> smooth;
  574. Vector<Ref<Material>> materials;
  575. Vector<Vector2> uvs;
  576. Ref<Material> material = get_material();
  577. for (int i = 0; i < mesh->get_surface_count(); i++) {
  578. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  579. continue;
  580. }
  581. Array arrays = mesh->surface_get_arrays(i);
  582. if (arrays.size() == 0) {
  583. _make_dirty();
  584. ERR_FAIL_COND_V(arrays.size() == 0, memnew(CSGBrush));
  585. }
  586. Vector<Vector3> avertices = arrays[Mesh::ARRAY_VERTEX];
  587. if (avertices.size() == 0) {
  588. continue;
  589. }
  590. const Vector3 *vr = avertices.ptr();
  591. Vector<Vector3> anormals = arrays[Mesh::ARRAY_NORMAL];
  592. const Vector3 *nr = nullptr;
  593. if (anormals.size()) {
  594. nr = anormals.ptr();
  595. }
  596. Vector<Vector2> auvs = arrays[Mesh::ARRAY_TEX_UV];
  597. const Vector2 *uvr = nullptr;
  598. if (auvs.size()) {
  599. uvr = auvs.ptr();
  600. }
  601. Ref<Material> mat;
  602. if (material.is_valid()) {
  603. mat = material;
  604. } else {
  605. mat = mesh->surface_get_material(i);
  606. }
  607. Vector<int> aindices = arrays[Mesh::ARRAY_INDEX];
  608. if (aindices.size()) {
  609. int as = vertices.size();
  610. int is = aindices.size();
  611. vertices.resize(as + is);
  612. smooth.resize((as + is) / 3);
  613. materials.resize((as + is) / 3);
  614. uvs.resize(as + is);
  615. Vector3 *vw = vertices.ptrw();
  616. bool *sw = smooth.ptrw();
  617. Vector2 *uvw = uvs.ptrw();
  618. Ref<Material> *mw = materials.ptrw();
  619. const int *ir = aindices.ptr();
  620. for (int j = 0; j < is; j += 3) {
  621. Vector3 vertex[3];
  622. Vector3 normal[3];
  623. Vector2 uv[3];
  624. for (int k = 0; k < 3; k++) {
  625. int idx = ir[j + k];
  626. vertex[k] = vr[idx];
  627. if (nr) {
  628. normal[k] = nr[idx];
  629. }
  630. if (uvr) {
  631. uv[k] = uvr[idx];
  632. }
  633. }
  634. bool flat = normal[0].distance_to(normal[1]) < CMP_EPSILON && normal[0].distance_to(normal[2]) < CMP_EPSILON;
  635. vw[as + j + 0] = vertex[0];
  636. vw[as + j + 1] = vertex[1];
  637. vw[as + j + 2] = vertex[2];
  638. uvw[as + j + 0] = uv[0];
  639. uvw[as + j + 1] = uv[1];
  640. uvw[as + j + 2] = uv[2];
  641. sw[(as + j) / 3] = !flat;
  642. mw[(as + j) / 3] = mat;
  643. }
  644. } else {
  645. int as = vertices.size();
  646. int is = avertices.size();
  647. vertices.resize(as + is);
  648. smooth.resize((as + is) / 3);
  649. uvs.resize(as + is);
  650. materials.resize((as + is) / 3);
  651. Vector3 *vw = vertices.ptrw();
  652. bool *sw = smooth.ptrw();
  653. Vector2 *uvw = uvs.ptrw();
  654. Ref<Material> *mw = materials.ptrw();
  655. for (int j = 0; j < is; j += 3) {
  656. Vector3 vertex[3];
  657. Vector3 normal[3];
  658. Vector2 uv[3];
  659. for (int k = 0; k < 3; k++) {
  660. vertex[k] = vr[j + k];
  661. if (nr) {
  662. normal[k] = nr[j + k];
  663. }
  664. if (uvr) {
  665. uv[k] = uvr[j + k];
  666. }
  667. }
  668. bool flat = normal[0].distance_to(normal[1]) < CMP_EPSILON && normal[0].distance_to(normal[2]) < CMP_EPSILON;
  669. vw[as + j + 0] = vertex[0];
  670. vw[as + j + 1] = vertex[1];
  671. vw[as + j + 2] = vertex[2];
  672. uvw[as + j + 0] = uv[0];
  673. uvw[as + j + 1] = uv[1];
  674. uvw[as + j + 2] = uv[2];
  675. sw[(as + j) / 3] = !flat;
  676. mw[(as + j) / 3] = mat;
  677. }
  678. }
  679. }
  680. if (vertices.size() == 0) {
  681. return memnew(CSGBrush);
  682. }
  683. return _create_brush_from_arrays(vertices, uvs, smooth, materials);
  684. }
  685. void CSGMesh3D::_mesh_changed() {
  686. _make_dirty();
  687. update_gizmo();
  688. }
  689. void CSGMesh3D::set_material(const Ref<Material> &p_material) {
  690. if (material == p_material) {
  691. return;
  692. }
  693. material = p_material;
  694. _make_dirty();
  695. }
  696. Ref<Material> CSGMesh3D::get_material() const {
  697. return material;
  698. }
  699. void CSGMesh3D::_bind_methods() {
  700. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CSGMesh3D::set_mesh);
  701. ClassDB::bind_method(D_METHOD("get_mesh"), &CSGMesh3D::get_mesh);
  702. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGMesh3D::set_material);
  703. ClassDB::bind_method(D_METHOD("get_material"), &CSGMesh3D::get_material);
  704. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  705. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material");
  706. }
  707. void CSGMesh3D::set_mesh(const Ref<Mesh> &p_mesh) {
  708. if (mesh == p_mesh) {
  709. return;
  710. }
  711. if (mesh.is_valid()) {
  712. mesh->disconnect("changed", callable_mp(this, &CSGMesh3D::_mesh_changed));
  713. }
  714. mesh = p_mesh;
  715. if (mesh.is_valid()) {
  716. mesh->connect("changed", callable_mp(this, &CSGMesh3D::_mesh_changed));
  717. }
  718. _make_dirty();
  719. }
  720. Ref<Mesh> CSGMesh3D::get_mesh() {
  721. return mesh;
  722. }
  723. ////////////////////////////////
  724. CSGBrush *CSGSphere3D::_build_brush() {
  725. // set our bounding box
  726. CSGBrush *brush = memnew(CSGBrush);
  727. int face_count = rings * radial_segments * 2 - radial_segments * 2;
  728. bool invert_val = is_inverting_faces();
  729. Ref<Material> material = get_material();
  730. Vector<Vector3> faces;
  731. Vector<Vector2> uvs;
  732. Vector<bool> smooth;
  733. Vector<Ref<Material>> materials;
  734. Vector<bool> invert;
  735. faces.resize(face_count * 3);
  736. uvs.resize(face_count * 3);
  737. smooth.resize(face_count);
  738. materials.resize(face_count);
  739. invert.resize(face_count);
  740. {
  741. Vector3 *facesw = faces.ptrw();
  742. Vector2 *uvsw = uvs.ptrw();
  743. bool *smoothw = smooth.ptrw();
  744. Ref<Material> *materialsw = materials.ptrw();
  745. bool *invertw = invert.ptrw();
  746. int face = 0;
  747. for (int i = 1; i <= rings; i++) {
  748. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / rings);
  749. double z0 = Math::sin(lat0);
  750. double zr0 = Math::cos(lat0);
  751. double u0 = double(i - 1) / rings;
  752. double lat1 = Math_PI * (-0.5 + (double)i / rings);
  753. double z1 = Math::sin(lat1);
  754. double zr1 = Math::cos(lat1);
  755. double u1 = double(i) / rings;
  756. for (int j = radial_segments; j >= 1; j--) {
  757. double lng0 = 2 * Math_PI * (double)(j - 1) / radial_segments;
  758. double x0 = Math::cos(lng0);
  759. double y0 = Math::sin(lng0);
  760. double v0 = double(i - 1) / radial_segments;
  761. double lng1 = 2 * Math_PI * (double)(j) / radial_segments;
  762. double x1 = Math::cos(lng1);
  763. double y1 = Math::sin(lng1);
  764. double v1 = double(i) / radial_segments;
  765. Vector3 v[4] = {
  766. Vector3(x1 * zr0, z0, y1 * zr0) * radius,
  767. Vector3(x1 * zr1, z1, y1 * zr1) * radius,
  768. Vector3(x0 * zr1, z1, y0 * zr1) * radius,
  769. Vector3(x0 * zr0, z0, y0 * zr0) * radius
  770. };
  771. Vector2 u[4] = {
  772. Vector2(v1, u0),
  773. Vector2(v1, u1),
  774. Vector2(v0, u1),
  775. Vector2(v0, u0),
  776. };
  777. if (i < rings) {
  778. //face 1
  779. facesw[face * 3 + 0] = v[0];
  780. facesw[face * 3 + 1] = v[1];
  781. facesw[face * 3 + 2] = v[2];
  782. uvsw[face * 3 + 0] = u[0];
  783. uvsw[face * 3 + 1] = u[1];
  784. uvsw[face * 3 + 2] = u[2];
  785. smoothw[face] = smooth_faces;
  786. invertw[face] = invert_val;
  787. materialsw[face] = material;
  788. face++;
  789. }
  790. if (i > 1) {
  791. //face 2
  792. facesw[face * 3 + 0] = v[2];
  793. facesw[face * 3 + 1] = v[3];
  794. facesw[face * 3 + 2] = v[0];
  795. uvsw[face * 3 + 0] = u[2];
  796. uvsw[face * 3 + 1] = u[3];
  797. uvsw[face * 3 + 2] = u[0];
  798. smoothw[face] = smooth_faces;
  799. invertw[face] = invert_val;
  800. materialsw[face] = material;
  801. face++;
  802. }
  803. }
  804. }
  805. if (face != face_count) {
  806. ERR_PRINT("Face mismatch bug! fix code");
  807. }
  808. }
  809. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  810. return brush;
  811. }
  812. void CSGSphere3D::_bind_methods() {
  813. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGSphere3D::set_radius);
  814. ClassDB::bind_method(D_METHOD("get_radius"), &CSGSphere3D::get_radius);
  815. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGSphere3D::set_radial_segments);
  816. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGSphere3D::get_radial_segments);
  817. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CSGSphere3D::set_rings);
  818. ClassDB::bind_method(D_METHOD("get_rings"), &CSGSphere3D::get_rings);
  819. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGSphere3D::set_smooth_faces);
  820. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGSphere3D::get_smooth_faces);
  821. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGSphere3D::set_material);
  822. ClassDB::bind_method(D_METHOD("get_material"), &CSGSphere3D::get_material);
  823. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001"), "set_radius", "get_radius");
  824. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments");
  825. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1"), "set_rings", "get_rings");
  826. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  827. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material");
  828. }
  829. void CSGSphere3D::set_radius(const float p_radius) {
  830. ERR_FAIL_COND(p_radius <= 0);
  831. radius = p_radius;
  832. _make_dirty();
  833. update_gizmo();
  834. _change_notify("radius");
  835. }
  836. float CSGSphere3D::get_radius() const {
  837. return radius;
  838. }
  839. void CSGSphere3D::set_radial_segments(const int p_radial_segments) {
  840. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  841. _make_dirty();
  842. update_gizmo();
  843. }
  844. int CSGSphere3D::get_radial_segments() const {
  845. return radial_segments;
  846. }
  847. void CSGSphere3D::set_rings(const int p_rings) {
  848. rings = p_rings > 1 ? p_rings : 1;
  849. _make_dirty();
  850. update_gizmo();
  851. }
  852. int CSGSphere3D::get_rings() const {
  853. return rings;
  854. }
  855. void CSGSphere3D::set_smooth_faces(const bool p_smooth_faces) {
  856. smooth_faces = p_smooth_faces;
  857. _make_dirty();
  858. }
  859. bool CSGSphere3D::get_smooth_faces() const {
  860. return smooth_faces;
  861. }
  862. void CSGSphere3D::set_material(const Ref<Material> &p_material) {
  863. material = p_material;
  864. _make_dirty();
  865. }
  866. Ref<Material> CSGSphere3D::get_material() const {
  867. return material;
  868. }
  869. CSGSphere3D::CSGSphere3D() {
  870. // defaults
  871. radius = 1.0;
  872. radial_segments = 12;
  873. rings = 6;
  874. smooth_faces = true;
  875. }
  876. ///////////////
  877. CSGBrush *CSGBox3D::_build_brush() {
  878. // set our bounding box
  879. CSGBrush *brush = memnew(CSGBrush);
  880. int face_count = 12; //it's a cube..
  881. bool invert_val = is_inverting_faces();
  882. Ref<Material> material = get_material();
  883. Vector<Vector3> faces;
  884. Vector<Vector2> uvs;
  885. Vector<bool> smooth;
  886. Vector<Ref<Material>> materials;
  887. Vector<bool> invert;
  888. faces.resize(face_count * 3);
  889. uvs.resize(face_count * 3);
  890. smooth.resize(face_count);
  891. materials.resize(face_count);
  892. invert.resize(face_count);
  893. {
  894. Vector3 *facesw = faces.ptrw();
  895. Vector2 *uvsw = uvs.ptrw();
  896. bool *smoothw = smooth.ptrw();
  897. Ref<Material> *materialsw = materials.ptrw();
  898. bool *invertw = invert.ptrw();
  899. int face = 0;
  900. Vector3 vertex_mul(width * 0.5, height * 0.5, depth * 0.5);
  901. {
  902. for (int i = 0; i < 6; i++) {
  903. Vector3 face_points[4];
  904. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  905. for (int j = 0; j < 4; j++) {
  906. float v[3];
  907. v[0] = 1.0;
  908. v[1] = 1 - 2 * ((j >> 1) & 1);
  909. v[2] = v[1] * (1 - 2 * (j & 1));
  910. for (int k = 0; k < 3; k++) {
  911. if (i < 3) {
  912. face_points[j][(i + k) % 3] = v[k];
  913. } else {
  914. face_points[3 - j][(i + k) % 3] = -v[k];
  915. }
  916. }
  917. }
  918. Vector2 u[4];
  919. for (int j = 0; j < 4; j++) {
  920. u[j] = Vector2(uv_points[j * 2 + 0], uv_points[j * 2 + 1]);
  921. }
  922. //face 1
  923. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  924. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  925. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  926. uvsw[face * 3 + 0] = u[0];
  927. uvsw[face * 3 + 1] = u[1];
  928. uvsw[face * 3 + 2] = u[2];
  929. smoothw[face] = false;
  930. invertw[face] = invert_val;
  931. materialsw[face] = material;
  932. face++;
  933. //face 1
  934. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  935. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  936. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  937. uvsw[face * 3 + 0] = u[2];
  938. uvsw[face * 3 + 1] = u[3];
  939. uvsw[face * 3 + 2] = u[0];
  940. smoothw[face] = false;
  941. invertw[face] = invert_val;
  942. materialsw[face] = material;
  943. face++;
  944. }
  945. }
  946. if (face != face_count) {
  947. ERR_PRINT("Face mismatch bug! fix code");
  948. }
  949. }
  950. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  951. return brush;
  952. }
  953. void CSGBox3D::_bind_methods() {
  954. ClassDB::bind_method(D_METHOD("set_width", "width"), &CSGBox3D::set_width);
  955. ClassDB::bind_method(D_METHOD("get_width"), &CSGBox3D::get_width);
  956. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGBox3D::set_height);
  957. ClassDB::bind_method(D_METHOD("get_height"), &CSGBox3D::get_height);
  958. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGBox3D::set_depth);
  959. ClassDB::bind_method(D_METHOD("get_depth"), &CSGBox3D::get_depth);
  960. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox3D::set_material);
  961. ClassDB::bind_method(D_METHOD("get_material"), &CSGBox3D::get_material);
  962. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_width", "get_width");
  963. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_height", "get_height");
  964. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_depth", "get_depth");
  965. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material");
  966. }
  967. void CSGBox3D::set_width(const float p_width) {
  968. width = p_width;
  969. _make_dirty();
  970. update_gizmo();
  971. _change_notify("width");
  972. }
  973. float CSGBox3D::get_width() const {
  974. return width;
  975. }
  976. void CSGBox3D::set_height(const float p_height) {
  977. height = p_height;
  978. _make_dirty();
  979. update_gizmo();
  980. _change_notify("height");
  981. }
  982. float CSGBox3D::get_height() const {
  983. return height;
  984. }
  985. void CSGBox3D::set_depth(const float p_depth) {
  986. depth = p_depth;
  987. _make_dirty();
  988. update_gizmo();
  989. _change_notify("depth");
  990. }
  991. float CSGBox3D::get_depth() const {
  992. return depth;
  993. }
  994. void CSGBox3D::set_material(const Ref<Material> &p_material) {
  995. material = p_material;
  996. _make_dirty();
  997. update_gizmo();
  998. }
  999. Ref<Material> CSGBox3D::get_material() const {
  1000. return material;
  1001. }
  1002. CSGBox3D::CSGBox3D() {
  1003. // defaults
  1004. width = 2.0;
  1005. height = 2.0;
  1006. depth = 2.0;
  1007. }
  1008. ///////////////
  1009. CSGBrush *CSGCylinder3D::_build_brush() {
  1010. // set our bounding box
  1011. CSGBrush *brush = memnew(CSGBrush);
  1012. int face_count = sides * (cone ? 1 : 2) + sides + (cone ? 0 : sides);
  1013. bool invert_val = is_inverting_faces();
  1014. Ref<Material> material = get_material();
  1015. Vector<Vector3> faces;
  1016. Vector<Vector2> uvs;
  1017. Vector<bool> smooth;
  1018. Vector<Ref<Material>> materials;
  1019. Vector<bool> invert;
  1020. faces.resize(face_count * 3);
  1021. uvs.resize(face_count * 3);
  1022. smooth.resize(face_count);
  1023. materials.resize(face_count);
  1024. invert.resize(face_count);
  1025. {
  1026. Vector3 *facesw = faces.ptrw();
  1027. Vector2 *uvsw = uvs.ptrw();
  1028. bool *smoothw = smooth.ptrw();
  1029. Ref<Material> *materialsw = materials.ptrw();
  1030. bool *invertw = invert.ptrw();
  1031. int face = 0;
  1032. Vector3 vertex_mul(radius, height * 0.5, radius);
  1033. {
  1034. for (int i = 0; i < sides; i++) {
  1035. float inc = float(i) / sides;
  1036. float inc_n = float((i + 1)) / sides;
  1037. float ang = inc * Math_PI * 2.0;
  1038. float ang_n = inc_n * Math_PI * 2.0;
  1039. Vector3 base(Math::cos(ang), 0, Math::sin(ang));
  1040. Vector3 base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
  1041. Vector3 face_points[4] = {
  1042. base + Vector3(0, -1, 0),
  1043. base_n + Vector3(0, -1, 0),
  1044. base_n * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1045. base * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1046. };
  1047. Vector2 u[4] = {
  1048. Vector2(inc, 0),
  1049. Vector2(inc_n, 0),
  1050. Vector2(inc_n, 1),
  1051. Vector2(inc, 1),
  1052. };
  1053. //side face 1
  1054. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1055. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1056. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1057. uvsw[face * 3 + 0] = u[0];
  1058. uvsw[face * 3 + 1] = u[1];
  1059. uvsw[face * 3 + 2] = u[2];
  1060. smoothw[face] = smooth_faces;
  1061. invertw[face] = invert_val;
  1062. materialsw[face] = material;
  1063. face++;
  1064. if (!cone) {
  1065. //side face 2
  1066. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1067. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1068. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1069. uvsw[face * 3 + 0] = u[2];
  1070. uvsw[face * 3 + 1] = u[3];
  1071. uvsw[face * 3 + 2] = u[0];
  1072. smoothw[face] = smooth_faces;
  1073. invertw[face] = invert_val;
  1074. materialsw[face] = material;
  1075. face++;
  1076. }
  1077. //bottom face 1
  1078. facesw[face * 3 + 0] = face_points[1] * vertex_mul;
  1079. facesw[face * 3 + 1] = face_points[0] * vertex_mul;
  1080. facesw[face * 3 + 2] = Vector3(0, -1, 0) * vertex_mul;
  1081. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1082. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1083. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1084. smoothw[face] = false;
  1085. invertw[face] = invert_val;
  1086. materialsw[face] = material;
  1087. face++;
  1088. if (!cone) {
  1089. //top face 1
  1090. facesw[face * 3 + 0] = face_points[3] * vertex_mul;
  1091. facesw[face * 3 + 1] = face_points[2] * vertex_mul;
  1092. facesw[face * 3 + 2] = Vector3(0, 1, 0) * vertex_mul;
  1093. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1094. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1095. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1096. smoothw[face] = false;
  1097. invertw[face] = invert_val;
  1098. materialsw[face] = material;
  1099. face++;
  1100. }
  1101. }
  1102. }
  1103. if (face != face_count) {
  1104. ERR_PRINT("Face mismatch bug! fix code");
  1105. }
  1106. }
  1107. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1108. return brush;
  1109. }
  1110. void CSGCylinder3D::_bind_methods() {
  1111. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCylinder3D::set_radius);
  1112. ClassDB::bind_method(D_METHOD("get_radius"), &CSGCylinder3D::get_radius);
  1113. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCylinder3D::set_height);
  1114. ClassDB::bind_method(D_METHOD("get_height"), &CSGCylinder3D::get_height);
  1115. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGCylinder3D::set_sides);
  1116. ClassDB::bind_method(D_METHOD("get_sides"), &CSGCylinder3D::get_sides);
  1117. ClassDB::bind_method(D_METHOD("set_cone", "cone"), &CSGCylinder3D::set_cone);
  1118. ClassDB::bind_method(D_METHOD("is_cone"), &CSGCylinder3D::is_cone);
  1119. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGCylinder3D::set_material);
  1120. ClassDB::bind_method(D_METHOD("get_material"), &CSGCylinder3D::get_material);
  1121. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGCylinder3D::set_smooth_faces);
  1122. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGCylinder3D::get_smooth_faces);
  1123. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_radius", "get_radius");
  1124. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_height", "get_height");
  1125. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1126. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cone"), "set_cone", "is_cone");
  1127. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1128. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1129. }
  1130. void CSGCylinder3D::set_radius(const float p_radius) {
  1131. radius = p_radius;
  1132. _make_dirty();
  1133. update_gizmo();
  1134. _change_notify("radius");
  1135. }
  1136. float CSGCylinder3D::get_radius() const {
  1137. return radius;
  1138. }
  1139. void CSGCylinder3D::set_height(const float p_height) {
  1140. height = p_height;
  1141. _make_dirty();
  1142. update_gizmo();
  1143. _change_notify("height");
  1144. }
  1145. float CSGCylinder3D::get_height() const {
  1146. return height;
  1147. }
  1148. void CSGCylinder3D::set_sides(const int p_sides) {
  1149. ERR_FAIL_COND(p_sides < 3);
  1150. sides = p_sides;
  1151. _make_dirty();
  1152. update_gizmo();
  1153. }
  1154. int CSGCylinder3D::get_sides() const {
  1155. return sides;
  1156. }
  1157. void CSGCylinder3D::set_cone(const bool p_cone) {
  1158. cone = p_cone;
  1159. _make_dirty();
  1160. update_gizmo();
  1161. }
  1162. bool CSGCylinder3D::is_cone() const {
  1163. return cone;
  1164. }
  1165. void CSGCylinder3D::set_smooth_faces(const bool p_smooth_faces) {
  1166. smooth_faces = p_smooth_faces;
  1167. _make_dirty();
  1168. }
  1169. bool CSGCylinder3D::get_smooth_faces() const {
  1170. return smooth_faces;
  1171. }
  1172. void CSGCylinder3D::set_material(const Ref<Material> &p_material) {
  1173. material = p_material;
  1174. _make_dirty();
  1175. }
  1176. Ref<Material> CSGCylinder3D::get_material() const {
  1177. return material;
  1178. }
  1179. CSGCylinder3D::CSGCylinder3D() {
  1180. // defaults
  1181. radius = 1.0;
  1182. height = 1.0;
  1183. sides = 8;
  1184. cone = false;
  1185. smooth_faces = true;
  1186. }
  1187. ///////////////
  1188. CSGBrush *CSGTorus3D::_build_brush() {
  1189. // set our bounding box
  1190. float min_radius = inner_radius;
  1191. float max_radius = outer_radius;
  1192. if (min_radius == max_radius) {
  1193. return memnew(CSGBrush); //sorry, can't
  1194. }
  1195. if (min_radius > max_radius) {
  1196. SWAP(min_radius, max_radius);
  1197. }
  1198. float radius = (max_radius - min_radius) * 0.5;
  1199. CSGBrush *brush = memnew(CSGBrush);
  1200. int face_count = ring_sides * sides * 2;
  1201. bool invert_val = is_inverting_faces();
  1202. Ref<Material> material = get_material();
  1203. Vector<Vector3> faces;
  1204. Vector<Vector2> uvs;
  1205. Vector<bool> smooth;
  1206. Vector<Ref<Material>> materials;
  1207. Vector<bool> invert;
  1208. faces.resize(face_count * 3);
  1209. uvs.resize(face_count * 3);
  1210. smooth.resize(face_count);
  1211. materials.resize(face_count);
  1212. invert.resize(face_count);
  1213. {
  1214. Vector3 *facesw = faces.ptrw();
  1215. Vector2 *uvsw = uvs.ptrw();
  1216. bool *smoothw = smooth.ptrw();
  1217. Ref<Material> *materialsw = materials.ptrw();
  1218. bool *invertw = invert.ptrw();
  1219. int face = 0;
  1220. {
  1221. for (int i = 0; i < sides; i++) {
  1222. float inci = float(i) / sides;
  1223. float inci_n = float((i + 1)) / sides;
  1224. float angi = inci * Math_PI * 2.0;
  1225. float angi_n = inci_n * Math_PI * 2.0;
  1226. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1227. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1228. for (int j = 0; j < ring_sides; j++) {
  1229. float incj = float(j) / ring_sides;
  1230. float incj_n = float((j + 1)) / ring_sides;
  1231. float angj = incj * Math_PI * 2.0;
  1232. float angj_n = incj_n * Math_PI * 2.0;
  1233. Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
  1234. Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
  1235. Vector3 face_points[4] = {
  1236. Vector3(normali.x * normalj.x, normalj.y, normali.z * normalj.x),
  1237. Vector3(normali.x * normalj_n.x, normalj_n.y, normali.z * normalj_n.x),
  1238. Vector3(normali_n.x * normalj_n.x, normalj_n.y, normali_n.z * normalj_n.x),
  1239. Vector3(normali_n.x * normalj.x, normalj.y, normali_n.z * normalj.x)
  1240. };
  1241. Vector2 u[4] = {
  1242. Vector2(inci, incj),
  1243. Vector2(inci, incj_n),
  1244. Vector2(inci_n, incj_n),
  1245. Vector2(inci_n, incj),
  1246. };
  1247. // face 1
  1248. facesw[face * 3 + 0] = face_points[0];
  1249. facesw[face * 3 + 1] = face_points[2];
  1250. facesw[face * 3 + 2] = face_points[1];
  1251. uvsw[face * 3 + 0] = u[0];
  1252. uvsw[face * 3 + 1] = u[2];
  1253. uvsw[face * 3 + 2] = u[1];
  1254. smoothw[face] = smooth_faces;
  1255. invertw[face] = invert_val;
  1256. materialsw[face] = material;
  1257. face++;
  1258. //face 2
  1259. facesw[face * 3 + 0] = face_points[3];
  1260. facesw[face * 3 + 1] = face_points[2];
  1261. facesw[face * 3 + 2] = face_points[0];
  1262. uvsw[face * 3 + 0] = u[3];
  1263. uvsw[face * 3 + 1] = u[2];
  1264. uvsw[face * 3 + 2] = u[0];
  1265. smoothw[face] = smooth_faces;
  1266. invertw[face] = invert_val;
  1267. materialsw[face] = material;
  1268. face++;
  1269. }
  1270. }
  1271. }
  1272. if (face != face_count) {
  1273. ERR_PRINT("Face mismatch bug! fix code");
  1274. }
  1275. }
  1276. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1277. return brush;
  1278. }
  1279. void CSGTorus3D::_bind_methods() {
  1280. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &CSGTorus3D::set_inner_radius);
  1281. ClassDB::bind_method(D_METHOD("get_inner_radius"), &CSGTorus3D::get_inner_radius);
  1282. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &CSGTorus3D::set_outer_radius);
  1283. ClassDB::bind_method(D_METHOD("get_outer_radius"), &CSGTorus3D::get_outer_radius);
  1284. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGTorus3D::set_sides);
  1285. ClassDB::bind_method(D_METHOD("get_sides"), &CSGTorus3D::get_sides);
  1286. ClassDB::bind_method(D_METHOD("set_ring_sides", "sides"), &CSGTorus3D::set_ring_sides);
  1287. ClassDB::bind_method(D_METHOD("get_ring_sides"), &CSGTorus3D::get_ring_sides);
  1288. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGTorus3D::set_material);
  1289. ClassDB::bind_method(D_METHOD("get_material"), &CSGTorus3D::get_material);
  1290. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGTorus3D::set_smooth_faces);
  1291. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGTorus3D::get_smooth_faces);
  1292. 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");
  1293. 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");
  1294. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1295. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_ring_sides", "get_ring_sides");
  1296. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1297. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1298. }
  1299. void CSGTorus3D::set_inner_radius(const float p_inner_radius) {
  1300. inner_radius = p_inner_radius;
  1301. _make_dirty();
  1302. update_gizmo();
  1303. _change_notify("inner_radius");
  1304. }
  1305. float CSGTorus3D::get_inner_radius() const {
  1306. return inner_radius;
  1307. }
  1308. void CSGTorus3D::set_outer_radius(const float p_outer_radius) {
  1309. outer_radius = p_outer_radius;
  1310. _make_dirty();
  1311. update_gizmo();
  1312. _change_notify("outer_radius");
  1313. }
  1314. float CSGTorus3D::get_outer_radius() const {
  1315. return outer_radius;
  1316. }
  1317. void CSGTorus3D::set_sides(const int p_sides) {
  1318. ERR_FAIL_COND(p_sides < 3);
  1319. sides = p_sides;
  1320. _make_dirty();
  1321. update_gizmo();
  1322. }
  1323. int CSGTorus3D::get_sides() const {
  1324. return sides;
  1325. }
  1326. void CSGTorus3D::set_ring_sides(const int p_ring_sides) {
  1327. ERR_FAIL_COND(p_ring_sides < 3);
  1328. ring_sides = p_ring_sides;
  1329. _make_dirty();
  1330. update_gizmo();
  1331. }
  1332. int CSGTorus3D::get_ring_sides() const {
  1333. return ring_sides;
  1334. }
  1335. void CSGTorus3D::set_smooth_faces(const bool p_smooth_faces) {
  1336. smooth_faces = p_smooth_faces;
  1337. _make_dirty();
  1338. }
  1339. bool CSGTorus3D::get_smooth_faces() const {
  1340. return smooth_faces;
  1341. }
  1342. void CSGTorus3D::set_material(const Ref<Material> &p_material) {
  1343. material = p_material;
  1344. _make_dirty();
  1345. }
  1346. Ref<Material> CSGTorus3D::get_material() const {
  1347. return material;
  1348. }
  1349. CSGTorus3D::CSGTorus3D() {
  1350. // defaults
  1351. inner_radius = 2.0;
  1352. outer_radius = 3.0;
  1353. sides = 8;
  1354. ring_sides = 6;
  1355. smooth_faces = true;
  1356. }
  1357. ///////////////
  1358. CSGBrush *CSGPolygon3D::_build_brush() {
  1359. // set our bounding box
  1360. if (polygon.size() < 3) {
  1361. return memnew(CSGBrush);
  1362. }
  1363. Vector<Point2> final_polygon = polygon;
  1364. if (Triangulate::get_area(final_polygon) > 0) {
  1365. final_polygon.invert();
  1366. }
  1367. Vector<int> triangles = Geometry2D::triangulate_polygon(final_polygon);
  1368. if (triangles.size() < 3) {
  1369. return memnew(CSGBrush);
  1370. }
  1371. Path3D *path = nullptr;
  1372. Ref<Curve3D> curve;
  1373. // get bounds for our polygon
  1374. Vector2 final_polygon_min;
  1375. Vector2 final_polygon_max;
  1376. for (int i = 0; i < final_polygon.size(); i++) {
  1377. Vector2 p = final_polygon[i];
  1378. if (i == 0) {
  1379. final_polygon_min = p;
  1380. final_polygon_max = final_polygon_min;
  1381. } else {
  1382. if (p.x < final_polygon_min.x) {
  1383. final_polygon_min.x = p.x;
  1384. }
  1385. if (p.y < final_polygon_min.y) {
  1386. final_polygon_min.y = p.y;
  1387. }
  1388. if (p.x > final_polygon_max.x) {
  1389. final_polygon_max.x = p.x;
  1390. }
  1391. if (p.y > final_polygon_max.y) {
  1392. final_polygon_max.y = p.y;
  1393. }
  1394. }
  1395. }
  1396. Vector2 final_polygon_size = final_polygon_max - final_polygon_min;
  1397. if (mode == MODE_PATH) {
  1398. if (!has_node(path_node)) {
  1399. return memnew(CSGBrush);
  1400. }
  1401. Node *n = get_node(path_node);
  1402. if (!n) {
  1403. return memnew(CSGBrush);
  1404. }
  1405. path = Object::cast_to<Path3D>(n);
  1406. if (!path) {
  1407. return memnew(CSGBrush);
  1408. }
  1409. if (path != path_cache) {
  1410. if (path_cache) {
  1411. path_cache->disconnect("tree_exited", callable_mp(this, &CSGPolygon3D::_path_exited));
  1412. path_cache->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1413. path_cache = nullptr;
  1414. }
  1415. path_cache = path;
  1416. path_cache->connect("tree_exited", callable_mp(this, &CSGPolygon3D::_path_exited));
  1417. path_cache->connect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1418. path_cache = nullptr;
  1419. }
  1420. curve = path->get_curve();
  1421. if (curve.is_null()) {
  1422. return memnew(CSGBrush);
  1423. }
  1424. if (curve->get_baked_length() <= 0) {
  1425. return memnew(CSGBrush);
  1426. }
  1427. }
  1428. CSGBrush *brush = memnew(CSGBrush);
  1429. int face_count = 0;
  1430. switch (mode) {
  1431. case MODE_DEPTH:
  1432. face_count = triangles.size() * 2 / 3 + (final_polygon.size()) * 2;
  1433. break;
  1434. case MODE_SPIN:
  1435. face_count = (spin_degrees < 360 ? triangles.size() * 2 / 3 : 0) + (final_polygon.size()) * 2 * spin_sides;
  1436. break;
  1437. case MODE_PATH: {
  1438. float bl = curve->get_baked_length();
  1439. int splits = MAX(2, Math::ceil(bl / path_interval));
  1440. if (path_joined) {
  1441. face_count = splits * final_polygon.size() * 2;
  1442. } else {
  1443. face_count = triangles.size() * 2 / 3 + splits * final_polygon.size() * 2;
  1444. }
  1445. } break;
  1446. }
  1447. bool invert_val = is_inverting_faces();
  1448. Ref<Material> material = get_material();
  1449. Vector<Vector3> faces;
  1450. Vector<Vector2> uvs;
  1451. Vector<bool> smooth;
  1452. Vector<Ref<Material>> materials;
  1453. Vector<bool> invert;
  1454. faces.resize(face_count * 3);
  1455. uvs.resize(face_count * 3);
  1456. smooth.resize(face_count);
  1457. materials.resize(face_count);
  1458. invert.resize(face_count);
  1459. AABB aabb; //must be computed
  1460. {
  1461. Vector3 *facesw = faces.ptrw();
  1462. Vector2 *uvsw = uvs.ptrw();
  1463. bool *smoothw = smooth.ptrw();
  1464. Ref<Material> *materialsw = materials.ptrw();
  1465. bool *invertw = invert.ptrw();
  1466. int face = 0;
  1467. switch (mode) {
  1468. case MODE_DEPTH: {
  1469. //add triangles, front and back
  1470. for (int i = 0; i < 2; i++) {
  1471. for (int j = 0; j < triangles.size(); j += 3) {
  1472. for (int k = 0; k < 3; k++) {
  1473. int src[3] = { 0, i == 0 ? 1 : 2, i == 0 ? 2 : 1 };
  1474. Vector2 p = final_polygon[triangles[j + src[k]]];
  1475. Vector3 v = Vector3(p.x, p.y, 0);
  1476. if (i == 0) {
  1477. v.z -= depth;
  1478. }
  1479. facesw[face * 3 + k] = v;
  1480. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1481. if (i == 0) {
  1482. uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
  1483. }
  1484. }
  1485. smoothw[face] = false;
  1486. materialsw[face] = material;
  1487. invertw[face] = invert_val;
  1488. face++;
  1489. }
  1490. }
  1491. //add triangles for depth
  1492. for (int i = 0; i < final_polygon.size(); i++) {
  1493. int i_n = (i + 1) % final_polygon.size();
  1494. Vector3 v[4] = {
  1495. Vector3(final_polygon[i].x, final_polygon[i].y, -depth),
  1496. Vector3(final_polygon[i_n].x, final_polygon[i_n].y, -depth),
  1497. Vector3(final_polygon[i_n].x, final_polygon[i_n].y, 0),
  1498. Vector3(final_polygon[i].x, final_polygon[i].y, 0),
  1499. };
  1500. Vector2 u[4] = {
  1501. Vector2(0, 0),
  1502. Vector2(0, 1),
  1503. Vector2(1, 1),
  1504. Vector2(1, 0)
  1505. };
  1506. // face 1
  1507. facesw[face * 3 + 0] = v[0];
  1508. facesw[face * 3 + 1] = v[1];
  1509. facesw[face * 3 + 2] = v[2];
  1510. uvsw[face * 3 + 0] = u[0];
  1511. uvsw[face * 3 + 1] = u[1];
  1512. uvsw[face * 3 + 2] = u[2];
  1513. smoothw[face] = smooth_faces;
  1514. invertw[face] = invert_val;
  1515. materialsw[face] = material;
  1516. face++;
  1517. // face 2
  1518. facesw[face * 3 + 0] = v[2];
  1519. facesw[face * 3 + 1] = v[3];
  1520. facesw[face * 3 + 2] = v[0];
  1521. uvsw[face * 3 + 0] = u[2];
  1522. uvsw[face * 3 + 1] = u[3];
  1523. uvsw[face * 3 + 2] = u[0];
  1524. smoothw[face] = smooth_faces;
  1525. invertw[face] = invert_val;
  1526. materialsw[face] = material;
  1527. face++;
  1528. }
  1529. } break;
  1530. case MODE_SPIN: {
  1531. for (int i = 0; i < spin_sides; i++) {
  1532. float inci = float(i) / spin_sides;
  1533. float inci_n = float((i + 1)) / spin_sides;
  1534. float angi = -(inci * spin_degrees / 360.0) * Math_PI * 2.0;
  1535. float angi_n = -(inci_n * spin_degrees / 360.0) * Math_PI * 2.0;
  1536. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1537. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1538. //add triangles for depth
  1539. for (int j = 0; j < final_polygon.size(); j++) {
  1540. int j_n = (j + 1) % final_polygon.size();
  1541. Vector3 v[4] = {
  1542. Vector3(normali.x * final_polygon[j].x, final_polygon[j].y, normali.z * final_polygon[j].x),
  1543. Vector3(normali.x * final_polygon[j_n].x, final_polygon[j_n].y, normali.z * final_polygon[j_n].x),
  1544. Vector3(normali_n.x * final_polygon[j_n].x, final_polygon[j_n].y, normali_n.z * final_polygon[j_n].x),
  1545. Vector3(normali_n.x * final_polygon[j].x, final_polygon[j].y, normali_n.z * final_polygon[j].x),
  1546. };
  1547. Vector2 u[4] = {
  1548. Vector2(0, 0),
  1549. Vector2(0, 1),
  1550. Vector2(1, 1),
  1551. Vector2(1, 0)
  1552. };
  1553. // face 1
  1554. facesw[face * 3 + 0] = v[0];
  1555. facesw[face * 3 + 1] = v[2];
  1556. facesw[face * 3 + 2] = v[1];
  1557. uvsw[face * 3 + 0] = u[0];
  1558. uvsw[face * 3 + 1] = u[2];
  1559. uvsw[face * 3 + 2] = u[1];
  1560. smoothw[face] = smooth_faces;
  1561. invertw[face] = invert_val;
  1562. materialsw[face] = material;
  1563. face++;
  1564. // face 2
  1565. facesw[face * 3 + 0] = v[2];
  1566. facesw[face * 3 + 1] = v[0];
  1567. facesw[face * 3 + 2] = v[3];
  1568. uvsw[face * 3 + 0] = u[2];
  1569. uvsw[face * 3 + 1] = u[0];
  1570. uvsw[face * 3 + 2] = u[3];
  1571. smoothw[face] = smooth_faces;
  1572. invertw[face] = invert_val;
  1573. materialsw[face] = material;
  1574. face++;
  1575. }
  1576. if (i == 0 && spin_degrees < 360) {
  1577. for (int j = 0; j < triangles.size(); j += 3) {
  1578. for (int k = 0; k < 3; k++) {
  1579. int src[3] = { 0, 2, 1 };
  1580. Vector2 p = final_polygon[triangles[j + src[k]]];
  1581. Vector3 v = Vector3(p.x, p.y, 0);
  1582. facesw[face * 3 + k] = v;
  1583. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1584. }
  1585. smoothw[face] = false;
  1586. materialsw[face] = material;
  1587. invertw[face] = invert_val;
  1588. face++;
  1589. }
  1590. }
  1591. if (i == spin_sides - 1 && spin_degrees < 360) {
  1592. for (int j = 0; j < triangles.size(); j += 3) {
  1593. for (int k = 0; k < 3; k++) {
  1594. int src[3] = { 0, 1, 2 };
  1595. Vector2 p = final_polygon[triangles[j + src[k]]];
  1596. Vector3 v = Vector3(normali_n.x * p.x, p.y, normali_n.z * p.x);
  1597. facesw[face * 3 + k] = v;
  1598. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1599. uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
  1600. }
  1601. smoothw[face] = false;
  1602. materialsw[face] = material;
  1603. invertw[face] = invert_val;
  1604. face++;
  1605. }
  1606. }
  1607. }
  1608. } break;
  1609. case MODE_PATH: {
  1610. float bl = curve->get_baked_length();
  1611. int splits = MAX(2, Math::ceil(bl / path_interval));
  1612. float u1 = 0.0;
  1613. float u2 = path_continuous_u ? 0.0 : 1.0;
  1614. Transform path_to_this;
  1615. if (!path_local) {
  1616. // center on paths origin
  1617. path_to_this = get_global_transform().affine_inverse() * path->get_global_transform();
  1618. }
  1619. Transform prev_xf;
  1620. Vector3 lookat_dir;
  1621. if (path_rotation == PATH_ROTATION_POLYGON) {
  1622. lookat_dir = (path->get_global_transform().affine_inverse() * get_global_transform()).xform(Vector3(0, 0, -1));
  1623. } else {
  1624. Vector3 p1, p2;
  1625. p1 = curve->interpolate_baked(0);
  1626. p2 = curve->interpolate_baked(0.1);
  1627. lookat_dir = (p2 - p1).normalized();
  1628. }
  1629. for (int i = 0; i <= splits; i++) {
  1630. float ofs = i * path_interval;
  1631. if (ofs > bl) {
  1632. ofs = bl;
  1633. }
  1634. if (i == splits && path_joined) {
  1635. ofs = 0.0;
  1636. }
  1637. Transform xf;
  1638. xf.origin = curve->interpolate_baked(ofs);
  1639. Vector3 local_dir;
  1640. if (path_rotation == PATH_ROTATION_PATH_FOLLOW && ofs > 0) {
  1641. //before end
  1642. Vector3 p1 = curve->interpolate_baked(ofs - 0.1);
  1643. Vector3 p2 = curve->interpolate_baked(ofs);
  1644. local_dir = (p2 - p1).normalized();
  1645. } else {
  1646. local_dir = lookat_dir;
  1647. }
  1648. xf = xf.looking_at(xf.origin + local_dir, Vector3(0, 1, 0));
  1649. Basis rot(Vector3(0, 0, 1), curve->interpolate_baked_tilt(ofs));
  1650. xf = xf * rot; //post mult
  1651. xf = path_to_this * xf;
  1652. if (i > 0) {
  1653. if (path_continuous_u) {
  1654. u1 = u2;
  1655. u2 += (prev_xf.origin - xf.origin).length();
  1656. };
  1657. //put triangles where they belong
  1658. //add triangles for depth
  1659. for (int j = 0; j < final_polygon.size(); j++) {
  1660. int j_n = (j + 1) % final_polygon.size();
  1661. Vector3 v[4] = {
  1662. prev_xf.xform(Vector3(final_polygon[j].x, final_polygon[j].y, 0)),
  1663. prev_xf.xform(Vector3(final_polygon[j_n].x, final_polygon[j_n].y, 0)),
  1664. xf.xform(Vector3(final_polygon[j_n].x, final_polygon[j_n].y, 0)),
  1665. xf.xform(Vector3(final_polygon[j].x, final_polygon[j].y, 0)),
  1666. };
  1667. Vector2 u[4] = {
  1668. Vector2(u1, 1),
  1669. Vector2(u1, 0),
  1670. Vector2(u2, 0),
  1671. Vector2(u2, 1)
  1672. };
  1673. // face 1
  1674. facesw[face * 3 + 0] = v[0];
  1675. facesw[face * 3 + 1] = v[1];
  1676. facesw[face * 3 + 2] = v[2];
  1677. uvsw[face * 3 + 0] = u[0];
  1678. uvsw[face * 3 + 1] = u[1];
  1679. uvsw[face * 3 + 2] = u[2];
  1680. smoothw[face] = smooth_faces;
  1681. invertw[face] = invert_val;
  1682. materialsw[face] = material;
  1683. face++;
  1684. // face 2
  1685. facesw[face * 3 + 0] = v[2];
  1686. facesw[face * 3 + 1] = v[3];
  1687. facesw[face * 3 + 2] = v[0];
  1688. uvsw[face * 3 + 0] = u[2];
  1689. uvsw[face * 3 + 1] = u[3];
  1690. uvsw[face * 3 + 2] = u[0];
  1691. smoothw[face] = smooth_faces;
  1692. invertw[face] = invert_val;
  1693. materialsw[face] = material;
  1694. face++;
  1695. }
  1696. }
  1697. if (i == 0 && !path_joined) {
  1698. for (int j = 0; j < triangles.size(); j += 3) {
  1699. for (int k = 0; k < 3; k++) {
  1700. int src[3] = { 0, 1, 2 };
  1701. Vector2 p = final_polygon[triangles[j + src[k]]];
  1702. Vector3 v = Vector3(p.x, p.y, 0);
  1703. facesw[face * 3 + k] = xf.xform(v);
  1704. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1705. }
  1706. smoothw[face] = false;
  1707. materialsw[face] = material;
  1708. invertw[face] = invert_val;
  1709. face++;
  1710. }
  1711. }
  1712. if (i == splits && !path_joined) {
  1713. for (int j = 0; j < triangles.size(); j += 3) {
  1714. for (int k = 0; k < 3; k++) {
  1715. int src[3] = { 0, 2, 1 };
  1716. Vector2 p = final_polygon[triangles[j + src[k]]];
  1717. Vector3 v = Vector3(p.x, p.y, 0);
  1718. facesw[face * 3 + k] = xf.xform(v);
  1719. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1720. uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
  1721. }
  1722. smoothw[face] = false;
  1723. materialsw[face] = material;
  1724. invertw[face] = invert_val;
  1725. face++;
  1726. }
  1727. }
  1728. prev_xf = xf;
  1729. }
  1730. } break;
  1731. }
  1732. if (face != face_count) {
  1733. ERR_PRINT("Face mismatch bug! fix code");
  1734. }
  1735. for (int i = 0; i < face_count * 3; i++) {
  1736. if (i == 0) {
  1737. aabb.position = facesw[i];
  1738. } else {
  1739. aabb.expand_to(facesw[i]);
  1740. }
  1741. // invert UVs on the Y-axis OpenGL = upside down
  1742. uvsw[i].y = 1.0 - uvsw[i].y;
  1743. }
  1744. }
  1745. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1746. return brush;
  1747. }
  1748. void CSGPolygon3D::_notification(int p_what) {
  1749. if (p_what == NOTIFICATION_EXIT_TREE) {
  1750. if (path_cache) {
  1751. path_cache->disconnect("tree_exited", callable_mp(this, &CSGPolygon3D::_path_exited));
  1752. path_cache->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1753. path_cache = nullptr;
  1754. }
  1755. }
  1756. }
  1757. void CSGPolygon3D::_validate_property(PropertyInfo &property) const {
  1758. if (property.name.begins_with("spin") && mode != MODE_SPIN) {
  1759. property.usage = 0;
  1760. }
  1761. if (property.name.begins_with("path") && mode != MODE_PATH) {
  1762. property.usage = 0;
  1763. }
  1764. if (property.name == "depth" && mode != MODE_DEPTH) {
  1765. property.usage = 0;
  1766. }
  1767. CSGShape3D::_validate_property(property);
  1768. }
  1769. void CSGPolygon3D::_path_changed() {
  1770. _make_dirty();
  1771. update_gizmo();
  1772. }
  1773. void CSGPolygon3D::_path_exited() {
  1774. path_cache = nullptr;
  1775. }
  1776. void CSGPolygon3D::_bind_methods() {
  1777. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CSGPolygon3D::set_polygon);
  1778. ClassDB::bind_method(D_METHOD("get_polygon"), &CSGPolygon3D::get_polygon);
  1779. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSGPolygon3D::set_mode);
  1780. ClassDB::bind_method(D_METHOD("get_mode"), &CSGPolygon3D::get_mode);
  1781. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGPolygon3D::set_depth);
  1782. ClassDB::bind_method(D_METHOD("get_depth"), &CSGPolygon3D::get_depth);
  1783. ClassDB::bind_method(D_METHOD("set_spin_degrees", "degrees"), &CSGPolygon3D::set_spin_degrees);
  1784. ClassDB::bind_method(D_METHOD("get_spin_degrees"), &CSGPolygon3D::get_spin_degrees);
  1785. ClassDB::bind_method(D_METHOD("set_spin_sides", "spin_sides"), &CSGPolygon3D::set_spin_sides);
  1786. ClassDB::bind_method(D_METHOD("get_spin_sides"), &CSGPolygon3D::get_spin_sides);
  1787. ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon3D::set_path_node);
  1788. ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon3D::get_path_node);
  1789. ClassDB::bind_method(D_METHOD("set_path_interval", "distance"), &CSGPolygon3D::set_path_interval);
  1790. ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon3D::get_path_interval);
  1791. ClassDB::bind_method(D_METHOD("set_path_rotation", "mode"), &CSGPolygon3D::set_path_rotation);
  1792. ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon3D::get_path_rotation);
  1793. ClassDB::bind_method(D_METHOD("set_path_local", "enable"), &CSGPolygon3D::set_path_local);
  1794. ClassDB::bind_method(D_METHOD("is_path_local"), &CSGPolygon3D::is_path_local);
  1795. ClassDB::bind_method(D_METHOD("set_path_continuous_u", "enable"), &CSGPolygon3D::set_path_continuous_u);
  1796. ClassDB::bind_method(D_METHOD("is_path_continuous_u"), &CSGPolygon3D::is_path_continuous_u);
  1797. ClassDB::bind_method(D_METHOD("set_path_joined", "enable"), &CSGPolygon3D::set_path_joined);
  1798. ClassDB::bind_method(D_METHOD("is_path_joined"), &CSGPolygon3D::is_path_joined);
  1799. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGPolygon3D::set_material);
  1800. ClassDB::bind_method(D_METHOD("get_material"), &CSGPolygon3D::get_material);
  1801. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGPolygon3D::set_smooth_faces);
  1802. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGPolygon3D::get_smooth_faces);
  1803. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CSGPolygon3D::_is_editable_3d_polygon);
  1804. ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &CSGPolygon3D::_has_editable_3d_polygon_no_depth);
  1805. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  1806. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Depth,Spin,Path"), "set_mode", "get_mode");
  1807. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_depth", "get_depth");
  1808. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
  1809. ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
  1810. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path"), "set_path_node", "get_path_node");
  1811. 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");
  1812. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
  1813. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
  1814. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
  1815. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_joined"), "set_path_joined", "is_path_joined");
  1816. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1817. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1818. BIND_ENUM_CONSTANT(MODE_DEPTH);
  1819. BIND_ENUM_CONSTANT(MODE_SPIN);
  1820. BIND_ENUM_CONSTANT(MODE_PATH);
  1821. BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
  1822. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
  1823. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
  1824. }
  1825. void CSGPolygon3D::set_polygon(const Vector<Vector2> &p_polygon) {
  1826. polygon = p_polygon;
  1827. _make_dirty();
  1828. update_gizmo();
  1829. }
  1830. Vector<Vector2> CSGPolygon3D::get_polygon() const {
  1831. return polygon;
  1832. }
  1833. void CSGPolygon3D::set_mode(Mode p_mode) {
  1834. mode = p_mode;
  1835. _make_dirty();
  1836. update_gizmo();
  1837. _change_notify();
  1838. }
  1839. CSGPolygon3D::Mode CSGPolygon3D::get_mode() const {
  1840. return mode;
  1841. }
  1842. void CSGPolygon3D::set_depth(const float p_depth) {
  1843. ERR_FAIL_COND(p_depth < 0.001);
  1844. depth = p_depth;
  1845. _make_dirty();
  1846. update_gizmo();
  1847. }
  1848. float CSGPolygon3D::get_depth() const {
  1849. return depth;
  1850. }
  1851. void CSGPolygon3D::set_path_continuous_u(bool p_enable) {
  1852. path_continuous_u = p_enable;
  1853. _make_dirty();
  1854. }
  1855. bool CSGPolygon3D::is_path_continuous_u() const {
  1856. return path_continuous_u;
  1857. }
  1858. void CSGPolygon3D::set_spin_degrees(const float p_spin_degrees) {
  1859. ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
  1860. spin_degrees = p_spin_degrees;
  1861. _make_dirty();
  1862. update_gizmo();
  1863. }
  1864. float CSGPolygon3D::get_spin_degrees() const {
  1865. return spin_degrees;
  1866. }
  1867. void CSGPolygon3D::set_spin_sides(const int p_spin_sides) {
  1868. ERR_FAIL_COND(p_spin_sides < 3);
  1869. spin_sides = p_spin_sides;
  1870. _make_dirty();
  1871. update_gizmo();
  1872. }
  1873. int CSGPolygon3D::get_spin_sides() const {
  1874. return spin_sides;
  1875. }
  1876. void CSGPolygon3D::set_path_node(const NodePath &p_path) {
  1877. path_node = p_path;
  1878. _make_dirty();
  1879. update_gizmo();
  1880. }
  1881. NodePath CSGPolygon3D::get_path_node() const {
  1882. return path_node;
  1883. }
  1884. void CSGPolygon3D::set_path_interval(float p_interval) {
  1885. ERR_FAIL_COND_MSG(p_interval < 0.001, "Path interval cannot be smaller than 0.001.");
  1886. path_interval = p_interval;
  1887. _make_dirty();
  1888. update_gizmo();
  1889. }
  1890. float CSGPolygon3D::get_path_interval() const {
  1891. return path_interval;
  1892. }
  1893. void CSGPolygon3D::set_path_rotation(PathRotation p_rotation) {
  1894. path_rotation = p_rotation;
  1895. _make_dirty();
  1896. update_gizmo();
  1897. }
  1898. CSGPolygon3D::PathRotation CSGPolygon3D::get_path_rotation() const {
  1899. return path_rotation;
  1900. }
  1901. void CSGPolygon3D::set_path_local(bool p_enable) {
  1902. path_local = p_enable;
  1903. _make_dirty();
  1904. update_gizmo();
  1905. }
  1906. bool CSGPolygon3D::is_path_local() const {
  1907. return path_local;
  1908. }
  1909. void CSGPolygon3D::set_path_joined(bool p_enable) {
  1910. path_joined = p_enable;
  1911. _make_dirty();
  1912. update_gizmo();
  1913. }
  1914. bool CSGPolygon3D::is_path_joined() const {
  1915. return path_joined;
  1916. }
  1917. void CSGPolygon3D::set_smooth_faces(const bool p_smooth_faces) {
  1918. smooth_faces = p_smooth_faces;
  1919. _make_dirty();
  1920. }
  1921. bool CSGPolygon3D::get_smooth_faces() const {
  1922. return smooth_faces;
  1923. }
  1924. void CSGPolygon3D::set_material(const Ref<Material> &p_material) {
  1925. material = p_material;
  1926. _make_dirty();
  1927. }
  1928. Ref<Material> CSGPolygon3D::get_material() const {
  1929. return material;
  1930. }
  1931. bool CSGPolygon3D::_is_editable_3d_polygon() const {
  1932. return true;
  1933. }
  1934. bool CSGPolygon3D::_has_editable_3d_polygon_no_depth() const {
  1935. return true;
  1936. }
  1937. CSGPolygon3D::CSGPolygon3D() {
  1938. // defaults
  1939. mode = MODE_DEPTH;
  1940. polygon.push_back(Vector2(0, 0));
  1941. polygon.push_back(Vector2(0, 1));
  1942. polygon.push_back(Vector2(1, 1));
  1943. polygon.push_back(Vector2(1, 0));
  1944. depth = 1.0;
  1945. spin_degrees = 360;
  1946. spin_sides = 8;
  1947. smooth_faces = false;
  1948. path_interval = 1;
  1949. path_rotation = PATH_ROTATION_PATH;
  1950. path_local = false;
  1951. path_continuous_u = false;
  1952. path_joined = false;
  1953. path_cache = nullptr;
  1954. }