csg_shape.cpp 69 KB

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