csg_shape.cpp 68 KB

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