csg_shape.cpp 85 KB

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