csg_shape.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829
  1. /**************************************************************************/
  2. /* csg_shape.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifdef DEV_ENABLED
  32. #include "core/io/json.h"
  33. #endif // DEV_ENABLED
  34. #include "core/math/geometry_2d.h"
  35. #include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
  36. #include "scene/resources/navigation_mesh.h"
  37. #ifndef NAVIGATION_3D_DISABLED
  38. #include "servers/navigation_server_3d.h"
  39. #endif // NAVIGATION_3D_DISABLED
  40. #include <manifold/manifold.h>
  41. #ifndef NAVIGATION_3D_DISABLED
  42. Callable CSGShape3D::_navmesh_source_geometry_parsing_callback;
  43. RID CSGShape3D::_navmesh_source_geometry_parser;
  44. void CSGShape3D::navmesh_parse_init() {
  45. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  46. if (!_navmesh_source_geometry_parser.is_valid()) {
  47. _navmesh_source_geometry_parsing_callback = callable_mp_static(&CSGShape3D::navmesh_parse_source_geometry);
  48. _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
  49. NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  50. }
  51. }
  52. void CSGShape3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
  53. CSGShape3D *csgshape3d = Object::cast_to<CSGShape3D>(p_node);
  54. if (csgshape3d == nullptr) {
  55. return;
  56. }
  57. NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  58. #ifndef PHYSICS_3D_DISABLED
  59. bool nav_collision = (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS && csgshape3d->is_using_collision() && (csgshape3d->get_collision_layer() & p_navigation_mesh->get_collision_mask()));
  60. #else
  61. bool nav_collision = false;
  62. #endif // PHYSICS_3D_DISABLED
  63. if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || nav_collision || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) {
  64. Array meshes = csgshape3d->get_meshes();
  65. if (!meshes.is_empty()) {
  66. Ref<Mesh> mesh = meshes[1];
  67. if (mesh.is_valid()) {
  68. p_source_geometry_data->add_mesh(mesh, csgshape3d->get_global_transform());
  69. }
  70. }
  71. }
  72. }
  73. #endif // NAVIGATION_3D_DISABLED
  74. #ifndef PHYSICS_3D_DISABLED
  75. void CSGShape3D::set_use_collision(bool p_enable) {
  76. if (use_collision == p_enable) {
  77. return;
  78. }
  79. use_collision = p_enable;
  80. if (!is_inside_tree() || !is_root_shape()) {
  81. return;
  82. }
  83. if (use_collision) {
  84. root_collision_shape.instantiate();
  85. root_collision_instance = PhysicsServer3D::get_singleton()->body_create();
  86. PhysicsServer3D::get_singleton()->body_set_mode(root_collision_instance, PhysicsServer3D::BODY_MODE_STATIC);
  87. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  88. PhysicsServer3D::get_singleton()->body_add_shape(root_collision_instance, root_collision_shape->get_rid());
  89. PhysicsServer3D::get_singleton()->body_set_space(root_collision_instance, get_world_3d()->get_space());
  90. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(root_collision_instance, get_instance_id());
  91. set_collision_layer(collision_layer);
  92. set_collision_mask(collision_mask);
  93. set_collision_priority(collision_priority);
  94. _make_dirty(); //force update
  95. } else {
  96. PhysicsServer3D::get_singleton()->free(root_collision_instance);
  97. root_collision_instance = RID();
  98. root_collision_shape.unref();
  99. }
  100. notify_property_list_changed();
  101. }
  102. bool CSGShape3D::is_using_collision() const {
  103. return use_collision;
  104. }
  105. void CSGShape3D::set_collision_layer(uint32_t p_layer) {
  106. collision_layer = p_layer;
  107. if (root_collision_instance.is_valid()) {
  108. PhysicsServer3D::get_singleton()->body_set_collision_layer(root_collision_instance, p_layer);
  109. }
  110. }
  111. uint32_t CSGShape3D::get_collision_layer() const {
  112. return collision_layer;
  113. }
  114. void CSGShape3D::set_collision_mask(uint32_t p_mask) {
  115. collision_mask = p_mask;
  116. if (root_collision_instance.is_valid()) {
  117. PhysicsServer3D::get_singleton()->body_set_collision_mask(root_collision_instance, p_mask);
  118. }
  119. }
  120. uint32_t CSGShape3D::get_collision_mask() const {
  121. return collision_mask;
  122. }
  123. void CSGShape3D::set_collision_layer_value(int p_layer_number, bool p_value) {
  124. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  125. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  126. uint32_t layer = get_collision_layer();
  127. if (p_value) {
  128. layer |= 1 << (p_layer_number - 1);
  129. } else {
  130. layer &= ~(1 << (p_layer_number - 1));
  131. }
  132. set_collision_layer(layer);
  133. }
  134. bool CSGShape3D::get_collision_layer_value(int p_layer_number) const {
  135. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  136. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  137. return get_collision_layer() & (1 << (p_layer_number - 1));
  138. }
  139. void CSGShape3D::set_collision_mask_value(int p_layer_number, bool p_value) {
  140. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  141. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  142. uint32_t mask = get_collision_mask();
  143. if (p_value) {
  144. mask |= 1 << (p_layer_number - 1);
  145. } else {
  146. mask &= ~(1 << (p_layer_number - 1));
  147. }
  148. set_collision_mask(mask);
  149. }
  150. bool CSGShape3D::get_collision_mask_value(int p_layer_number) const {
  151. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  152. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  153. return get_collision_mask() & (1 << (p_layer_number - 1));
  154. }
  155. RID CSGShape3D::_get_root_collision_instance() const {
  156. if (root_collision_instance.is_valid()) {
  157. return root_collision_instance;
  158. } else if (parent_shape) {
  159. return parent_shape->_get_root_collision_instance();
  160. }
  161. return RID();
  162. }
  163. void CSGShape3D::set_collision_priority(real_t p_priority) {
  164. collision_priority = p_priority;
  165. if (root_collision_instance.is_valid()) {
  166. PhysicsServer3D::get_singleton()->body_set_collision_priority(root_collision_instance, p_priority);
  167. }
  168. }
  169. real_t CSGShape3D::get_collision_priority() const {
  170. return collision_priority;
  171. }
  172. #endif // PHYSICS_3D_DISABLED
  173. bool CSGShape3D::is_root_shape() const {
  174. return !parent_shape;
  175. }
  176. #ifndef DISABLE_DEPRECATED
  177. void CSGShape3D::set_snap(float p_snap) {
  178. if (snap == p_snap) {
  179. return;
  180. }
  181. snap = p_snap;
  182. _make_dirty();
  183. }
  184. float CSGShape3D::get_snap() const {
  185. return snap;
  186. }
  187. #endif // DISABLE_DEPRECATED
  188. void CSGShape3D::_make_dirty(bool p_parent_removing) {
  189. #ifndef PHYSICS_3D_DISABLED
  190. if ((p_parent_removing || is_root_shape()) && !dirty) {
  191. callable_mp(this, &CSGShape3D::update_shape).call_deferred(); // Must be deferred; otherwise, is_root_shape() will use the previous parent.
  192. }
  193. #endif // PHYSICS_3D_DISABLED
  194. if (!is_root_shape()) {
  195. parent_shape->_make_dirty();
  196. }
  197. #ifndef PHYSICS_3D_DISABLED
  198. else if (!dirty) {
  199. callable_mp(this, &CSGShape3D::update_shape).call_deferred();
  200. }
  201. #endif // PHYSICS_3D_DISABLED
  202. dirty = true;
  203. }
  204. enum ManifoldProperty {
  205. MANIFOLD_PROPERTY_POSITION_X = 0,
  206. MANIFOLD_PROPERTY_POSITION_Y,
  207. MANIFOLD_PROPERTY_POSITION_Z,
  208. MANIFOLD_PROPERTY_INVERT,
  209. MANIFOLD_PROPERTY_SMOOTH_GROUP,
  210. MANIFOLD_PROPERTY_UV_X_0,
  211. MANIFOLD_PROPERTY_UV_Y_0,
  212. MANIFOLD_PROPERTY_MAX
  213. };
  214. static void _unpack_manifold(
  215. const manifold::Manifold &p_manifold,
  216. const HashMap<int32_t, Ref<Material>> &p_mesh_materials,
  217. CSGBrush *r_mesh_merge) {
  218. manifold::MeshGL64 mesh = p_manifold.GetMeshGL64();
  219. constexpr int32_t order[3] = { 0, 2, 1 };
  220. for (size_t run_i = 0; run_i < mesh.runIndex.size() - 1; run_i++) {
  221. uint32_t original_id = -1;
  222. if (run_i < mesh.runOriginalID.size()) {
  223. original_id = mesh.runOriginalID[run_i];
  224. }
  225. Ref<Material> material;
  226. if (p_mesh_materials.has(original_id)) {
  227. material = p_mesh_materials[original_id];
  228. }
  229. // Find or reserve a material ID in the brush.
  230. int32_t material_id = r_mesh_merge->materials.find(material);
  231. if (material_id == -1) {
  232. material_id = r_mesh_merge->materials.size();
  233. r_mesh_merge->materials.push_back(material);
  234. }
  235. size_t begin = mesh.runIndex[run_i];
  236. size_t end = mesh.runIndex[run_i + 1];
  237. for (size_t vert_i = begin; vert_i < end; vert_i += 3) {
  238. CSGBrush::Face face;
  239. face.material = material_id;
  240. int32_t first_property_index = mesh.triVerts[vert_i + order[0]];
  241. face.smooth = mesh.vertProperties[first_property_index * mesh.numProp + MANIFOLD_PROPERTY_SMOOTH_GROUP] > 0.5f;
  242. face.invert = mesh.vertProperties[first_property_index * mesh.numProp + MANIFOLD_PROPERTY_INVERT] > 0.5f;
  243. for (int32_t tri_order_i = 0; tri_order_i < 3; tri_order_i++) {
  244. int32_t property_i = mesh.triVerts[vert_i + order[tri_order_i]];
  245. ERR_FAIL_COND_MSG(property_i * mesh.numProp >= mesh.vertProperties.size(), "Invalid index into vertex properties");
  246. face.vertices[tri_order_i] = Vector3(
  247. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_POSITION_X],
  248. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_POSITION_Y],
  249. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_POSITION_Z]);
  250. face.uvs[tri_order_i] = Vector2(
  251. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_UV_X_0],
  252. mesh.vertProperties[property_i * mesh.numProp + MANIFOLD_PROPERTY_UV_Y_0]);
  253. }
  254. r_mesh_merge->faces.push_back(face);
  255. }
  256. }
  257. r_mesh_merge->_regen_face_aabbs();
  258. }
  259. #ifdef DEV_ENABLED
  260. static String _export_meshgl_as_json(const manifold::MeshGL64 &p_mesh) {
  261. Dictionary mesh_dict;
  262. mesh_dict["numProp"] = p_mesh.numProp;
  263. Array vert_properties;
  264. for (const double &val : p_mesh.vertProperties) {
  265. vert_properties.append(val);
  266. }
  267. mesh_dict["vertProperties"] = vert_properties;
  268. Array tri_verts;
  269. for (const uint64_t &val : p_mesh.triVerts) {
  270. tri_verts.append(val);
  271. }
  272. mesh_dict["triVerts"] = tri_verts;
  273. Array merge_from_vert;
  274. for (const uint64_t &val : p_mesh.mergeFromVert) {
  275. merge_from_vert.append(val);
  276. }
  277. mesh_dict["mergeFromVert"] = merge_from_vert;
  278. Array merge_to_vert;
  279. for (const uint64_t &val : p_mesh.mergeToVert) {
  280. merge_to_vert.append(val);
  281. }
  282. mesh_dict["mergeToVert"] = merge_to_vert;
  283. Array run_index;
  284. for (const uint64_t &val : p_mesh.runIndex) {
  285. run_index.append(val);
  286. }
  287. mesh_dict["runIndex"] = run_index;
  288. Array run_original_id;
  289. for (const uint32_t &val : p_mesh.runOriginalID) {
  290. run_original_id.append(val);
  291. }
  292. mesh_dict["runOriginalID"] = run_original_id;
  293. Array run_transform;
  294. for (const double &val : p_mesh.runTransform) {
  295. run_transform.append(val);
  296. }
  297. mesh_dict["runTransform"] = run_transform;
  298. Array face_id;
  299. for (const uint64_t &val : p_mesh.faceID) {
  300. face_id.append(val);
  301. }
  302. mesh_dict["faceID"] = face_id;
  303. Array halfedge_tangent;
  304. for (const double &val : p_mesh.halfedgeTangent) {
  305. halfedge_tangent.append(val);
  306. }
  307. mesh_dict["halfedgeTangent"] = halfedge_tangent;
  308. mesh_dict["tolerance"] = p_mesh.tolerance;
  309. String json_string = JSON::stringify(mesh_dict);
  310. return json_string;
  311. }
  312. #endif // DEV_ENABLED
  313. static void _pack_manifold(
  314. const CSGBrush *const p_mesh_merge,
  315. manifold::Manifold &r_manifold,
  316. HashMap<int32_t, Ref<Material>> &p_mesh_materials,
  317. CSGShape3D *p_csg_shape) {
  318. ERR_FAIL_NULL_MSG(p_mesh_merge, "p_mesh_merge is null");
  319. ERR_FAIL_NULL_MSG(p_csg_shape, "p_shape is null");
  320. HashMap<uint32_t, Vector<CSGBrush::Face>> faces_by_material;
  321. for (int face_i = 0; face_i < p_mesh_merge->faces.size(); face_i++) {
  322. const CSGBrush::Face &face = p_mesh_merge->faces[face_i];
  323. faces_by_material[face.material].push_back(face);
  324. }
  325. manifold::MeshGL64 mesh;
  326. mesh.numProp = MANIFOLD_PROPERTY_MAX;
  327. mesh.runOriginalID.reserve(faces_by_material.size());
  328. mesh.runIndex.reserve(faces_by_material.size() + 1);
  329. mesh.vertProperties.reserve(p_mesh_merge->faces.size() * 3 * MANIFOLD_PROPERTY_MAX);
  330. // Make a run of triangles for each material.
  331. for (const KeyValue<uint32_t, Vector<CSGBrush::Face>> &E : faces_by_material) {
  332. const uint32_t material_id = E.key;
  333. const Vector<CSGBrush::Face> &faces = E.value;
  334. mesh.runIndex.push_back(mesh.triVerts.size());
  335. // Associate the material with an ID.
  336. uint32_t reserved_id = r_manifold.ReserveIDs(1);
  337. mesh.runOriginalID.push_back(reserved_id);
  338. Ref<Material> material;
  339. if (material_id < p_mesh_merge->materials.size()) {
  340. material = p_mesh_merge->materials[material_id];
  341. }
  342. p_mesh_materials.insert(reserved_id, material);
  343. for (const CSGBrush::Face &face : faces) {
  344. for (int32_t tri_order_i = 0; tri_order_i < 3; tri_order_i++) {
  345. constexpr int32_t order[3] = { 0, 2, 1 };
  346. int i = order[tri_order_i];
  347. mesh.triVerts.push_back(mesh.vertProperties.size() / MANIFOLD_PROPERTY_MAX);
  348. size_t begin = mesh.vertProperties.size();
  349. mesh.vertProperties.resize(mesh.vertProperties.size() + MANIFOLD_PROPERTY_MAX);
  350. // Add the vertex properties.
  351. // Use CSGBrush constants rather than push_back for clarity.
  352. double *vert = &mesh.vertProperties[begin];
  353. vert[MANIFOLD_PROPERTY_POSITION_X] = face.vertices[i].x;
  354. vert[MANIFOLD_PROPERTY_POSITION_Y] = face.vertices[i].y;
  355. vert[MANIFOLD_PROPERTY_POSITION_Z] = face.vertices[i].z;
  356. vert[MANIFOLD_PROPERTY_UV_X_0] = face.uvs[i].x;
  357. vert[MANIFOLD_PROPERTY_UV_Y_0] = face.uvs[i].y;
  358. vert[MANIFOLD_PROPERTY_SMOOTH_GROUP] = face.smooth ? 1.0f : 0.0f;
  359. vert[MANIFOLD_PROPERTY_INVERT] = face.invert ? 1.0f : 0.0f;
  360. }
  361. }
  362. }
  363. // runIndex needs an explicit end value.
  364. mesh.runIndex.push_back(mesh.triVerts.size());
  365. mesh.tolerance = 2 * FLT_EPSILON;
  366. ERR_FAIL_COND_MSG(mesh.vertProperties.size() % mesh.numProp != 0, "Invalid vertex properties size.");
  367. mesh.Merge();
  368. #ifdef DEV_ENABLED
  369. print_verbose(_export_meshgl_as_json(mesh));
  370. #endif // DEV_ENABLED
  371. r_manifold = manifold::Manifold(mesh);
  372. }
  373. struct ManifoldOperation {
  374. manifold::Manifold manifold;
  375. manifold::OpType operation;
  376. static manifold::OpType convert_csg_op(CSGShape3D::Operation op) {
  377. switch (op) {
  378. case CSGShape3D::OPERATION_SUBTRACTION:
  379. return manifold::OpType::Subtract;
  380. case CSGShape3D::OPERATION_INTERSECTION:
  381. return manifold::OpType::Intersect;
  382. default:
  383. return manifold::OpType::Add;
  384. }
  385. }
  386. ManifoldOperation() :
  387. operation(manifold::OpType::Add) {}
  388. ManifoldOperation(const manifold::Manifold &m, manifold::OpType op) :
  389. manifold(m), operation(op) {}
  390. };
  391. CSGBrush *CSGShape3D::_get_brush() {
  392. if (!dirty) {
  393. return brush;
  394. }
  395. if (brush) {
  396. memdelete(brush);
  397. }
  398. brush = nullptr;
  399. CSGBrush *n = _build_brush();
  400. HashMap<int32_t, Ref<Material>> mesh_materials;
  401. manifold::Manifold root_manifold;
  402. _pack_manifold(n, root_manifold, mesh_materials, this);
  403. manifold::OpType current_op = ManifoldOperation::convert_csg_op(get_operation());
  404. std::vector<manifold::Manifold> manifolds;
  405. manifolds.push_back(root_manifold);
  406. for (int i = 0; i < get_child_count(); i++) {
  407. CSGShape3D *child = Object::cast_to<CSGShape3D>(get_child(i));
  408. if (!child || !child->is_visible()) {
  409. continue;
  410. }
  411. CSGBrush *child_brush = child->_get_brush();
  412. if (!child_brush) {
  413. continue;
  414. }
  415. CSGBrush transformed_brush;
  416. transformed_brush.copy_from(*child_brush, child->get_transform());
  417. manifold::Manifold child_manifold;
  418. _pack_manifold(&transformed_brush, child_manifold, mesh_materials, child);
  419. manifold::OpType child_operation = ManifoldOperation::convert_csg_op(child->get_operation());
  420. if (child_operation != current_op) {
  421. manifold::Manifold result = manifold::Manifold::BatchBoolean(manifolds, current_op);
  422. manifolds.clear();
  423. manifolds.push_back(result);
  424. current_op = child_operation;
  425. }
  426. manifolds.push_back(child_manifold);
  427. }
  428. if (!manifolds.empty()) {
  429. manifold::Manifold manifold_result = manifold::Manifold::BatchBoolean(manifolds, current_op);
  430. if (n) {
  431. memdelete(n);
  432. }
  433. n = memnew(CSGBrush);
  434. _unpack_manifold(manifold_result, mesh_materials, n);
  435. }
  436. AABB aabb;
  437. if (n && !n->faces.is_empty()) {
  438. aabb.position = n->faces[0].vertices[0];
  439. for (const CSGBrush::Face &face : n->faces) {
  440. for (int i = 0; i < 3; ++i) {
  441. aabb.expand_to(face.vertices[i]);
  442. }
  443. }
  444. }
  445. node_aabb = aabb;
  446. brush = n;
  447. dirty = false;
  448. update_configuration_warnings();
  449. return brush;
  450. }
  451. int CSGShape3D::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
  452. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  453. return surface.vertices.size() / 3;
  454. }
  455. int CSGShape3D::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
  456. // always 3
  457. return 3;
  458. }
  459. void CSGShape3D::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
  460. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  461. Vector3 v = surface.verticesw[iFace * 3 + iVert];
  462. fvPosOut[0] = v.x;
  463. fvPosOut[1] = v.y;
  464. fvPosOut[2] = v.z;
  465. }
  466. void CSGShape3D::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert) {
  467. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  468. Vector3 n = surface.normalsw[iFace * 3 + iVert];
  469. fvNormOut[0] = n.x;
  470. fvNormOut[1] = n.y;
  471. fvNormOut[2] = n.z;
  472. }
  473. void CSGShape3D::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
  474. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  475. Vector2 t = surface.uvsw[iFace * 3 + iVert];
  476. fvTexcOut[0] = t.x;
  477. fvTexcOut[1] = t.y;
  478. }
  479. void CSGShape3D::mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  480. const tbool bIsOrientationPreserving, const int iFace, const int iVert) {
  481. ShapeUpdateSurface &surface = *((ShapeUpdateSurface *)pContext->m_pUserData);
  482. int i = iFace * 3 + iVert;
  483. Vector3 normal = surface.normalsw[i];
  484. Vector3 tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
  485. Vector3 bitangent = Vector3(-fvBiTangent[0], -fvBiTangent[1], -fvBiTangent[2]); // for some reason these are reversed, something with the coordinate system in Godot
  486. float d = bitangent.dot(normal.cross(tangent));
  487. i *= 4;
  488. surface.tansw[i++] = tangent.x;
  489. surface.tansw[i++] = tangent.y;
  490. surface.tansw[i++] = tangent.z;
  491. surface.tansw[i++] = d < 0 ? -1 : 1;
  492. }
  493. void CSGShape3D::update_shape() {
  494. if (!is_root_shape()) {
  495. return;
  496. }
  497. set_base(RID());
  498. root_mesh.unref(); //byebye root mesh
  499. CSGBrush *n = _get_brush();
  500. ERR_FAIL_NULL_MSG(n, "Cannot get CSGBrush.");
  501. AHashMap<Vector3, Vector3> vec_map;
  502. Vector<int> face_count;
  503. face_count.resize(n->materials.size() + 1);
  504. for (int i = 0; i < face_count.size(); i++) {
  505. face_count.write[i] = 0;
  506. }
  507. for (int i = 0; i < n->faces.size(); i++) {
  508. int mat = n->faces[i].material;
  509. ERR_CONTINUE(mat < -1 || mat >= face_count.size());
  510. int idx = mat == -1 ? face_count.size() - 1 : mat;
  511. if (n->faces[i].smooth) {
  512. Plane p(n->faces[i].vertices[0], n->faces[i].vertices[1], n->faces[i].vertices[2]);
  513. for (int j = 0; j < 3; j++) {
  514. Vector3 v = n->faces[i].vertices[j];
  515. Vector3 *vec = vec_map.getptr(v);
  516. if (vec) {
  517. *vec += p.normal;
  518. } else {
  519. vec_map.insert(v, p.normal);
  520. }
  521. }
  522. }
  523. face_count.write[idx]++;
  524. }
  525. Vector<ShapeUpdateSurface> surfaces;
  526. surfaces.resize(face_count.size());
  527. //create arrays
  528. for (int i = 0; i < surfaces.size(); i++) {
  529. surfaces.write[i].vertices.resize(face_count[i] * 3);
  530. surfaces.write[i].normals.resize(face_count[i] * 3);
  531. surfaces.write[i].uvs.resize(face_count[i] * 3);
  532. if (calculate_tangents) {
  533. surfaces.write[i].tans.resize(face_count[i] * 3 * 4);
  534. }
  535. surfaces.write[i].last_added = 0;
  536. if (i != surfaces.size() - 1) {
  537. surfaces.write[i].material = n->materials[i];
  538. }
  539. surfaces.write[i].verticesw = surfaces.write[i].vertices.ptrw();
  540. surfaces.write[i].normalsw = surfaces.write[i].normals.ptrw();
  541. surfaces.write[i].uvsw = surfaces.write[i].uvs.ptrw();
  542. if (calculate_tangents) {
  543. surfaces.write[i].tansw = surfaces.write[i].tans.ptrw();
  544. }
  545. }
  546. //fill arrays
  547. {
  548. for (int i = 0; i < n->faces.size(); i++) {
  549. int order[3] = { 0, 1, 2 };
  550. if (n->faces[i].invert) {
  551. SWAP(order[1], order[2]);
  552. }
  553. int mat = n->faces[i].material;
  554. ERR_CONTINUE(mat < -1 || mat >= face_count.size());
  555. int idx = mat == -1 ? face_count.size() - 1 : mat;
  556. int last = surfaces[idx].last_added;
  557. Plane p(n->faces[i].vertices[0], n->faces[i].vertices[1], n->faces[i].vertices[2]);
  558. for (int j = 0; j < 3; j++) {
  559. Vector3 v = n->faces[i].vertices[j];
  560. Vector3 normal = p.normal;
  561. if (n->faces[i].smooth) {
  562. Vector3 *ptr = vec_map.getptr(v);
  563. if (ptr) {
  564. normal = ptr->normalized();
  565. }
  566. }
  567. if (n->faces[i].invert) {
  568. normal = -normal;
  569. }
  570. int k = last + order[j];
  571. surfaces[idx].verticesw[k] = v;
  572. surfaces[idx].uvsw[k] = n->faces[i].uvs[j];
  573. surfaces[idx].normalsw[k] = normal;
  574. if (calculate_tangents) {
  575. // zero out our tangents for now
  576. k *= 4;
  577. surfaces[idx].tansw[k++] = 0.0;
  578. surfaces[idx].tansw[k++] = 0.0;
  579. surfaces[idx].tansw[k++] = 0.0;
  580. surfaces[idx].tansw[k++] = 0.0;
  581. }
  582. }
  583. surfaces.write[idx].last_added += 3;
  584. }
  585. }
  586. root_mesh.instantiate();
  587. //create surfaces
  588. for (int i = 0; i < surfaces.size(); i++) {
  589. // calculate tangents for this surface
  590. bool have_tangents = calculate_tangents;
  591. if (have_tangents) {
  592. SMikkTSpaceInterface mkif;
  593. mkif.m_getNormal = mikktGetNormal;
  594. mkif.m_getNumFaces = mikktGetNumFaces;
  595. mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
  596. mkif.m_getPosition = mikktGetPosition;
  597. mkif.m_getTexCoord = mikktGetTexCoord;
  598. mkif.m_setTSpace = mikktSetTSpaceDefault;
  599. mkif.m_setTSpaceBasic = nullptr;
  600. SMikkTSpaceContext msc;
  601. msc.m_pInterface = &mkif;
  602. msc.m_pUserData = &surfaces.write[i];
  603. have_tangents = genTangSpaceDefault(&msc);
  604. }
  605. if (surfaces[i].last_added == 0) {
  606. continue;
  607. }
  608. // and convert to surface array
  609. Array array;
  610. array.resize(Mesh::ARRAY_MAX);
  611. array[Mesh::ARRAY_VERTEX] = surfaces[i].vertices;
  612. array[Mesh::ARRAY_NORMAL] = surfaces[i].normals;
  613. array[Mesh::ARRAY_TEX_UV] = surfaces[i].uvs;
  614. if (have_tangents) {
  615. array[Mesh::ARRAY_TANGENT] = surfaces[i].tans;
  616. }
  617. int idx = root_mesh->get_surface_count();
  618. root_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array);
  619. root_mesh->surface_set_material(idx, surfaces[i].material);
  620. }
  621. set_base(root_mesh->get_rid());
  622. #ifndef PHYSICS_3D_DISABLED
  623. _update_collision_faces();
  624. #endif // PHYSICS_3D_DISABLED
  625. }
  626. Ref<ArrayMesh> CSGShape3D::bake_static_mesh() {
  627. Ref<ArrayMesh> baked_mesh;
  628. if (is_root_shape() && root_mesh.is_valid()) {
  629. baked_mesh = root_mesh;
  630. }
  631. return baked_mesh;
  632. }
  633. #ifndef PHYSICS_3D_DISABLED
  634. Vector<Vector3> CSGShape3D::_get_brush_collision_faces() {
  635. Vector<Vector3> collision_faces;
  636. CSGBrush *n = _get_brush();
  637. ERR_FAIL_NULL_V_MSG(n, collision_faces, "Cannot get CSGBrush.");
  638. collision_faces.resize(n->faces.size() * 3);
  639. Vector3 *collision_faces_ptrw = collision_faces.ptrw();
  640. for (int i = 0; i < n->faces.size(); i++) {
  641. int order[3] = { 0, 1, 2 };
  642. if (n->faces[i].invert) {
  643. SWAP(order[1], order[2]);
  644. }
  645. collision_faces_ptrw[i * 3 + 0] = n->faces[i].vertices[order[0]];
  646. collision_faces_ptrw[i * 3 + 1] = n->faces[i].vertices[order[1]];
  647. collision_faces_ptrw[i * 3 + 2] = n->faces[i].vertices[order[2]];
  648. }
  649. return collision_faces;
  650. }
  651. void CSGShape3D::_update_collision_faces() {
  652. if (use_collision && is_root_shape() && root_collision_shape.is_valid()) {
  653. root_collision_shape->set_faces(_get_brush_collision_faces());
  654. if (_is_debug_collision_shape_visible()) {
  655. _update_debug_collision_shape();
  656. }
  657. }
  658. }
  659. Ref<ConcavePolygonShape3D> CSGShape3D::bake_collision_shape() {
  660. Ref<ConcavePolygonShape3D> baked_collision_shape;
  661. if (is_root_shape() && root_collision_shape.is_valid()) {
  662. baked_collision_shape.instantiate();
  663. baked_collision_shape->set_faces(root_collision_shape->get_faces());
  664. } else if (is_root_shape()) {
  665. baked_collision_shape.instantiate();
  666. baked_collision_shape->set_faces(_get_brush_collision_faces());
  667. }
  668. return baked_collision_shape;
  669. }
  670. bool CSGShape3D::_is_debug_collision_shape_visible() {
  671. return !Engine::get_singleton()->is_editor_hint() && is_inside_tree() && get_tree()->is_debugging_collisions_hint();
  672. }
  673. void CSGShape3D::_update_debug_collision_shape() {
  674. if (!use_collision || !is_root_shape() || root_collision_shape.is_null() || !_is_debug_collision_shape_visible()) {
  675. return;
  676. }
  677. ERR_FAIL_NULL(RenderingServer::get_singleton());
  678. if (root_collision_debug_instance.is_null()) {
  679. root_collision_debug_instance = RS::get_singleton()->instance_create();
  680. }
  681. Ref<Mesh> debug_mesh = root_collision_shape->get_debug_mesh();
  682. RS::get_singleton()->instance_set_scenario(root_collision_debug_instance, get_world_3d()->get_scenario());
  683. RS::get_singleton()->instance_set_base(root_collision_debug_instance, debug_mesh->get_rid());
  684. RS::get_singleton()->instance_set_transform(root_collision_debug_instance, get_global_transform());
  685. }
  686. void CSGShape3D::_clear_debug_collision_shape() {
  687. if (root_collision_debug_instance.is_valid()) {
  688. RS::get_singleton()->free(root_collision_debug_instance);
  689. root_collision_debug_instance = RID();
  690. }
  691. }
  692. void CSGShape3D::_on_transform_changed() {
  693. if (root_collision_debug_instance.is_valid() && !debug_shape_old_transform.is_equal_approx(get_global_transform())) {
  694. debug_shape_old_transform = get_global_transform();
  695. RS::get_singleton()->instance_set_transform(root_collision_debug_instance, debug_shape_old_transform);
  696. }
  697. }
  698. #endif // PHYSICS_3D_DISABLED
  699. AABB CSGShape3D::get_aabb() const {
  700. return node_aabb;
  701. }
  702. Vector<Vector3> CSGShape3D::get_brush_faces() {
  703. ERR_FAIL_COND_V(!is_inside_tree(), Vector<Vector3>());
  704. CSGBrush *b = _get_brush();
  705. if (!b) {
  706. return Vector<Vector3>();
  707. }
  708. Vector<Vector3> faces;
  709. int fc = b->faces.size();
  710. faces.resize(fc * 3);
  711. {
  712. Vector3 *w = faces.ptrw();
  713. for (int i = 0; i < fc; i++) {
  714. w[i * 3 + 0] = b->faces[i].vertices[0];
  715. w[i * 3 + 1] = b->faces[i].vertices[1];
  716. w[i * 3 + 2] = b->faces[i].vertices[2];
  717. }
  718. }
  719. return faces;
  720. }
  721. void CSGShape3D::_notification(int p_what) {
  722. switch (p_what) {
  723. case NOTIFICATION_PARENTED: {
  724. Node *parentn = get_parent();
  725. if (parentn) {
  726. parent_shape = Object::cast_to<CSGShape3D>(parentn);
  727. if (parent_shape) {
  728. set_base(RID());
  729. root_mesh.unref();
  730. }
  731. }
  732. if (!brush || parent_shape) {
  733. // Update this node if uninitialized, or both this node and its new parent if it gets added to another CSG shape
  734. _make_dirty();
  735. }
  736. last_visible = is_visible();
  737. } break;
  738. case NOTIFICATION_UNPARENTED: {
  739. if (!is_root_shape()) {
  740. // Update this node and its previous parent only if it's currently being removed from another CSG shape
  741. _make_dirty(true); // Must be forced since is_root_shape() uses the previous parent
  742. }
  743. parent_shape = nullptr;
  744. } break;
  745. case NOTIFICATION_CHILD_ORDER_CHANGED: {
  746. _make_dirty();
  747. } break;
  748. case NOTIFICATION_VISIBILITY_CHANGED: {
  749. if (!is_root_shape() && last_visible != is_visible()) {
  750. // Update this node's parent only if its own visibility has changed, not the visibility of parent nodes
  751. parent_shape->_make_dirty();
  752. }
  753. last_visible = is_visible();
  754. } break;
  755. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  756. if (!is_root_shape()) {
  757. // Update this node's parent only if its own transformation has changed, not the transformation of parent nodes
  758. parent_shape->_make_dirty();
  759. }
  760. } break;
  761. #ifndef PHYSICS_3D_DISABLED
  762. case NOTIFICATION_ENTER_TREE: {
  763. if (use_collision && is_root_shape()) {
  764. root_collision_shape.instantiate();
  765. root_collision_instance = PhysicsServer3D::get_singleton()->body_create();
  766. PhysicsServer3D::get_singleton()->body_set_mode(root_collision_instance, PhysicsServer3D::BODY_MODE_STATIC);
  767. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  768. PhysicsServer3D::get_singleton()->body_add_shape(root_collision_instance, root_collision_shape->get_rid());
  769. PhysicsServer3D::get_singleton()->body_set_space(root_collision_instance, get_world_3d()->get_space());
  770. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(root_collision_instance, get_instance_id());
  771. set_collision_layer(collision_layer);
  772. set_collision_mask(collision_mask);
  773. set_collision_priority(collision_priority);
  774. debug_shape_old_transform = get_global_transform();
  775. _make_dirty();
  776. }
  777. } break;
  778. case NOTIFICATION_EXIT_TREE: {
  779. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  780. PhysicsServer3D::get_singleton()->free(root_collision_instance);
  781. root_collision_instance = RID();
  782. root_collision_shape.unref();
  783. _clear_debug_collision_shape();
  784. }
  785. } break;
  786. case NOTIFICATION_TRANSFORM_CHANGED: {
  787. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  788. PhysicsServer3D::get_singleton()->body_set_state(root_collision_instance, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  789. }
  790. _on_transform_changed();
  791. } break;
  792. #endif // PHYSICS_3D_DISABLED
  793. }
  794. }
  795. void CSGShape3D::set_operation(Operation p_operation) {
  796. operation = p_operation;
  797. _make_dirty();
  798. update_gizmos();
  799. }
  800. CSGShape3D::Operation CSGShape3D::get_operation() const {
  801. return operation;
  802. }
  803. void CSGShape3D::set_calculate_tangents(bool p_calculate_tangents) {
  804. calculate_tangents = p_calculate_tangents;
  805. _make_dirty();
  806. }
  807. bool CSGShape3D::is_calculating_tangents() const {
  808. return calculate_tangents;
  809. }
  810. void CSGShape3D::_validate_property(PropertyInfo &p_property) const {
  811. if (!Engine::get_singleton()->is_editor_hint()) {
  812. return;
  813. }
  814. bool is_collision_prefixed = p_property.name.begins_with("collision_");
  815. if ((is_collision_prefixed || p_property.name.begins_with("use_collision")) && is_inside_tree() && !is_root_shape()) {
  816. //hide collision if not root
  817. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  818. } else if (is_collision_prefixed && !bool(get("use_collision"))) {
  819. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  820. }
  821. }
  822. Array CSGShape3D::get_meshes() const {
  823. if (root_mesh.is_valid()) {
  824. Array arr;
  825. arr.resize(2);
  826. arr[0] = Transform3D();
  827. arr[1] = root_mesh;
  828. return arr;
  829. }
  830. return Array();
  831. }
  832. PackedStringArray CSGShape3D::get_configuration_warnings() const {
  833. PackedStringArray warnings = Node::get_configuration_warnings();
  834. const CSGShape3D *current_shape = this;
  835. while (current_shape) {
  836. if (!current_shape->brush || current_shape->brush->faces.is_empty()) {
  837. warnings.push_back(RTR("The CSGShape3D has an empty shape.\nCSGShape3D empty shapes typically occur because the mesh is not manifold.\nA manifold mesh forms a solid object without gaps, holes, or loose edges.\nEach edge must be a member of exactly two faces."));
  838. break;
  839. }
  840. current_shape = current_shape->parent_shape;
  841. }
  842. return warnings;
  843. }
  844. Ref<TriangleMesh> CSGShape3D::generate_triangle_mesh() const {
  845. if (root_mesh.is_valid()) {
  846. return root_mesh->generate_triangle_mesh();
  847. }
  848. return Ref<TriangleMesh>();
  849. }
  850. void CSGShape3D::_bind_methods() {
  851. ClassDB::bind_method(D_METHOD("is_root_shape"), &CSGShape3D::is_root_shape);
  852. ClassDB::bind_method(D_METHOD("set_operation", "operation"), &CSGShape3D::set_operation);
  853. ClassDB::bind_method(D_METHOD("get_operation"), &CSGShape3D::get_operation);
  854. #ifndef DISABLE_DEPRECATED
  855. ClassDB::bind_method(D_METHOD("_update_shape"), &CSGShape3D::update_shape);
  856. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &CSGShape3D::set_snap);
  857. ClassDB::bind_method(D_METHOD("get_snap"), &CSGShape3D::get_snap);
  858. #endif // DISABLE_DEPRECATED
  859. #ifndef PHYSICS_3D_DISABLED
  860. ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape3D::set_use_collision);
  861. ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape3D::is_using_collision);
  862. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CSGShape3D::set_collision_layer);
  863. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CSGShape3D::get_collision_layer);
  864. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CSGShape3D::set_collision_mask);
  865. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CSGShape3D::get_collision_mask);
  866. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CSGShape3D::set_collision_mask_value);
  867. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CSGShape3D::get_collision_mask_value);
  868. ClassDB::bind_method(D_METHOD("_get_root_collision_instance"), &CSGShape3D::_get_root_collision_instance);
  869. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CSGShape3D::set_collision_layer_value);
  870. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CSGShape3D::get_collision_layer_value);
  871. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CSGShape3D::set_collision_priority);
  872. ClassDB::bind_method(D_METHOD("get_collision_priority"), &CSGShape3D::get_collision_priority);
  873. ClassDB::bind_method(D_METHOD("bake_collision_shape"), &CSGShape3D::bake_collision_shape);
  874. #endif // PHYSICS_3D_DISABLED
  875. ClassDB::bind_method(D_METHOD("set_calculate_tangents", "enabled"), &CSGShape3D::set_calculate_tangents);
  876. ClassDB::bind_method(D_METHOD("is_calculating_tangents"), &CSGShape3D::is_calculating_tangents);
  877. ClassDB::bind_method(D_METHOD("get_meshes"), &CSGShape3D::get_meshes);
  878. ClassDB::bind_method(D_METHOD("bake_static_mesh"), &CSGShape3D::bake_static_mesh);
  879. ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "Union,Intersection,Subtraction"), "set_operation", "get_operation");
  880. #ifndef DISABLE_DEPRECATED
  881. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_RANGE, "0.000001,1,0.000001,suffix:m", PROPERTY_USAGE_NONE), "set_snap", "get_snap");
  882. #endif // DISABLE_DEPRECATED
  883. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "calculate_tangents"), "set_calculate_tangents", "is_calculating_tangents");
  884. #ifndef PHYSICS_3D_DISABLED
  885. ADD_GROUP("Collision", "collision_");
  886. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_collision"), "set_use_collision", "is_using_collision");
  887. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  888. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  889. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  890. #endif // PHYSICS_3D_DISABLED
  891. BIND_ENUM_CONSTANT(OPERATION_UNION);
  892. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  893. BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
  894. }
  895. CSGShape3D::CSGShape3D() {
  896. set_notify_local_transform(true);
  897. }
  898. CSGShape3D::~CSGShape3D() {
  899. if (brush) {
  900. memdelete(brush);
  901. brush = nullptr;
  902. }
  903. }
  904. //////////////////////////////////
  905. CSGBrush *CSGCombiner3D::_build_brush() {
  906. return memnew(CSGBrush); //does not build anything
  907. }
  908. CSGCombiner3D::CSGCombiner3D() {
  909. }
  910. /////////////////////
  911. 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) {
  912. CSGBrush *new_brush = memnew(CSGBrush);
  913. Vector<bool> invert;
  914. invert.resize(p_vertices.size() / 3);
  915. {
  916. int ic = invert.size();
  917. bool *w = invert.ptrw();
  918. for (int i = 0; i < ic; i++) {
  919. w[i] = flip_faces;
  920. }
  921. }
  922. new_brush->build_from_faces(p_vertices, p_uv, p_smooth, p_materials, invert);
  923. return new_brush;
  924. }
  925. void CSGPrimitive3D::_bind_methods() {
  926. ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &CSGPrimitive3D::set_flip_faces);
  927. ClassDB::bind_method(D_METHOD("get_flip_faces"), &CSGPrimitive3D::get_flip_faces);
  928. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
  929. }
  930. void CSGPrimitive3D::set_flip_faces(bool p_invert) {
  931. if (flip_faces == p_invert) {
  932. return;
  933. }
  934. flip_faces = p_invert;
  935. _make_dirty();
  936. }
  937. bool CSGPrimitive3D::get_flip_faces() {
  938. return flip_faces;
  939. }
  940. CSGPrimitive3D::CSGPrimitive3D() {
  941. flip_faces = false;
  942. }
  943. /////////////////////
  944. CSGBrush *CSGMesh3D::_build_brush() {
  945. if (mesh.is_null()) {
  946. return memnew(CSGBrush);
  947. }
  948. Vector<Vector3> vertices;
  949. Vector<bool> smooth;
  950. Vector<Ref<Material>> materials;
  951. Vector<Vector2> uvs;
  952. Ref<Material> base_material = get_material();
  953. for (int i = 0; i < mesh->get_surface_count(); i++) {
  954. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  955. continue;
  956. }
  957. Array arrays = mesh->surface_get_arrays(i);
  958. if (arrays.is_empty()) {
  959. _make_dirty();
  960. ERR_FAIL_COND_V(arrays.is_empty(), memnew(CSGBrush));
  961. }
  962. Vector<Vector3> avertices = arrays[Mesh::ARRAY_VERTEX];
  963. if (avertices.is_empty()) {
  964. continue;
  965. }
  966. const Vector3 *vr = avertices.ptr();
  967. Vector<Vector3> anormals = arrays[Mesh::ARRAY_NORMAL];
  968. const Vector3 *nr = nullptr;
  969. if (anormals.size()) {
  970. nr = anormals.ptr();
  971. }
  972. Vector<Vector2> auvs = arrays[Mesh::ARRAY_TEX_UV];
  973. const Vector2 *uvr = nullptr;
  974. if (auvs.size()) {
  975. uvr = auvs.ptr();
  976. }
  977. Ref<Material> mat;
  978. if (base_material.is_valid()) {
  979. mat = base_material;
  980. } else {
  981. mat = mesh->surface_get_material(i);
  982. }
  983. Vector<int> aindices = arrays[Mesh::ARRAY_INDEX];
  984. if (aindices.size()) {
  985. int as = vertices.size();
  986. int is = aindices.size();
  987. vertices.resize(as + is);
  988. smooth.resize((as + is) / 3);
  989. materials.resize((as + is) / 3);
  990. uvs.resize(as + is);
  991. Vector3 *vw = vertices.ptrw();
  992. bool *sw = smooth.ptrw();
  993. Vector2 *uvw = uvs.ptrw();
  994. Ref<Material> *mw = materials.ptrw();
  995. const int *ir = aindices.ptr();
  996. for (int j = 0; j < is; j += 3) {
  997. Vector3 vertex[3];
  998. Vector3 normal[3];
  999. Vector2 uv[3];
  1000. for (int k = 0; k < 3; k++) {
  1001. int idx = ir[j + k];
  1002. vertex[k] = vr[idx];
  1003. if (nr) {
  1004. normal[k] = nr[idx];
  1005. }
  1006. if (uvr) {
  1007. uv[k] = uvr[idx];
  1008. }
  1009. }
  1010. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  1011. vw[as + j + 0] = vertex[0];
  1012. vw[as + j + 1] = vertex[1];
  1013. vw[as + j + 2] = vertex[2];
  1014. uvw[as + j + 0] = uv[0];
  1015. uvw[as + j + 1] = uv[1];
  1016. uvw[as + j + 2] = uv[2];
  1017. sw[(as + j) / 3] = !flat;
  1018. mw[(as + j) / 3] = mat;
  1019. }
  1020. } else {
  1021. int as = vertices.size();
  1022. int is = avertices.size();
  1023. vertices.resize(as + is);
  1024. smooth.resize((as + is) / 3);
  1025. uvs.resize(as + is);
  1026. materials.resize((as + is) / 3);
  1027. Vector3 *vw = vertices.ptrw();
  1028. bool *sw = smooth.ptrw();
  1029. Vector2 *uvw = uvs.ptrw();
  1030. Ref<Material> *mw = materials.ptrw();
  1031. for (int j = 0; j < is; j += 3) {
  1032. Vector3 vertex[3];
  1033. Vector3 normal[3];
  1034. Vector2 uv[3];
  1035. for (int k = 0; k < 3; k++) {
  1036. vertex[k] = vr[j + k];
  1037. if (nr) {
  1038. normal[k] = nr[j + k];
  1039. }
  1040. if (uvr) {
  1041. uv[k] = uvr[j + k];
  1042. }
  1043. }
  1044. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  1045. vw[as + j + 0] = vertex[0];
  1046. vw[as + j + 1] = vertex[1];
  1047. vw[as + j + 2] = vertex[2];
  1048. uvw[as + j + 0] = uv[0];
  1049. uvw[as + j + 1] = uv[1];
  1050. uvw[as + j + 2] = uv[2];
  1051. sw[(as + j) / 3] = !flat;
  1052. mw[(as + j) / 3] = mat;
  1053. }
  1054. }
  1055. }
  1056. if (vertices.is_empty()) {
  1057. return memnew(CSGBrush);
  1058. }
  1059. return _create_brush_from_arrays(vertices, uvs, smooth, materials);
  1060. }
  1061. void CSGMesh3D::_mesh_changed() {
  1062. _make_dirty();
  1063. callable_mp((Node3D *)this, &Node3D::update_gizmos).call_deferred();
  1064. }
  1065. void CSGMesh3D::set_material(const Ref<Material> &p_material) {
  1066. if (material == p_material) {
  1067. return;
  1068. }
  1069. material = p_material;
  1070. _make_dirty();
  1071. }
  1072. Ref<Material> CSGMesh3D::get_material() const {
  1073. return material;
  1074. }
  1075. void CSGMesh3D::_bind_methods() {
  1076. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CSGMesh3D::set_mesh);
  1077. ClassDB::bind_method(D_METHOD("get_mesh"), &CSGMesh3D::get_mesh);
  1078. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGMesh3D::set_material);
  1079. ClassDB::bind_method(D_METHOD("get_material"), &CSGMesh3D::get_material);
  1080. // Hide PrimitiveMeshes that are always non-manifold and therefore can't be used as CSG meshes.
  1081. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh,-PlaneMesh,-PointMesh,-QuadMesh,-RibbonTrailMesh"), "set_mesh", "get_mesh");
  1082. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1083. }
  1084. void CSGMesh3D::set_mesh(const Ref<Mesh> &p_mesh) {
  1085. if (mesh == p_mesh) {
  1086. return;
  1087. }
  1088. if (mesh.is_valid()) {
  1089. mesh->disconnect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  1090. }
  1091. mesh = p_mesh;
  1092. if (mesh.is_valid()) {
  1093. mesh->connect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  1094. }
  1095. _mesh_changed();
  1096. }
  1097. Ref<Mesh> CSGMesh3D::get_mesh() {
  1098. return mesh;
  1099. }
  1100. ////////////////////////////////
  1101. CSGBrush *CSGSphere3D::_build_brush() {
  1102. // set our bounding box
  1103. CSGBrush *new_brush = memnew(CSGBrush);
  1104. int face_count = rings * radial_segments * 2 - radial_segments * 2;
  1105. bool invert_val = get_flip_faces();
  1106. Ref<Material> base_material = get_material();
  1107. Vector<Vector3> faces;
  1108. Vector<Vector2> uvs;
  1109. Vector<bool> smooth;
  1110. Vector<Ref<Material>> materials;
  1111. Vector<bool> invert;
  1112. faces.resize(face_count * 3);
  1113. uvs.resize(face_count * 3);
  1114. smooth.resize(face_count);
  1115. materials.resize(face_count);
  1116. invert.resize(face_count);
  1117. {
  1118. Vector3 *facesw = faces.ptrw();
  1119. Vector2 *uvsw = uvs.ptrw();
  1120. bool *smoothw = smooth.ptrw();
  1121. Ref<Material> *materialsw = materials.ptrw();
  1122. bool *invertw = invert.ptrw();
  1123. // We want to follow an order that's convenient for UVs.
  1124. // For latitude step we start at the top and move down like in an image.
  1125. const double latitude_step = -Math::PI / rings;
  1126. const double longitude_step = Math::TAU / radial_segments;
  1127. int face = 0;
  1128. for (int i = 0; i < rings; i++) {
  1129. double cos0 = 0;
  1130. double sin0 = 1;
  1131. if (i > 0) {
  1132. double latitude0 = latitude_step * i + Math::TAU / 4;
  1133. cos0 = Math::cos(latitude0);
  1134. sin0 = Math::sin(latitude0);
  1135. }
  1136. double v0 = double(i) / rings;
  1137. double cos1 = 0;
  1138. double sin1 = -1;
  1139. if (i < rings - 1) {
  1140. double latitude1 = latitude_step * (i + 1) + Math::TAU / 4;
  1141. cos1 = Math::cos(latitude1);
  1142. sin1 = Math::sin(latitude1);
  1143. }
  1144. double v1 = double(i + 1) / rings;
  1145. for (int j = 0; j < radial_segments; j++) {
  1146. double longitude0 = longitude_step * j;
  1147. // We give sin to X and cos to Z on purpose.
  1148. // This allows UVs to be CCW on +X so it maps to images well.
  1149. double x0 = Math::sin(longitude0);
  1150. double z0 = Math::cos(longitude0);
  1151. double u0 = double(j) / radial_segments;
  1152. double longitude1 = longitude_step * (j + 1);
  1153. if (j == radial_segments - 1) {
  1154. longitude1 = 0;
  1155. }
  1156. double x1 = Math::sin(longitude1);
  1157. double z1 = Math::cos(longitude1);
  1158. double u1 = double(j + 1) / radial_segments;
  1159. Vector3 v[4] = {
  1160. Vector3(x0 * cos0, sin0, z0 * cos0) * radius,
  1161. Vector3(x1 * cos0, sin0, z1 * cos0) * radius,
  1162. Vector3(x1 * cos1, sin1, z1 * cos1) * radius,
  1163. Vector3(x0 * cos1, sin1, z0 * cos1) * radius,
  1164. };
  1165. Vector2 u[4] = {
  1166. Vector2(u0, v0),
  1167. Vector2(u1, v0),
  1168. Vector2(u1, v1),
  1169. Vector2(u0, v1),
  1170. };
  1171. // Draw the first face, but skip this at the north pole (i == 0).
  1172. if (i > 0) {
  1173. facesw[face * 3 + 0] = v[0];
  1174. facesw[face * 3 + 1] = v[1];
  1175. facesw[face * 3 + 2] = v[2];
  1176. uvsw[face * 3 + 0] = u[0];
  1177. uvsw[face * 3 + 1] = u[1];
  1178. uvsw[face * 3 + 2] = u[2];
  1179. smoothw[face] = smooth_faces;
  1180. invertw[face] = invert_val;
  1181. materialsw[face] = base_material;
  1182. face++;
  1183. }
  1184. // Draw the second face, but skip this at the south pole (i == rings - 1).
  1185. if (i < rings - 1) {
  1186. facesw[face * 3 + 0] = v[2];
  1187. facesw[face * 3 + 1] = v[3];
  1188. facesw[face * 3 + 2] = v[0];
  1189. uvsw[face * 3 + 0] = u[2];
  1190. uvsw[face * 3 + 1] = u[3];
  1191. uvsw[face * 3 + 2] = u[0];
  1192. smoothw[face] = smooth_faces;
  1193. invertw[face] = invert_val;
  1194. materialsw[face] = base_material;
  1195. face++;
  1196. }
  1197. }
  1198. }
  1199. if (face != face_count) {
  1200. ERR_PRINT("Face mismatch bug! fix code");
  1201. }
  1202. }
  1203. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1204. return new_brush;
  1205. }
  1206. void CSGSphere3D::_bind_methods() {
  1207. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGSphere3D::set_radius);
  1208. ClassDB::bind_method(D_METHOD("get_radius"), &CSGSphere3D::get_radius);
  1209. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGSphere3D::set_radial_segments);
  1210. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGSphere3D::get_radial_segments);
  1211. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CSGSphere3D::set_rings);
  1212. ClassDB::bind_method(D_METHOD("get_rings"), &CSGSphere3D::get_rings);
  1213. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGSphere3D::set_smooth_faces);
  1214. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGSphere3D::get_smooth_faces);
  1215. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGSphere3D::set_material);
  1216. ClassDB::bind_method(D_METHOD("get_material"), &CSGSphere3D::get_material);
  1217. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,suffix:m"), "set_radius", "get_radius");
  1218. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments");
  1219. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1"), "set_rings", "get_rings");
  1220. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1221. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1222. }
  1223. void CSGSphere3D::set_radius(const float p_radius) {
  1224. ERR_FAIL_COND(p_radius <= 0);
  1225. radius = p_radius;
  1226. _make_dirty();
  1227. update_gizmos();
  1228. }
  1229. float CSGSphere3D::get_radius() const {
  1230. return radius;
  1231. }
  1232. void CSGSphere3D::set_radial_segments(const int p_radial_segments) {
  1233. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  1234. _make_dirty();
  1235. update_gizmos();
  1236. }
  1237. int CSGSphere3D::get_radial_segments() const {
  1238. return radial_segments;
  1239. }
  1240. void CSGSphere3D::set_rings(const int p_rings) {
  1241. rings = p_rings > 1 ? p_rings : 1;
  1242. _make_dirty();
  1243. update_gizmos();
  1244. }
  1245. int CSGSphere3D::get_rings() const {
  1246. return rings;
  1247. }
  1248. void CSGSphere3D::set_smooth_faces(const bool p_smooth_faces) {
  1249. smooth_faces = p_smooth_faces;
  1250. _make_dirty();
  1251. }
  1252. bool CSGSphere3D::get_smooth_faces() const {
  1253. return smooth_faces;
  1254. }
  1255. void CSGSphere3D::set_material(const Ref<Material> &p_material) {
  1256. material = p_material;
  1257. _make_dirty();
  1258. }
  1259. Ref<Material> CSGSphere3D::get_material() const {
  1260. return material;
  1261. }
  1262. CSGSphere3D::CSGSphere3D() {
  1263. // defaults
  1264. radius = 0.5;
  1265. radial_segments = 12;
  1266. rings = 6;
  1267. smooth_faces = true;
  1268. }
  1269. ///////////////
  1270. CSGBrush *CSGBox3D::_build_brush() {
  1271. // set our bounding box
  1272. CSGBrush *new_brush = memnew(CSGBrush);
  1273. int face_count = 12; //it's a cube..
  1274. bool invert_val = get_flip_faces();
  1275. Ref<Material> base_material = get_material();
  1276. Vector<Vector3> faces;
  1277. Vector<Vector2> uvs;
  1278. Vector<bool> smooth;
  1279. Vector<Ref<Material>> materials;
  1280. Vector<bool> invert;
  1281. faces.resize(face_count * 3);
  1282. uvs.resize(face_count * 3);
  1283. smooth.resize(face_count);
  1284. materials.resize(face_count);
  1285. invert.resize(face_count);
  1286. {
  1287. Vector3 *facesw = faces.ptrw();
  1288. Vector2 *uvsw = uvs.ptrw();
  1289. bool *smoothw = smooth.ptrw();
  1290. Ref<Material> *materialsw = materials.ptrw();
  1291. bool *invertw = invert.ptrw();
  1292. int face = 0;
  1293. Vector3 vertex_mul = size / 2;
  1294. {
  1295. for (int i = 0; i < 6; i++) {
  1296. Vector3 face_points[4];
  1297. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  1298. for (int j = 0; j < 4; j++) {
  1299. float v[3];
  1300. v[0] = 1.0;
  1301. v[1] = 1 - 2 * ((j >> 1) & 1);
  1302. v[2] = v[1] * (1 - 2 * (j & 1));
  1303. for (int k = 0; k < 3; k++) {
  1304. if (i < 3) {
  1305. face_points[j][(i + k) % 3] = v[k];
  1306. } else {
  1307. face_points[3 - j][(i + k) % 3] = -v[k];
  1308. }
  1309. }
  1310. }
  1311. Vector2 u[4];
  1312. for (int j = 0; j < 4; j++) {
  1313. u[j] = Vector2(uv_points[j * 2 + 0], uv_points[j * 2 + 1]);
  1314. }
  1315. //face 1
  1316. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1317. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1318. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1319. uvsw[face * 3 + 0] = u[0];
  1320. uvsw[face * 3 + 1] = u[1];
  1321. uvsw[face * 3 + 2] = u[2];
  1322. smoothw[face] = false;
  1323. invertw[face] = invert_val;
  1324. materialsw[face] = base_material;
  1325. face++;
  1326. //face 2
  1327. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1328. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1329. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1330. uvsw[face * 3 + 0] = u[2];
  1331. uvsw[face * 3 + 1] = u[3];
  1332. uvsw[face * 3 + 2] = u[0];
  1333. smoothw[face] = false;
  1334. invertw[face] = invert_val;
  1335. materialsw[face] = base_material;
  1336. face++;
  1337. }
  1338. }
  1339. if (face != face_count) {
  1340. ERR_PRINT("Face mismatch bug! fix code");
  1341. }
  1342. }
  1343. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1344. return new_brush;
  1345. }
  1346. void CSGBox3D::_bind_methods() {
  1347. ClassDB::bind_method(D_METHOD("set_size", "size"), &CSGBox3D::set_size);
  1348. ClassDB::bind_method(D_METHOD("get_size"), &CSGBox3D::get_size);
  1349. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox3D::set_material);
  1350. ClassDB::bind_method(D_METHOD("get_material"), &CSGBox3D::get_material);
  1351. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1352. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1353. }
  1354. void CSGBox3D::set_size(const Vector3 &p_size) {
  1355. size = p_size;
  1356. _make_dirty();
  1357. update_gizmos();
  1358. }
  1359. Vector3 CSGBox3D::get_size() const {
  1360. return size;
  1361. }
  1362. #ifndef DISABLE_DEPRECATED
  1363. // Kept for compatibility from 3.x to 4.0.
  1364. bool CSGBox3D::_set(const StringName &p_name, const Variant &p_value) {
  1365. if (p_name == "width") {
  1366. size.x = p_value;
  1367. _make_dirty();
  1368. update_gizmos();
  1369. return true;
  1370. } else if (p_name == "height") {
  1371. size.y = p_value;
  1372. _make_dirty();
  1373. update_gizmos();
  1374. return true;
  1375. } else if (p_name == "depth") {
  1376. size.z = p_value;
  1377. _make_dirty();
  1378. update_gizmos();
  1379. return true;
  1380. } else {
  1381. return false;
  1382. }
  1383. }
  1384. #endif
  1385. void CSGBox3D::set_material(const Ref<Material> &p_material) {
  1386. material = p_material;
  1387. _make_dirty();
  1388. update_gizmos();
  1389. }
  1390. Ref<Material> CSGBox3D::get_material() const {
  1391. return material;
  1392. }
  1393. ///////////////
  1394. CSGBrush *CSGCylinder3D::_build_brush() {
  1395. // set our bounding box
  1396. CSGBrush *new_brush = memnew(CSGBrush);
  1397. int face_count = sides * (cone ? 1 : 2) + sides + (cone ? 0 : sides);
  1398. bool invert_val = get_flip_faces();
  1399. Ref<Material> base_material = get_material();
  1400. Vector<Vector3> faces;
  1401. Vector<Vector2> uvs;
  1402. Vector<bool> smooth;
  1403. Vector<Ref<Material>> materials;
  1404. Vector<bool> invert;
  1405. faces.resize(face_count * 3);
  1406. uvs.resize(face_count * 3);
  1407. smooth.resize(face_count);
  1408. materials.resize(face_count);
  1409. invert.resize(face_count);
  1410. {
  1411. Vector3 *facesw = faces.ptrw();
  1412. Vector2 *uvsw = uvs.ptrw();
  1413. bool *smoothw = smooth.ptrw();
  1414. Ref<Material> *materialsw = materials.ptrw();
  1415. bool *invertw = invert.ptrw();
  1416. int face = 0;
  1417. Vector3 vertex_mul(radius, height * 0.5, radius);
  1418. {
  1419. for (int i = 0; i < sides; i++) {
  1420. float inc = float(i) / sides;
  1421. float inc_n = float((i + 1)) / sides;
  1422. if (i == sides - 1) {
  1423. inc_n = 0;
  1424. }
  1425. float ang = inc * Math::TAU;
  1426. float ang_n = inc_n * Math::TAU;
  1427. Vector3 face_base(Math::cos(ang), 0, Math::sin(ang));
  1428. Vector3 face_base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
  1429. Vector3 face_points[4] = {
  1430. face_base + Vector3(0, -1, 0),
  1431. face_base_n + Vector3(0, -1, 0),
  1432. face_base_n * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1433. face_base * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1434. };
  1435. Vector2 u[4] = {
  1436. Vector2(inc, 0),
  1437. Vector2(inc_n, 0),
  1438. Vector2(inc_n, 1),
  1439. Vector2(inc, 1),
  1440. };
  1441. //side face 1
  1442. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1443. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1444. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1445. uvsw[face * 3 + 0] = u[0];
  1446. uvsw[face * 3 + 1] = u[1];
  1447. uvsw[face * 3 + 2] = u[2];
  1448. smoothw[face] = smooth_faces;
  1449. invertw[face] = invert_val;
  1450. materialsw[face] = base_material;
  1451. face++;
  1452. if (!cone) {
  1453. //side face 2
  1454. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1455. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1456. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1457. uvsw[face * 3 + 0] = u[2];
  1458. uvsw[face * 3 + 1] = u[3];
  1459. uvsw[face * 3 + 2] = u[0];
  1460. smoothw[face] = smooth_faces;
  1461. invertw[face] = invert_val;
  1462. materialsw[face] = base_material;
  1463. face++;
  1464. }
  1465. //bottom face 1
  1466. facesw[face * 3 + 0] = face_points[1] * vertex_mul;
  1467. facesw[face * 3 + 1] = face_points[0] * vertex_mul;
  1468. facesw[face * 3 + 2] = Vector3(0, -1, 0) * vertex_mul;
  1469. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1470. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1471. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1472. smoothw[face] = false;
  1473. invertw[face] = invert_val;
  1474. materialsw[face] = base_material;
  1475. face++;
  1476. if (!cone) {
  1477. //top face 1
  1478. facesw[face * 3 + 0] = face_points[3] * vertex_mul;
  1479. facesw[face * 3 + 1] = face_points[2] * vertex_mul;
  1480. facesw[face * 3 + 2] = Vector3(0, 1, 0) * vertex_mul;
  1481. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1482. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1483. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1484. smoothw[face] = false;
  1485. invertw[face] = invert_val;
  1486. materialsw[face] = base_material;
  1487. face++;
  1488. }
  1489. }
  1490. }
  1491. if (face != face_count) {
  1492. ERR_PRINT("Face mismatch bug! fix code");
  1493. }
  1494. }
  1495. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1496. return new_brush;
  1497. }
  1498. void CSGCylinder3D::_bind_methods() {
  1499. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCylinder3D::set_radius);
  1500. ClassDB::bind_method(D_METHOD("get_radius"), &CSGCylinder3D::get_radius);
  1501. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCylinder3D::set_height);
  1502. ClassDB::bind_method(D_METHOD("get_height"), &CSGCylinder3D::get_height);
  1503. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGCylinder3D::set_sides);
  1504. ClassDB::bind_method(D_METHOD("get_sides"), &CSGCylinder3D::get_sides);
  1505. ClassDB::bind_method(D_METHOD("set_cone", "cone"), &CSGCylinder3D::set_cone);
  1506. ClassDB::bind_method(D_METHOD("is_cone"), &CSGCylinder3D::is_cone);
  1507. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGCylinder3D::set_material);
  1508. ClassDB::bind_method(D_METHOD("get_material"), &CSGCylinder3D::get_material);
  1509. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGCylinder3D::set_smooth_faces);
  1510. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGCylinder3D::get_smooth_faces);
  1511. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_radius", "get_radius");
  1512. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_height", "get_height");
  1513. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1514. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cone"), "set_cone", "is_cone");
  1515. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1516. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1517. }
  1518. void CSGCylinder3D::set_radius(const float p_radius) {
  1519. radius = p_radius;
  1520. _make_dirty();
  1521. update_gizmos();
  1522. }
  1523. float CSGCylinder3D::get_radius() const {
  1524. return radius;
  1525. }
  1526. void CSGCylinder3D::set_height(const float p_height) {
  1527. height = p_height;
  1528. _make_dirty();
  1529. update_gizmos();
  1530. }
  1531. float CSGCylinder3D::get_height() const {
  1532. return height;
  1533. }
  1534. void CSGCylinder3D::set_sides(const int p_sides) {
  1535. ERR_FAIL_COND(p_sides < 3);
  1536. sides = p_sides;
  1537. _make_dirty();
  1538. update_gizmos();
  1539. }
  1540. int CSGCylinder3D::get_sides() const {
  1541. return sides;
  1542. }
  1543. void CSGCylinder3D::set_cone(const bool p_cone) {
  1544. cone = p_cone;
  1545. _make_dirty();
  1546. update_gizmos();
  1547. }
  1548. bool CSGCylinder3D::is_cone() const {
  1549. return cone;
  1550. }
  1551. void CSGCylinder3D::set_smooth_faces(const bool p_smooth_faces) {
  1552. smooth_faces = p_smooth_faces;
  1553. _make_dirty();
  1554. }
  1555. bool CSGCylinder3D::get_smooth_faces() const {
  1556. return smooth_faces;
  1557. }
  1558. void CSGCylinder3D::set_material(const Ref<Material> &p_material) {
  1559. material = p_material;
  1560. _make_dirty();
  1561. }
  1562. Ref<Material> CSGCylinder3D::get_material() const {
  1563. return material;
  1564. }
  1565. CSGCylinder3D::CSGCylinder3D() {
  1566. // defaults
  1567. radius = 0.5;
  1568. height = 2.0;
  1569. sides = 8;
  1570. cone = false;
  1571. smooth_faces = true;
  1572. }
  1573. ///////////////
  1574. CSGBrush *CSGTorus3D::_build_brush() {
  1575. // set our bounding box
  1576. float min_radius = inner_radius;
  1577. float max_radius = outer_radius;
  1578. if (min_radius == max_radius) {
  1579. return memnew(CSGBrush); //sorry, can't
  1580. }
  1581. if (min_radius > max_radius) {
  1582. SWAP(min_radius, max_radius);
  1583. }
  1584. float radius = (max_radius - min_radius) * 0.5;
  1585. CSGBrush *new_brush = memnew(CSGBrush);
  1586. int face_count = ring_sides * sides * 2;
  1587. bool invert_val = get_flip_faces();
  1588. Ref<Material> base_material = get_material();
  1589. Vector<Vector3> faces;
  1590. Vector<Vector2> uvs;
  1591. Vector<bool> smooth;
  1592. Vector<Ref<Material>> materials;
  1593. Vector<bool> invert;
  1594. faces.resize(face_count * 3);
  1595. uvs.resize(face_count * 3);
  1596. smooth.resize(face_count);
  1597. materials.resize(face_count);
  1598. invert.resize(face_count);
  1599. {
  1600. Vector3 *facesw = faces.ptrw();
  1601. Vector2 *uvsw = uvs.ptrw();
  1602. bool *smoothw = smooth.ptrw();
  1603. Ref<Material> *materialsw = materials.ptrw();
  1604. bool *invertw = invert.ptrw();
  1605. int face = 0;
  1606. {
  1607. for (int i = 0; i < sides; i++) {
  1608. float inci = float(i) / sides;
  1609. float inci_n = float((i + 1)) / sides;
  1610. if (i == sides - 1) {
  1611. inci_n = 0;
  1612. }
  1613. float angi = inci * Math::TAU;
  1614. float angi_n = inci_n * Math::TAU;
  1615. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1616. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1617. for (int j = 0; j < ring_sides; j++) {
  1618. float incj = float(j) / ring_sides;
  1619. float incj_n = float((j + 1)) / ring_sides;
  1620. if (j == ring_sides - 1) {
  1621. incj_n = 0;
  1622. }
  1623. float angj = incj * Math::TAU;
  1624. float angj_n = incj_n * Math::TAU;
  1625. Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
  1626. Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
  1627. Vector3 face_points[4] = {
  1628. Vector3(normali.x * normalj.x, normalj.y, normali.z * normalj.x),
  1629. Vector3(normali.x * normalj_n.x, normalj_n.y, normali.z * normalj_n.x),
  1630. Vector3(normali_n.x * normalj_n.x, normalj_n.y, normali_n.z * normalj_n.x),
  1631. Vector3(normali_n.x * normalj.x, normalj.y, normali_n.z * normalj.x)
  1632. };
  1633. Vector2 u[4] = {
  1634. Vector2(inci, incj),
  1635. Vector2(inci, incj_n),
  1636. Vector2(inci_n, incj_n),
  1637. Vector2(inci_n, incj),
  1638. };
  1639. // face 1
  1640. facesw[face * 3 + 0] = face_points[0];
  1641. facesw[face * 3 + 1] = face_points[2];
  1642. facesw[face * 3 + 2] = face_points[1];
  1643. uvsw[face * 3 + 0] = u[0];
  1644. uvsw[face * 3 + 1] = u[2];
  1645. uvsw[face * 3 + 2] = u[1];
  1646. smoothw[face] = smooth_faces;
  1647. invertw[face] = invert_val;
  1648. materialsw[face] = base_material;
  1649. face++;
  1650. //face 2
  1651. facesw[face * 3 + 0] = face_points[3];
  1652. facesw[face * 3 + 1] = face_points[2];
  1653. facesw[face * 3 + 2] = face_points[0];
  1654. uvsw[face * 3 + 0] = u[3];
  1655. uvsw[face * 3 + 1] = u[2];
  1656. uvsw[face * 3 + 2] = u[0];
  1657. smoothw[face] = smooth_faces;
  1658. invertw[face] = invert_val;
  1659. materialsw[face] = base_material;
  1660. face++;
  1661. }
  1662. }
  1663. }
  1664. if (face != face_count) {
  1665. ERR_PRINT("Face mismatch bug! fix code");
  1666. }
  1667. }
  1668. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1669. return new_brush;
  1670. }
  1671. void CSGTorus3D::_bind_methods() {
  1672. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &CSGTorus3D::set_inner_radius);
  1673. ClassDB::bind_method(D_METHOD("get_inner_radius"), &CSGTorus3D::get_inner_radius);
  1674. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &CSGTorus3D::set_outer_radius);
  1675. ClassDB::bind_method(D_METHOD("get_outer_radius"), &CSGTorus3D::get_outer_radius);
  1676. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGTorus3D::set_sides);
  1677. ClassDB::bind_method(D_METHOD("get_sides"), &CSGTorus3D::get_sides);
  1678. ClassDB::bind_method(D_METHOD("set_ring_sides", "sides"), &CSGTorus3D::set_ring_sides);
  1679. ClassDB::bind_method(D_METHOD("get_ring_sides"), &CSGTorus3D::get_ring_sides);
  1680. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGTorus3D::set_material);
  1681. ClassDB::bind_method(D_METHOD("get_material"), &CSGTorus3D::get_material);
  1682. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGTorus3D::set_smooth_faces);
  1683. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGTorus3D::get_smooth_faces);
  1684. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inner_radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_inner_radius", "get_inner_radius");
  1685. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "outer_radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_outer_radius", "get_outer_radius");
  1686. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1687. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_ring_sides", "get_ring_sides");
  1688. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1689. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1690. }
  1691. void CSGTorus3D::set_inner_radius(const float p_inner_radius) {
  1692. inner_radius = p_inner_radius;
  1693. _make_dirty();
  1694. update_gizmos();
  1695. }
  1696. float CSGTorus3D::get_inner_radius() const {
  1697. return inner_radius;
  1698. }
  1699. void CSGTorus3D::set_outer_radius(const float p_outer_radius) {
  1700. outer_radius = p_outer_radius;
  1701. _make_dirty();
  1702. update_gizmos();
  1703. }
  1704. float CSGTorus3D::get_outer_radius() const {
  1705. return outer_radius;
  1706. }
  1707. void CSGTorus3D::set_sides(const int p_sides) {
  1708. ERR_FAIL_COND(p_sides < 3);
  1709. sides = p_sides;
  1710. _make_dirty();
  1711. update_gizmos();
  1712. }
  1713. int CSGTorus3D::get_sides() const {
  1714. return sides;
  1715. }
  1716. void CSGTorus3D::set_ring_sides(const int p_ring_sides) {
  1717. ERR_FAIL_COND(p_ring_sides < 3);
  1718. ring_sides = p_ring_sides;
  1719. _make_dirty();
  1720. update_gizmos();
  1721. }
  1722. int CSGTorus3D::get_ring_sides() const {
  1723. return ring_sides;
  1724. }
  1725. void CSGTorus3D::set_smooth_faces(const bool p_smooth_faces) {
  1726. smooth_faces = p_smooth_faces;
  1727. _make_dirty();
  1728. }
  1729. bool CSGTorus3D::get_smooth_faces() const {
  1730. return smooth_faces;
  1731. }
  1732. void CSGTorus3D::set_material(const Ref<Material> &p_material) {
  1733. material = p_material;
  1734. _make_dirty();
  1735. }
  1736. Ref<Material> CSGTorus3D::get_material() const {
  1737. return material;
  1738. }
  1739. CSGTorus3D::CSGTorus3D() {
  1740. // defaults
  1741. inner_radius = 0.5;
  1742. outer_radius = 1.0;
  1743. sides = 8;
  1744. ring_sides = 6;
  1745. smooth_faces = true;
  1746. }
  1747. ///////////////
  1748. CSGBrush *CSGPolygon3D::_build_brush() {
  1749. CSGBrush *new_brush = memnew(CSGBrush);
  1750. if (polygon.size() < 3) {
  1751. return new_brush;
  1752. }
  1753. // Triangulate polygon shape.
  1754. Vector<Point2> shape_polygon = polygon;
  1755. if (Triangulate::get_area(shape_polygon) > 0) {
  1756. shape_polygon.reverse();
  1757. }
  1758. int shape_sides = shape_polygon.size();
  1759. Vector<int> shape_faces = Geometry2D::triangulate_polygon(shape_polygon);
  1760. ERR_FAIL_COND_V_MSG(shape_faces.size() < 3, new_brush, "Failed to triangulate CSGPolygon. Make sure the polygon doesn't have any intersecting edges.");
  1761. // Get polygon enclosing Rect2.
  1762. Rect2 shape_rect(shape_polygon[0], Vector2());
  1763. for (int i = 1; i < shape_sides; i++) {
  1764. shape_rect.expand_to(shape_polygon[i]);
  1765. }
  1766. // If MODE_PATH, check if curve has changed.
  1767. Ref<Curve3D> curve;
  1768. if (mode == MODE_PATH) {
  1769. Path3D *current_path = Object::cast_to<Path3D>(get_node_or_null(path_node));
  1770. if (path != current_path) {
  1771. if (path) {
  1772. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1773. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1774. path->set_update_callback(Callable());
  1775. }
  1776. path = current_path;
  1777. if (path) {
  1778. path->connect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1779. path->connect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1780. path->set_update_callback(callable_mp(this, &CSGPolygon3D::_path_changed));
  1781. }
  1782. }
  1783. if (!path) {
  1784. return new_brush;
  1785. }
  1786. curve = path->get_curve();
  1787. if (curve.is_null() || curve->get_point_count() < 2) {
  1788. return new_brush;
  1789. }
  1790. }
  1791. // Calculate the number extrusions, ends and faces.
  1792. int extrusions = 0;
  1793. int extrusion_face_count = shape_sides * 2;
  1794. int end_count = 0;
  1795. int shape_face_count = shape_faces.size() / 3;
  1796. real_t curve_length = 1.0;
  1797. switch (mode) {
  1798. case MODE_DEPTH:
  1799. extrusions = 1;
  1800. end_count = 2;
  1801. break;
  1802. case MODE_SPIN:
  1803. extrusions = spin_sides;
  1804. if (spin_degrees < 360) {
  1805. end_count = 2;
  1806. }
  1807. break;
  1808. case MODE_PATH: {
  1809. curve_length = curve->get_baked_length();
  1810. if (path_interval_type == PATH_INTERVAL_DISTANCE) {
  1811. extrusions = MAX(1, Math::ceil(curve_length / path_interval)) + 1;
  1812. } else {
  1813. extrusions = Math::ceil(1.0 * curve->get_point_count() / path_interval);
  1814. }
  1815. if (!path_joined) {
  1816. end_count = 2;
  1817. extrusions -= 1;
  1818. }
  1819. } break;
  1820. }
  1821. int face_count = extrusions * extrusion_face_count + end_count * shape_face_count;
  1822. // Initialize variables used to create the mesh.
  1823. Ref<Material> base_material = get_material();
  1824. Vector<Vector3> faces;
  1825. Vector<Vector2> uvs;
  1826. Vector<bool> smooth;
  1827. Vector<Ref<Material>> materials;
  1828. Vector<bool> invert;
  1829. faces.resize(face_count * 3);
  1830. uvs.resize(face_count * 3);
  1831. smooth.resize(face_count);
  1832. materials.resize(face_count);
  1833. invert.resize(face_count);
  1834. int faces_removed = 0;
  1835. {
  1836. Vector3 *facesw = faces.ptrw();
  1837. Vector2 *uvsw = uvs.ptrw();
  1838. bool *smoothw = smooth.ptrw();
  1839. Ref<Material> *materialsw = materials.ptrw();
  1840. bool *invertw = invert.ptrw();
  1841. int face = 0;
  1842. Transform3D base_xform;
  1843. Transform3D current_xform;
  1844. Transform3D previous_xform;
  1845. Transform3D previous_previous_xform;
  1846. double u_step = 1.0 / extrusions;
  1847. if (path_u_distance > 0.0) {
  1848. u_step *= curve_length / path_u_distance;
  1849. }
  1850. double v_step = 1.0 / shape_sides;
  1851. double spin_step = Math::deg_to_rad(spin_degrees / spin_sides);
  1852. double extrusion_step = 1.0 / extrusions;
  1853. if (mode == MODE_PATH) {
  1854. if (path_joined) {
  1855. extrusion_step = 1.0 / (extrusions - 1);
  1856. }
  1857. extrusion_step *= curve_length;
  1858. }
  1859. if (mode == MODE_PATH) {
  1860. if (!path_local) {
  1861. base_xform = path->get_global_transform();
  1862. }
  1863. Vector3 current_point;
  1864. Vector3 current_up = Vector3(0, 1, 0);
  1865. Vector3 direction;
  1866. switch (path_rotation) {
  1867. case PATH_ROTATION_POLYGON:
  1868. current_point = curve->sample_baked(0);
  1869. direction = Vector3(0, 0, -1);
  1870. break;
  1871. case PATH_ROTATION_PATH:
  1872. case PATH_ROTATION_PATH_FOLLOW:
  1873. if (!path_rotation_accurate) {
  1874. current_point = curve->sample_baked(0);
  1875. Vector3 next_point = curve->sample_baked(extrusion_step);
  1876. direction = next_point - current_point;
  1877. if (path_joined) {
  1878. Vector3 last_point = curve->sample_baked(curve->get_baked_length());
  1879. direction = next_point - last_point;
  1880. }
  1881. } else {
  1882. Transform3D current_sample_xform = curve->sample_baked_with_rotation(0);
  1883. current_point = current_sample_xform.get_origin();
  1884. direction = current_sample_xform.get_basis().xform(Vector3(0, 0, -1));
  1885. }
  1886. if (path_rotation == PATH_ROTATION_PATH_FOLLOW) {
  1887. current_up = curve->sample_baked_up_vector(0, true);
  1888. }
  1889. break;
  1890. }
  1891. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1892. current_xform = base_xform.translated_local(current_point) * facing;
  1893. }
  1894. // Create the mesh.
  1895. if (end_count > 0) {
  1896. // Add front end face.
  1897. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  1898. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  1899. // We need to reverse the rotation of the shape face vertices.
  1900. int index = shape_faces[face_idx * 3 + 2 - face_vertex_idx];
  1901. Point2 p = shape_polygon[index];
  1902. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  1903. // Use the left side of the bottom half of the y-inverted texture.
  1904. uv.x = uv.x / 2;
  1905. uv.y = 1 - (uv.y / 2);
  1906. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  1907. uvsw[face * 3 + face_vertex_idx] = uv;
  1908. }
  1909. smoothw[face] = false;
  1910. materialsw[face] = base_material;
  1911. invertw[face] = flip_faces;
  1912. face++;
  1913. }
  1914. }
  1915. real_t angle_simplify_dot = Math::cos(Math::deg_to_rad(path_simplify_angle));
  1916. Vector3 previous_simplify_dir = Vector3(0, 0, 0);
  1917. int faces_combined = 0;
  1918. // Add extrusion faces.
  1919. for (int x0 = 0; x0 < extrusions; x0++) {
  1920. previous_previous_xform = previous_xform;
  1921. previous_xform = current_xform;
  1922. switch (mode) {
  1923. case MODE_DEPTH: {
  1924. current_xform.translate_local(Vector3(0, 0, -depth));
  1925. } break;
  1926. case MODE_SPIN: {
  1927. if (end_count == 0 && x0 == extrusions - 1) {
  1928. current_xform = base_xform;
  1929. } else {
  1930. current_xform.rotate(Vector3(0, 1, 0), spin_step);
  1931. }
  1932. } break;
  1933. case MODE_PATH: {
  1934. double previous_offset = x0 * extrusion_step;
  1935. double current_offset = (x0 + 1) * extrusion_step;
  1936. if (path_joined && x0 == extrusions - 1) {
  1937. current_offset = 0;
  1938. }
  1939. Vector3 previous_point = curve->sample_baked(previous_offset);
  1940. Transform3D current_sample_xform = curve->sample_baked_with_rotation(current_offset);
  1941. Vector3 current_point = current_sample_xform.get_origin();
  1942. Vector3 current_up = Vector3(0, 1, 0);
  1943. Vector3 current_extrusion_dir = (current_point - previous_point).normalized();
  1944. Vector3 direction;
  1945. // If the angles are similar, remove the previous face and replace it with this one.
  1946. if (path_simplify_angle > 0.0 && x0 > 0 && previous_simplify_dir.dot(current_extrusion_dir) > angle_simplify_dot) {
  1947. faces_combined += 1;
  1948. previous_xform = previous_previous_xform;
  1949. face -= extrusion_face_count;
  1950. faces_removed += extrusion_face_count;
  1951. } else {
  1952. faces_combined = 0;
  1953. previous_simplify_dir = current_extrusion_dir;
  1954. }
  1955. switch (path_rotation) {
  1956. case PATH_ROTATION_POLYGON:
  1957. direction = Vector3(0, 0, -1);
  1958. break;
  1959. case PATH_ROTATION_PATH:
  1960. case PATH_ROTATION_PATH_FOLLOW:
  1961. if (!path_rotation_accurate) {
  1962. double next_offset = (x0 + 2) * extrusion_step;
  1963. if (x0 == extrusions - 1) {
  1964. next_offset = path_joined ? extrusion_step : current_offset;
  1965. }
  1966. Vector3 next_point = curve->sample_baked(next_offset);
  1967. direction = next_point - previous_point;
  1968. } else {
  1969. direction = current_sample_xform.get_basis().xform(Vector3(0, 0, -1));
  1970. }
  1971. if (path_rotation == PATH_ROTATION_PATH_FOLLOW) {
  1972. current_up = curve->sample_baked_up_vector(current_offset, true);
  1973. }
  1974. break;
  1975. }
  1976. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1977. current_xform = base_xform.translated_local(current_point) * facing;
  1978. } break;
  1979. }
  1980. double u0 = (x0 - faces_combined) * u_step;
  1981. double u1 = ((x0 + 1) * u_step);
  1982. if (mode == MODE_PATH && !path_continuous_u) {
  1983. u0 = 0.0;
  1984. u1 = 1.0;
  1985. }
  1986. for (int y0 = 0; y0 < shape_sides; y0++) {
  1987. int y1 = (y0 + 1) % shape_sides;
  1988. // Use the top half of the texture.
  1989. double v0 = (y0 * v_step) / 2;
  1990. double v1 = ((y0 + 1) * v_step) / 2;
  1991. Vector3 v[4] = {
  1992. previous_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1993. current_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1994. current_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1995. previous_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1996. };
  1997. Vector2 u[4] = {
  1998. Vector2(u0, v0),
  1999. Vector2(u1, v0),
  2000. Vector2(u1, v1),
  2001. Vector2(u0, v1),
  2002. };
  2003. // Face 1
  2004. facesw[face * 3 + 0] = v[0];
  2005. facesw[face * 3 + 1] = v[1];
  2006. facesw[face * 3 + 2] = v[2];
  2007. uvsw[face * 3 + 0] = u[0];
  2008. uvsw[face * 3 + 1] = u[1];
  2009. uvsw[face * 3 + 2] = u[2];
  2010. smoothw[face] = smooth_faces;
  2011. invertw[face] = flip_faces;
  2012. materialsw[face] = base_material;
  2013. face++;
  2014. // Face 2
  2015. facesw[face * 3 + 0] = v[2];
  2016. facesw[face * 3 + 1] = v[3];
  2017. facesw[face * 3 + 2] = v[0];
  2018. uvsw[face * 3 + 0] = u[2];
  2019. uvsw[face * 3 + 1] = u[3];
  2020. uvsw[face * 3 + 2] = u[0];
  2021. smoothw[face] = smooth_faces;
  2022. invertw[face] = flip_faces;
  2023. materialsw[face] = base_material;
  2024. face++;
  2025. }
  2026. }
  2027. if (end_count > 1) {
  2028. // Add back end face.
  2029. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  2030. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  2031. int index = shape_faces[face_idx * 3 + face_vertex_idx];
  2032. Point2 p = shape_polygon[index];
  2033. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  2034. // Use the x-inverted ride side of the bottom half of the y-inverted texture.
  2035. uv.x = 1 - uv.x / 2;
  2036. uv.y = 1 - (uv.y / 2);
  2037. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  2038. uvsw[face * 3 + face_vertex_idx] = uv;
  2039. }
  2040. smoothw[face] = false;
  2041. materialsw[face] = base_material;
  2042. invertw[face] = flip_faces;
  2043. face++;
  2044. }
  2045. }
  2046. face_count -= faces_removed;
  2047. ERR_FAIL_COND_V_MSG(face != face_count, new_brush, "Bug: Failed to create the CSGPolygon mesh correctly.");
  2048. }
  2049. if (faces_removed > 0) {
  2050. faces.resize(face_count * 3);
  2051. uvs.resize(face_count * 3);
  2052. smooth.resize(face_count);
  2053. materials.resize(face_count);
  2054. invert.resize(face_count);
  2055. }
  2056. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  2057. return new_brush;
  2058. }
  2059. void CSGPolygon3D::_notification(int p_what) {
  2060. if (p_what == NOTIFICATION_EXIT_TREE) {
  2061. if (path) {
  2062. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  2063. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  2064. path = nullptr;
  2065. }
  2066. }
  2067. }
  2068. void CSGPolygon3D::_validate_property(PropertyInfo &p_property) const {
  2069. if (p_property.name.begins_with("spin") && mode != MODE_SPIN) {
  2070. p_property.usage = PROPERTY_USAGE_NONE;
  2071. }
  2072. if (p_property.name.begins_with("path") && mode != MODE_PATH) {
  2073. p_property.usage = PROPERTY_USAGE_NONE;
  2074. }
  2075. if (p_property.name == "depth" && mode != MODE_DEPTH) {
  2076. p_property.usage = PROPERTY_USAGE_NONE;
  2077. }
  2078. }
  2079. void CSGPolygon3D::_path_changed() {
  2080. _make_dirty();
  2081. update_gizmos();
  2082. }
  2083. void CSGPolygon3D::_path_exited() {
  2084. path = nullptr;
  2085. }
  2086. void CSGPolygon3D::_bind_methods() {
  2087. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CSGPolygon3D::set_polygon);
  2088. ClassDB::bind_method(D_METHOD("get_polygon"), &CSGPolygon3D::get_polygon);
  2089. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSGPolygon3D::set_mode);
  2090. ClassDB::bind_method(D_METHOD("get_mode"), &CSGPolygon3D::get_mode);
  2091. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGPolygon3D::set_depth);
  2092. ClassDB::bind_method(D_METHOD("get_depth"), &CSGPolygon3D::get_depth);
  2093. ClassDB::bind_method(D_METHOD("set_spin_degrees", "degrees"), &CSGPolygon3D::set_spin_degrees);
  2094. ClassDB::bind_method(D_METHOD("get_spin_degrees"), &CSGPolygon3D::get_spin_degrees);
  2095. ClassDB::bind_method(D_METHOD("set_spin_sides", "spin_sides"), &CSGPolygon3D::set_spin_sides);
  2096. ClassDB::bind_method(D_METHOD("get_spin_sides"), &CSGPolygon3D::get_spin_sides);
  2097. ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon3D::set_path_node);
  2098. ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon3D::get_path_node);
  2099. ClassDB::bind_method(D_METHOD("set_path_interval_type", "interval_type"), &CSGPolygon3D::set_path_interval_type);
  2100. ClassDB::bind_method(D_METHOD("get_path_interval_type"), &CSGPolygon3D::get_path_interval_type);
  2101. ClassDB::bind_method(D_METHOD("set_path_interval", "interval"), &CSGPolygon3D::set_path_interval);
  2102. ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon3D::get_path_interval);
  2103. ClassDB::bind_method(D_METHOD("set_path_simplify_angle", "degrees"), &CSGPolygon3D::set_path_simplify_angle);
  2104. ClassDB::bind_method(D_METHOD("get_path_simplify_angle"), &CSGPolygon3D::get_path_simplify_angle);
  2105. ClassDB::bind_method(D_METHOD("set_path_rotation", "path_rotation"), &CSGPolygon3D::set_path_rotation);
  2106. ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon3D::get_path_rotation);
  2107. ClassDB::bind_method(D_METHOD("set_path_rotation_accurate", "enable"), &CSGPolygon3D::set_path_rotation_accurate);
  2108. ClassDB::bind_method(D_METHOD("get_path_rotation_accurate"), &CSGPolygon3D::get_path_rotation_accurate);
  2109. ClassDB::bind_method(D_METHOD("set_path_local", "enable"), &CSGPolygon3D::set_path_local);
  2110. ClassDB::bind_method(D_METHOD("is_path_local"), &CSGPolygon3D::is_path_local);
  2111. ClassDB::bind_method(D_METHOD("set_path_continuous_u", "enable"), &CSGPolygon3D::set_path_continuous_u);
  2112. ClassDB::bind_method(D_METHOD("is_path_continuous_u"), &CSGPolygon3D::is_path_continuous_u);
  2113. ClassDB::bind_method(D_METHOD("set_path_u_distance", "distance"), &CSGPolygon3D::set_path_u_distance);
  2114. ClassDB::bind_method(D_METHOD("get_path_u_distance"), &CSGPolygon3D::get_path_u_distance);
  2115. ClassDB::bind_method(D_METHOD("set_path_joined", "enable"), &CSGPolygon3D::set_path_joined);
  2116. ClassDB::bind_method(D_METHOD("is_path_joined"), &CSGPolygon3D::is_path_joined);
  2117. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGPolygon3D::set_material);
  2118. ClassDB::bind_method(D_METHOD("get_material"), &CSGPolygon3D::get_material);
  2119. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGPolygon3D::set_smooth_faces);
  2120. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGPolygon3D::get_smooth_faces);
  2121. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CSGPolygon3D::_is_editable_3d_polygon);
  2122. ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &CSGPolygon3D::_has_editable_3d_polygon_no_depth);
  2123. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  2124. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Depth,Spin,Path"), "set_mode", "get_mode");
  2125. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.01,100.0,0.01,or_greater,exp,suffix:m"), "set_depth", "get_depth");
  2126. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
  2127. ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
  2128. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path3D"), "set_path_node", "get_path_node");
  2129. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_interval_type", PROPERTY_HINT_ENUM, "Distance,Subdivide"), "set_path_interval_type", "get_path_interval_type");
  2130. 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");
  2131. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_simplify_angle", PROPERTY_HINT_RANGE, "0.0,180.0,0.1"), "set_path_simplify_angle", "get_path_simplify_angle");
  2132. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
  2133. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_rotation_accurate"), "set_path_rotation_accurate", "get_path_rotation_accurate");
  2134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
  2135. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
  2136. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_u_distance", PROPERTY_HINT_RANGE, "0.0,10.0,0.01,or_greater,suffix:m"), "set_path_u_distance", "get_path_u_distance");
  2137. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_joined"), "set_path_joined", "is_path_joined");
  2138. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  2139. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  2140. BIND_ENUM_CONSTANT(MODE_DEPTH);
  2141. BIND_ENUM_CONSTANT(MODE_SPIN);
  2142. BIND_ENUM_CONSTANT(MODE_PATH);
  2143. BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
  2144. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
  2145. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
  2146. BIND_ENUM_CONSTANT(PATH_INTERVAL_DISTANCE);
  2147. BIND_ENUM_CONSTANT(PATH_INTERVAL_SUBDIVIDE);
  2148. }
  2149. void CSGPolygon3D::set_polygon(const Vector<Vector2> &p_polygon) {
  2150. polygon = p_polygon;
  2151. _make_dirty();
  2152. update_gizmos();
  2153. }
  2154. Vector<Vector2> CSGPolygon3D::get_polygon() const {
  2155. return polygon;
  2156. }
  2157. void CSGPolygon3D::set_mode(Mode p_mode) {
  2158. mode = p_mode;
  2159. _make_dirty();
  2160. update_gizmos();
  2161. notify_property_list_changed();
  2162. }
  2163. CSGPolygon3D::Mode CSGPolygon3D::get_mode() const {
  2164. return mode;
  2165. }
  2166. void CSGPolygon3D::set_depth(const float p_depth) {
  2167. ERR_FAIL_COND(p_depth < 0.001);
  2168. depth = p_depth;
  2169. _make_dirty();
  2170. update_gizmos();
  2171. }
  2172. float CSGPolygon3D::get_depth() const {
  2173. return depth;
  2174. }
  2175. void CSGPolygon3D::set_path_continuous_u(bool p_enable) {
  2176. path_continuous_u = p_enable;
  2177. _make_dirty();
  2178. }
  2179. bool CSGPolygon3D::is_path_continuous_u() const {
  2180. return path_continuous_u;
  2181. }
  2182. void CSGPolygon3D::set_path_u_distance(real_t p_path_u_distance) {
  2183. path_u_distance = p_path_u_distance;
  2184. _make_dirty();
  2185. update_gizmos();
  2186. }
  2187. real_t CSGPolygon3D::get_path_u_distance() const {
  2188. return path_u_distance;
  2189. }
  2190. void CSGPolygon3D::set_spin_degrees(const float p_spin_degrees) {
  2191. ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
  2192. spin_degrees = p_spin_degrees;
  2193. _make_dirty();
  2194. update_gizmos();
  2195. }
  2196. float CSGPolygon3D::get_spin_degrees() const {
  2197. return spin_degrees;
  2198. }
  2199. void CSGPolygon3D::set_spin_sides(int p_spin_sides) {
  2200. ERR_FAIL_COND(p_spin_sides < 3);
  2201. spin_sides = p_spin_sides;
  2202. _make_dirty();
  2203. update_gizmos();
  2204. }
  2205. int CSGPolygon3D::get_spin_sides() const {
  2206. return spin_sides;
  2207. }
  2208. void CSGPolygon3D::set_path_node(const NodePath &p_path) {
  2209. path_node = p_path;
  2210. _make_dirty();
  2211. update_gizmos();
  2212. }
  2213. NodePath CSGPolygon3D::get_path_node() const {
  2214. return path_node;
  2215. }
  2216. void CSGPolygon3D::set_path_interval_type(PathIntervalType p_interval_type) {
  2217. path_interval_type = p_interval_type;
  2218. _make_dirty();
  2219. update_gizmos();
  2220. }
  2221. CSGPolygon3D::PathIntervalType CSGPolygon3D::get_path_interval_type() const {
  2222. return path_interval_type;
  2223. }
  2224. void CSGPolygon3D::set_path_interval(float p_interval) {
  2225. path_interval = p_interval;
  2226. _make_dirty();
  2227. update_gizmos();
  2228. }
  2229. float CSGPolygon3D::get_path_interval() const {
  2230. return path_interval;
  2231. }
  2232. void CSGPolygon3D::set_path_simplify_angle(float p_angle) {
  2233. path_simplify_angle = p_angle;
  2234. _make_dirty();
  2235. update_gizmos();
  2236. }
  2237. float CSGPolygon3D::get_path_simplify_angle() const {
  2238. return path_simplify_angle;
  2239. }
  2240. void CSGPolygon3D::set_path_rotation(PathRotation p_rotation) {
  2241. path_rotation = p_rotation;
  2242. _make_dirty();
  2243. update_gizmos();
  2244. }
  2245. CSGPolygon3D::PathRotation CSGPolygon3D::get_path_rotation() const {
  2246. return path_rotation;
  2247. }
  2248. void CSGPolygon3D::set_path_rotation_accurate(bool p_enabled) {
  2249. path_rotation_accurate = p_enabled;
  2250. _make_dirty();
  2251. update_gizmos();
  2252. }
  2253. bool CSGPolygon3D::get_path_rotation_accurate() const {
  2254. return path_rotation_accurate;
  2255. }
  2256. void CSGPolygon3D::set_path_local(bool p_enable) {
  2257. path_local = p_enable;
  2258. _make_dirty();
  2259. update_gizmos();
  2260. }
  2261. bool CSGPolygon3D::is_path_local() const {
  2262. return path_local;
  2263. }
  2264. void CSGPolygon3D::set_path_joined(bool p_enable) {
  2265. path_joined = p_enable;
  2266. _make_dirty();
  2267. update_gizmos();
  2268. }
  2269. bool CSGPolygon3D::is_path_joined() const {
  2270. return path_joined;
  2271. }
  2272. void CSGPolygon3D::set_smooth_faces(const bool p_smooth_faces) {
  2273. smooth_faces = p_smooth_faces;
  2274. _make_dirty();
  2275. }
  2276. bool CSGPolygon3D::get_smooth_faces() const {
  2277. return smooth_faces;
  2278. }
  2279. void CSGPolygon3D::set_material(const Ref<Material> &p_material) {
  2280. material = p_material;
  2281. _make_dirty();
  2282. }
  2283. Ref<Material> CSGPolygon3D::get_material() const {
  2284. return material;
  2285. }
  2286. bool CSGPolygon3D::_is_editable_3d_polygon() const {
  2287. return true;
  2288. }
  2289. bool CSGPolygon3D::_has_editable_3d_polygon_no_depth() const {
  2290. return true;
  2291. }
  2292. CSGPolygon3D::CSGPolygon3D() {
  2293. // defaults
  2294. mode = MODE_DEPTH;
  2295. polygon.push_back(Vector2(0, 0));
  2296. polygon.push_back(Vector2(0, 1));
  2297. polygon.push_back(Vector2(1, 1));
  2298. polygon.push_back(Vector2(1, 0));
  2299. depth = 1.0;
  2300. spin_degrees = 360;
  2301. spin_sides = 8;
  2302. smooth_faces = false;
  2303. path_interval_type = PATH_INTERVAL_DISTANCE;
  2304. path_interval = 1.0;
  2305. path_simplify_angle = 0.0;
  2306. path_rotation = PATH_ROTATION_PATH_FOLLOW;
  2307. path_rotation_accurate = false;
  2308. path_local = false;
  2309. path_continuous_u = true;
  2310. path_u_distance = 1.0;
  2311. path_joined = false;
  2312. path = nullptr;
  2313. }