csg_shape.cpp 68 KB

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