csg_shape.cpp 67 KB

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