csg_shape.cpp 69 KB

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