csg_shape.cpp 71 KB

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