csg_shape.cpp 86 KB

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