csg_shape.cpp 82 KB

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