csg_shape.cpp 68 KB

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