csg_shape.cpp 82 KB

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