csg_shape.cpp 67 KB

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