csg_shape.cpp 69 KB

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