csg_shape.cpp 82 KB

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