csg_shape.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826
  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. bool is_collision_prefixed = p_property.name.begins_with("collision_");
  812. if ((is_collision_prefixed || p_property.name.begins_with("use_collision")) && is_inside_tree() && !is_root_shape()) {
  813. //hide collision if not root
  814. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  815. } else if (is_collision_prefixed && !bool(get("use_collision"))) {
  816. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  817. }
  818. }
  819. Array CSGShape3D::get_meshes() const {
  820. if (root_mesh.is_valid()) {
  821. Array arr;
  822. arr.resize(2);
  823. arr[0] = Transform3D();
  824. arr[1] = root_mesh;
  825. return arr;
  826. }
  827. return Array();
  828. }
  829. PackedStringArray CSGShape3D::get_configuration_warnings() const {
  830. PackedStringArray warnings = Node::get_configuration_warnings();
  831. const CSGShape3D *current_shape = this;
  832. while (current_shape) {
  833. if (!current_shape->brush || current_shape->brush->faces.is_empty()) {
  834. 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."));
  835. break;
  836. }
  837. current_shape = current_shape->parent_shape;
  838. }
  839. return warnings;
  840. }
  841. Ref<TriangleMesh> CSGShape3D::generate_triangle_mesh() const {
  842. if (root_mesh.is_valid()) {
  843. return root_mesh->generate_triangle_mesh();
  844. }
  845. return Ref<TriangleMesh>();
  846. }
  847. void CSGShape3D::_bind_methods() {
  848. ClassDB::bind_method(D_METHOD("is_root_shape"), &CSGShape3D::is_root_shape);
  849. ClassDB::bind_method(D_METHOD("set_operation", "operation"), &CSGShape3D::set_operation);
  850. ClassDB::bind_method(D_METHOD("get_operation"), &CSGShape3D::get_operation);
  851. #ifndef DISABLE_DEPRECATED
  852. ClassDB::bind_method(D_METHOD("_update_shape"), &CSGShape3D::update_shape);
  853. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &CSGShape3D::set_snap);
  854. ClassDB::bind_method(D_METHOD("get_snap"), &CSGShape3D::get_snap);
  855. #endif // DISABLE_DEPRECATED
  856. #ifndef PHYSICS_3D_DISABLED
  857. ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape3D::set_use_collision);
  858. ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape3D::is_using_collision);
  859. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CSGShape3D::set_collision_layer);
  860. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CSGShape3D::get_collision_layer);
  861. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CSGShape3D::set_collision_mask);
  862. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CSGShape3D::get_collision_mask);
  863. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CSGShape3D::set_collision_mask_value);
  864. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CSGShape3D::get_collision_mask_value);
  865. ClassDB::bind_method(D_METHOD("_get_root_collision_instance"), &CSGShape3D::_get_root_collision_instance);
  866. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CSGShape3D::set_collision_layer_value);
  867. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CSGShape3D::get_collision_layer_value);
  868. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CSGShape3D::set_collision_priority);
  869. ClassDB::bind_method(D_METHOD("get_collision_priority"), &CSGShape3D::get_collision_priority);
  870. ClassDB::bind_method(D_METHOD("bake_collision_shape"), &CSGShape3D::bake_collision_shape);
  871. #endif // PHYSICS_3D_DISABLED
  872. ClassDB::bind_method(D_METHOD("set_calculate_tangents", "enabled"), &CSGShape3D::set_calculate_tangents);
  873. ClassDB::bind_method(D_METHOD("is_calculating_tangents"), &CSGShape3D::is_calculating_tangents);
  874. ClassDB::bind_method(D_METHOD("get_meshes"), &CSGShape3D::get_meshes);
  875. ClassDB::bind_method(D_METHOD("bake_static_mesh"), &CSGShape3D::bake_static_mesh);
  876. ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "Union,Intersection,Subtraction"), "set_operation", "get_operation");
  877. #ifndef DISABLE_DEPRECATED
  878. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_RANGE, "0.000001,1,0.000001,suffix:m", PROPERTY_USAGE_NONE), "set_snap", "get_snap");
  879. #endif // DISABLE_DEPRECATED
  880. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "calculate_tangents"), "set_calculate_tangents", "is_calculating_tangents");
  881. #ifndef PHYSICS_3D_DISABLED
  882. ADD_GROUP("Collision", "collision_");
  883. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_collision"), "set_use_collision", "is_using_collision");
  884. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  885. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  886. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  887. #endif // PHYSICS_3D_DISABLED
  888. BIND_ENUM_CONSTANT(OPERATION_UNION);
  889. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  890. BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
  891. }
  892. CSGShape3D::CSGShape3D() {
  893. set_notify_local_transform(true);
  894. }
  895. CSGShape3D::~CSGShape3D() {
  896. if (brush) {
  897. memdelete(brush);
  898. brush = nullptr;
  899. }
  900. }
  901. //////////////////////////////////
  902. CSGBrush *CSGCombiner3D::_build_brush() {
  903. return memnew(CSGBrush); //does not build anything
  904. }
  905. CSGCombiner3D::CSGCombiner3D() {
  906. }
  907. /////////////////////
  908. 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) {
  909. CSGBrush *new_brush = memnew(CSGBrush);
  910. Vector<bool> invert;
  911. invert.resize(p_vertices.size() / 3);
  912. {
  913. int ic = invert.size();
  914. bool *w = invert.ptrw();
  915. for (int i = 0; i < ic; i++) {
  916. w[i] = flip_faces;
  917. }
  918. }
  919. new_brush->build_from_faces(p_vertices, p_uv, p_smooth, p_materials, invert);
  920. return new_brush;
  921. }
  922. void CSGPrimitive3D::_bind_methods() {
  923. ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &CSGPrimitive3D::set_flip_faces);
  924. ClassDB::bind_method(D_METHOD("get_flip_faces"), &CSGPrimitive3D::get_flip_faces);
  925. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
  926. }
  927. void CSGPrimitive3D::set_flip_faces(bool p_invert) {
  928. if (flip_faces == p_invert) {
  929. return;
  930. }
  931. flip_faces = p_invert;
  932. _make_dirty();
  933. }
  934. bool CSGPrimitive3D::get_flip_faces() {
  935. return flip_faces;
  936. }
  937. CSGPrimitive3D::CSGPrimitive3D() {
  938. flip_faces = false;
  939. }
  940. /////////////////////
  941. CSGBrush *CSGMesh3D::_build_brush() {
  942. if (mesh.is_null()) {
  943. return memnew(CSGBrush);
  944. }
  945. Vector<Vector3> vertices;
  946. Vector<bool> smooth;
  947. Vector<Ref<Material>> materials;
  948. Vector<Vector2> uvs;
  949. Ref<Material> base_material = get_material();
  950. for (int i = 0; i < mesh->get_surface_count(); i++) {
  951. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  952. continue;
  953. }
  954. Array arrays = mesh->surface_get_arrays(i);
  955. if (arrays.is_empty()) {
  956. _make_dirty();
  957. ERR_FAIL_COND_V(arrays.is_empty(), memnew(CSGBrush));
  958. }
  959. Vector<Vector3> avertices = arrays[Mesh::ARRAY_VERTEX];
  960. if (avertices.is_empty()) {
  961. continue;
  962. }
  963. const Vector3 *vr = avertices.ptr();
  964. Vector<Vector3> anormals = arrays[Mesh::ARRAY_NORMAL];
  965. const Vector3 *nr = nullptr;
  966. if (anormals.size()) {
  967. nr = anormals.ptr();
  968. }
  969. Vector<Vector2> auvs = arrays[Mesh::ARRAY_TEX_UV];
  970. const Vector2 *uvr = nullptr;
  971. if (auvs.size()) {
  972. uvr = auvs.ptr();
  973. }
  974. Ref<Material> mat;
  975. if (base_material.is_valid()) {
  976. mat = base_material;
  977. } else {
  978. mat = mesh->surface_get_material(i);
  979. }
  980. Vector<int> aindices = arrays[Mesh::ARRAY_INDEX];
  981. if (aindices.size()) {
  982. int as = vertices.size();
  983. int is = aindices.size();
  984. vertices.resize(as + is);
  985. smooth.resize((as + is) / 3);
  986. materials.resize((as + is) / 3);
  987. uvs.resize(as + is);
  988. Vector3 *vw = vertices.ptrw();
  989. bool *sw = smooth.ptrw();
  990. Vector2 *uvw = uvs.ptrw();
  991. Ref<Material> *mw = materials.ptrw();
  992. const int *ir = aindices.ptr();
  993. for (int j = 0; j < is; j += 3) {
  994. Vector3 vertex[3];
  995. Vector3 normal[3];
  996. Vector2 uv[3];
  997. for (int k = 0; k < 3; k++) {
  998. int idx = ir[j + k];
  999. vertex[k] = vr[idx];
  1000. if (nr) {
  1001. normal[k] = nr[idx];
  1002. }
  1003. if (uvr) {
  1004. uv[k] = uvr[idx];
  1005. }
  1006. }
  1007. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  1008. vw[as + j + 0] = vertex[0];
  1009. vw[as + j + 1] = vertex[1];
  1010. vw[as + j + 2] = vertex[2];
  1011. uvw[as + j + 0] = uv[0];
  1012. uvw[as + j + 1] = uv[1];
  1013. uvw[as + j + 2] = uv[2];
  1014. sw[(as + j) / 3] = !flat;
  1015. mw[(as + j) / 3] = mat;
  1016. }
  1017. } else {
  1018. int as = vertices.size();
  1019. int is = avertices.size();
  1020. vertices.resize(as + is);
  1021. smooth.resize((as + is) / 3);
  1022. uvs.resize(as + is);
  1023. materials.resize((as + is) / 3);
  1024. Vector3 *vw = vertices.ptrw();
  1025. bool *sw = smooth.ptrw();
  1026. Vector2 *uvw = uvs.ptrw();
  1027. Ref<Material> *mw = materials.ptrw();
  1028. for (int j = 0; j < is; j += 3) {
  1029. Vector3 vertex[3];
  1030. Vector3 normal[3];
  1031. Vector2 uv[3];
  1032. for (int k = 0; k < 3; k++) {
  1033. vertex[k] = vr[j + k];
  1034. if (nr) {
  1035. normal[k] = nr[j + k];
  1036. }
  1037. if (uvr) {
  1038. uv[k] = uvr[j + k];
  1039. }
  1040. }
  1041. bool flat = normal[0].is_equal_approx(normal[1]) && normal[0].is_equal_approx(normal[2]);
  1042. vw[as + j + 0] = vertex[0];
  1043. vw[as + j + 1] = vertex[1];
  1044. vw[as + j + 2] = vertex[2];
  1045. uvw[as + j + 0] = uv[0];
  1046. uvw[as + j + 1] = uv[1];
  1047. uvw[as + j + 2] = uv[2];
  1048. sw[(as + j) / 3] = !flat;
  1049. mw[(as + j) / 3] = mat;
  1050. }
  1051. }
  1052. }
  1053. if (vertices.is_empty()) {
  1054. return memnew(CSGBrush);
  1055. }
  1056. return _create_brush_from_arrays(vertices, uvs, smooth, materials);
  1057. }
  1058. void CSGMesh3D::_mesh_changed() {
  1059. _make_dirty();
  1060. callable_mp((Node3D *)this, &Node3D::update_gizmos).call_deferred();
  1061. }
  1062. void CSGMesh3D::set_material(const Ref<Material> &p_material) {
  1063. if (material == p_material) {
  1064. return;
  1065. }
  1066. material = p_material;
  1067. _make_dirty();
  1068. }
  1069. Ref<Material> CSGMesh3D::get_material() const {
  1070. return material;
  1071. }
  1072. void CSGMesh3D::_bind_methods() {
  1073. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CSGMesh3D::set_mesh);
  1074. ClassDB::bind_method(D_METHOD("get_mesh"), &CSGMesh3D::get_mesh);
  1075. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGMesh3D::set_material);
  1076. ClassDB::bind_method(D_METHOD("get_material"), &CSGMesh3D::get_material);
  1077. // Hide PrimitiveMeshes that are always non-manifold and therefore can't be used as CSG meshes.
  1078. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh,-PlaneMesh,-PointMesh,-QuadMesh,-RibbonTrailMesh"), "set_mesh", "get_mesh");
  1079. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1080. }
  1081. void CSGMesh3D::set_mesh(const Ref<Mesh> &p_mesh) {
  1082. if (mesh == p_mesh) {
  1083. return;
  1084. }
  1085. if (mesh.is_valid()) {
  1086. mesh->disconnect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  1087. }
  1088. mesh = p_mesh;
  1089. if (mesh.is_valid()) {
  1090. mesh->connect_changed(callable_mp(this, &CSGMesh3D::_mesh_changed));
  1091. }
  1092. _mesh_changed();
  1093. }
  1094. Ref<Mesh> CSGMesh3D::get_mesh() {
  1095. return mesh;
  1096. }
  1097. ////////////////////////////////
  1098. CSGBrush *CSGSphere3D::_build_brush() {
  1099. // set our bounding box
  1100. CSGBrush *new_brush = memnew(CSGBrush);
  1101. int face_count = rings * radial_segments * 2 - radial_segments * 2;
  1102. bool invert_val = get_flip_faces();
  1103. Ref<Material> base_material = get_material();
  1104. Vector<Vector3> faces;
  1105. Vector<Vector2> uvs;
  1106. Vector<bool> smooth;
  1107. Vector<Ref<Material>> materials;
  1108. Vector<bool> invert;
  1109. faces.resize(face_count * 3);
  1110. uvs.resize(face_count * 3);
  1111. smooth.resize(face_count);
  1112. materials.resize(face_count);
  1113. invert.resize(face_count);
  1114. {
  1115. Vector3 *facesw = faces.ptrw();
  1116. Vector2 *uvsw = uvs.ptrw();
  1117. bool *smoothw = smooth.ptrw();
  1118. Ref<Material> *materialsw = materials.ptrw();
  1119. bool *invertw = invert.ptrw();
  1120. // We want to follow an order that's convenient for UVs.
  1121. // For latitude step we start at the top and move down like in an image.
  1122. const double latitude_step = -Math::PI / rings;
  1123. const double longitude_step = Math::TAU / radial_segments;
  1124. int face = 0;
  1125. for (int i = 0; i < rings; i++) {
  1126. double cos0 = 0;
  1127. double sin0 = 1;
  1128. if (i > 0) {
  1129. double latitude0 = latitude_step * i + Math::TAU / 4;
  1130. cos0 = Math::cos(latitude0);
  1131. sin0 = Math::sin(latitude0);
  1132. }
  1133. double v0 = double(i) / rings;
  1134. double cos1 = 0;
  1135. double sin1 = -1;
  1136. if (i < rings - 1) {
  1137. double latitude1 = latitude_step * (i + 1) + Math::TAU / 4;
  1138. cos1 = Math::cos(latitude1);
  1139. sin1 = Math::sin(latitude1);
  1140. }
  1141. double v1 = double(i + 1) / rings;
  1142. for (int j = 0; j < radial_segments; j++) {
  1143. double longitude0 = longitude_step * j;
  1144. // We give sin to X and cos to Z on purpose.
  1145. // This allows UVs to be CCW on +X so it maps to images well.
  1146. double x0 = Math::sin(longitude0);
  1147. double z0 = Math::cos(longitude0);
  1148. double u0 = double(j) / radial_segments;
  1149. double longitude1 = longitude_step * (j + 1);
  1150. if (j == radial_segments - 1) {
  1151. longitude1 = 0;
  1152. }
  1153. double x1 = Math::sin(longitude1);
  1154. double z1 = Math::cos(longitude1);
  1155. double u1 = double(j + 1) / radial_segments;
  1156. Vector3 v[4] = {
  1157. Vector3(x0 * cos0, sin0, z0 * cos0) * radius,
  1158. Vector3(x1 * cos0, sin0, z1 * cos0) * radius,
  1159. Vector3(x1 * cos1, sin1, z1 * cos1) * radius,
  1160. Vector3(x0 * cos1, sin1, z0 * cos1) * radius,
  1161. };
  1162. Vector2 u[4] = {
  1163. Vector2(u0, v0),
  1164. Vector2(u1, v0),
  1165. Vector2(u1, v1),
  1166. Vector2(u0, v1),
  1167. };
  1168. // Draw the first face, but skip this at the north pole (i == 0).
  1169. if (i > 0) {
  1170. facesw[face * 3 + 0] = v[0];
  1171. facesw[face * 3 + 1] = v[1];
  1172. facesw[face * 3 + 2] = v[2];
  1173. uvsw[face * 3 + 0] = u[0];
  1174. uvsw[face * 3 + 1] = u[1];
  1175. uvsw[face * 3 + 2] = u[2];
  1176. smoothw[face] = smooth_faces;
  1177. invertw[face] = invert_val;
  1178. materialsw[face] = base_material;
  1179. face++;
  1180. }
  1181. // Draw the second face, but skip this at the south pole (i == rings - 1).
  1182. if (i < rings - 1) {
  1183. facesw[face * 3 + 0] = v[2];
  1184. facesw[face * 3 + 1] = v[3];
  1185. facesw[face * 3 + 2] = v[0];
  1186. uvsw[face * 3 + 0] = u[2];
  1187. uvsw[face * 3 + 1] = u[3];
  1188. uvsw[face * 3 + 2] = u[0];
  1189. smoothw[face] = smooth_faces;
  1190. invertw[face] = invert_val;
  1191. materialsw[face] = base_material;
  1192. face++;
  1193. }
  1194. }
  1195. }
  1196. if (face != face_count) {
  1197. ERR_PRINT("Face mismatch bug! fix code");
  1198. }
  1199. }
  1200. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1201. return new_brush;
  1202. }
  1203. void CSGSphere3D::_bind_methods() {
  1204. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGSphere3D::set_radius);
  1205. ClassDB::bind_method(D_METHOD("get_radius"), &CSGSphere3D::get_radius);
  1206. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGSphere3D::set_radial_segments);
  1207. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGSphere3D::get_radial_segments);
  1208. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CSGSphere3D::set_rings);
  1209. ClassDB::bind_method(D_METHOD("get_rings"), &CSGSphere3D::get_rings);
  1210. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGSphere3D::set_smooth_faces);
  1211. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGSphere3D::get_smooth_faces);
  1212. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGSphere3D::set_material);
  1213. ClassDB::bind_method(D_METHOD("get_material"), &CSGSphere3D::get_material);
  1214. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,suffix:m"), "set_radius", "get_radius");
  1215. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments");
  1216. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1"), "set_rings", "get_rings");
  1217. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1218. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1219. }
  1220. void CSGSphere3D::set_radius(const float p_radius) {
  1221. ERR_FAIL_COND(p_radius <= 0);
  1222. radius = p_radius;
  1223. _make_dirty();
  1224. update_gizmos();
  1225. }
  1226. float CSGSphere3D::get_radius() const {
  1227. return radius;
  1228. }
  1229. void CSGSphere3D::set_radial_segments(const int p_radial_segments) {
  1230. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  1231. _make_dirty();
  1232. update_gizmos();
  1233. }
  1234. int CSGSphere3D::get_radial_segments() const {
  1235. return radial_segments;
  1236. }
  1237. void CSGSphere3D::set_rings(const int p_rings) {
  1238. rings = p_rings > 1 ? p_rings : 1;
  1239. _make_dirty();
  1240. update_gizmos();
  1241. }
  1242. int CSGSphere3D::get_rings() const {
  1243. return rings;
  1244. }
  1245. void CSGSphere3D::set_smooth_faces(const bool p_smooth_faces) {
  1246. smooth_faces = p_smooth_faces;
  1247. _make_dirty();
  1248. }
  1249. bool CSGSphere3D::get_smooth_faces() const {
  1250. return smooth_faces;
  1251. }
  1252. void CSGSphere3D::set_material(const Ref<Material> &p_material) {
  1253. material = p_material;
  1254. _make_dirty();
  1255. }
  1256. Ref<Material> CSGSphere3D::get_material() const {
  1257. return material;
  1258. }
  1259. CSGSphere3D::CSGSphere3D() {
  1260. // defaults
  1261. radius = 0.5;
  1262. radial_segments = 12;
  1263. rings = 6;
  1264. smooth_faces = true;
  1265. }
  1266. ///////////////
  1267. CSGBrush *CSGBox3D::_build_brush() {
  1268. // set our bounding box
  1269. CSGBrush *new_brush = memnew(CSGBrush);
  1270. int face_count = 12; //it's a cube..
  1271. bool invert_val = get_flip_faces();
  1272. Ref<Material> base_material = get_material();
  1273. Vector<Vector3> faces;
  1274. Vector<Vector2> uvs;
  1275. Vector<bool> smooth;
  1276. Vector<Ref<Material>> materials;
  1277. Vector<bool> invert;
  1278. faces.resize(face_count * 3);
  1279. uvs.resize(face_count * 3);
  1280. smooth.resize(face_count);
  1281. materials.resize(face_count);
  1282. invert.resize(face_count);
  1283. {
  1284. Vector3 *facesw = faces.ptrw();
  1285. Vector2 *uvsw = uvs.ptrw();
  1286. bool *smoothw = smooth.ptrw();
  1287. Ref<Material> *materialsw = materials.ptrw();
  1288. bool *invertw = invert.ptrw();
  1289. int face = 0;
  1290. Vector3 vertex_mul = size / 2;
  1291. {
  1292. for (int i = 0; i < 6; i++) {
  1293. Vector3 face_points[4];
  1294. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  1295. for (int j = 0; j < 4; j++) {
  1296. float v[3];
  1297. v[0] = 1.0;
  1298. v[1] = 1 - 2 * ((j >> 1) & 1);
  1299. v[2] = v[1] * (1 - 2 * (j & 1));
  1300. for (int k = 0; k < 3; k++) {
  1301. if (i < 3) {
  1302. face_points[j][(i + k) % 3] = v[k];
  1303. } else {
  1304. face_points[3 - j][(i + k) % 3] = -v[k];
  1305. }
  1306. }
  1307. }
  1308. Vector2 u[4];
  1309. for (int j = 0; j < 4; j++) {
  1310. u[j] = Vector2(uv_points[j * 2 + 0], uv_points[j * 2 + 1]);
  1311. }
  1312. //face 1
  1313. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1314. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1315. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1316. uvsw[face * 3 + 0] = u[0];
  1317. uvsw[face * 3 + 1] = u[1];
  1318. uvsw[face * 3 + 2] = u[2];
  1319. smoothw[face] = false;
  1320. invertw[face] = invert_val;
  1321. materialsw[face] = base_material;
  1322. face++;
  1323. //face 2
  1324. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1325. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1326. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1327. uvsw[face * 3 + 0] = u[2];
  1328. uvsw[face * 3 + 1] = u[3];
  1329. uvsw[face * 3 + 2] = u[0];
  1330. smoothw[face] = false;
  1331. invertw[face] = invert_val;
  1332. materialsw[face] = base_material;
  1333. face++;
  1334. }
  1335. }
  1336. if (face != face_count) {
  1337. ERR_PRINT("Face mismatch bug! fix code");
  1338. }
  1339. }
  1340. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1341. return new_brush;
  1342. }
  1343. void CSGBox3D::_bind_methods() {
  1344. ClassDB::bind_method(D_METHOD("set_size", "size"), &CSGBox3D::set_size);
  1345. ClassDB::bind_method(D_METHOD("get_size"), &CSGBox3D::get_size);
  1346. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox3D::set_material);
  1347. ClassDB::bind_method(D_METHOD("get_material"), &CSGBox3D::get_material);
  1348. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1349. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1350. }
  1351. void CSGBox3D::set_size(const Vector3 &p_size) {
  1352. size = p_size;
  1353. _make_dirty();
  1354. update_gizmos();
  1355. }
  1356. Vector3 CSGBox3D::get_size() const {
  1357. return size;
  1358. }
  1359. #ifndef DISABLE_DEPRECATED
  1360. // Kept for compatibility from 3.x to 4.0.
  1361. bool CSGBox3D::_set(const StringName &p_name, const Variant &p_value) {
  1362. if (p_name == "width") {
  1363. size.x = p_value;
  1364. _make_dirty();
  1365. update_gizmos();
  1366. return true;
  1367. } else if (p_name == "height") {
  1368. size.y = p_value;
  1369. _make_dirty();
  1370. update_gizmos();
  1371. return true;
  1372. } else if (p_name == "depth") {
  1373. size.z = p_value;
  1374. _make_dirty();
  1375. update_gizmos();
  1376. return true;
  1377. } else {
  1378. return false;
  1379. }
  1380. }
  1381. #endif
  1382. void CSGBox3D::set_material(const Ref<Material> &p_material) {
  1383. material = p_material;
  1384. _make_dirty();
  1385. update_gizmos();
  1386. }
  1387. Ref<Material> CSGBox3D::get_material() const {
  1388. return material;
  1389. }
  1390. ///////////////
  1391. CSGBrush *CSGCylinder3D::_build_brush() {
  1392. // set our bounding box
  1393. CSGBrush *new_brush = memnew(CSGBrush);
  1394. int face_count = sides * (cone ? 1 : 2) + sides + (cone ? 0 : sides);
  1395. bool invert_val = get_flip_faces();
  1396. Ref<Material> base_material = get_material();
  1397. Vector<Vector3> faces;
  1398. Vector<Vector2> uvs;
  1399. Vector<bool> smooth;
  1400. Vector<Ref<Material>> materials;
  1401. Vector<bool> invert;
  1402. faces.resize(face_count * 3);
  1403. uvs.resize(face_count * 3);
  1404. smooth.resize(face_count);
  1405. materials.resize(face_count);
  1406. invert.resize(face_count);
  1407. {
  1408. Vector3 *facesw = faces.ptrw();
  1409. Vector2 *uvsw = uvs.ptrw();
  1410. bool *smoothw = smooth.ptrw();
  1411. Ref<Material> *materialsw = materials.ptrw();
  1412. bool *invertw = invert.ptrw();
  1413. int face = 0;
  1414. Vector3 vertex_mul(radius, height * 0.5, radius);
  1415. {
  1416. for (int i = 0; i < sides; i++) {
  1417. float inc = float(i) / sides;
  1418. float inc_n = float((i + 1)) / sides;
  1419. if (i == sides - 1) {
  1420. inc_n = 0;
  1421. }
  1422. float ang = inc * Math::TAU;
  1423. float ang_n = inc_n * Math::TAU;
  1424. Vector3 face_base(Math::cos(ang), 0, Math::sin(ang));
  1425. Vector3 face_base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
  1426. Vector3 face_points[4] = {
  1427. face_base + Vector3(0, -1, 0),
  1428. face_base_n + Vector3(0, -1, 0),
  1429. face_base_n * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1430. face_base * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1431. };
  1432. Vector2 u[4] = {
  1433. Vector2(inc, 0),
  1434. Vector2(inc_n, 0),
  1435. Vector2(inc_n, 1),
  1436. Vector2(inc, 1),
  1437. };
  1438. //side face 1
  1439. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1440. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1441. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1442. uvsw[face * 3 + 0] = u[0];
  1443. uvsw[face * 3 + 1] = u[1];
  1444. uvsw[face * 3 + 2] = u[2];
  1445. smoothw[face] = smooth_faces;
  1446. invertw[face] = invert_val;
  1447. materialsw[face] = base_material;
  1448. face++;
  1449. if (!cone) {
  1450. //side face 2
  1451. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1452. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1453. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1454. uvsw[face * 3 + 0] = u[2];
  1455. uvsw[face * 3 + 1] = u[3];
  1456. uvsw[face * 3 + 2] = u[0];
  1457. smoothw[face] = smooth_faces;
  1458. invertw[face] = invert_val;
  1459. materialsw[face] = base_material;
  1460. face++;
  1461. }
  1462. //bottom face 1
  1463. facesw[face * 3 + 0] = face_points[1] * vertex_mul;
  1464. facesw[face * 3 + 1] = face_points[0] * vertex_mul;
  1465. facesw[face * 3 + 2] = Vector3(0, -1, 0) * vertex_mul;
  1466. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1467. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1468. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1469. smoothw[face] = false;
  1470. invertw[face] = invert_val;
  1471. materialsw[face] = base_material;
  1472. face++;
  1473. if (!cone) {
  1474. //top face 1
  1475. facesw[face * 3 + 0] = face_points[3] * vertex_mul;
  1476. facesw[face * 3 + 1] = face_points[2] * vertex_mul;
  1477. facesw[face * 3 + 2] = Vector3(0, 1, 0) * vertex_mul;
  1478. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1479. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1480. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1481. smoothw[face] = false;
  1482. invertw[face] = invert_val;
  1483. materialsw[face] = base_material;
  1484. face++;
  1485. }
  1486. }
  1487. }
  1488. if (face != face_count) {
  1489. ERR_PRINT("Face mismatch bug! fix code");
  1490. }
  1491. }
  1492. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1493. return new_brush;
  1494. }
  1495. void CSGCylinder3D::_bind_methods() {
  1496. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCylinder3D::set_radius);
  1497. ClassDB::bind_method(D_METHOD("get_radius"), &CSGCylinder3D::get_radius);
  1498. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCylinder3D::set_height);
  1499. ClassDB::bind_method(D_METHOD("get_height"), &CSGCylinder3D::get_height);
  1500. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGCylinder3D::set_sides);
  1501. ClassDB::bind_method(D_METHOD("get_sides"), &CSGCylinder3D::get_sides);
  1502. ClassDB::bind_method(D_METHOD("set_cone", "cone"), &CSGCylinder3D::set_cone);
  1503. ClassDB::bind_method(D_METHOD("is_cone"), &CSGCylinder3D::is_cone);
  1504. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGCylinder3D::set_material);
  1505. ClassDB::bind_method(D_METHOD("get_material"), &CSGCylinder3D::get_material);
  1506. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGCylinder3D::set_smooth_faces);
  1507. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGCylinder3D::get_smooth_faces);
  1508. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_radius", "get_radius");
  1509. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_height", "get_height");
  1510. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1511. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cone"), "set_cone", "is_cone");
  1512. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1513. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1514. }
  1515. void CSGCylinder3D::set_radius(const float p_radius) {
  1516. radius = p_radius;
  1517. _make_dirty();
  1518. update_gizmos();
  1519. }
  1520. float CSGCylinder3D::get_radius() const {
  1521. return radius;
  1522. }
  1523. void CSGCylinder3D::set_height(const float p_height) {
  1524. height = p_height;
  1525. _make_dirty();
  1526. update_gizmos();
  1527. }
  1528. float CSGCylinder3D::get_height() const {
  1529. return height;
  1530. }
  1531. void CSGCylinder3D::set_sides(const int p_sides) {
  1532. ERR_FAIL_COND(p_sides < 3);
  1533. sides = p_sides;
  1534. _make_dirty();
  1535. update_gizmos();
  1536. }
  1537. int CSGCylinder3D::get_sides() const {
  1538. return sides;
  1539. }
  1540. void CSGCylinder3D::set_cone(const bool p_cone) {
  1541. cone = p_cone;
  1542. _make_dirty();
  1543. update_gizmos();
  1544. }
  1545. bool CSGCylinder3D::is_cone() const {
  1546. return cone;
  1547. }
  1548. void CSGCylinder3D::set_smooth_faces(const bool p_smooth_faces) {
  1549. smooth_faces = p_smooth_faces;
  1550. _make_dirty();
  1551. }
  1552. bool CSGCylinder3D::get_smooth_faces() const {
  1553. return smooth_faces;
  1554. }
  1555. void CSGCylinder3D::set_material(const Ref<Material> &p_material) {
  1556. material = p_material;
  1557. _make_dirty();
  1558. }
  1559. Ref<Material> CSGCylinder3D::get_material() const {
  1560. return material;
  1561. }
  1562. CSGCylinder3D::CSGCylinder3D() {
  1563. // defaults
  1564. radius = 0.5;
  1565. height = 2.0;
  1566. sides = 8;
  1567. cone = false;
  1568. smooth_faces = true;
  1569. }
  1570. ///////////////
  1571. CSGBrush *CSGTorus3D::_build_brush() {
  1572. // set our bounding box
  1573. float min_radius = inner_radius;
  1574. float max_radius = outer_radius;
  1575. if (min_radius == max_radius) {
  1576. return memnew(CSGBrush); //sorry, can't
  1577. }
  1578. if (min_radius > max_radius) {
  1579. SWAP(min_radius, max_radius);
  1580. }
  1581. float radius = (max_radius - min_radius) * 0.5;
  1582. CSGBrush *new_brush = memnew(CSGBrush);
  1583. int face_count = ring_sides * sides * 2;
  1584. bool invert_val = get_flip_faces();
  1585. Ref<Material> base_material = get_material();
  1586. Vector<Vector3> faces;
  1587. Vector<Vector2> uvs;
  1588. Vector<bool> smooth;
  1589. Vector<Ref<Material>> materials;
  1590. Vector<bool> invert;
  1591. faces.resize(face_count * 3);
  1592. uvs.resize(face_count * 3);
  1593. smooth.resize(face_count);
  1594. materials.resize(face_count);
  1595. invert.resize(face_count);
  1596. {
  1597. Vector3 *facesw = faces.ptrw();
  1598. Vector2 *uvsw = uvs.ptrw();
  1599. bool *smoothw = smooth.ptrw();
  1600. Ref<Material> *materialsw = materials.ptrw();
  1601. bool *invertw = invert.ptrw();
  1602. int face = 0;
  1603. {
  1604. for (int i = 0; i < sides; i++) {
  1605. float inci = float(i) / sides;
  1606. float inci_n = float((i + 1)) / sides;
  1607. if (i == sides - 1) {
  1608. inci_n = 0;
  1609. }
  1610. float angi = inci * Math::TAU;
  1611. float angi_n = inci_n * Math::TAU;
  1612. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1613. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1614. for (int j = 0; j < ring_sides; j++) {
  1615. float incj = float(j) / ring_sides;
  1616. float incj_n = float((j + 1)) / ring_sides;
  1617. if (j == ring_sides - 1) {
  1618. incj_n = 0;
  1619. }
  1620. float angj = incj * Math::TAU;
  1621. float angj_n = incj_n * Math::TAU;
  1622. Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
  1623. Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
  1624. Vector3 face_points[4] = {
  1625. Vector3(normali.x * normalj.x, normalj.y, normali.z * normalj.x),
  1626. Vector3(normali.x * normalj_n.x, normalj_n.y, normali.z * normalj_n.x),
  1627. Vector3(normali_n.x * normalj_n.x, normalj_n.y, normali_n.z * normalj_n.x),
  1628. Vector3(normali_n.x * normalj.x, normalj.y, normali_n.z * normalj.x)
  1629. };
  1630. Vector2 u[4] = {
  1631. Vector2(inci, incj),
  1632. Vector2(inci, incj_n),
  1633. Vector2(inci_n, incj_n),
  1634. Vector2(inci_n, incj),
  1635. };
  1636. // face 1
  1637. facesw[face * 3 + 0] = face_points[0];
  1638. facesw[face * 3 + 1] = face_points[2];
  1639. facesw[face * 3 + 2] = face_points[1];
  1640. uvsw[face * 3 + 0] = u[0];
  1641. uvsw[face * 3 + 1] = u[2];
  1642. uvsw[face * 3 + 2] = u[1];
  1643. smoothw[face] = smooth_faces;
  1644. invertw[face] = invert_val;
  1645. materialsw[face] = base_material;
  1646. face++;
  1647. //face 2
  1648. facesw[face * 3 + 0] = face_points[3];
  1649. facesw[face * 3 + 1] = face_points[2];
  1650. facesw[face * 3 + 2] = face_points[0];
  1651. uvsw[face * 3 + 0] = u[3];
  1652. uvsw[face * 3 + 1] = u[2];
  1653. uvsw[face * 3 + 2] = u[0];
  1654. smoothw[face] = smooth_faces;
  1655. invertw[face] = invert_val;
  1656. materialsw[face] = base_material;
  1657. face++;
  1658. }
  1659. }
  1660. }
  1661. if (face != face_count) {
  1662. ERR_PRINT("Face mismatch bug! fix code");
  1663. }
  1664. }
  1665. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1666. return new_brush;
  1667. }
  1668. void CSGTorus3D::_bind_methods() {
  1669. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &CSGTorus3D::set_inner_radius);
  1670. ClassDB::bind_method(D_METHOD("get_inner_radius"), &CSGTorus3D::get_inner_radius);
  1671. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &CSGTorus3D::set_outer_radius);
  1672. ClassDB::bind_method(D_METHOD("get_outer_radius"), &CSGTorus3D::get_outer_radius);
  1673. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGTorus3D::set_sides);
  1674. ClassDB::bind_method(D_METHOD("get_sides"), &CSGTorus3D::get_sides);
  1675. ClassDB::bind_method(D_METHOD("set_ring_sides", "sides"), &CSGTorus3D::set_ring_sides);
  1676. ClassDB::bind_method(D_METHOD("get_ring_sides"), &CSGTorus3D::get_ring_sides);
  1677. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGTorus3D::set_material);
  1678. ClassDB::bind_method(D_METHOD("get_material"), &CSGTorus3D::get_material);
  1679. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGTorus3D::set_smooth_faces);
  1680. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGTorus3D::get_smooth_faces);
  1681. 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");
  1682. 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");
  1683. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1684. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_ring_sides", "get_ring_sides");
  1685. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1686. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  1687. }
  1688. void CSGTorus3D::set_inner_radius(const float p_inner_radius) {
  1689. inner_radius = p_inner_radius;
  1690. _make_dirty();
  1691. update_gizmos();
  1692. }
  1693. float CSGTorus3D::get_inner_radius() const {
  1694. return inner_radius;
  1695. }
  1696. void CSGTorus3D::set_outer_radius(const float p_outer_radius) {
  1697. outer_radius = p_outer_radius;
  1698. _make_dirty();
  1699. update_gizmos();
  1700. }
  1701. float CSGTorus3D::get_outer_radius() const {
  1702. return outer_radius;
  1703. }
  1704. void CSGTorus3D::set_sides(const int p_sides) {
  1705. ERR_FAIL_COND(p_sides < 3);
  1706. sides = p_sides;
  1707. _make_dirty();
  1708. update_gizmos();
  1709. }
  1710. int CSGTorus3D::get_sides() const {
  1711. return sides;
  1712. }
  1713. void CSGTorus3D::set_ring_sides(const int p_ring_sides) {
  1714. ERR_FAIL_COND(p_ring_sides < 3);
  1715. ring_sides = p_ring_sides;
  1716. _make_dirty();
  1717. update_gizmos();
  1718. }
  1719. int CSGTorus3D::get_ring_sides() const {
  1720. return ring_sides;
  1721. }
  1722. void CSGTorus3D::set_smooth_faces(const bool p_smooth_faces) {
  1723. smooth_faces = p_smooth_faces;
  1724. _make_dirty();
  1725. }
  1726. bool CSGTorus3D::get_smooth_faces() const {
  1727. return smooth_faces;
  1728. }
  1729. void CSGTorus3D::set_material(const Ref<Material> &p_material) {
  1730. material = p_material;
  1731. _make_dirty();
  1732. }
  1733. Ref<Material> CSGTorus3D::get_material() const {
  1734. return material;
  1735. }
  1736. CSGTorus3D::CSGTorus3D() {
  1737. // defaults
  1738. inner_radius = 0.5;
  1739. outer_radius = 1.0;
  1740. sides = 8;
  1741. ring_sides = 6;
  1742. smooth_faces = true;
  1743. }
  1744. ///////////////
  1745. CSGBrush *CSGPolygon3D::_build_brush() {
  1746. CSGBrush *new_brush = memnew(CSGBrush);
  1747. if (polygon.size() < 3) {
  1748. return new_brush;
  1749. }
  1750. // Triangulate polygon shape.
  1751. Vector<Point2> shape_polygon = polygon;
  1752. if (Triangulate::get_area(shape_polygon) > 0) {
  1753. shape_polygon.reverse();
  1754. }
  1755. int shape_sides = shape_polygon.size();
  1756. Vector<int> shape_faces = Geometry2D::triangulate_polygon(shape_polygon);
  1757. 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.");
  1758. // Get polygon enclosing Rect2.
  1759. Rect2 shape_rect(shape_polygon[0], Vector2());
  1760. for (int i = 1; i < shape_sides; i++) {
  1761. shape_rect.expand_to(shape_polygon[i]);
  1762. }
  1763. // If MODE_PATH, check if curve has changed.
  1764. Ref<Curve3D> curve;
  1765. if (mode == MODE_PATH) {
  1766. Path3D *current_path = Object::cast_to<Path3D>(get_node_or_null(path_node));
  1767. if (path != current_path) {
  1768. if (path) {
  1769. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1770. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1771. path->set_update_callback(Callable());
  1772. }
  1773. path = current_path;
  1774. if (path) {
  1775. path->connect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  1776. path->connect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  1777. path->set_update_callback(callable_mp(this, &CSGPolygon3D::_path_changed));
  1778. }
  1779. }
  1780. if (!path) {
  1781. return new_brush;
  1782. }
  1783. curve = path->get_curve();
  1784. if (curve.is_null() || curve->get_point_count() < 2) {
  1785. return new_brush;
  1786. }
  1787. }
  1788. // Calculate the number extrusions, ends and faces.
  1789. int extrusions = 0;
  1790. int extrusion_face_count = shape_sides * 2;
  1791. int end_count = 0;
  1792. int shape_face_count = shape_faces.size() / 3;
  1793. real_t curve_length = 1.0;
  1794. switch (mode) {
  1795. case MODE_DEPTH:
  1796. extrusions = 1;
  1797. end_count = 2;
  1798. break;
  1799. case MODE_SPIN:
  1800. extrusions = spin_sides;
  1801. if (spin_degrees < 360) {
  1802. end_count = 2;
  1803. }
  1804. break;
  1805. case MODE_PATH: {
  1806. curve_length = curve->get_baked_length();
  1807. if (path_interval_type == PATH_INTERVAL_DISTANCE) {
  1808. extrusions = MAX(1, Math::ceil(curve_length / path_interval)) + 1;
  1809. } else {
  1810. extrusions = Math::ceil(1.0 * curve->get_point_count() / path_interval);
  1811. }
  1812. if (!path_joined) {
  1813. end_count = 2;
  1814. extrusions -= 1;
  1815. }
  1816. } break;
  1817. }
  1818. int face_count = extrusions * extrusion_face_count + end_count * shape_face_count;
  1819. // Initialize variables used to create the mesh.
  1820. Ref<Material> base_material = get_material();
  1821. Vector<Vector3> faces;
  1822. Vector<Vector2> uvs;
  1823. Vector<bool> smooth;
  1824. Vector<Ref<Material>> materials;
  1825. Vector<bool> invert;
  1826. faces.resize(face_count * 3);
  1827. uvs.resize(face_count * 3);
  1828. smooth.resize(face_count);
  1829. materials.resize(face_count);
  1830. invert.resize(face_count);
  1831. int faces_removed = 0;
  1832. {
  1833. Vector3 *facesw = faces.ptrw();
  1834. Vector2 *uvsw = uvs.ptrw();
  1835. bool *smoothw = smooth.ptrw();
  1836. Ref<Material> *materialsw = materials.ptrw();
  1837. bool *invertw = invert.ptrw();
  1838. int face = 0;
  1839. Transform3D base_xform;
  1840. Transform3D current_xform;
  1841. Transform3D previous_xform;
  1842. Transform3D previous_previous_xform;
  1843. double u_step = 1.0 / extrusions;
  1844. if (path_u_distance > 0.0) {
  1845. u_step *= curve_length / path_u_distance;
  1846. }
  1847. double v_step = 1.0 / shape_sides;
  1848. double spin_step = Math::deg_to_rad(spin_degrees / spin_sides);
  1849. double extrusion_step = 1.0 / extrusions;
  1850. if (mode == MODE_PATH) {
  1851. if (path_joined) {
  1852. extrusion_step = 1.0 / (extrusions - 1);
  1853. }
  1854. extrusion_step *= curve_length;
  1855. }
  1856. if (mode == MODE_PATH) {
  1857. if (!path_local) {
  1858. base_xform = path->get_global_transform();
  1859. }
  1860. Vector3 current_point;
  1861. Vector3 current_up = Vector3(0, 1, 0);
  1862. Vector3 direction;
  1863. switch (path_rotation) {
  1864. case PATH_ROTATION_POLYGON:
  1865. current_point = curve->sample_baked(0);
  1866. direction = Vector3(0, 0, -1);
  1867. break;
  1868. case PATH_ROTATION_PATH:
  1869. case PATH_ROTATION_PATH_FOLLOW:
  1870. if (!path_rotation_accurate) {
  1871. current_point = curve->sample_baked(0);
  1872. Vector3 next_point = curve->sample_baked(extrusion_step);
  1873. direction = next_point - current_point;
  1874. if (path_joined) {
  1875. Vector3 last_point = curve->sample_baked(curve->get_baked_length());
  1876. direction = next_point - last_point;
  1877. }
  1878. } else {
  1879. Transform3D current_sample_xform = curve->sample_baked_with_rotation(0);
  1880. current_point = current_sample_xform.get_origin();
  1881. direction = current_sample_xform.get_basis().xform(Vector3(0, 0, -1));
  1882. }
  1883. if (path_rotation == PATH_ROTATION_PATH_FOLLOW) {
  1884. current_up = curve->sample_baked_up_vector(0, true);
  1885. }
  1886. break;
  1887. }
  1888. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1889. current_xform = base_xform.translated_local(current_point) * facing;
  1890. }
  1891. // Create the mesh.
  1892. if (end_count > 0) {
  1893. // Add front end face.
  1894. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  1895. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  1896. // We need to reverse the rotation of the shape face vertices.
  1897. int index = shape_faces[face_idx * 3 + 2 - face_vertex_idx];
  1898. Point2 p = shape_polygon[index];
  1899. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  1900. // Use the left side of the bottom half of the y-inverted texture.
  1901. uv.x = uv.x / 2;
  1902. uv.y = 1 - (uv.y / 2);
  1903. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  1904. uvsw[face * 3 + face_vertex_idx] = uv;
  1905. }
  1906. smoothw[face] = false;
  1907. materialsw[face] = base_material;
  1908. invertw[face] = flip_faces;
  1909. face++;
  1910. }
  1911. }
  1912. real_t angle_simplify_dot = Math::cos(Math::deg_to_rad(path_simplify_angle));
  1913. Vector3 previous_simplify_dir = Vector3(0, 0, 0);
  1914. int faces_combined = 0;
  1915. // Add extrusion faces.
  1916. for (int x0 = 0; x0 < extrusions; x0++) {
  1917. previous_previous_xform = previous_xform;
  1918. previous_xform = current_xform;
  1919. switch (mode) {
  1920. case MODE_DEPTH: {
  1921. current_xform.translate_local(Vector3(0, 0, -depth));
  1922. } break;
  1923. case MODE_SPIN: {
  1924. if (end_count == 0 && x0 == extrusions - 1) {
  1925. current_xform = base_xform;
  1926. } else {
  1927. current_xform.rotate(Vector3(0, 1, 0), spin_step);
  1928. }
  1929. } break;
  1930. case MODE_PATH: {
  1931. double previous_offset = x0 * extrusion_step;
  1932. double current_offset = (x0 + 1) * extrusion_step;
  1933. if (path_joined && x0 == extrusions - 1) {
  1934. current_offset = 0;
  1935. }
  1936. Vector3 previous_point = curve->sample_baked(previous_offset);
  1937. Transform3D current_sample_xform = curve->sample_baked_with_rotation(current_offset);
  1938. Vector3 current_point = current_sample_xform.get_origin();
  1939. Vector3 current_up = Vector3(0, 1, 0);
  1940. Vector3 current_extrusion_dir = (current_point - previous_point).normalized();
  1941. Vector3 direction;
  1942. // If the angles are similar, remove the previous face and replace it with this one.
  1943. if (path_simplify_angle > 0.0 && x0 > 0 && previous_simplify_dir.dot(current_extrusion_dir) > angle_simplify_dot) {
  1944. faces_combined += 1;
  1945. previous_xform = previous_previous_xform;
  1946. face -= extrusion_face_count;
  1947. faces_removed += extrusion_face_count;
  1948. } else {
  1949. faces_combined = 0;
  1950. previous_simplify_dir = current_extrusion_dir;
  1951. }
  1952. switch (path_rotation) {
  1953. case PATH_ROTATION_POLYGON:
  1954. direction = Vector3(0, 0, -1);
  1955. break;
  1956. case PATH_ROTATION_PATH:
  1957. case PATH_ROTATION_PATH_FOLLOW:
  1958. if (!path_rotation_accurate) {
  1959. double next_offset = (x0 + 2) * extrusion_step;
  1960. if (x0 == extrusions - 1) {
  1961. next_offset = path_joined ? extrusion_step : current_offset;
  1962. }
  1963. Vector3 next_point = curve->sample_baked(next_offset);
  1964. direction = next_point - previous_point;
  1965. } else {
  1966. direction = current_sample_xform.get_basis().xform(Vector3(0, 0, -1));
  1967. }
  1968. if (path_rotation == PATH_ROTATION_PATH_FOLLOW) {
  1969. current_up = curve->sample_baked_up_vector(current_offset, true);
  1970. }
  1971. break;
  1972. }
  1973. Transform3D facing = Transform3D().looking_at(direction, current_up);
  1974. current_xform = base_xform.translated_local(current_point) * facing;
  1975. } break;
  1976. }
  1977. double u0 = (x0 - faces_combined) * u_step;
  1978. double u1 = ((x0 + 1) * u_step);
  1979. if (mode == MODE_PATH && !path_continuous_u) {
  1980. u0 = 0.0;
  1981. u1 = 1.0;
  1982. }
  1983. for (int y0 = 0; y0 < shape_sides; y0++) {
  1984. int y1 = (y0 + 1) % shape_sides;
  1985. // Use the top half of the texture.
  1986. double v0 = (y0 * v_step) / 2;
  1987. double v1 = ((y0 + 1) * v_step) / 2;
  1988. Vector3 v[4] = {
  1989. previous_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1990. current_xform.xform(Vector3(shape_polygon[y0].x, shape_polygon[y0].y, 0)),
  1991. current_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1992. previous_xform.xform(Vector3(shape_polygon[y1].x, shape_polygon[y1].y, 0)),
  1993. };
  1994. Vector2 u[4] = {
  1995. Vector2(u0, v0),
  1996. Vector2(u1, v0),
  1997. Vector2(u1, v1),
  1998. Vector2(u0, v1),
  1999. };
  2000. // Face 1
  2001. facesw[face * 3 + 0] = v[0];
  2002. facesw[face * 3 + 1] = v[1];
  2003. facesw[face * 3 + 2] = v[2];
  2004. uvsw[face * 3 + 0] = u[0];
  2005. uvsw[face * 3 + 1] = u[1];
  2006. uvsw[face * 3 + 2] = u[2];
  2007. smoothw[face] = smooth_faces;
  2008. invertw[face] = flip_faces;
  2009. materialsw[face] = base_material;
  2010. face++;
  2011. // Face 2
  2012. facesw[face * 3 + 0] = v[2];
  2013. facesw[face * 3 + 1] = v[3];
  2014. facesw[face * 3 + 2] = v[0];
  2015. uvsw[face * 3 + 0] = u[2];
  2016. uvsw[face * 3 + 1] = u[3];
  2017. uvsw[face * 3 + 2] = u[0];
  2018. smoothw[face] = smooth_faces;
  2019. invertw[face] = flip_faces;
  2020. materialsw[face] = base_material;
  2021. face++;
  2022. }
  2023. }
  2024. if (end_count > 1) {
  2025. // Add back end face.
  2026. for (int face_idx = 0; face_idx < shape_face_count; face_idx++) {
  2027. for (int face_vertex_idx = 0; face_vertex_idx < 3; face_vertex_idx++) {
  2028. int index = shape_faces[face_idx * 3 + face_vertex_idx];
  2029. Point2 p = shape_polygon[index];
  2030. Point2 uv = (p - shape_rect.position) / shape_rect.size;
  2031. // Use the x-inverted ride side of the bottom half of the y-inverted texture.
  2032. uv.x = 1 - uv.x / 2;
  2033. uv.y = 1 - (uv.y / 2);
  2034. facesw[face * 3 + face_vertex_idx] = current_xform.xform(Vector3(p.x, p.y, 0));
  2035. uvsw[face * 3 + face_vertex_idx] = uv;
  2036. }
  2037. smoothw[face] = false;
  2038. materialsw[face] = base_material;
  2039. invertw[face] = flip_faces;
  2040. face++;
  2041. }
  2042. }
  2043. face_count -= faces_removed;
  2044. ERR_FAIL_COND_V_MSG(face != face_count, new_brush, "Bug: Failed to create the CSGPolygon mesh correctly.");
  2045. }
  2046. if (faces_removed > 0) {
  2047. faces.resize(face_count * 3);
  2048. uvs.resize(face_count * 3);
  2049. smooth.resize(face_count);
  2050. materials.resize(face_count);
  2051. invert.resize(face_count);
  2052. }
  2053. new_brush->build_from_faces(faces, uvs, smooth, materials, invert);
  2054. return new_brush;
  2055. }
  2056. void CSGPolygon3D::_notification(int p_what) {
  2057. if (p_what == NOTIFICATION_EXIT_TREE) {
  2058. if (path) {
  2059. path->disconnect(SceneStringName(tree_exited), callable_mp(this, &CSGPolygon3D::_path_exited));
  2060. path->disconnect("curve_changed", callable_mp(this, &CSGPolygon3D::_path_changed));
  2061. path = nullptr;
  2062. }
  2063. }
  2064. }
  2065. void CSGPolygon3D::_validate_property(PropertyInfo &p_property) const {
  2066. if (p_property.name.begins_with("spin") && mode != MODE_SPIN) {
  2067. p_property.usage = PROPERTY_USAGE_NONE;
  2068. }
  2069. if (p_property.name.begins_with("path") && mode != MODE_PATH) {
  2070. p_property.usage = PROPERTY_USAGE_NONE;
  2071. }
  2072. if (p_property.name == "depth" && mode != MODE_DEPTH) {
  2073. p_property.usage = PROPERTY_USAGE_NONE;
  2074. }
  2075. }
  2076. void CSGPolygon3D::_path_changed() {
  2077. _make_dirty();
  2078. update_gizmos();
  2079. }
  2080. void CSGPolygon3D::_path_exited() {
  2081. path = nullptr;
  2082. }
  2083. void CSGPolygon3D::_bind_methods() {
  2084. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CSGPolygon3D::set_polygon);
  2085. ClassDB::bind_method(D_METHOD("get_polygon"), &CSGPolygon3D::get_polygon);
  2086. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSGPolygon3D::set_mode);
  2087. ClassDB::bind_method(D_METHOD("get_mode"), &CSGPolygon3D::get_mode);
  2088. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGPolygon3D::set_depth);
  2089. ClassDB::bind_method(D_METHOD("get_depth"), &CSGPolygon3D::get_depth);
  2090. ClassDB::bind_method(D_METHOD("set_spin_degrees", "degrees"), &CSGPolygon3D::set_spin_degrees);
  2091. ClassDB::bind_method(D_METHOD("get_spin_degrees"), &CSGPolygon3D::get_spin_degrees);
  2092. ClassDB::bind_method(D_METHOD("set_spin_sides", "spin_sides"), &CSGPolygon3D::set_spin_sides);
  2093. ClassDB::bind_method(D_METHOD("get_spin_sides"), &CSGPolygon3D::get_spin_sides);
  2094. ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon3D::set_path_node);
  2095. ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon3D::get_path_node);
  2096. ClassDB::bind_method(D_METHOD("set_path_interval_type", "interval_type"), &CSGPolygon3D::set_path_interval_type);
  2097. ClassDB::bind_method(D_METHOD("get_path_interval_type"), &CSGPolygon3D::get_path_interval_type);
  2098. ClassDB::bind_method(D_METHOD("set_path_interval", "interval"), &CSGPolygon3D::set_path_interval);
  2099. ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon3D::get_path_interval);
  2100. ClassDB::bind_method(D_METHOD("set_path_simplify_angle", "degrees"), &CSGPolygon3D::set_path_simplify_angle);
  2101. ClassDB::bind_method(D_METHOD("get_path_simplify_angle"), &CSGPolygon3D::get_path_simplify_angle);
  2102. ClassDB::bind_method(D_METHOD("set_path_rotation", "path_rotation"), &CSGPolygon3D::set_path_rotation);
  2103. ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon3D::get_path_rotation);
  2104. ClassDB::bind_method(D_METHOD("set_path_rotation_accurate", "enable"), &CSGPolygon3D::set_path_rotation_accurate);
  2105. ClassDB::bind_method(D_METHOD("get_path_rotation_accurate"), &CSGPolygon3D::get_path_rotation_accurate);
  2106. ClassDB::bind_method(D_METHOD("set_path_local", "enable"), &CSGPolygon3D::set_path_local);
  2107. ClassDB::bind_method(D_METHOD("is_path_local"), &CSGPolygon3D::is_path_local);
  2108. ClassDB::bind_method(D_METHOD("set_path_continuous_u", "enable"), &CSGPolygon3D::set_path_continuous_u);
  2109. ClassDB::bind_method(D_METHOD("is_path_continuous_u"), &CSGPolygon3D::is_path_continuous_u);
  2110. ClassDB::bind_method(D_METHOD("set_path_u_distance", "distance"), &CSGPolygon3D::set_path_u_distance);
  2111. ClassDB::bind_method(D_METHOD("get_path_u_distance"), &CSGPolygon3D::get_path_u_distance);
  2112. ClassDB::bind_method(D_METHOD("set_path_joined", "enable"), &CSGPolygon3D::set_path_joined);
  2113. ClassDB::bind_method(D_METHOD("is_path_joined"), &CSGPolygon3D::is_path_joined);
  2114. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGPolygon3D::set_material);
  2115. ClassDB::bind_method(D_METHOD("get_material"), &CSGPolygon3D::get_material);
  2116. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGPolygon3D::set_smooth_faces);
  2117. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGPolygon3D::get_smooth_faces);
  2118. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CSGPolygon3D::_is_editable_3d_polygon);
  2119. ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &CSGPolygon3D::_has_editable_3d_polygon_no_depth);
  2120. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  2121. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Depth,Spin,Path"), "set_mode", "get_mode");
  2122. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.01,100.0,0.01,or_greater,exp,suffix:m"), "set_depth", "get_depth");
  2123. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
  2124. ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
  2125. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path3D"), "set_path_node", "get_path_node");
  2126. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_interval_type", PROPERTY_HINT_ENUM, "Distance,Subdivide"), "set_path_interval_type", "get_path_interval_type");
  2127. 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");
  2128. 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");
  2129. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
  2130. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_rotation_accurate"), "set_path_rotation_accurate", "get_path_rotation_accurate");
  2131. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
  2132. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
  2133. 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");
  2134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_joined"), "set_path_joined", "is_path_joined");
  2135. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  2136. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  2137. BIND_ENUM_CONSTANT(MODE_DEPTH);
  2138. BIND_ENUM_CONSTANT(MODE_SPIN);
  2139. BIND_ENUM_CONSTANT(MODE_PATH);
  2140. BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
  2141. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
  2142. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
  2143. BIND_ENUM_CONSTANT(PATH_INTERVAL_DISTANCE);
  2144. BIND_ENUM_CONSTANT(PATH_INTERVAL_SUBDIVIDE);
  2145. }
  2146. void CSGPolygon3D::set_polygon(const Vector<Vector2> &p_polygon) {
  2147. polygon = p_polygon;
  2148. _make_dirty();
  2149. update_gizmos();
  2150. }
  2151. Vector<Vector2> CSGPolygon3D::get_polygon() const {
  2152. return polygon;
  2153. }
  2154. void CSGPolygon3D::set_mode(Mode p_mode) {
  2155. mode = p_mode;
  2156. _make_dirty();
  2157. update_gizmos();
  2158. notify_property_list_changed();
  2159. }
  2160. CSGPolygon3D::Mode CSGPolygon3D::get_mode() const {
  2161. return mode;
  2162. }
  2163. void CSGPolygon3D::set_depth(const float p_depth) {
  2164. ERR_FAIL_COND(p_depth < 0.001);
  2165. depth = p_depth;
  2166. _make_dirty();
  2167. update_gizmos();
  2168. }
  2169. float CSGPolygon3D::get_depth() const {
  2170. return depth;
  2171. }
  2172. void CSGPolygon3D::set_path_continuous_u(bool p_enable) {
  2173. path_continuous_u = p_enable;
  2174. _make_dirty();
  2175. }
  2176. bool CSGPolygon3D::is_path_continuous_u() const {
  2177. return path_continuous_u;
  2178. }
  2179. void CSGPolygon3D::set_path_u_distance(real_t p_path_u_distance) {
  2180. path_u_distance = p_path_u_distance;
  2181. _make_dirty();
  2182. update_gizmos();
  2183. }
  2184. real_t CSGPolygon3D::get_path_u_distance() const {
  2185. return path_u_distance;
  2186. }
  2187. void CSGPolygon3D::set_spin_degrees(const float p_spin_degrees) {
  2188. ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
  2189. spin_degrees = p_spin_degrees;
  2190. _make_dirty();
  2191. update_gizmos();
  2192. }
  2193. float CSGPolygon3D::get_spin_degrees() const {
  2194. return spin_degrees;
  2195. }
  2196. void CSGPolygon3D::set_spin_sides(int p_spin_sides) {
  2197. ERR_FAIL_COND(p_spin_sides < 3);
  2198. spin_sides = p_spin_sides;
  2199. _make_dirty();
  2200. update_gizmos();
  2201. }
  2202. int CSGPolygon3D::get_spin_sides() const {
  2203. return spin_sides;
  2204. }
  2205. void CSGPolygon3D::set_path_node(const NodePath &p_path) {
  2206. path_node = p_path;
  2207. _make_dirty();
  2208. update_gizmos();
  2209. }
  2210. NodePath CSGPolygon3D::get_path_node() const {
  2211. return path_node;
  2212. }
  2213. void CSGPolygon3D::set_path_interval_type(PathIntervalType p_interval_type) {
  2214. path_interval_type = p_interval_type;
  2215. _make_dirty();
  2216. update_gizmos();
  2217. }
  2218. CSGPolygon3D::PathIntervalType CSGPolygon3D::get_path_interval_type() const {
  2219. return path_interval_type;
  2220. }
  2221. void CSGPolygon3D::set_path_interval(float p_interval) {
  2222. path_interval = p_interval;
  2223. _make_dirty();
  2224. update_gizmos();
  2225. }
  2226. float CSGPolygon3D::get_path_interval() const {
  2227. return path_interval;
  2228. }
  2229. void CSGPolygon3D::set_path_simplify_angle(float p_angle) {
  2230. path_simplify_angle = p_angle;
  2231. _make_dirty();
  2232. update_gizmos();
  2233. }
  2234. float CSGPolygon3D::get_path_simplify_angle() const {
  2235. return path_simplify_angle;
  2236. }
  2237. void CSGPolygon3D::set_path_rotation(PathRotation p_rotation) {
  2238. path_rotation = p_rotation;
  2239. _make_dirty();
  2240. update_gizmos();
  2241. }
  2242. CSGPolygon3D::PathRotation CSGPolygon3D::get_path_rotation() const {
  2243. return path_rotation;
  2244. }
  2245. void CSGPolygon3D::set_path_rotation_accurate(bool p_enabled) {
  2246. path_rotation_accurate = p_enabled;
  2247. _make_dirty();
  2248. update_gizmos();
  2249. }
  2250. bool CSGPolygon3D::get_path_rotation_accurate() const {
  2251. return path_rotation_accurate;
  2252. }
  2253. void CSGPolygon3D::set_path_local(bool p_enable) {
  2254. path_local = p_enable;
  2255. _make_dirty();
  2256. update_gizmos();
  2257. }
  2258. bool CSGPolygon3D::is_path_local() const {
  2259. return path_local;
  2260. }
  2261. void CSGPolygon3D::set_path_joined(bool p_enable) {
  2262. path_joined = p_enable;
  2263. _make_dirty();
  2264. update_gizmos();
  2265. }
  2266. bool CSGPolygon3D::is_path_joined() const {
  2267. return path_joined;
  2268. }
  2269. void CSGPolygon3D::set_smooth_faces(const bool p_smooth_faces) {
  2270. smooth_faces = p_smooth_faces;
  2271. _make_dirty();
  2272. }
  2273. bool CSGPolygon3D::get_smooth_faces() const {
  2274. return smooth_faces;
  2275. }
  2276. void CSGPolygon3D::set_material(const Ref<Material> &p_material) {
  2277. material = p_material;
  2278. _make_dirty();
  2279. }
  2280. Ref<Material> CSGPolygon3D::get_material() const {
  2281. return material;
  2282. }
  2283. bool CSGPolygon3D::_is_editable_3d_polygon() const {
  2284. return true;
  2285. }
  2286. bool CSGPolygon3D::_has_editable_3d_polygon_no_depth() const {
  2287. return true;
  2288. }
  2289. CSGPolygon3D::CSGPolygon3D() {
  2290. // defaults
  2291. mode = MODE_DEPTH;
  2292. polygon.push_back(Vector2(0, 0));
  2293. polygon.push_back(Vector2(0, 1));
  2294. polygon.push_back(Vector2(1, 1));
  2295. polygon.push_back(Vector2(1, 0));
  2296. depth = 1.0;
  2297. spin_degrees = 360;
  2298. spin_sides = 8;
  2299. smooth_faces = false;
  2300. path_interval_type = PATH_INTERVAL_DISTANCE;
  2301. path_interval = 1.0;
  2302. path_simplify_angle = 0.0;
  2303. path_rotation = PATH_ROTATION_PATH_FOLLOW;
  2304. path_rotation_accurate = false;
  2305. path_local = false;
  2306. path_continuous_u = true;
  2307. path_u_distance = 1.0;
  2308. path_joined = false;
  2309. path = nullptr;
  2310. }