csg_shape.cpp 83 KB

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