primitive_meshes.cpp 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898
  1. /**************************************************************************/
  2. /* primitive_meshes.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 "primitive_meshes.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/math/math_funcs.h"
  33. #include "scene/resources/theme.h"
  34. #include "scene/theme/theme_db.h"
  35. #include "servers/rendering_server.h"
  36. #include "thirdparty/misc/polypartition.h"
  37. #define PADDING_REF_SIZE 1024.0
  38. /**
  39. PrimitiveMesh
  40. */
  41. void PrimitiveMesh::_update() const {
  42. Array arr;
  43. if (GDVIRTUAL_CALL(_create_mesh_array, arr)) {
  44. ERR_FAIL_COND_MSG(arr.size() != RS::ARRAY_MAX, "_create_mesh_array must return an array of Mesh.ARRAY_MAX elements.");
  45. } else {
  46. arr.resize(RS::ARRAY_MAX);
  47. _create_mesh_array(arr);
  48. }
  49. Vector<Vector3> points = arr[RS::ARRAY_VERTEX];
  50. ERR_FAIL_COND_MSG(points.is_empty(), "_create_mesh_array must return at least a vertex array.");
  51. aabb = AABB();
  52. int pc = points.size();
  53. ERR_FAIL_COND(pc == 0);
  54. {
  55. const Vector3 *r = points.ptr();
  56. for (int i = 0; i < pc; i++) {
  57. if (i == 0) {
  58. aabb.position = r[i];
  59. } else {
  60. aabb.expand_to(r[i]);
  61. }
  62. }
  63. }
  64. Vector<int> indices = arr[RS::ARRAY_INDEX];
  65. if (flip_faces) {
  66. Vector<Vector3> normals = arr[RS::ARRAY_NORMAL];
  67. if (normals.size() && indices.size()) {
  68. {
  69. int nc = normals.size();
  70. Vector3 *w = normals.ptrw();
  71. for (int i = 0; i < nc; i++) {
  72. w[i] = -w[i];
  73. }
  74. }
  75. {
  76. int ic = indices.size();
  77. int *w = indices.ptrw();
  78. for (int i = 0; i < ic; i += 3) {
  79. SWAP(w[i + 0], w[i + 1]);
  80. }
  81. }
  82. arr[RS::ARRAY_NORMAL] = normals;
  83. arr[RS::ARRAY_INDEX] = indices;
  84. }
  85. }
  86. if (add_uv2) {
  87. // _create_mesh_array should populate our UV2, this is a fallback in case it doesn't.
  88. // As we don't know anything about the geometry we only pad the right and bottom edge
  89. // of our texture.
  90. Vector<Vector2> uv = arr[RS::ARRAY_TEX_UV];
  91. Vector<Vector2> uv2 = arr[RS::ARRAY_TEX_UV2];
  92. if (uv.size() > 0 && uv2.size() == 0) {
  93. Vector2 uv2_scale = get_uv2_scale();
  94. uv2.resize(uv.size());
  95. Vector2 *uv2w = uv2.ptrw();
  96. for (int i = 0; i < uv.size(); i++) {
  97. uv2w[i] = uv[i] * uv2_scale;
  98. }
  99. }
  100. arr[RS::ARRAY_TEX_UV2] = uv2;
  101. }
  102. array_len = pc;
  103. index_array_len = indices.size();
  104. // in with the new
  105. RenderingServer::get_singleton()->mesh_clear(mesh);
  106. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(mesh, (RenderingServer::PrimitiveType)primitive_type, arr);
  107. RenderingServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
  108. pending_request = false;
  109. clear_cache();
  110. const_cast<PrimitiveMesh *>(this)->emit_changed();
  111. }
  112. void PrimitiveMesh::request_update() {
  113. if (pending_request) {
  114. return;
  115. }
  116. _update();
  117. }
  118. int PrimitiveMesh::get_surface_count() const {
  119. if (pending_request) {
  120. _update();
  121. }
  122. return 1;
  123. }
  124. int PrimitiveMesh::surface_get_array_len(int p_idx) const {
  125. ERR_FAIL_INDEX_V(p_idx, 1, -1);
  126. if (pending_request) {
  127. _update();
  128. }
  129. return array_len;
  130. }
  131. int PrimitiveMesh::surface_get_array_index_len(int p_idx) const {
  132. ERR_FAIL_INDEX_V(p_idx, 1, -1);
  133. if (pending_request) {
  134. _update();
  135. }
  136. return index_array_len;
  137. }
  138. Array PrimitiveMesh::surface_get_arrays(int p_surface) const {
  139. ERR_FAIL_INDEX_V(p_surface, 1, Array());
  140. if (pending_request) {
  141. _update();
  142. }
  143. return RenderingServer::get_singleton()->mesh_surface_get_arrays(mesh, 0);
  144. }
  145. Dictionary PrimitiveMesh::surface_get_lods(int p_surface) const {
  146. return Dictionary(); //not really supported
  147. }
  148. TypedArray<Array> PrimitiveMesh::surface_get_blend_shape_arrays(int p_surface) const {
  149. return TypedArray<Array>(); //not really supported
  150. }
  151. BitField<Mesh::ArrayFormat> PrimitiveMesh::surface_get_format(int p_idx) const {
  152. ERR_FAIL_INDEX_V(p_idx, 1, 0);
  153. uint64_t mesh_format = RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_FORMAT_NORMAL | RS::ARRAY_FORMAT_TANGENT | RS::ARRAY_FORMAT_TEX_UV | RS::ARRAY_FORMAT_INDEX;
  154. if (add_uv2) {
  155. mesh_format |= RS::ARRAY_FORMAT_TEX_UV2;
  156. }
  157. return mesh_format;
  158. }
  159. Mesh::PrimitiveType PrimitiveMesh::surface_get_primitive_type(int p_idx) const {
  160. return primitive_type;
  161. }
  162. void PrimitiveMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  163. ERR_FAIL_INDEX(p_idx, 1);
  164. set_material(p_material);
  165. }
  166. Ref<Material> PrimitiveMesh::surface_get_material(int p_idx) const {
  167. ERR_FAIL_INDEX_V(p_idx, 1, nullptr);
  168. return material;
  169. }
  170. int PrimitiveMesh::get_blend_shape_count() const {
  171. return 0;
  172. }
  173. StringName PrimitiveMesh::get_blend_shape_name(int p_index) const {
  174. return StringName();
  175. }
  176. void PrimitiveMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
  177. }
  178. AABB PrimitiveMesh::get_aabb() const {
  179. if (pending_request) {
  180. _update();
  181. }
  182. return aabb;
  183. }
  184. RID PrimitiveMesh::get_rid() const {
  185. if (pending_request) {
  186. _update();
  187. }
  188. return mesh;
  189. }
  190. void PrimitiveMesh::_bind_methods() {
  191. ClassDB::bind_method(D_METHOD("set_material", "material"), &PrimitiveMesh::set_material);
  192. ClassDB::bind_method(D_METHOD("get_material"), &PrimitiveMesh::get_material);
  193. ClassDB::bind_method(D_METHOD("get_mesh_arrays"), &PrimitiveMesh::get_mesh_arrays);
  194. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &PrimitiveMesh::set_custom_aabb);
  195. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &PrimitiveMesh::get_custom_aabb);
  196. ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &PrimitiveMesh::set_flip_faces);
  197. ClassDB::bind_method(D_METHOD("get_flip_faces"), &PrimitiveMesh::get_flip_faces);
  198. ClassDB::bind_method(D_METHOD("set_add_uv2", "add_uv2"), &PrimitiveMesh::set_add_uv2);
  199. ClassDB::bind_method(D_METHOD("get_add_uv2"), &PrimitiveMesh::get_add_uv2);
  200. ClassDB::bind_method(D_METHOD("set_uv2_padding", "uv2_padding"), &PrimitiveMesh::set_uv2_padding);
  201. ClassDB::bind_method(D_METHOD("get_uv2_padding"), &PrimitiveMesh::get_uv2_padding);
  202. ClassDB::bind_method(D_METHOD("request_update"), &PrimitiveMesh::request_update);
  203. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  204. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_custom_aabb", "get_custom_aabb");
  205. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
  206. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "add_uv2"), "set_add_uv2", "get_add_uv2");
  207. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "uv2_padding", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), "set_uv2_padding", "get_uv2_padding");
  208. GDVIRTUAL_BIND(_create_mesh_array);
  209. }
  210. void PrimitiveMesh::set_material(const Ref<Material> &p_material) {
  211. if (p_material == material) {
  212. return;
  213. }
  214. material = p_material;
  215. if (!pending_request) {
  216. // just apply it, else it'll happen when _update is called.
  217. RenderingServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
  218. notify_property_list_changed();
  219. emit_changed();
  220. }
  221. }
  222. Ref<Material> PrimitiveMesh::get_material() const {
  223. return material;
  224. }
  225. Array PrimitiveMesh::get_mesh_arrays() const {
  226. return surface_get_arrays(0);
  227. }
  228. void PrimitiveMesh::set_custom_aabb(const AABB &p_custom) {
  229. if (p_custom.is_equal_approx(custom_aabb)) {
  230. return;
  231. }
  232. custom_aabb = p_custom;
  233. RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  234. emit_changed();
  235. }
  236. AABB PrimitiveMesh::get_custom_aabb() const {
  237. return custom_aabb;
  238. }
  239. void PrimitiveMesh::set_flip_faces(bool p_enable) {
  240. if (p_enable == flip_faces) {
  241. return;
  242. }
  243. flip_faces = p_enable;
  244. request_update();
  245. }
  246. bool PrimitiveMesh::get_flip_faces() const {
  247. return flip_faces;
  248. }
  249. void PrimitiveMesh::set_add_uv2(bool p_enable) {
  250. if (p_enable == add_uv2) {
  251. return;
  252. }
  253. add_uv2 = p_enable;
  254. _update_lightmap_size();
  255. request_update();
  256. }
  257. void PrimitiveMesh::set_uv2_padding(float p_padding) {
  258. if (Math::is_equal_approx(p_padding, uv2_padding)) {
  259. return;
  260. }
  261. uv2_padding = p_padding;
  262. _update_lightmap_size();
  263. request_update();
  264. }
  265. Vector2 PrimitiveMesh::get_uv2_scale(Vector2 p_margin_scale) const {
  266. Vector2 uv2_scale;
  267. Vector2 lightmap_size = get_lightmap_size_hint();
  268. // Calculate it as a margin, if no lightmap size hint is given we assume "PADDING_REF_SIZE" as our texture size.
  269. uv2_scale.x = p_margin_scale.x * uv2_padding / (lightmap_size.x == 0.0 ? PADDING_REF_SIZE : lightmap_size.x);
  270. uv2_scale.y = p_margin_scale.y * uv2_padding / (lightmap_size.y == 0.0 ? PADDING_REF_SIZE : lightmap_size.y);
  271. // Inverse it to turn our margin into a scale
  272. uv2_scale = Vector2(1.0, 1.0) - uv2_scale;
  273. return uv2_scale;
  274. }
  275. float PrimitiveMesh::get_lightmap_texel_size() const {
  276. return texel_size;
  277. }
  278. void PrimitiveMesh::_on_settings_changed() {
  279. float new_texel_size = float(GLOBAL_GET("rendering/lightmapping/primitive_meshes/texel_size"));
  280. if (new_texel_size <= 0.0) {
  281. new_texel_size = 0.2;
  282. }
  283. if (texel_size == new_texel_size) {
  284. return;
  285. }
  286. texel_size = new_texel_size;
  287. _update_lightmap_size();
  288. request_update();
  289. }
  290. PrimitiveMesh::PrimitiveMesh() {
  291. ERR_FAIL_NULL(RenderingServer::get_singleton());
  292. mesh = RenderingServer::get_singleton()->mesh_create();
  293. ERR_FAIL_NULL(ProjectSettings::get_singleton());
  294. texel_size = float(GLOBAL_GET("rendering/lightmapping/primitive_meshes/texel_size"));
  295. if (texel_size <= 0.0) {
  296. texel_size = 0.2;
  297. }
  298. ProjectSettings *project_settings = ProjectSettings::get_singleton();
  299. project_settings->connect("settings_changed", callable_mp(this, &PrimitiveMesh::_on_settings_changed));
  300. }
  301. PrimitiveMesh::~PrimitiveMesh() {
  302. ERR_FAIL_NULL(RenderingServer::get_singleton());
  303. RenderingServer::get_singleton()->free(mesh);
  304. ERR_FAIL_NULL(ProjectSettings::get_singleton());
  305. ProjectSettings *project_settings = ProjectSettings::get_singleton();
  306. project_settings->disconnect("settings_changed", callable_mp(this, &PrimitiveMesh::_on_settings_changed));
  307. }
  308. /**
  309. CapsuleMesh
  310. */
  311. void CapsuleMesh::_update_lightmap_size() {
  312. if (get_add_uv2()) {
  313. // size must have changed, update lightmap size hint
  314. Size2i _lightmap_size_hint;
  315. float padding = get_uv2_padding();
  316. float radial_length = radius * Math_PI * 0.5; // circumference of 90 degree bend
  317. float vertical_length = radial_length * 2 + (height - 2.0 * radius); // total vertical length
  318. _lightmap_size_hint.x = MAX(1.0, 4.0 * radial_length / texel_size) + padding;
  319. _lightmap_size_hint.y = MAX(1.0, vertical_length / texel_size) + padding;
  320. set_lightmap_size_hint(_lightmap_size_hint);
  321. }
  322. }
  323. void CapsuleMesh::_create_mesh_array(Array &p_arr) const {
  324. bool _add_uv2 = get_add_uv2();
  325. float _uv2_padding = get_uv2_padding() * texel_size;
  326. create_mesh_array(p_arr, radius, height, radial_segments, rings, _add_uv2, _uv2_padding);
  327. }
  328. void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const float height, const int radial_segments, const int rings, bool p_add_uv2, const float p_uv2_padding) {
  329. int i, j, prevrow, thisrow, point;
  330. float x, y, z, u, v, w;
  331. float onethird = 1.0 / 3.0;
  332. float twothirds = 2.0 / 3.0;
  333. // Only used if we calculate UV2
  334. float radial_width = 2.0 * radius * Math_PI;
  335. float radial_h = radial_width / (radial_width + p_uv2_padding);
  336. float radial_length = radius * Math_PI * 0.5; // circumference of 90 degree bend
  337. float vertical_length = radial_length * 2 + (height - 2.0 * radius) + p_uv2_padding; // total vertical length
  338. float radial_v = radial_length / vertical_length; // v size of top and bottom section
  339. float height_v = (height - 2.0 * radius) / vertical_length; // v size of height section
  340. // note, this has been aligned with our collision shape but I've left the descriptions as top/middle/bottom
  341. Vector<Vector3> points;
  342. Vector<Vector3> normals;
  343. Vector<float> tangents;
  344. Vector<Vector2> uvs;
  345. Vector<Vector2> uv2s;
  346. Vector<int> indices;
  347. point = 0;
  348. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  349. tangents.push_back(m_x); \
  350. tangents.push_back(m_y); \
  351. tangents.push_back(m_z); \
  352. tangents.push_back(m_d);
  353. /* top hemisphere */
  354. thisrow = 0;
  355. prevrow = 0;
  356. for (j = 0; j <= (rings + 1); j++) {
  357. v = j;
  358. v /= (rings + 1);
  359. if (j == (rings + 1)) {
  360. w = 1.0;
  361. y = 0.0;
  362. } else {
  363. w = Math::sin(0.5 * Math_PI * v);
  364. y = Math::cos(0.5 * Math_PI * v);
  365. }
  366. for (i = 0; i <= radial_segments; i++) {
  367. u = i;
  368. u /= radial_segments;
  369. if (i == radial_segments) {
  370. x = 0.0;
  371. z = 1.0;
  372. } else {
  373. x = -Math::sin(u * Math_TAU);
  374. z = Math::cos(u * Math_TAU);
  375. }
  376. Vector3 p = Vector3(x * w, y, -z * w);
  377. points.push_back(p * radius + Vector3(0.0, 0.5 * height - radius, 0.0));
  378. normals.push_back(p);
  379. ADD_TANGENT(-z, 0.0, -x, 1.0)
  380. uvs.push_back(Vector2(u, v * onethird));
  381. if (p_add_uv2) {
  382. uv2s.push_back(Vector2(u * radial_h, v * radial_v));
  383. }
  384. point++;
  385. if (i > 0 && j > 0) {
  386. indices.push_back(prevrow + i - 1);
  387. indices.push_back(prevrow + i);
  388. indices.push_back(thisrow + i - 1);
  389. indices.push_back(prevrow + i);
  390. indices.push_back(thisrow + i);
  391. indices.push_back(thisrow + i - 1);
  392. }
  393. }
  394. prevrow = thisrow;
  395. thisrow = point;
  396. }
  397. /* cylinder */
  398. thisrow = point;
  399. prevrow = 0;
  400. for (j = 0; j <= (rings + 1); j++) {
  401. v = j;
  402. v /= (rings + 1);
  403. y = (height - 2.0 * radius) * v;
  404. y = (0.5 * height - radius) - y;
  405. for (i = 0; i <= radial_segments; i++) {
  406. u = i;
  407. u /= radial_segments;
  408. if (i == radial_segments) {
  409. x = 0.0;
  410. z = 1.0;
  411. } else {
  412. x = -Math::sin(u * Math_TAU);
  413. z = Math::cos(u * Math_TAU);
  414. }
  415. Vector3 p = Vector3(x * radius, y, -z * radius);
  416. points.push_back(p);
  417. normals.push_back(Vector3(x, 0.0, -z));
  418. ADD_TANGENT(-z, 0.0, -x, 1.0)
  419. uvs.push_back(Vector2(u, onethird + (v * onethird)));
  420. if (p_add_uv2) {
  421. uv2s.push_back(Vector2(u * radial_h, radial_v + (v * height_v)));
  422. }
  423. point++;
  424. if (i > 0 && j > 0) {
  425. indices.push_back(prevrow + i - 1);
  426. indices.push_back(prevrow + i);
  427. indices.push_back(thisrow + i - 1);
  428. indices.push_back(prevrow + i);
  429. indices.push_back(thisrow + i);
  430. indices.push_back(thisrow + i - 1);
  431. }
  432. }
  433. prevrow = thisrow;
  434. thisrow = point;
  435. }
  436. /* bottom hemisphere */
  437. thisrow = point;
  438. prevrow = 0;
  439. for (j = 0; j <= (rings + 1); j++) {
  440. v = j;
  441. v /= (rings + 1);
  442. if (j == (rings + 1)) {
  443. w = 0.0;
  444. y = -1.0;
  445. } else {
  446. w = Math::cos(0.5 * Math_PI * v);
  447. y = -Math::sin(0.5 * Math_PI * v);
  448. }
  449. for (i = 0; i <= radial_segments; i++) {
  450. u = i;
  451. u /= radial_segments;
  452. if (i == radial_segments) {
  453. x = 0.0;
  454. z = 1.0;
  455. } else {
  456. x = -Math::sin(u * Math_TAU);
  457. z = Math::cos(u * Math_TAU);
  458. }
  459. Vector3 p = Vector3(x * w, y, -z * w);
  460. points.push_back(p * radius + Vector3(0.0, -0.5 * height + radius, 0.0));
  461. normals.push_back(p);
  462. ADD_TANGENT(-z, 0.0, -x, 1.0)
  463. uvs.push_back(Vector2(u, twothirds + v * onethird));
  464. if (p_add_uv2) {
  465. uv2s.push_back(Vector2(u * radial_h, radial_v + height_v + v * radial_v));
  466. }
  467. point++;
  468. if (i > 0 && j > 0) {
  469. indices.push_back(prevrow + i - 1);
  470. indices.push_back(prevrow + i);
  471. indices.push_back(thisrow + i - 1);
  472. indices.push_back(prevrow + i);
  473. indices.push_back(thisrow + i);
  474. indices.push_back(thisrow + i - 1);
  475. }
  476. }
  477. prevrow = thisrow;
  478. thisrow = point;
  479. }
  480. p_arr[RS::ARRAY_VERTEX] = points;
  481. p_arr[RS::ARRAY_NORMAL] = normals;
  482. p_arr[RS::ARRAY_TANGENT] = tangents;
  483. p_arr[RS::ARRAY_TEX_UV] = uvs;
  484. if (p_add_uv2) {
  485. p_arr[RS::ARRAY_TEX_UV2] = uv2s;
  486. }
  487. p_arr[RS::ARRAY_INDEX] = indices;
  488. }
  489. void CapsuleMesh::_bind_methods() {
  490. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleMesh::set_radius);
  491. ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleMesh::get_radius);
  492. ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleMesh::set_height);
  493. ClassDB::bind_method(D_METHOD("get_height"), &CapsuleMesh::get_height);
  494. ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CapsuleMesh::set_radial_segments);
  495. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CapsuleMesh::get_radial_segments);
  496. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CapsuleMesh::set_rings);
  497. ClassDB::bind_method(D_METHOD("get_rings"), &CapsuleMesh::get_rings);
  498. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  499. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_height", "get_height");
  500. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  501. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_rings", "get_rings");
  502. ADD_LINKED_PROPERTY("radius", "height");
  503. ADD_LINKED_PROPERTY("height", "radius");
  504. }
  505. void CapsuleMesh::set_radius(const float p_radius) {
  506. if (Math::is_equal_approx(radius, p_radius)) {
  507. return;
  508. }
  509. radius = p_radius;
  510. if (radius > height * 0.5) {
  511. height = radius * 2.0;
  512. }
  513. _update_lightmap_size();
  514. request_update();
  515. }
  516. float CapsuleMesh::get_radius() const {
  517. return radius;
  518. }
  519. void CapsuleMesh::set_height(const float p_height) {
  520. if (Math::is_equal_approx(height, p_height)) {
  521. return;
  522. }
  523. height = p_height;
  524. if (radius > height * 0.5) {
  525. radius = height * 0.5;
  526. }
  527. _update_lightmap_size();
  528. request_update();
  529. }
  530. float CapsuleMesh::get_height() const {
  531. return height;
  532. }
  533. void CapsuleMesh::set_radial_segments(const int p_segments) {
  534. if (radial_segments == p_segments) {
  535. return;
  536. }
  537. radial_segments = p_segments > 4 ? p_segments : 4;
  538. request_update();
  539. }
  540. int CapsuleMesh::get_radial_segments() const {
  541. return radial_segments;
  542. }
  543. void CapsuleMesh::set_rings(const int p_rings) {
  544. if (rings == p_rings) {
  545. return;
  546. }
  547. ERR_FAIL_COND(p_rings < 0);
  548. rings = p_rings;
  549. request_update();
  550. }
  551. int CapsuleMesh::get_rings() const {
  552. return rings;
  553. }
  554. CapsuleMesh::CapsuleMesh() {}
  555. /**
  556. BoxMesh
  557. */
  558. void BoxMesh::_update_lightmap_size() {
  559. if (get_add_uv2()) {
  560. // size must have changed, update lightmap size hint
  561. Size2i _lightmap_size_hint;
  562. float padding = get_uv2_padding();
  563. float width = (size.x + size.z) / texel_size;
  564. float length = (size.y + size.y + MAX(size.x, size.z)) / texel_size;
  565. _lightmap_size_hint.x = MAX(1.0, width) + 2.0 * padding;
  566. _lightmap_size_hint.y = MAX(1.0, length) + 3.0 * padding;
  567. set_lightmap_size_hint(_lightmap_size_hint);
  568. }
  569. }
  570. void BoxMesh::_create_mesh_array(Array &p_arr) const {
  571. // Note about padding, with our box each face of the box faces a different direction so we want a seam
  572. // around every face. We thus add our padding to the right and bottom of each face.
  573. // With 3 faces along the width and 2 along the height of the texture we need to adjust our scale
  574. // accordingly.
  575. bool _add_uv2 = get_add_uv2();
  576. float _uv2_padding = get_uv2_padding() * texel_size;
  577. BoxMesh::create_mesh_array(p_arr, size, subdivide_w, subdivide_h, subdivide_d, _add_uv2, _uv2_padding);
  578. }
  579. void BoxMesh::create_mesh_array(Array &p_arr, Vector3 size, int subdivide_w, int subdivide_h, int subdivide_d, bool p_add_uv2, const float p_uv2_padding) {
  580. int i, j, prevrow, thisrow, point;
  581. float x, y, z;
  582. float onethird = 1.0 / 3.0;
  583. float twothirds = 2.0 / 3.0;
  584. // Only used if we calculate UV2
  585. // TODO this could be improved by changing the order depending on which side is the longest (basically the below works best if size.y is the longest)
  586. float total_h = (size.x + size.z + (2.0 * p_uv2_padding));
  587. float padding_h = p_uv2_padding / total_h;
  588. float width_h = size.x / total_h;
  589. float depth_h = size.z / total_h;
  590. float total_v = (size.y + size.y + MAX(size.x, size.z) + (3.0 * p_uv2_padding));
  591. float padding_v = p_uv2_padding / total_v;
  592. float width_v = size.x / total_v;
  593. float height_v = size.y / total_v;
  594. float depth_v = size.z / total_v;
  595. Vector3 start_pos = size * -0.5;
  596. // set our bounding box
  597. Vector<Vector3> points;
  598. Vector<Vector3> normals;
  599. Vector<float> tangents;
  600. Vector<Vector2> uvs;
  601. Vector<Vector2> uv2s;
  602. Vector<int> indices;
  603. point = 0;
  604. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  605. tangents.push_back(m_x); \
  606. tangents.push_back(m_y); \
  607. tangents.push_back(m_z); \
  608. tangents.push_back(m_d);
  609. // front + back
  610. y = start_pos.y;
  611. thisrow = point;
  612. prevrow = 0;
  613. for (j = 0; j <= subdivide_h + 1; j++) {
  614. float v = j;
  615. float v2 = v / (subdivide_w + 1.0);
  616. v /= (2.0 * (subdivide_h + 1.0));
  617. x = start_pos.x;
  618. for (i = 0; i <= subdivide_w + 1; i++) {
  619. float u = i;
  620. float u2 = u / (subdivide_w + 1.0);
  621. u /= (3.0 * (subdivide_w + 1.0));
  622. // front
  623. points.push_back(Vector3(x, -y, -start_pos.z)); // double negative on the Z!
  624. normals.push_back(Vector3(0.0, 0.0, 1.0));
  625. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  626. uvs.push_back(Vector2(u, v));
  627. if (p_add_uv2) {
  628. uv2s.push_back(Vector2(u2 * width_h, v2 * height_v));
  629. }
  630. point++;
  631. // back
  632. points.push_back(Vector3(-x, -y, start_pos.z));
  633. normals.push_back(Vector3(0.0, 0.0, -1.0));
  634. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  635. uvs.push_back(Vector2(twothirds + u, v));
  636. if (p_add_uv2) {
  637. uv2s.push_back(Vector2(u2 * width_h, height_v + padding_v + (v2 * height_v)));
  638. }
  639. point++;
  640. if (i > 0 && j > 0) {
  641. int i2 = i * 2;
  642. // front
  643. indices.push_back(prevrow + i2 - 2);
  644. indices.push_back(prevrow + i2);
  645. indices.push_back(thisrow + i2 - 2);
  646. indices.push_back(prevrow + i2);
  647. indices.push_back(thisrow + i2);
  648. indices.push_back(thisrow + i2 - 2);
  649. // back
  650. indices.push_back(prevrow + i2 - 1);
  651. indices.push_back(prevrow + i2 + 1);
  652. indices.push_back(thisrow + i2 - 1);
  653. indices.push_back(prevrow + i2 + 1);
  654. indices.push_back(thisrow + i2 + 1);
  655. indices.push_back(thisrow + i2 - 1);
  656. }
  657. x += size.x / (subdivide_w + 1.0);
  658. }
  659. y += size.y / (subdivide_h + 1.0);
  660. prevrow = thisrow;
  661. thisrow = point;
  662. }
  663. // left + right
  664. y = start_pos.y;
  665. thisrow = point;
  666. prevrow = 0;
  667. for (j = 0; j <= (subdivide_h + 1); j++) {
  668. float v = j;
  669. float v2 = v / (subdivide_h + 1.0);
  670. v /= (2.0 * (subdivide_h + 1.0));
  671. z = start_pos.z;
  672. for (i = 0; i <= (subdivide_d + 1); i++) {
  673. float u = i;
  674. float u2 = u / (subdivide_d + 1.0);
  675. u /= (3.0 * (subdivide_d + 1.0));
  676. // right
  677. points.push_back(Vector3(-start_pos.x, -y, -z));
  678. normals.push_back(Vector3(1.0, 0.0, 0.0));
  679. ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
  680. uvs.push_back(Vector2(onethird + u, v));
  681. if (p_add_uv2) {
  682. uv2s.push_back(Vector2(width_h + padding_h + (u2 * depth_h), v2 * height_v));
  683. }
  684. point++;
  685. // left
  686. points.push_back(Vector3(start_pos.x, -y, z));
  687. normals.push_back(Vector3(-1.0, 0.0, 0.0));
  688. ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
  689. uvs.push_back(Vector2(u, 0.5 + v));
  690. if (p_add_uv2) {
  691. uv2s.push_back(Vector2(width_h + padding_h + (u2 * depth_h), height_v + padding_v + (v2 * height_v)));
  692. }
  693. point++;
  694. if (i > 0 && j > 0) {
  695. int i2 = i * 2;
  696. // right
  697. indices.push_back(prevrow + i2 - 2);
  698. indices.push_back(prevrow + i2);
  699. indices.push_back(thisrow + i2 - 2);
  700. indices.push_back(prevrow + i2);
  701. indices.push_back(thisrow + i2);
  702. indices.push_back(thisrow + i2 - 2);
  703. // left
  704. indices.push_back(prevrow + i2 - 1);
  705. indices.push_back(prevrow + i2 + 1);
  706. indices.push_back(thisrow + i2 - 1);
  707. indices.push_back(prevrow + i2 + 1);
  708. indices.push_back(thisrow + i2 + 1);
  709. indices.push_back(thisrow + i2 - 1);
  710. }
  711. z += size.z / (subdivide_d + 1.0);
  712. }
  713. y += size.y / (subdivide_h + 1.0);
  714. prevrow = thisrow;
  715. thisrow = point;
  716. }
  717. // top + bottom
  718. z = start_pos.z;
  719. thisrow = point;
  720. prevrow = 0;
  721. for (j = 0; j <= (subdivide_d + 1); j++) {
  722. float v = j;
  723. float v2 = v / (subdivide_d + 1.0);
  724. v /= (2.0 * (subdivide_d + 1.0));
  725. x = start_pos.x;
  726. for (i = 0; i <= (subdivide_w + 1); i++) {
  727. float u = i;
  728. float u2 = u / (subdivide_w + 1.0);
  729. u /= (3.0 * (subdivide_w + 1.0));
  730. // top
  731. points.push_back(Vector3(-x, -start_pos.y, -z));
  732. normals.push_back(Vector3(0.0, 1.0, 0.0));
  733. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  734. uvs.push_back(Vector2(onethird + u, 0.5 + v));
  735. if (p_add_uv2) {
  736. uv2s.push_back(Vector2(u2 * width_h, ((height_v + padding_v) * 2.0) + (v2 * depth_v)));
  737. }
  738. point++;
  739. // bottom
  740. points.push_back(Vector3(x, start_pos.y, -z));
  741. normals.push_back(Vector3(0.0, -1.0, 0.0));
  742. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  743. uvs.push_back(Vector2(twothirds + u, 0.5 + v));
  744. if (p_add_uv2) {
  745. uv2s.push_back(Vector2(width_h + padding_h + (u2 * depth_h), ((height_v + padding_v) * 2.0) + (v2 * width_v)));
  746. }
  747. point++;
  748. if (i > 0 && j > 0) {
  749. int i2 = i * 2;
  750. // top
  751. indices.push_back(prevrow + i2 - 2);
  752. indices.push_back(prevrow + i2);
  753. indices.push_back(thisrow + i2 - 2);
  754. indices.push_back(prevrow + i2);
  755. indices.push_back(thisrow + i2);
  756. indices.push_back(thisrow + i2 - 2);
  757. // bottom
  758. indices.push_back(prevrow + i2 - 1);
  759. indices.push_back(prevrow + i2 + 1);
  760. indices.push_back(thisrow + i2 - 1);
  761. indices.push_back(prevrow + i2 + 1);
  762. indices.push_back(thisrow + i2 + 1);
  763. indices.push_back(thisrow + i2 - 1);
  764. }
  765. x += size.x / (subdivide_w + 1.0);
  766. }
  767. z += size.z / (subdivide_d + 1.0);
  768. prevrow = thisrow;
  769. thisrow = point;
  770. }
  771. p_arr[RS::ARRAY_VERTEX] = points;
  772. p_arr[RS::ARRAY_NORMAL] = normals;
  773. p_arr[RS::ARRAY_TANGENT] = tangents;
  774. p_arr[RS::ARRAY_TEX_UV] = uvs;
  775. if (p_add_uv2) {
  776. p_arr[RS::ARRAY_TEX_UV2] = uv2s;
  777. }
  778. p_arr[RS::ARRAY_INDEX] = indices;
  779. }
  780. void BoxMesh::_bind_methods() {
  781. ClassDB::bind_method(D_METHOD("set_size", "size"), &BoxMesh::set_size);
  782. ClassDB::bind_method(D_METHOD("get_size"), &BoxMesh::get_size);
  783. ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &BoxMesh::set_subdivide_width);
  784. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &BoxMesh::get_subdivide_width);
  785. ClassDB::bind_method(D_METHOD("set_subdivide_height", "divisions"), &BoxMesh::set_subdivide_height);
  786. ClassDB::bind_method(D_METHOD("get_subdivide_height"), &BoxMesh::get_subdivide_height);
  787. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "divisions"), &BoxMesh::set_subdivide_depth);
  788. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &BoxMesh::get_subdivide_depth);
  789. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  790. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  791. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
  792. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  793. }
  794. void BoxMesh::set_size(const Vector3 &p_size) {
  795. if (p_size.is_equal_approx(size)) {
  796. return;
  797. }
  798. size = p_size;
  799. _update_lightmap_size();
  800. request_update();
  801. }
  802. Vector3 BoxMesh::get_size() const {
  803. return size;
  804. }
  805. void BoxMesh::set_subdivide_width(const int p_divisions) {
  806. if (p_divisions == subdivide_w) {
  807. return;
  808. }
  809. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  810. request_update();
  811. }
  812. int BoxMesh::get_subdivide_width() const {
  813. return subdivide_w;
  814. }
  815. void BoxMesh::set_subdivide_height(const int p_divisions) {
  816. if (p_divisions == subdivide_h) {
  817. return;
  818. }
  819. subdivide_h = p_divisions > 0 ? p_divisions : 0;
  820. request_update();
  821. }
  822. int BoxMesh::get_subdivide_height() const {
  823. return subdivide_h;
  824. }
  825. void BoxMesh::set_subdivide_depth(const int p_divisions) {
  826. if (p_divisions == subdivide_d) {
  827. return;
  828. }
  829. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  830. request_update();
  831. }
  832. int BoxMesh::get_subdivide_depth() const {
  833. return subdivide_d;
  834. }
  835. BoxMesh::BoxMesh() {}
  836. /**
  837. CylinderMesh
  838. */
  839. void CylinderMesh::_update_lightmap_size() {
  840. if (get_add_uv2()) {
  841. // size must have changed, update lightmap size hint
  842. Size2i _lightmap_size_hint;
  843. float padding = get_uv2_padding();
  844. float top_circumference = top_radius * Math_PI * 2.0;
  845. float bottom_circumference = bottom_radius * Math_PI * 2.0;
  846. float _width = MAX(top_circumference, bottom_circumference) / texel_size + padding;
  847. _width = MAX(_width, (((top_radius + bottom_radius) / texel_size) + padding) * 2.0); // this is extremely unlikely to be larger, will only happen if padding is larger then our diameter.
  848. _lightmap_size_hint.x = MAX(1.0, _width);
  849. float _height = ((height + (MAX(top_radius, bottom_radius) * 2.0)) / texel_size) + (2.0 * padding);
  850. _lightmap_size_hint.y = MAX(1.0, _height);
  851. set_lightmap_size_hint(_lightmap_size_hint);
  852. }
  853. }
  854. void CylinderMesh::_create_mesh_array(Array &p_arr) const {
  855. bool _add_uv2 = get_add_uv2();
  856. float _uv2_padding = get_uv2_padding() * texel_size;
  857. create_mesh_array(p_arr, top_radius, bottom_radius, height, radial_segments, rings, cap_top, cap_bottom, _add_uv2, _uv2_padding);
  858. }
  859. void CylinderMesh::create_mesh_array(Array &p_arr, float top_radius, float bottom_radius, float height, int radial_segments, int rings, bool cap_top, bool cap_bottom, bool p_add_uv2, const float p_uv2_padding) {
  860. int i, j, prevrow, thisrow, point;
  861. float x, y, z, u, v, radius, radius_h;
  862. // Only used if we calculate UV2
  863. float top_circumference = top_radius * Math_PI * 2.0;
  864. float bottom_circumference = bottom_radius * Math_PI * 2.0;
  865. float vertical_length = height + MAX(2.0 * top_radius, 2.0 * bottom_radius) + (2.0 * p_uv2_padding);
  866. float height_v = height / vertical_length;
  867. float padding_v = p_uv2_padding / vertical_length;
  868. float horizontal_length = MAX(MAX(2.0 * (top_radius + bottom_radius + p_uv2_padding), top_circumference + p_uv2_padding), bottom_circumference + p_uv2_padding);
  869. float center_h = 0.5 * (horizontal_length - p_uv2_padding) / horizontal_length;
  870. float top_h = top_circumference / horizontal_length;
  871. float bottom_h = bottom_circumference / horizontal_length;
  872. float padding_h = p_uv2_padding / horizontal_length;
  873. Vector<Vector3> points;
  874. Vector<Vector3> normals;
  875. Vector<float> tangents;
  876. Vector<Vector2> uvs;
  877. Vector<Vector2> uv2s;
  878. Vector<int> indices;
  879. point = 0;
  880. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  881. tangents.push_back(m_x); \
  882. tangents.push_back(m_y); \
  883. tangents.push_back(m_z); \
  884. tangents.push_back(m_d);
  885. thisrow = 0;
  886. prevrow = 0;
  887. const real_t side_normal_y = (bottom_radius - top_radius) / height;
  888. for (j = 0; j <= (rings + 1); j++) {
  889. v = j;
  890. v /= (rings + 1);
  891. radius = top_radius + ((bottom_radius - top_radius) * v);
  892. radius_h = top_h + ((bottom_h - top_h) * v);
  893. y = height * v;
  894. y = (height * 0.5) - y;
  895. for (i = 0; i <= radial_segments; i++) {
  896. u = i;
  897. u /= radial_segments;
  898. if (i == radial_segments) {
  899. x = 0.0;
  900. z = 1.0;
  901. } else {
  902. x = Math::sin(u * Math_TAU);
  903. z = Math::cos(u * Math_TAU);
  904. }
  905. Vector3 p = Vector3(x * radius, y, z * radius);
  906. points.push_back(p);
  907. normals.push_back(Vector3(x, side_normal_y, z).normalized());
  908. ADD_TANGENT(z, 0.0, -x, 1.0)
  909. uvs.push_back(Vector2(u, v * 0.5));
  910. if (p_add_uv2) {
  911. uv2s.push_back(Vector2(center_h + (u - 0.5) * radius_h, v * height_v));
  912. }
  913. point++;
  914. if (i > 0 && j > 0) {
  915. indices.push_back(prevrow + i - 1);
  916. indices.push_back(prevrow + i);
  917. indices.push_back(thisrow + i - 1);
  918. indices.push_back(prevrow + i);
  919. indices.push_back(thisrow + i);
  920. indices.push_back(thisrow + i - 1);
  921. }
  922. }
  923. prevrow = thisrow;
  924. thisrow = point;
  925. }
  926. // Adjust for bottom section, only used if we calculate UV2s.
  927. top_h = top_radius / horizontal_length;
  928. float top_v = top_radius / vertical_length;
  929. bottom_h = bottom_radius / horizontal_length;
  930. float bottom_v = bottom_radius / vertical_length;
  931. // Add top.
  932. if (cap_top && top_radius > 0.0) {
  933. y = height * 0.5;
  934. thisrow = point;
  935. points.push_back(Vector3(0.0, y, 0.0));
  936. normals.push_back(Vector3(0.0, 1.0, 0.0));
  937. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  938. uvs.push_back(Vector2(0.25, 0.75));
  939. if (p_add_uv2) {
  940. uv2s.push_back(Vector2(top_h, height_v + padding_v + MAX(top_v, bottom_v)));
  941. }
  942. point++;
  943. for (i = 0; i <= radial_segments; i++) {
  944. float r = i;
  945. r /= radial_segments;
  946. if (i == radial_segments) {
  947. x = 0.0;
  948. z = 1.0;
  949. } else {
  950. x = Math::sin(r * Math_TAU);
  951. z = Math::cos(r * Math_TAU);
  952. }
  953. u = ((x + 1.0) * 0.25);
  954. v = 0.5 + ((z + 1.0) * 0.25);
  955. Vector3 p = Vector3(x * top_radius, y, z * top_radius);
  956. points.push_back(p);
  957. normals.push_back(Vector3(0.0, 1.0, 0.0));
  958. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  959. uvs.push_back(Vector2(u, v));
  960. if (p_add_uv2) {
  961. uv2s.push_back(Vector2(top_h + (x * top_h), height_v + padding_v + MAX(top_v, bottom_v) + (z * top_v)));
  962. }
  963. point++;
  964. if (i > 0) {
  965. indices.push_back(thisrow);
  966. indices.push_back(point - 1);
  967. indices.push_back(point - 2);
  968. }
  969. }
  970. }
  971. // Add bottom.
  972. if (cap_bottom && bottom_radius > 0.0) {
  973. y = height * -0.5;
  974. thisrow = point;
  975. points.push_back(Vector3(0.0, y, 0.0));
  976. normals.push_back(Vector3(0.0, -1.0, 0.0));
  977. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  978. uvs.push_back(Vector2(0.75, 0.75));
  979. if (p_add_uv2) {
  980. uv2s.push_back(Vector2(top_h + top_h + padding_h + bottom_h, height_v + padding_v + MAX(top_v, bottom_v)));
  981. }
  982. point++;
  983. for (i = 0; i <= radial_segments; i++) {
  984. float r = i;
  985. r /= radial_segments;
  986. if (i == radial_segments) {
  987. x = 0.0;
  988. z = 1.0;
  989. } else {
  990. x = Math::sin(r * Math_TAU);
  991. z = Math::cos(r * Math_TAU);
  992. }
  993. u = 0.5 + ((x + 1.0) * 0.25);
  994. v = 1.0 - ((z + 1.0) * 0.25);
  995. Vector3 p = Vector3(x * bottom_radius, y, z * bottom_radius);
  996. points.push_back(p);
  997. normals.push_back(Vector3(0.0, -1.0, 0.0));
  998. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  999. uvs.push_back(Vector2(u, v));
  1000. if (p_add_uv2) {
  1001. uv2s.push_back(Vector2(top_h + top_h + padding_h + bottom_h + (x * bottom_h), height_v + padding_v + MAX(top_v, bottom_v) - (z * bottom_v)));
  1002. }
  1003. point++;
  1004. if (i > 0) {
  1005. indices.push_back(thisrow);
  1006. indices.push_back(point - 2);
  1007. indices.push_back(point - 1);
  1008. }
  1009. }
  1010. }
  1011. p_arr[RS::ARRAY_VERTEX] = points;
  1012. p_arr[RS::ARRAY_NORMAL] = normals;
  1013. p_arr[RS::ARRAY_TANGENT] = tangents;
  1014. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1015. if (p_add_uv2) {
  1016. p_arr[RS::ARRAY_TEX_UV2] = uv2s;
  1017. }
  1018. p_arr[RS::ARRAY_INDEX] = indices;
  1019. }
  1020. void CylinderMesh::_bind_methods() {
  1021. ClassDB::bind_method(D_METHOD("set_top_radius", "radius"), &CylinderMesh::set_top_radius);
  1022. ClassDB::bind_method(D_METHOD("get_top_radius"), &CylinderMesh::get_top_radius);
  1023. ClassDB::bind_method(D_METHOD("set_bottom_radius", "radius"), &CylinderMesh::set_bottom_radius);
  1024. ClassDB::bind_method(D_METHOD("get_bottom_radius"), &CylinderMesh::get_bottom_radius);
  1025. ClassDB::bind_method(D_METHOD("set_height", "height"), &CylinderMesh::set_height);
  1026. ClassDB::bind_method(D_METHOD("get_height"), &CylinderMesh::get_height);
  1027. ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CylinderMesh::set_radial_segments);
  1028. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CylinderMesh::get_radial_segments);
  1029. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CylinderMesh::set_rings);
  1030. ClassDB::bind_method(D_METHOD("get_rings"), &CylinderMesh::get_rings);
  1031. ClassDB::bind_method(D_METHOD("set_cap_top", "cap_top"), &CylinderMesh::set_cap_top);
  1032. ClassDB::bind_method(D_METHOD("is_cap_top"), &CylinderMesh::is_cap_top);
  1033. ClassDB::bind_method(D_METHOD("set_cap_bottom", "cap_bottom"), &CylinderMesh::set_cap_bottom);
  1034. ClassDB::bind_method(D_METHOD("is_cap_bottom"), &CylinderMesh::is_cap_bottom);
  1035. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "top_radius", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater,suffix:m"), "set_top_radius", "get_top_radius");
  1036. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bottom_radius", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater,suffix:m"), "set_bottom_radius", "get_bottom_radius");
  1037. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_height", "get_height");
  1038. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  1039. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_rings", "get_rings");
  1040. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cap_top"), "set_cap_top", "is_cap_top");
  1041. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cap_bottom"), "set_cap_bottom", "is_cap_bottom");
  1042. }
  1043. void CylinderMesh::set_top_radius(const float p_radius) {
  1044. if (Math::is_equal_approx(p_radius, top_radius)) {
  1045. return;
  1046. }
  1047. top_radius = p_radius;
  1048. _update_lightmap_size();
  1049. request_update();
  1050. }
  1051. float CylinderMesh::get_top_radius() const {
  1052. return top_radius;
  1053. }
  1054. void CylinderMesh::set_bottom_radius(const float p_radius) {
  1055. if (Math::is_equal_approx(p_radius, bottom_radius)) {
  1056. return;
  1057. }
  1058. bottom_radius = p_radius;
  1059. _update_lightmap_size();
  1060. request_update();
  1061. }
  1062. float CylinderMesh::get_bottom_radius() const {
  1063. return bottom_radius;
  1064. }
  1065. void CylinderMesh::set_height(const float p_height) {
  1066. if (Math::is_equal_approx(p_height, height)) {
  1067. return;
  1068. }
  1069. height = p_height;
  1070. _update_lightmap_size();
  1071. request_update();
  1072. }
  1073. float CylinderMesh::get_height() const {
  1074. return height;
  1075. }
  1076. void CylinderMesh::set_radial_segments(const int p_segments) {
  1077. if (p_segments == radial_segments) {
  1078. return;
  1079. }
  1080. radial_segments = p_segments > 4 ? p_segments : 4;
  1081. request_update();
  1082. }
  1083. int CylinderMesh::get_radial_segments() const {
  1084. return radial_segments;
  1085. }
  1086. void CylinderMesh::set_rings(const int p_rings) {
  1087. if (p_rings == rings) {
  1088. return;
  1089. }
  1090. ERR_FAIL_COND(p_rings < 0);
  1091. rings = p_rings;
  1092. request_update();
  1093. }
  1094. int CylinderMesh::get_rings() const {
  1095. return rings;
  1096. }
  1097. void CylinderMesh::set_cap_top(bool p_cap_top) {
  1098. if (p_cap_top == cap_top) {
  1099. return;
  1100. }
  1101. cap_top = p_cap_top;
  1102. request_update();
  1103. }
  1104. bool CylinderMesh::is_cap_top() const {
  1105. return cap_top;
  1106. }
  1107. void CylinderMesh::set_cap_bottom(bool p_cap_bottom) {
  1108. if (p_cap_bottom == cap_bottom) {
  1109. return;
  1110. }
  1111. cap_bottom = p_cap_bottom;
  1112. request_update();
  1113. }
  1114. bool CylinderMesh::is_cap_bottom() const {
  1115. return cap_bottom;
  1116. }
  1117. CylinderMesh::CylinderMesh() {}
  1118. /**
  1119. PlaneMesh
  1120. */
  1121. void PlaneMesh::_update_lightmap_size() {
  1122. if (get_add_uv2()) {
  1123. // size must have changed, update lightmap size hint
  1124. Size2i _lightmap_size_hint;
  1125. float padding = get_uv2_padding();
  1126. _lightmap_size_hint.x = MAX(1.0, (size.x / texel_size) + padding);
  1127. _lightmap_size_hint.y = MAX(1.0, (size.y / texel_size) + padding);
  1128. set_lightmap_size_hint(_lightmap_size_hint);
  1129. }
  1130. }
  1131. void PlaneMesh::_create_mesh_array(Array &p_arr) const {
  1132. int i, j, prevrow, thisrow, point;
  1133. float x, z;
  1134. // Plane mesh can use default UV2 calculation as implemented in Primitive Mesh
  1135. Size2 start_pos = size * -0.5;
  1136. Vector3 normal = Vector3(0.0, 1.0, 0.0);
  1137. if (orientation == FACE_X) {
  1138. normal = Vector3(1.0, 0.0, 0.0);
  1139. } else if (orientation == FACE_Z) {
  1140. normal = Vector3(0.0, 0.0, 1.0);
  1141. }
  1142. Vector<Vector3> points;
  1143. Vector<Vector3> normals;
  1144. Vector<float> tangents;
  1145. Vector<Vector2> uvs;
  1146. Vector<int> indices;
  1147. point = 0;
  1148. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1149. tangents.push_back(m_x); \
  1150. tangents.push_back(m_y); \
  1151. tangents.push_back(m_z); \
  1152. tangents.push_back(m_d);
  1153. /* top + bottom */
  1154. z = start_pos.y;
  1155. thisrow = point;
  1156. prevrow = 0;
  1157. for (j = 0; j <= (subdivide_d + 1); j++) {
  1158. x = start_pos.x;
  1159. for (i = 0; i <= (subdivide_w + 1); i++) {
  1160. float u = i;
  1161. float v = j;
  1162. u /= (subdivide_w + 1.0);
  1163. v /= (subdivide_d + 1.0);
  1164. if (orientation == FACE_X) {
  1165. points.push_back(Vector3(0.0, z, x) + center_offset);
  1166. } else if (orientation == FACE_Y) {
  1167. points.push_back(Vector3(-x, 0.0, -z) + center_offset);
  1168. } else if (orientation == FACE_Z) {
  1169. points.push_back(Vector3(-x, z, 0.0) + center_offset);
  1170. }
  1171. normals.push_back(normal);
  1172. if (orientation == FACE_X) {
  1173. ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
  1174. } else {
  1175. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  1176. }
  1177. uvs.push_back(Vector2(1.0 - u, 1.0 - v)); /* 1.0 - uv to match orientation with Quad */
  1178. point++;
  1179. if (i > 0 && j > 0) {
  1180. indices.push_back(prevrow + i - 1);
  1181. indices.push_back(prevrow + i);
  1182. indices.push_back(thisrow + i - 1);
  1183. indices.push_back(prevrow + i);
  1184. indices.push_back(thisrow + i);
  1185. indices.push_back(thisrow + i - 1);
  1186. }
  1187. x += size.x / (subdivide_w + 1.0);
  1188. }
  1189. z += size.y / (subdivide_d + 1.0);
  1190. prevrow = thisrow;
  1191. thisrow = point;
  1192. }
  1193. p_arr[RS::ARRAY_VERTEX] = points;
  1194. p_arr[RS::ARRAY_NORMAL] = normals;
  1195. p_arr[RS::ARRAY_TANGENT] = tangents;
  1196. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1197. p_arr[RS::ARRAY_INDEX] = indices;
  1198. }
  1199. void PlaneMesh::_bind_methods() {
  1200. ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaneMesh::set_size);
  1201. ClassDB::bind_method(D_METHOD("get_size"), &PlaneMesh::get_size);
  1202. ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &PlaneMesh::set_subdivide_width);
  1203. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PlaneMesh::get_subdivide_width);
  1204. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "subdivide"), &PlaneMesh::set_subdivide_depth);
  1205. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PlaneMesh::get_subdivide_depth);
  1206. ClassDB::bind_method(D_METHOD("set_center_offset", "offset"), &PlaneMesh::set_center_offset);
  1207. ClassDB::bind_method(D_METHOD("get_center_offset"), &PlaneMesh::get_center_offset);
  1208. ClassDB::bind_method(D_METHOD("set_orientation", "orientation"), &PlaneMesh::set_orientation);
  1209. ClassDB::bind_method(D_METHOD("get_orientation"), &PlaneMesh::get_orientation);
  1210. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1211. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  1212. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  1213. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_center_offset", "get_center_offset");
  1214. ADD_PROPERTY(PropertyInfo(Variant::INT, "orientation", PROPERTY_HINT_ENUM, "Face X,Face Y,Face Z"), "set_orientation", "get_orientation");
  1215. BIND_ENUM_CONSTANT(FACE_X)
  1216. BIND_ENUM_CONSTANT(FACE_Y)
  1217. BIND_ENUM_CONSTANT(FACE_Z)
  1218. }
  1219. void PlaneMesh::set_size(const Size2 &p_size) {
  1220. if (p_size == size) {
  1221. return;
  1222. }
  1223. size = p_size;
  1224. _update_lightmap_size();
  1225. request_update();
  1226. }
  1227. Size2 PlaneMesh::get_size() const {
  1228. return size;
  1229. }
  1230. void PlaneMesh::set_subdivide_width(const int p_divisions) {
  1231. if (p_divisions == subdivide_w || (subdivide_w == 0 && p_divisions < 0)) {
  1232. return;
  1233. }
  1234. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  1235. request_update();
  1236. }
  1237. int PlaneMesh::get_subdivide_width() const {
  1238. return subdivide_w;
  1239. }
  1240. void PlaneMesh::set_subdivide_depth(const int p_divisions) {
  1241. if (p_divisions == subdivide_d || (subdivide_d == 0 && p_divisions < 0)) {
  1242. return;
  1243. }
  1244. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  1245. request_update();
  1246. }
  1247. int PlaneMesh::get_subdivide_depth() const {
  1248. return subdivide_d;
  1249. }
  1250. void PlaneMesh::set_center_offset(const Vector3 p_offset) {
  1251. if (p_offset.is_equal_approx(center_offset)) {
  1252. return;
  1253. }
  1254. center_offset = p_offset;
  1255. request_update();
  1256. }
  1257. Vector3 PlaneMesh::get_center_offset() const {
  1258. return center_offset;
  1259. }
  1260. void PlaneMesh::set_orientation(const Orientation p_orientation) {
  1261. if (p_orientation == orientation) {
  1262. return;
  1263. }
  1264. orientation = p_orientation;
  1265. request_update();
  1266. }
  1267. PlaneMesh::Orientation PlaneMesh::get_orientation() const {
  1268. return orientation;
  1269. }
  1270. PlaneMesh::PlaneMesh() {}
  1271. /**
  1272. PrismMesh
  1273. */
  1274. void PrismMesh::_update_lightmap_size() {
  1275. if (get_add_uv2()) {
  1276. // size must have changed, update lightmap size hint
  1277. Size2i _lightmap_size_hint;
  1278. float padding = get_uv2_padding();
  1279. // left_to_right does not effect the surface area of the prism so we ignore that.
  1280. // TODO we could combine the two triangles and save some space but we need to re-align the uv1 and adjust the tangent.
  1281. float width = (size.x + size.z) / texel_size;
  1282. float length = (size.y + size.y + size.z) / texel_size;
  1283. _lightmap_size_hint.x = MAX(1.0, width) + 2.0 * padding;
  1284. _lightmap_size_hint.y = MAX(1.0, length) + 3.0 * padding;
  1285. set_lightmap_size_hint(_lightmap_size_hint);
  1286. }
  1287. }
  1288. void PrismMesh::_create_mesh_array(Array &p_arr) const {
  1289. int i, j, prevrow, thisrow, point;
  1290. float x, y, z;
  1291. float onethird = 1.0 / 3.0;
  1292. float twothirds = 2.0 / 3.0;
  1293. // Only used if we calculate UV2
  1294. bool _add_uv2 = get_add_uv2();
  1295. float _uv2_padding = get_uv2_padding() * texel_size;
  1296. float horizontal_total = size.x + size.z + 2.0 * _uv2_padding;
  1297. float width_h = size.x / horizontal_total;
  1298. float depth_h = size.z / horizontal_total;
  1299. float padding_h = _uv2_padding / horizontal_total;
  1300. float vertical_total = (size.y + size.y + size.z) + (3.0 * _uv2_padding);
  1301. float height_v = size.y / vertical_total;
  1302. float depth_v = size.z / vertical_total;
  1303. float padding_v = _uv2_padding / vertical_total;
  1304. // and start building
  1305. Vector3 start_pos = size * -0.5;
  1306. // set our bounding box
  1307. Vector<Vector3> points;
  1308. Vector<Vector3> normals;
  1309. Vector<float> tangents;
  1310. Vector<Vector2> uvs;
  1311. Vector<Vector2> uv2s;
  1312. Vector<int> indices;
  1313. point = 0;
  1314. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1315. tangents.push_back(m_x); \
  1316. tangents.push_back(m_y); \
  1317. tangents.push_back(m_z); \
  1318. tangents.push_back(m_d);
  1319. /* front + back */
  1320. y = start_pos.y;
  1321. thisrow = point;
  1322. prevrow = 0;
  1323. for (j = 0; j <= (subdivide_h + 1); j++) {
  1324. float scale = j / (subdivide_h + 1.0);
  1325. float scaled_size_x = size.x * scale;
  1326. float start_x = start_pos.x + (1.0 - scale) * size.x * left_to_right;
  1327. float offset_front = (1.0 - scale) * onethird * left_to_right;
  1328. float offset_back = (1.0 - scale) * onethird * (1.0 - left_to_right);
  1329. float v = j;
  1330. float v2 = scale;
  1331. v /= 2.0 * (subdivide_h + 1.0);
  1332. x = 0.0;
  1333. for (i = 0; i <= (subdivide_w + 1); i++) {
  1334. float u = i;
  1335. float u2 = i / (subdivide_w + 1.0);
  1336. u /= (3.0 * (subdivide_w + 1.0));
  1337. u *= scale;
  1338. /* front */
  1339. points.push_back(Vector3(start_x + x, -y, -start_pos.z)); // double negative on the Z!
  1340. normals.push_back(Vector3(0.0, 0.0, 1.0));
  1341. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  1342. uvs.push_back(Vector2(offset_front + u, v));
  1343. if (_add_uv2) {
  1344. uv2s.push_back(Vector2(u2 * scale * width_h, v2 * height_v));
  1345. }
  1346. point++;
  1347. /* back */
  1348. points.push_back(Vector3(start_x + scaled_size_x - x, -y, start_pos.z));
  1349. normals.push_back(Vector3(0.0, 0.0, -1.0));
  1350. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  1351. uvs.push_back(Vector2(twothirds + offset_back + u, v));
  1352. if (_add_uv2) {
  1353. uv2s.push_back(Vector2(u2 * scale * width_h, height_v + padding_v + v2 * height_v));
  1354. }
  1355. point++;
  1356. if (i > 0 && j == 1) {
  1357. int i2 = i * 2;
  1358. /* front */
  1359. indices.push_back(prevrow + i2);
  1360. indices.push_back(thisrow + i2);
  1361. indices.push_back(thisrow + i2 - 2);
  1362. /* back */
  1363. indices.push_back(prevrow + i2 + 1);
  1364. indices.push_back(thisrow + i2 + 1);
  1365. indices.push_back(thisrow + i2 - 1);
  1366. } else if (i > 0 && j > 0) {
  1367. int i2 = i * 2;
  1368. /* front */
  1369. indices.push_back(prevrow + i2 - 2);
  1370. indices.push_back(prevrow + i2);
  1371. indices.push_back(thisrow + i2 - 2);
  1372. indices.push_back(prevrow + i2);
  1373. indices.push_back(thisrow + i2);
  1374. indices.push_back(thisrow + i2 - 2);
  1375. /* back */
  1376. indices.push_back(prevrow + i2 - 1);
  1377. indices.push_back(prevrow + i2 + 1);
  1378. indices.push_back(thisrow + i2 - 1);
  1379. indices.push_back(prevrow + i2 + 1);
  1380. indices.push_back(thisrow + i2 + 1);
  1381. indices.push_back(thisrow + i2 - 1);
  1382. }
  1383. x += scale * size.x / (subdivide_w + 1.0);
  1384. }
  1385. y += size.y / (subdivide_h + 1.0);
  1386. prevrow = thisrow;
  1387. thisrow = point;
  1388. }
  1389. /* left + right */
  1390. Vector3 normal_left, normal_right;
  1391. normal_left = Vector3(-size.y, size.x * left_to_right, 0.0);
  1392. normal_right = Vector3(size.y, size.x * (1.0 - left_to_right), 0.0);
  1393. normal_left.normalize();
  1394. normal_right.normalize();
  1395. y = start_pos.y;
  1396. thisrow = point;
  1397. prevrow = 0;
  1398. for (j = 0; j <= (subdivide_h + 1); j++) {
  1399. float left, right;
  1400. float scale = j / (subdivide_h + 1.0);
  1401. left = start_pos.x + (size.x * (1.0 - scale) * left_to_right);
  1402. right = left + (size.x * scale);
  1403. float v = j;
  1404. float v2 = scale;
  1405. v /= 2.0 * (subdivide_h + 1.0);
  1406. z = start_pos.z;
  1407. for (i = 0; i <= (subdivide_d + 1); i++) {
  1408. float u = i;
  1409. float u2 = u / (subdivide_d + 1.0);
  1410. u /= (3.0 * (subdivide_d + 1.0));
  1411. /* right */
  1412. points.push_back(Vector3(right, -y, -z));
  1413. normals.push_back(normal_right);
  1414. ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
  1415. uvs.push_back(Vector2(onethird + u, v));
  1416. if (_add_uv2) {
  1417. uv2s.push_back(Vector2(width_h + padding_h + u2 * depth_h, v2 * height_v));
  1418. }
  1419. point++;
  1420. /* left */
  1421. points.push_back(Vector3(left, -y, z));
  1422. normals.push_back(normal_left);
  1423. ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
  1424. uvs.push_back(Vector2(u, 0.5 + v));
  1425. if (_add_uv2) {
  1426. uv2s.push_back(Vector2(width_h + padding_h + u2 * depth_h, height_v + padding_v + v2 * height_v));
  1427. }
  1428. point++;
  1429. if (i > 0 && j > 0) {
  1430. int i2 = i * 2;
  1431. /* right */
  1432. indices.push_back(prevrow + i2 - 2);
  1433. indices.push_back(prevrow + i2);
  1434. indices.push_back(thisrow + i2 - 2);
  1435. indices.push_back(prevrow + i2);
  1436. indices.push_back(thisrow + i2);
  1437. indices.push_back(thisrow + i2 - 2);
  1438. /* left */
  1439. indices.push_back(prevrow + i2 - 1);
  1440. indices.push_back(prevrow + i2 + 1);
  1441. indices.push_back(thisrow + i2 - 1);
  1442. indices.push_back(prevrow + i2 + 1);
  1443. indices.push_back(thisrow + i2 + 1);
  1444. indices.push_back(thisrow + i2 - 1);
  1445. }
  1446. z += size.z / (subdivide_d + 1.0);
  1447. }
  1448. y += size.y / (subdivide_h + 1.0);
  1449. prevrow = thisrow;
  1450. thisrow = point;
  1451. }
  1452. /* bottom */
  1453. z = start_pos.z;
  1454. thisrow = point;
  1455. prevrow = 0;
  1456. for (j = 0; j <= (subdivide_d + 1); j++) {
  1457. float v = j;
  1458. float v2 = v / (subdivide_d + 1.0);
  1459. v /= (2.0 * (subdivide_d + 1.0));
  1460. x = start_pos.x;
  1461. for (i = 0; i <= (subdivide_w + 1); i++) {
  1462. float u = i;
  1463. float u2 = u / (subdivide_w + 1.0);
  1464. u /= (3.0 * (subdivide_w + 1.0));
  1465. /* bottom */
  1466. points.push_back(Vector3(x, start_pos.y, -z));
  1467. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1468. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  1469. uvs.push_back(Vector2(twothirds + u, 0.5 + v));
  1470. if (_add_uv2) {
  1471. uv2s.push_back(Vector2(u2 * width_h, 2.0 * (height_v + padding_v) + v2 * depth_v));
  1472. }
  1473. point++;
  1474. if (i > 0 && j > 0) {
  1475. /* bottom */
  1476. indices.push_back(prevrow + i - 1);
  1477. indices.push_back(prevrow + i);
  1478. indices.push_back(thisrow + i - 1);
  1479. indices.push_back(prevrow + i);
  1480. indices.push_back(thisrow + i);
  1481. indices.push_back(thisrow + i - 1);
  1482. }
  1483. x += size.x / (subdivide_w + 1.0);
  1484. }
  1485. z += size.z / (subdivide_d + 1.0);
  1486. prevrow = thisrow;
  1487. thisrow = point;
  1488. }
  1489. p_arr[RS::ARRAY_VERTEX] = points;
  1490. p_arr[RS::ARRAY_NORMAL] = normals;
  1491. p_arr[RS::ARRAY_TANGENT] = tangents;
  1492. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1493. if (_add_uv2) {
  1494. p_arr[RS::ARRAY_TEX_UV2] = uv2s;
  1495. }
  1496. p_arr[RS::ARRAY_INDEX] = indices;
  1497. }
  1498. void PrismMesh::_bind_methods() {
  1499. ClassDB::bind_method(D_METHOD("set_left_to_right", "left_to_right"), &PrismMesh::set_left_to_right);
  1500. ClassDB::bind_method(D_METHOD("get_left_to_right"), &PrismMesh::get_left_to_right);
  1501. ClassDB::bind_method(D_METHOD("set_size", "size"), &PrismMesh::set_size);
  1502. ClassDB::bind_method(D_METHOD("get_size"), &PrismMesh::get_size);
  1503. ClassDB::bind_method(D_METHOD("set_subdivide_width", "segments"), &PrismMesh::set_subdivide_width);
  1504. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PrismMesh::get_subdivide_width);
  1505. ClassDB::bind_method(D_METHOD("set_subdivide_height", "segments"), &PrismMesh::set_subdivide_height);
  1506. ClassDB::bind_method(D_METHOD("get_subdivide_height"), &PrismMesh::get_subdivide_height);
  1507. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "segments"), &PrismMesh::set_subdivide_depth);
  1508. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PrismMesh::get_subdivide_depth);
  1509. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "left_to_right", PROPERTY_HINT_RANGE, "-2.0,2.0,0.1"), "set_left_to_right", "get_left_to_right");
  1510. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1511. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  1512. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
  1513. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  1514. }
  1515. void PrismMesh::set_left_to_right(const float p_left_to_right) {
  1516. if (Math::is_equal_approx(p_left_to_right, left_to_right)) {
  1517. return;
  1518. }
  1519. left_to_right = p_left_to_right;
  1520. request_update();
  1521. }
  1522. float PrismMesh::get_left_to_right() const {
  1523. return left_to_right;
  1524. }
  1525. void PrismMesh::set_size(const Vector3 &p_size) {
  1526. if (p_size.is_equal_approx(size)) {
  1527. return;
  1528. }
  1529. size = p_size;
  1530. _update_lightmap_size();
  1531. request_update();
  1532. }
  1533. Vector3 PrismMesh::get_size() const {
  1534. return size;
  1535. }
  1536. void PrismMesh::set_subdivide_width(const int p_divisions) {
  1537. if (p_divisions == subdivide_w || (p_divisions < 0 && subdivide_w == 0)) {
  1538. return;
  1539. }
  1540. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  1541. request_update();
  1542. }
  1543. int PrismMesh::get_subdivide_width() const {
  1544. return subdivide_w;
  1545. }
  1546. void PrismMesh::set_subdivide_height(const int p_divisions) {
  1547. if (p_divisions == subdivide_h || (p_divisions < 0 && subdivide_h == 0)) {
  1548. return;
  1549. }
  1550. subdivide_h = p_divisions > 0 ? p_divisions : 0;
  1551. request_update();
  1552. }
  1553. int PrismMesh::get_subdivide_height() const {
  1554. return subdivide_h;
  1555. }
  1556. void PrismMesh::set_subdivide_depth(const int p_divisions) {
  1557. if (p_divisions == subdivide_d || (p_divisions < 0 && subdivide_d == 0)) {
  1558. return;
  1559. }
  1560. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  1561. request_update();
  1562. }
  1563. int PrismMesh::get_subdivide_depth() const {
  1564. return subdivide_d;
  1565. }
  1566. PrismMesh::PrismMesh() {}
  1567. /**
  1568. SphereMesh
  1569. */
  1570. void SphereMesh::_update_lightmap_size() {
  1571. if (get_add_uv2()) {
  1572. // size must have changed, update lightmap size hint
  1573. Size2i _lightmap_size_hint;
  1574. float padding = get_uv2_padding();
  1575. float _width = radius * Math_TAU;
  1576. _lightmap_size_hint.x = MAX(1.0, (_width / texel_size) + padding);
  1577. float _height = (is_hemisphere ? 1.0 : 0.5) * height * Math_PI; // note, with hemisphere height is our radius, while with a full sphere it is the diameter..
  1578. _lightmap_size_hint.y = MAX(1.0, (_height / texel_size) + padding);
  1579. set_lightmap_size_hint(_lightmap_size_hint);
  1580. }
  1581. }
  1582. void SphereMesh::_create_mesh_array(Array &p_arr) const {
  1583. bool _add_uv2 = get_add_uv2();
  1584. float _uv2_padding = get_uv2_padding() * texel_size;
  1585. create_mesh_array(p_arr, radius, height, radial_segments, rings, is_hemisphere, _add_uv2, _uv2_padding);
  1586. }
  1587. void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int radial_segments, int rings, bool is_hemisphere, bool p_add_uv2, const float p_uv2_padding) {
  1588. int i, j, prevrow, thisrow, point;
  1589. float x, y, z;
  1590. float scale = height / radius * (is_hemisphere ? 1.0 : 0.5);
  1591. // Only used if we calculate UV2
  1592. float circumference = radius * Math_TAU;
  1593. float horizontal_length = circumference + p_uv2_padding;
  1594. float center_h = 0.5 * circumference / horizontal_length;
  1595. float height_v = scale * Math_PI / ((scale * Math_PI) + p_uv2_padding / radius);
  1596. // set our bounding box
  1597. Vector<Vector3> points;
  1598. Vector<Vector3> normals;
  1599. Vector<float> tangents;
  1600. Vector<Vector2> uvs;
  1601. Vector<Vector2> uv2s;
  1602. Vector<int> indices;
  1603. point = 0;
  1604. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1605. tangents.push_back(m_x); \
  1606. tangents.push_back(m_y); \
  1607. tangents.push_back(m_z); \
  1608. tangents.push_back(m_d);
  1609. thisrow = 0;
  1610. prevrow = 0;
  1611. for (j = 0; j <= (rings + 1); j++) {
  1612. float v = j;
  1613. float w;
  1614. v /= (rings + 1);
  1615. if (j == (rings + 1)) {
  1616. w = 0.0;
  1617. y = -1.0;
  1618. } else {
  1619. w = Math::sin(Math_PI * v);
  1620. y = Math::cos(Math_PI * v);
  1621. }
  1622. for (i = 0; i <= radial_segments; i++) {
  1623. float u = i;
  1624. u /= radial_segments;
  1625. if (i == radial_segments) {
  1626. x = 0.0;
  1627. z = 1.0;
  1628. } else {
  1629. x = Math::sin(u * Math_TAU);
  1630. z = Math::cos(u * Math_TAU);
  1631. }
  1632. if (is_hemisphere && y < 0.0) {
  1633. points.push_back(Vector3(x * radius * w, 0.0, z * radius * w));
  1634. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1635. } else {
  1636. Vector3 p = Vector3(x * w, y * scale, z * w);
  1637. points.push_back(p * radius);
  1638. Vector3 normal = Vector3(x * w * scale, y, z * w * scale);
  1639. normals.push_back(normal.normalized());
  1640. }
  1641. ADD_TANGENT(z, 0.0, -x, 1.0)
  1642. uvs.push_back(Vector2(u, v));
  1643. if (p_add_uv2) {
  1644. float w_h = w * 2.0 * center_h;
  1645. uv2s.push_back(Vector2(center_h + ((u - 0.5) * w_h), v * height_v));
  1646. }
  1647. point++;
  1648. if (i > 0 && j > 0) {
  1649. indices.push_back(prevrow + i - 1);
  1650. indices.push_back(prevrow + i);
  1651. indices.push_back(thisrow + i - 1);
  1652. indices.push_back(prevrow + i);
  1653. indices.push_back(thisrow + i);
  1654. indices.push_back(thisrow + i - 1);
  1655. }
  1656. }
  1657. prevrow = thisrow;
  1658. thisrow = point;
  1659. }
  1660. p_arr[RS::ARRAY_VERTEX] = points;
  1661. p_arr[RS::ARRAY_NORMAL] = normals;
  1662. p_arr[RS::ARRAY_TANGENT] = tangents;
  1663. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1664. if (p_add_uv2) {
  1665. p_arr[RS::ARRAY_TEX_UV2] = uv2s;
  1666. }
  1667. p_arr[RS::ARRAY_INDEX] = indices;
  1668. }
  1669. void SphereMesh::_bind_methods() {
  1670. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SphereMesh::set_radius);
  1671. ClassDB::bind_method(D_METHOD("get_radius"), &SphereMesh::get_radius);
  1672. ClassDB::bind_method(D_METHOD("set_height", "height"), &SphereMesh::set_height);
  1673. ClassDB::bind_method(D_METHOD("get_height"), &SphereMesh::get_height);
  1674. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &SphereMesh::set_radial_segments);
  1675. ClassDB::bind_method(D_METHOD("get_radial_segments"), &SphereMesh::get_radial_segments);
  1676. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &SphereMesh::set_rings);
  1677. ClassDB::bind_method(D_METHOD("get_rings"), &SphereMesh::get_rings);
  1678. ClassDB::bind_method(D_METHOD("set_is_hemisphere", "is_hemisphere"), &SphereMesh::set_is_hemisphere);
  1679. ClassDB::bind_method(D_METHOD("get_is_hemisphere"), &SphereMesh::get_is_hemisphere);
  1680. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  1681. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_height", "get_height");
  1682. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  1683. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
  1684. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_hemisphere"), "set_is_hemisphere", "get_is_hemisphere");
  1685. }
  1686. void SphereMesh::set_radius(const float p_radius) {
  1687. if (Math::is_equal_approx(p_radius, radius)) {
  1688. return;
  1689. }
  1690. radius = p_radius;
  1691. _update_lightmap_size();
  1692. request_update();
  1693. }
  1694. float SphereMesh::get_radius() const {
  1695. return radius;
  1696. }
  1697. void SphereMesh::set_height(const float p_height) {
  1698. if (Math::is_equal_approx(height, p_height)) {
  1699. return;
  1700. }
  1701. height = p_height;
  1702. _update_lightmap_size();
  1703. request_update();
  1704. }
  1705. float SphereMesh::get_height() const {
  1706. return height;
  1707. }
  1708. void SphereMesh::set_radial_segments(const int p_radial_segments) {
  1709. if (p_radial_segments == radial_segments || (radial_segments == 4 && p_radial_segments < 4)) {
  1710. return;
  1711. }
  1712. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  1713. request_update();
  1714. }
  1715. int SphereMesh::get_radial_segments() const {
  1716. return radial_segments;
  1717. }
  1718. void SphereMesh::set_rings(const int p_rings) {
  1719. if (p_rings == rings) {
  1720. return;
  1721. }
  1722. ERR_FAIL_COND(p_rings < 1);
  1723. rings = p_rings;
  1724. request_update();
  1725. }
  1726. int SphereMesh::get_rings() const {
  1727. return rings;
  1728. }
  1729. void SphereMesh::set_is_hemisphere(const bool p_is_hemisphere) {
  1730. if (p_is_hemisphere == is_hemisphere) {
  1731. return;
  1732. }
  1733. is_hemisphere = p_is_hemisphere;
  1734. _update_lightmap_size();
  1735. request_update();
  1736. }
  1737. bool SphereMesh::get_is_hemisphere() const {
  1738. return is_hemisphere;
  1739. }
  1740. SphereMesh::SphereMesh() {}
  1741. /**
  1742. TorusMesh
  1743. */
  1744. void TorusMesh::_update_lightmap_size() {
  1745. if (get_add_uv2()) {
  1746. // size must have changed, update lightmap size hint
  1747. Size2i _lightmap_size_hint;
  1748. float padding = get_uv2_padding();
  1749. float min_radius = inner_radius;
  1750. float max_radius = outer_radius;
  1751. if (min_radius > max_radius) {
  1752. SWAP(min_radius, max_radius);
  1753. }
  1754. float radius = (max_radius - min_radius) * 0.5;
  1755. float _width = max_radius * Math_TAU;
  1756. _lightmap_size_hint.x = MAX(1.0, (_width / texel_size) + padding);
  1757. float _height = radius * Math_TAU;
  1758. _lightmap_size_hint.y = MAX(1.0, (_height / texel_size) + padding);
  1759. set_lightmap_size_hint(_lightmap_size_hint);
  1760. }
  1761. }
  1762. void TorusMesh::_create_mesh_array(Array &p_arr) const {
  1763. // set our bounding box
  1764. Vector<Vector3> points;
  1765. Vector<Vector3> normals;
  1766. Vector<float> tangents;
  1767. Vector<Vector2> uvs;
  1768. Vector<Vector2> uv2s;
  1769. Vector<int> indices;
  1770. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1771. tangents.push_back(m_x); \
  1772. tangents.push_back(m_y); \
  1773. tangents.push_back(m_z); \
  1774. tangents.push_back(m_d);
  1775. ERR_FAIL_COND_MSG(inner_radius == outer_radius, "Inner radius and outer radius cannot be the same.");
  1776. float min_radius = inner_radius;
  1777. float max_radius = outer_radius;
  1778. if (min_radius > max_radius) {
  1779. SWAP(min_radius, max_radius);
  1780. }
  1781. float radius = (max_radius - min_radius) * 0.5;
  1782. // Only used if we calculate UV2
  1783. bool _add_uv2 = get_add_uv2();
  1784. float _uv2_padding = get_uv2_padding() * texel_size;
  1785. float horizontal_total = max_radius * Math_TAU + _uv2_padding;
  1786. float max_h = max_radius * Math_TAU / horizontal_total;
  1787. float delta_h = (max_radius - min_radius) * Math_TAU / horizontal_total;
  1788. float height_v = radius * Math_TAU / (radius * Math_TAU + _uv2_padding);
  1789. for (int i = 0; i <= rings; i++) {
  1790. int prevrow = (i - 1) * (ring_segments + 1);
  1791. int thisrow = i * (ring_segments + 1);
  1792. float inci = float(i) / rings;
  1793. float angi = inci * Math_TAU;
  1794. Vector2 normali = (i == rings) ? Vector2(0.0, -1.0) : Vector2(-Math::sin(angi), -Math::cos(angi));
  1795. for (int j = 0; j <= ring_segments; j++) {
  1796. float incj = float(j) / ring_segments;
  1797. float angj = incj * Math_TAU;
  1798. Vector2 normalj = (j == ring_segments) ? Vector2(-1.0, 0.0) : Vector2(-Math::cos(angj), Math::sin(angj));
  1799. Vector2 normalk = normalj * radius + Vector2(min_radius + radius, 0);
  1800. float offset_h = 0.5 * (1.0 - normalj.x) * delta_h;
  1801. float adj_h = max_h - offset_h;
  1802. offset_h *= 0.5;
  1803. points.push_back(Vector3(normali.x * normalk.x, normalk.y, normali.y * normalk.x));
  1804. normals.push_back(Vector3(normali.x * normalj.x, normalj.y, normali.y * normalj.x));
  1805. ADD_TANGENT(normali.y, 0.0, -normali.x, 1.0);
  1806. uvs.push_back(Vector2(inci, incj));
  1807. if (_add_uv2) {
  1808. uv2s.push_back(Vector2(offset_h + inci * adj_h, incj * height_v));
  1809. }
  1810. if (i > 0 && j > 0) {
  1811. indices.push_back(thisrow + j - 1);
  1812. indices.push_back(prevrow + j);
  1813. indices.push_back(prevrow + j - 1);
  1814. indices.push_back(thisrow + j - 1);
  1815. indices.push_back(thisrow + j);
  1816. indices.push_back(prevrow + j);
  1817. }
  1818. }
  1819. }
  1820. p_arr[RS::ARRAY_VERTEX] = points;
  1821. p_arr[RS::ARRAY_NORMAL] = normals;
  1822. p_arr[RS::ARRAY_TANGENT] = tangents;
  1823. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1824. if (_add_uv2) {
  1825. p_arr[RS::ARRAY_TEX_UV2] = uv2s;
  1826. }
  1827. p_arr[RS::ARRAY_INDEX] = indices;
  1828. }
  1829. void TorusMesh::_bind_methods() {
  1830. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &TorusMesh::set_inner_radius);
  1831. ClassDB::bind_method(D_METHOD("get_inner_radius"), &TorusMesh::get_inner_radius);
  1832. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &TorusMesh::set_outer_radius);
  1833. ClassDB::bind_method(D_METHOD("get_outer_radius"), &TorusMesh::get_outer_radius);
  1834. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &TorusMesh::set_rings);
  1835. ClassDB::bind_method(D_METHOD("get_rings"), &TorusMesh::get_rings);
  1836. ClassDB::bind_method(D_METHOD("set_ring_segments", "rings"), &TorusMesh::set_ring_segments);
  1837. ClassDB::bind_method(D_METHOD("get_ring_segments"), &TorusMesh::get_ring_segments);
  1838. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inner_radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp"), "set_inner_radius", "get_inner_radius");
  1839. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "outer_radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp"), "set_outer_radius", "get_outer_radius");
  1840. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "3,128,1,or_greater"), "set_rings", "get_rings");
  1841. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_segments", PROPERTY_HINT_RANGE, "3,64,1,or_greater"), "set_ring_segments", "get_ring_segments");
  1842. }
  1843. void TorusMesh::set_inner_radius(const float p_inner_radius) {
  1844. if (Math::is_equal_approx(p_inner_radius, inner_radius)) {
  1845. return;
  1846. }
  1847. inner_radius = p_inner_radius;
  1848. request_update();
  1849. }
  1850. float TorusMesh::get_inner_radius() const {
  1851. return inner_radius;
  1852. }
  1853. void TorusMesh::set_outer_radius(const float p_outer_radius) {
  1854. if (Math::is_equal_approx(p_outer_radius, outer_radius)) {
  1855. return;
  1856. }
  1857. outer_radius = p_outer_radius;
  1858. request_update();
  1859. }
  1860. float TorusMesh::get_outer_radius() const {
  1861. return outer_radius;
  1862. }
  1863. void TorusMesh::set_rings(const int p_rings) {
  1864. if (p_rings == rings) {
  1865. return;
  1866. }
  1867. ERR_FAIL_COND(p_rings < 3);
  1868. rings = p_rings;
  1869. request_update();
  1870. }
  1871. int TorusMesh::get_rings() const {
  1872. return rings;
  1873. }
  1874. void TorusMesh::set_ring_segments(const int p_ring_segments) {
  1875. if (p_ring_segments == ring_segments) {
  1876. return;
  1877. }
  1878. ERR_FAIL_COND(p_ring_segments < 3);
  1879. ring_segments = p_ring_segments;
  1880. request_update();
  1881. }
  1882. int TorusMesh::get_ring_segments() const {
  1883. return ring_segments;
  1884. }
  1885. TorusMesh::TorusMesh() {}
  1886. /**
  1887. PointMesh
  1888. */
  1889. void PointMesh::_create_mesh_array(Array &p_arr) const {
  1890. Vector<Vector3> faces;
  1891. faces.resize(1);
  1892. faces.set(0, Vector3(0.0, 0.0, 0.0));
  1893. p_arr[RS::ARRAY_VERTEX] = faces;
  1894. }
  1895. PointMesh::PointMesh() {
  1896. primitive_type = PRIMITIVE_POINTS;
  1897. }
  1898. // TUBE TRAIL
  1899. void TubeTrailMesh::set_radius(const float p_radius) {
  1900. if (Math::is_equal_approx(p_radius, radius)) {
  1901. return;
  1902. }
  1903. radius = p_radius;
  1904. request_update();
  1905. }
  1906. float TubeTrailMesh::get_radius() const {
  1907. return radius;
  1908. }
  1909. void TubeTrailMesh::set_radial_steps(const int p_radial_steps) {
  1910. if (p_radial_steps == radial_steps) {
  1911. return;
  1912. }
  1913. ERR_FAIL_COND(p_radial_steps < 3 || p_radial_steps > 128);
  1914. radial_steps = p_radial_steps;
  1915. request_update();
  1916. }
  1917. int TubeTrailMesh::get_radial_steps() const {
  1918. return radial_steps;
  1919. }
  1920. void TubeTrailMesh::set_sections(const int p_sections) {
  1921. if (p_sections == sections) {
  1922. return;
  1923. }
  1924. ERR_FAIL_COND(p_sections < 2 || p_sections > 128);
  1925. sections = p_sections;
  1926. request_update();
  1927. }
  1928. int TubeTrailMesh::get_sections() const {
  1929. return sections;
  1930. }
  1931. void TubeTrailMesh::set_section_length(float p_section_length) {
  1932. if (p_section_length == section_length) {
  1933. return;
  1934. }
  1935. section_length = p_section_length;
  1936. request_update();
  1937. }
  1938. float TubeTrailMesh::get_section_length() const {
  1939. return section_length;
  1940. }
  1941. void TubeTrailMesh::set_section_rings(const int p_section_rings) {
  1942. if (p_section_rings == section_rings) {
  1943. return;
  1944. }
  1945. ERR_FAIL_COND(p_section_rings < 1 || p_section_rings > 1024);
  1946. section_rings = p_section_rings;
  1947. request_update();
  1948. }
  1949. int TubeTrailMesh::get_section_rings() const {
  1950. return section_rings;
  1951. }
  1952. void TubeTrailMesh::set_cap_top(bool p_cap_top) {
  1953. if (p_cap_top == cap_top) {
  1954. return;
  1955. }
  1956. cap_top = p_cap_top;
  1957. request_update();
  1958. }
  1959. bool TubeTrailMesh::is_cap_top() const {
  1960. return cap_top;
  1961. }
  1962. void TubeTrailMesh::set_cap_bottom(bool p_cap_bottom) {
  1963. if (p_cap_bottom == cap_bottom) {
  1964. return;
  1965. }
  1966. cap_bottom = p_cap_bottom;
  1967. request_update();
  1968. }
  1969. bool TubeTrailMesh::is_cap_bottom() const {
  1970. return cap_bottom;
  1971. }
  1972. void TubeTrailMesh::set_curve(const Ref<Curve> &p_curve) {
  1973. if (curve == p_curve) {
  1974. return;
  1975. }
  1976. if (curve.is_valid()) {
  1977. curve->disconnect_changed(callable_mp(this, &TubeTrailMesh::_curve_changed));
  1978. }
  1979. curve = p_curve;
  1980. if (curve.is_valid()) {
  1981. curve->connect_changed(callable_mp(this, &TubeTrailMesh::_curve_changed));
  1982. }
  1983. request_update();
  1984. }
  1985. Ref<Curve> TubeTrailMesh::get_curve() const {
  1986. return curve;
  1987. }
  1988. void TubeTrailMesh::_curve_changed() {
  1989. request_update();
  1990. }
  1991. int TubeTrailMesh::get_builtin_bind_pose_count() const {
  1992. return sections + 1;
  1993. }
  1994. Transform3D TubeTrailMesh::get_builtin_bind_pose(int p_index) const {
  1995. float depth = section_length * sections;
  1996. Transform3D xform;
  1997. xform.origin.y = depth / 2.0 - section_length * float(p_index);
  1998. xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y
  1999. return xform;
  2000. }
  2001. void TubeTrailMesh::_create_mesh_array(Array &p_arr) const {
  2002. // Seeing use case for TubeTrailMesh, no need to do anything more then default UV2 calculation
  2003. PackedVector3Array points;
  2004. PackedVector3Array normals;
  2005. PackedFloat32Array tangents;
  2006. PackedVector2Array uvs;
  2007. PackedInt32Array bone_indices;
  2008. PackedFloat32Array bone_weights;
  2009. PackedInt32Array indices;
  2010. int point = 0;
  2011. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  2012. tangents.push_back(m_x); \
  2013. tangents.push_back(m_y); \
  2014. tangents.push_back(m_z); \
  2015. tangents.push_back(m_d);
  2016. int thisrow = 0;
  2017. int prevrow = 0;
  2018. int total_rings = section_rings * sections;
  2019. float depth = section_length * sections;
  2020. for (int j = 0; j <= total_rings; j++) {
  2021. float v = j;
  2022. v /= total_rings;
  2023. float y = depth * v;
  2024. y = (depth * 0.5) - y;
  2025. int bone = j / section_rings;
  2026. float blend = 1.0 - float(j % section_rings) / float(section_rings);
  2027. for (int i = 0; i <= radial_steps; i++) {
  2028. float u = i;
  2029. u /= radial_steps;
  2030. float r = radius;
  2031. if (curve.is_valid() && curve->get_point_count() > 0) {
  2032. r *= curve->sample_baked(v);
  2033. }
  2034. float x = 0.0;
  2035. float z = 1.0;
  2036. if (i < radial_steps) {
  2037. x = Math::sin(u * Math_TAU);
  2038. z = Math::cos(u * Math_TAU);
  2039. }
  2040. Vector3 p = Vector3(x * r, y, z * r);
  2041. points.push_back(p);
  2042. normals.push_back(Vector3(x, 0, z));
  2043. ADD_TANGENT(z, 0.0, -x, 1.0)
  2044. uvs.push_back(Vector2(u, v * 0.5));
  2045. point++;
  2046. {
  2047. bone_indices.push_back(bone);
  2048. bone_indices.push_back(MIN(sections, bone + 1));
  2049. bone_indices.push_back(0);
  2050. bone_indices.push_back(0);
  2051. bone_weights.push_back(blend);
  2052. bone_weights.push_back(1.0 - blend);
  2053. bone_weights.push_back(0);
  2054. bone_weights.push_back(0);
  2055. }
  2056. if (i > 0 && j > 0) {
  2057. indices.push_back(prevrow + i - 1);
  2058. indices.push_back(prevrow + i);
  2059. indices.push_back(thisrow + i - 1);
  2060. indices.push_back(prevrow + i);
  2061. indices.push_back(thisrow + i);
  2062. indices.push_back(thisrow + i - 1);
  2063. }
  2064. }
  2065. prevrow = thisrow;
  2066. thisrow = point;
  2067. }
  2068. if (cap_top) {
  2069. // add top
  2070. float scale_pos = 1.0;
  2071. if (curve.is_valid() && curve->get_point_count() > 0) {
  2072. scale_pos = curve->sample_baked(0);
  2073. }
  2074. if (scale_pos > CMP_EPSILON) {
  2075. float y = depth * 0.5;
  2076. thisrow = point;
  2077. points.push_back(Vector3(0.0, y, 0));
  2078. normals.push_back(Vector3(0.0, 1.0, 0.0));
  2079. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  2080. uvs.push_back(Vector2(0.25, 0.75));
  2081. point++;
  2082. bone_indices.push_back(0);
  2083. bone_indices.push_back(0);
  2084. bone_indices.push_back(0);
  2085. bone_indices.push_back(0);
  2086. bone_weights.push_back(1.0);
  2087. bone_weights.push_back(0);
  2088. bone_weights.push_back(0);
  2089. bone_weights.push_back(0);
  2090. float rm = radius * scale_pos;
  2091. for (int i = 0; i <= radial_steps; i++) {
  2092. float r = i;
  2093. r /= radial_steps;
  2094. float x = 0.0;
  2095. float z = 1.0;
  2096. if (i < radial_steps) {
  2097. x = Math::sin(r * Math_TAU);
  2098. z = Math::cos(r * Math_TAU);
  2099. }
  2100. float u = ((x + 1.0) * 0.25);
  2101. float v = 0.5 + ((z + 1.0) * 0.25);
  2102. Vector3 p = Vector3(x * rm, y, z * rm);
  2103. points.push_back(p);
  2104. normals.push_back(Vector3(0.0, 1.0, 0.0));
  2105. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  2106. uvs.push_back(Vector2(u, v));
  2107. point++;
  2108. bone_indices.push_back(0);
  2109. bone_indices.push_back(0);
  2110. bone_indices.push_back(0);
  2111. bone_indices.push_back(0);
  2112. bone_weights.push_back(1.0);
  2113. bone_weights.push_back(0);
  2114. bone_weights.push_back(0);
  2115. bone_weights.push_back(0);
  2116. if (i > 0) {
  2117. indices.push_back(thisrow);
  2118. indices.push_back(point - 1);
  2119. indices.push_back(point - 2);
  2120. }
  2121. }
  2122. }
  2123. }
  2124. if (cap_bottom) {
  2125. float scale_neg = 1.0;
  2126. if (curve.is_valid() && curve->get_point_count() > 0) {
  2127. scale_neg = curve->sample_baked(1.0);
  2128. }
  2129. if (scale_neg > CMP_EPSILON) {
  2130. // add bottom
  2131. float y = depth * -0.5;
  2132. thisrow = point;
  2133. points.push_back(Vector3(0.0, y, 0.0));
  2134. normals.push_back(Vector3(0.0, -1.0, 0.0));
  2135. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  2136. uvs.push_back(Vector2(0.75, 0.75));
  2137. point++;
  2138. bone_indices.push_back(sections);
  2139. bone_indices.push_back(0);
  2140. bone_indices.push_back(0);
  2141. bone_indices.push_back(0);
  2142. bone_weights.push_back(1.0);
  2143. bone_weights.push_back(0);
  2144. bone_weights.push_back(0);
  2145. bone_weights.push_back(0);
  2146. float rm = radius * scale_neg;
  2147. for (int i = 0; i <= radial_steps; i++) {
  2148. float r = i;
  2149. r /= radial_steps;
  2150. float x = 0.0;
  2151. float z = 1.0;
  2152. if (i < radial_steps) {
  2153. x = Math::sin(r * Math_TAU);
  2154. z = Math::cos(r * Math_TAU);
  2155. }
  2156. float u = 0.5 + ((x + 1.0) * 0.25);
  2157. float v = 1.0 - ((z + 1.0) * 0.25);
  2158. Vector3 p = Vector3(x * rm, y, z * rm);
  2159. points.push_back(p);
  2160. normals.push_back(Vector3(0.0, -1.0, 0.0));
  2161. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  2162. uvs.push_back(Vector2(u, v));
  2163. point++;
  2164. bone_indices.push_back(sections);
  2165. bone_indices.push_back(0);
  2166. bone_indices.push_back(0);
  2167. bone_indices.push_back(0);
  2168. bone_weights.push_back(1.0);
  2169. bone_weights.push_back(0);
  2170. bone_weights.push_back(0);
  2171. bone_weights.push_back(0);
  2172. if (i > 0) {
  2173. indices.push_back(thisrow);
  2174. indices.push_back(point - 2);
  2175. indices.push_back(point - 1);
  2176. }
  2177. }
  2178. }
  2179. }
  2180. p_arr[RS::ARRAY_VERTEX] = points;
  2181. p_arr[RS::ARRAY_NORMAL] = normals;
  2182. p_arr[RS::ARRAY_TANGENT] = tangents;
  2183. p_arr[RS::ARRAY_TEX_UV] = uvs;
  2184. p_arr[RS::ARRAY_BONES] = bone_indices;
  2185. p_arr[RS::ARRAY_WEIGHTS] = bone_weights;
  2186. p_arr[RS::ARRAY_INDEX] = indices;
  2187. }
  2188. void TubeTrailMesh::_bind_methods() {
  2189. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &TubeTrailMesh::set_radius);
  2190. ClassDB::bind_method(D_METHOD("get_radius"), &TubeTrailMesh::get_radius);
  2191. ClassDB::bind_method(D_METHOD("set_radial_steps", "radial_steps"), &TubeTrailMesh::set_radial_steps);
  2192. ClassDB::bind_method(D_METHOD("get_radial_steps"), &TubeTrailMesh::get_radial_steps);
  2193. ClassDB::bind_method(D_METHOD("set_sections", "sections"), &TubeTrailMesh::set_sections);
  2194. ClassDB::bind_method(D_METHOD("get_sections"), &TubeTrailMesh::get_sections);
  2195. ClassDB::bind_method(D_METHOD("set_section_length", "section_length"), &TubeTrailMesh::set_section_length);
  2196. ClassDB::bind_method(D_METHOD("get_section_length"), &TubeTrailMesh::get_section_length);
  2197. ClassDB::bind_method(D_METHOD("set_section_rings", "section_rings"), &TubeTrailMesh::set_section_rings);
  2198. ClassDB::bind_method(D_METHOD("get_section_rings"), &TubeTrailMesh::get_section_rings);
  2199. ClassDB::bind_method(D_METHOD("set_cap_top", "cap_top"), &TubeTrailMesh::set_cap_top);
  2200. ClassDB::bind_method(D_METHOD("is_cap_top"), &TubeTrailMesh::is_cap_top);
  2201. ClassDB::bind_method(D_METHOD("set_cap_bottom", "cap_bottom"), &TubeTrailMesh::set_cap_bottom);
  2202. ClassDB::bind_method(D_METHOD("is_cap_bottom"), &TubeTrailMesh::is_cap_bottom);
  2203. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &TubeTrailMesh::set_curve);
  2204. ClassDB::bind_method(D_METHOD("get_curve"), &TubeTrailMesh::get_curve);
  2205. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  2206. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_steps", PROPERTY_HINT_RANGE, "3,128,1"), "set_radial_steps", "get_radial_steps");
  2207. ADD_PROPERTY(PropertyInfo(Variant::INT, "sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_sections", "get_sections");
  2208. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "section_length", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001,or_greater,suffix:m"), "set_section_length", "get_section_length");
  2209. ADD_PROPERTY(PropertyInfo(Variant::INT, "section_rings", PROPERTY_HINT_RANGE, "1,128,1"), "set_section_rings", "get_section_rings");
  2210. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cap_top"), "set_cap_top", "is_cap_top");
  2211. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cap_bottom"), "set_cap_bottom", "is_cap_bottom");
  2212. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  2213. }
  2214. TubeTrailMesh::TubeTrailMesh() {
  2215. }
  2216. // RIBBON TRAIL
  2217. void RibbonTrailMesh::set_shape(Shape p_shape) {
  2218. if (p_shape == shape) {
  2219. return;
  2220. }
  2221. shape = p_shape;
  2222. request_update();
  2223. }
  2224. RibbonTrailMesh::Shape RibbonTrailMesh::get_shape() const {
  2225. return shape;
  2226. }
  2227. void RibbonTrailMesh::set_size(const float p_size) {
  2228. if (Math::is_equal_approx(p_size, size)) {
  2229. return;
  2230. }
  2231. size = p_size;
  2232. request_update();
  2233. }
  2234. float RibbonTrailMesh::get_size() const {
  2235. return size;
  2236. }
  2237. void RibbonTrailMesh::set_sections(const int p_sections) {
  2238. if (p_sections == sections) {
  2239. return;
  2240. }
  2241. ERR_FAIL_COND(p_sections < 2 || p_sections > 128);
  2242. sections = p_sections;
  2243. request_update();
  2244. }
  2245. int RibbonTrailMesh::get_sections() const {
  2246. return sections;
  2247. }
  2248. void RibbonTrailMesh::set_section_length(float p_section_length) {
  2249. if (p_section_length == section_length) {
  2250. return;
  2251. }
  2252. section_length = p_section_length;
  2253. request_update();
  2254. }
  2255. float RibbonTrailMesh::get_section_length() const {
  2256. return section_length;
  2257. }
  2258. void RibbonTrailMesh::set_section_segments(const int p_section_segments) {
  2259. if (p_section_segments == section_segments) {
  2260. return;
  2261. }
  2262. ERR_FAIL_COND(p_section_segments < 1 || p_section_segments > 1024);
  2263. section_segments = p_section_segments;
  2264. request_update();
  2265. }
  2266. int RibbonTrailMesh::get_section_segments() const {
  2267. return section_segments;
  2268. }
  2269. void RibbonTrailMesh::set_curve(const Ref<Curve> &p_curve) {
  2270. if (curve == p_curve) {
  2271. return;
  2272. }
  2273. if (curve.is_valid()) {
  2274. curve->disconnect_changed(callable_mp(this, &RibbonTrailMesh::_curve_changed));
  2275. }
  2276. curve = p_curve;
  2277. if (curve.is_valid()) {
  2278. curve->connect_changed(callable_mp(this, &RibbonTrailMesh::_curve_changed));
  2279. }
  2280. request_update();
  2281. }
  2282. Ref<Curve> RibbonTrailMesh::get_curve() const {
  2283. return curve;
  2284. }
  2285. void RibbonTrailMesh::_curve_changed() {
  2286. request_update();
  2287. }
  2288. int RibbonTrailMesh::get_builtin_bind_pose_count() const {
  2289. return sections + 1;
  2290. }
  2291. Transform3D RibbonTrailMesh::get_builtin_bind_pose(int p_index) const {
  2292. float depth = section_length * sections;
  2293. Transform3D xform;
  2294. xform.origin.y = depth / 2.0 - section_length * float(p_index);
  2295. xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y
  2296. return xform;
  2297. }
  2298. void RibbonTrailMesh::_create_mesh_array(Array &p_arr) const {
  2299. // Seeing use case of ribbon trail mesh, no need to implement special UV2 calculation
  2300. PackedVector3Array points;
  2301. PackedVector3Array normals;
  2302. PackedFloat32Array tangents;
  2303. PackedVector2Array uvs;
  2304. PackedInt32Array bone_indices;
  2305. PackedFloat32Array bone_weights;
  2306. PackedInt32Array indices;
  2307. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  2308. tangents.push_back(m_x); \
  2309. tangents.push_back(m_y); \
  2310. tangents.push_back(m_z); \
  2311. tangents.push_back(m_d);
  2312. int total_segments = section_segments * sections;
  2313. float depth = section_length * sections;
  2314. for (int j = 0; j <= total_segments; j++) {
  2315. float v = j;
  2316. v /= total_segments;
  2317. float y = depth * v;
  2318. y = (depth * 0.5) - y;
  2319. int bone = j / section_segments;
  2320. float blend = 1.0 - float(j % section_segments) / float(section_segments);
  2321. float s = size;
  2322. if (curve.is_valid() && curve->get_point_count() > 0) {
  2323. s *= curve->sample_baked(v);
  2324. }
  2325. points.push_back(Vector3(-s * 0.5, y, 0));
  2326. points.push_back(Vector3(+s * 0.5, y, 0));
  2327. if (shape == SHAPE_CROSS) {
  2328. points.push_back(Vector3(0, y, -s * 0.5));
  2329. points.push_back(Vector3(0, y, +s * 0.5));
  2330. }
  2331. normals.push_back(Vector3(0, 0, 1));
  2332. normals.push_back(Vector3(0, 0, 1));
  2333. if (shape == SHAPE_CROSS) {
  2334. normals.push_back(Vector3(1, 0, 0));
  2335. normals.push_back(Vector3(1, 0, 0));
  2336. }
  2337. uvs.push_back(Vector2(0, v));
  2338. uvs.push_back(Vector2(1, v));
  2339. if (shape == SHAPE_CROSS) {
  2340. uvs.push_back(Vector2(0, v));
  2341. uvs.push_back(Vector2(1, v));
  2342. }
  2343. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  2344. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  2345. if (shape == SHAPE_CROSS) {
  2346. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  2347. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  2348. }
  2349. for (int i = 0; i < (shape == SHAPE_CROSS ? 4 : 2); i++) {
  2350. bone_indices.push_back(bone);
  2351. bone_indices.push_back(MIN(sections, bone + 1));
  2352. bone_indices.push_back(0);
  2353. bone_indices.push_back(0);
  2354. bone_weights.push_back(blend);
  2355. bone_weights.push_back(1.0 - blend);
  2356. bone_weights.push_back(0);
  2357. bone_weights.push_back(0);
  2358. }
  2359. if (j > 0) {
  2360. if (shape == SHAPE_CROSS) {
  2361. int base = j * 4 - 4;
  2362. indices.push_back(base + 0);
  2363. indices.push_back(base + 1);
  2364. indices.push_back(base + 4);
  2365. indices.push_back(base + 1);
  2366. indices.push_back(base + 5);
  2367. indices.push_back(base + 4);
  2368. indices.push_back(base + 2);
  2369. indices.push_back(base + 3);
  2370. indices.push_back(base + 6);
  2371. indices.push_back(base + 3);
  2372. indices.push_back(base + 7);
  2373. indices.push_back(base + 6);
  2374. } else {
  2375. int base = j * 2 - 2;
  2376. indices.push_back(base + 0);
  2377. indices.push_back(base + 1);
  2378. indices.push_back(base + 2);
  2379. indices.push_back(base + 1);
  2380. indices.push_back(base + 3);
  2381. indices.push_back(base + 2);
  2382. }
  2383. }
  2384. }
  2385. p_arr[RS::ARRAY_VERTEX] = points;
  2386. p_arr[RS::ARRAY_NORMAL] = normals;
  2387. p_arr[RS::ARRAY_TANGENT] = tangents;
  2388. p_arr[RS::ARRAY_TEX_UV] = uvs;
  2389. p_arr[RS::ARRAY_BONES] = bone_indices;
  2390. p_arr[RS::ARRAY_WEIGHTS] = bone_weights;
  2391. p_arr[RS::ARRAY_INDEX] = indices;
  2392. }
  2393. void RibbonTrailMesh::_bind_methods() {
  2394. ClassDB::bind_method(D_METHOD("set_size", "size"), &RibbonTrailMesh::set_size);
  2395. ClassDB::bind_method(D_METHOD("get_size"), &RibbonTrailMesh::get_size);
  2396. ClassDB::bind_method(D_METHOD("set_sections", "sections"), &RibbonTrailMesh::set_sections);
  2397. ClassDB::bind_method(D_METHOD("get_sections"), &RibbonTrailMesh::get_sections);
  2398. ClassDB::bind_method(D_METHOD("set_section_length", "section_length"), &RibbonTrailMesh::set_section_length);
  2399. ClassDB::bind_method(D_METHOD("get_section_length"), &RibbonTrailMesh::get_section_length);
  2400. ClassDB::bind_method(D_METHOD("set_section_segments", "section_segments"), &RibbonTrailMesh::set_section_segments);
  2401. ClassDB::bind_method(D_METHOD("get_section_segments"), &RibbonTrailMesh::get_section_segments);
  2402. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &RibbonTrailMesh::set_curve);
  2403. ClassDB::bind_method(D_METHOD("get_curve"), &RibbonTrailMesh::get_curve);
  2404. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &RibbonTrailMesh::set_shape);
  2405. ClassDB::bind_method(D_METHOD("get_shape"), &RibbonTrailMesh::get_shape);
  2406. ADD_PROPERTY(PropertyInfo(Variant::INT, "shape", PROPERTY_HINT_ENUM, "Flat,Cross"), "set_shape", "get_shape");
  2407. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_size", "get_size");
  2408. ADD_PROPERTY(PropertyInfo(Variant::INT, "sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_sections", "get_sections");
  2409. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "section_length", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001,or_greater,suffix:m"), "set_section_length", "get_section_length");
  2410. ADD_PROPERTY(PropertyInfo(Variant::INT, "section_segments", PROPERTY_HINT_RANGE, "1,128,1"), "set_section_segments", "get_section_segments");
  2411. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  2412. BIND_ENUM_CONSTANT(SHAPE_FLAT)
  2413. BIND_ENUM_CONSTANT(SHAPE_CROSS)
  2414. }
  2415. RibbonTrailMesh::RibbonTrailMesh() {
  2416. }
  2417. /*************************************************************************/
  2418. /* TextMesh */
  2419. /*************************************************************************/
  2420. void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph &p_gl) const {
  2421. if (cache.has(p_key)) {
  2422. return;
  2423. }
  2424. GlyphMeshData &gl_data = cache[p_key];
  2425. Dictionary d = TS->font_get_glyph_contours(p_gl.font_rid, p_gl.font_size, p_gl.index);
  2426. PackedVector3Array points = d["points"];
  2427. PackedInt32Array contours = d["contours"];
  2428. bool orientation = d["orientation"];
  2429. if (points.size() < 3 || contours.size() < 1) {
  2430. return; // No full contours, only glyph control points (or nothing), ignore.
  2431. }
  2432. // Approximate Bezier curves as polygons.
  2433. // See https://freetype.org/freetype2/docs/glyphs/glyphs-6.html, for more info.
  2434. for (int i = 0; i < contours.size(); i++) {
  2435. int32_t start = (i == 0) ? 0 : (contours[i - 1] + 1);
  2436. int32_t end = contours[i];
  2437. Vector<ContourPoint> polygon;
  2438. for (int32_t j = start; j <= end; j++) {
  2439. if (points[j].z == TextServer::CONTOUR_CURVE_TAG_ON) {
  2440. // Point on the curve.
  2441. Vector2 p = Vector2(points[j].x, points[j].y) * pixel_size;
  2442. polygon.push_back(ContourPoint(p, true));
  2443. } else if (points[j].z == TextServer::CONTOUR_CURVE_TAG_OFF_CONIC) {
  2444. // Conic Bezier arc.
  2445. int32_t next = (j == end) ? start : (j + 1);
  2446. int32_t prev = (j == start) ? end : (j - 1);
  2447. Vector2 p0;
  2448. Vector2 p1 = Vector2(points[j].x, points[j].y);
  2449. Vector2 p2;
  2450. // For successive conic OFF points add a virtual ON point in the middle.
  2451. if (points[prev].z == TextServer::CONTOUR_CURVE_TAG_OFF_CONIC) {
  2452. p0 = (Vector2(points[prev].x, points[prev].y) + Vector2(points[j].x, points[j].y)) / 2.0;
  2453. } else if (points[prev].z == TextServer::CONTOUR_CURVE_TAG_ON) {
  2454. p0 = Vector2(points[prev].x, points[prev].y);
  2455. } else {
  2456. ERR_FAIL_MSG(vformat("Invalid conic arc point sequence at %d:%d", i, j));
  2457. }
  2458. if (points[next].z == TextServer::CONTOUR_CURVE_TAG_OFF_CONIC) {
  2459. p2 = (Vector2(points[j].x, points[j].y) + Vector2(points[next].x, points[next].y)) / 2.0;
  2460. } else if (points[next].z == TextServer::CONTOUR_CURVE_TAG_ON) {
  2461. p2 = Vector2(points[next].x, points[next].y);
  2462. } else {
  2463. ERR_FAIL_MSG(vformat("Invalid conic arc point sequence at %d:%d", i, j));
  2464. }
  2465. real_t step = CLAMP(curve_step / (p0 - p2).length(), 0.01, 0.5);
  2466. real_t t = step;
  2467. while (t < 1.0) {
  2468. real_t omt = (1.0 - t);
  2469. real_t omt2 = omt * omt;
  2470. real_t t2 = t * t;
  2471. Vector2 point = p1 + omt2 * (p0 - p1) + t2 * (p2 - p1);
  2472. Vector2 p = point * pixel_size;
  2473. polygon.push_back(ContourPoint(p, false));
  2474. t += step;
  2475. }
  2476. } else if (points[j].z == TextServer::CONTOUR_CURVE_TAG_OFF_CUBIC) {
  2477. // Cubic Bezier arc.
  2478. int32_t cur = j;
  2479. int32_t next1 = (j == end) ? start : (j + 1);
  2480. int32_t next2 = (next1 == end) ? start : (next1 + 1);
  2481. int32_t prev = (j == start) ? end : (j - 1);
  2482. // There must be exactly two OFF points and two ON points for each cubic arc.
  2483. if (points[prev].z != TextServer::CONTOUR_CURVE_TAG_ON) {
  2484. cur = (cur == 0) ? end : cur - 1;
  2485. next1 = (next1 == 0) ? end : next1 - 1;
  2486. next2 = (next2 == 0) ? end : next2 - 1;
  2487. prev = (prev == 0) ? end : prev - 1;
  2488. } else {
  2489. j++;
  2490. }
  2491. ERR_FAIL_COND_MSG(points[prev].z != TextServer::CONTOUR_CURVE_TAG_ON, vformat("Invalid cubic arc point sequence at %d:%d", i, prev));
  2492. ERR_FAIL_COND_MSG(points[cur].z != TextServer::CONTOUR_CURVE_TAG_OFF_CUBIC, vformat("Invalid cubic arc point sequence at %d:%d", i, cur));
  2493. ERR_FAIL_COND_MSG(points[next1].z != TextServer::CONTOUR_CURVE_TAG_OFF_CUBIC, vformat("Invalid cubic arc point sequence at %d:%d", i, next1));
  2494. ERR_FAIL_COND_MSG(points[next2].z != TextServer::CONTOUR_CURVE_TAG_ON, vformat("Invalid cubic arc point sequence at %d:%d", i, next2));
  2495. Vector2 p0 = Vector2(points[prev].x, points[prev].y);
  2496. Vector2 p1 = Vector2(points[cur].x, points[cur].y);
  2497. Vector2 p2 = Vector2(points[next1].x, points[next1].y);
  2498. Vector2 p3 = Vector2(points[next2].x, points[next2].y);
  2499. real_t step = CLAMP(curve_step / (p0 - p3).length(), 0.01, 0.5);
  2500. real_t t = step;
  2501. while (t < 1.0) {
  2502. Vector2 point = p0.bezier_interpolate(p1, p2, p3, t);
  2503. Vector2 p = point * pixel_size;
  2504. polygon.push_back(ContourPoint(p, false));
  2505. t += step;
  2506. }
  2507. } else {
  2508. ERR_FAIL_MSG(vformat("Unknown point tag at %d:%d", i, j));
  2509. }
  2510. }
  2511. if (polygon.size() < 3) {
  2512. continue; // Skip glyph control points.
  2513. }
  2514. if (!orientation) {
  2515. polygon.reverse();
  2516. }
  2517. gl_data.contours.push_back(polygon);
  2518. }
  2519. // Calculate bounds.
  2520. List<TPPLPoly> in_poly;
  2521. for (int i = 0; i < gl_data.contours.size(); i++) {
  2522. TPPLPoly inp;
  2523. inp.Init(gl_data.contours[i].size());
  2524. real_t length = 0.0;
  2525. for (int j = 0; j < gl_data.contours[i].size(); j++) {
  2526. int next = (j + 1 == gl_data.contours[i].size()) ? 0 : (j + 1);
  2527. gl_data.min_p = gl_data.min_p.min(gl_data.contours[i][j].point);
  2528. gl_data.max_p = gl_data.max_p.max(gl_data.contours[i][j].point);
  2529. length += (gl_data.contours[i][next].point - gl_data.contours[i][j].point).length();
  2530. inp.GetPoint(j) = gl_data.contours[i][j].point;
  2531. }
  2532. TPPLOrientation poly_orient = inp.GetOrientation();
  2533. if (poly_orient == TPPL_ORIENTATION_CW) {
  2534. inp.SetHole(true);
  2535. }
  2536. in_poly.push_back(inp);
  2537. gl_data.contours_info.push_back(ContourInfo(length, poly_orient == TPPL_ORIENTATION_CCW));
  2538. }
  2539. TPPLPartition tpart;
  2540. //Decompose and triangulate.
  2541. List<TPPLPoly> out_poly;
  2542. if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) {
  2543. ERR_FAIL_MSG("Convex decomposing failed. Make sure the font doesn't contain self-intersecting lines, as these are not supported in TextMesh.");
  2544. }
  2545. List<TPPLPoly> out_tris;
  2546. for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
  2547. if (tpart.Triangulate_OPT(&(I->get()), &out_tris) == 0) {
  2548. ERR_FAIL_MSG("Triangulation failed. Make sure the font doesn't contain self-intersecting lines, as these are not supported in TextMesh.");
  2549. }
  2550. }
  2551. for (List<TPPLPoly>::Element *I = out_tris.front(); I; I = I->next()) {
  2552. TPPLPoly &tp = I->get();
  2553. ERR_FAIL_COND(tp.GetNumPoints() != 3); // Triangles only.
  2554. for (int i = 0; i < 3; i++) {
  2555. gl_data.triangles.push_back(Vector2(tp.GetPoint(i).x, tp.GetPoint(i).y));
  2556. }
  2557. }
  2558. }
  2559. void TextMesh::_create_mesh_array(Array &p_arr) const {
  2560. Ref<Font> font = _get_font_or_default();
  2561. ERR_FAIL_COND(font.is_null());
  2562. if (dirty_cache) {
  2563. cache.clear();
  2564. dirty_cache = false;
  2565. }
  2566. // When a shaped text is invalidated by an external source, we want to reshape it.
  2567. if (!TS->shaped_text_is_ready(text_rid)) {
  2568. dirty_text = true;
  2569. }
  2570. for (const RID &line_rid : lines_rid) {
  2571. if (!TS->shaped_text_is_ready(line_rid)) {
  2572. dirty_lines = true;
  2573. break;
  2574. }
  2575. }
  2576. // Update text buffer.
  2577. if (dirty_text) {
  2578. TS->shaped_text_clear(text_rid);
  2579. TS->shaped_text_set_direction(text_rid, text_direction);
  2580. String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
  2581. TS->shaped_text_add_string(text_rid, txt, font->get_rids(), font_size, font->get_opentype_features(), language);
  2582. TypedArray<Vector3i> stt;
  2583. if (st_parser == TextServer::STRUCTURED_TEXT_CUSTOM) {
  2584. GDVIRTUAL_CALL(_structured_text_parser, st_args, txt, stt);
  2585. } else {
  2586. stt = TS->parse_structured_text(st_parser, st_args, txt);
  2587. }
  2588. TS->shaped_text_set_bidi_override(text_rid, stt);
  2589. dirty_text = false;
  2590. dirty_font = false;
  2591. dirty_lines = true;
  2592. } else if (dirty_font) {
  2593. int spans = TS->shaped_get_span_count(text_rid);
  2594. for (int i = 0; i < spans; i++) {
  2595. TS->shaped_set_span_update_font(text_rid, i, font->get_rids(), font_size, font->get_opentype_features());
  2596. }
  2597. dirty_font = false;
  2598. dirty_lines = true;
  2599. }
  2600. if (dirty_lines) {
  2601. for (int i = 0; i < lines_rid.size(); i++) {
  2602. TS->free_rid(lines_rid[i]);
  2603. }
  2604. lines_rid.clear();
  2605. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  2606. switch (autowrap_mode) {
  2607. case TextServer::AUTOWRAP_WORD_SMART:
  2608. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  2609. break;
  2610. case TextServer::AUTOWRAP_WORD:
  2611. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  2612. break;
  2613. case TextServer::AUTOWRAP_ARBITRARY:
  2614. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  2615. break;
  2616. case TextServer::AUTOWRAP_OFF:
  2617. break;
  2618. }
  2619. PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(text_rid, width, 0, autowrap_flags);
  2620. float max_line_w = 0.0;
  2621. for (int i = 0; i < line_breaks.size(); i = i + 2) {
  2622. RID line = TS->shaped_text_substr(text_rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
  2623. max_line_w = MAX(max_line_w, TS->shaped_text_get_width(line));
  2624. lines_rid.push_back(line);
  2625. }
  2626. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  2627. int jst_to_line = lines_rid.size();
  2628. if (lines_rid.size() == 1 && jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  2629. jst_to_line = lines_rid.size();
  2630. } else {
  2631. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  2632. jst_to_line = lines_rid.size() - 1;
  2633. }
  2634. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  2635. for (int i = lines_rid.size() - 1; i >= 0; i--) {
  2636. if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
  2637. jst_to_line = i;
  2638. break;
  2639. }
  2640. }
  2641. }
  2642. }
  2643. for (int i = 0; i < jst_to_line; i++) {
  2644. TS->shaped_text_fit_to_width(lines_rid[i], (width > 0) ? width : max_line_w, jst_flags);
  2645. }
  2646. }
  2647. dirty_lines = false;
  2648. }
  2649. float total_h = 0.0;
  2650. for (int i = 0; i < lines_rid.size(); i++) {
  2651. total_h += (TS->shaped_text_get_size(lines_rid[i]).y + line_spacing) * pixel_size;
  2652. }
  2653. float vbegin = 0.0;
  2654. switch (vertical_alignment) {
  2655. case VERTICAL_ALIGNMENT_FILL:
  2656. case VERTICAL_ALIGNMENT_TOP: {
  2657. // Nothing.
  2658. } break;
  2659. case VERTICAL_ALIGNMENT_CENTER: {
  2660. vbegin = (total_h - line_spacing * pixel_size) / 2.0;
  2661. } break;
  2662. case VERTICAL_ALIGNMENT_BOTTOM: {
  2663. vbegin = (total_h - line_spacing * pixel_size);
  2664. } break;
  2665. }
  2666. Vector<Vector3> vertices;
  2667. Vector<Vector3> normals;
  2668. Vector<float> tangents;
  2669. Vector<Vector2> uvs;
  2670. Vector<int32_t> indices;
  2671. Vector2 min_p = Vector2(INFINITY, INFINITY);
  2672. Vector2 max_p = Vector2(-INFINITY, -INFINITY);
  2673. int32_t p_size = 0;
  2674. int32_t i_size = 0;
  2675. Vector2 offset = Vector2(0, vbegin + lbl_offset.y * pixel_size);
  2676. for (int i = 0; i < lines_rid.size(); i++) {
  2677. const Glyph *glyphs = TS->shaped_text_get_glyphs(lines_rid[i]);
  2678. int gl_size = TS->shaped_text_get_glyph_count(lines_rid[i]);
  2679. float line_width = TS->shaped_text_get_width(lines_rid[i]) * pixel_size;
  2680. switch (horizontal_alignment) {
  2681. case HORIZONTAL_ALIGNMENT_LEFT:
  2682. offset.x = 0.0;
  2683. break;
  2684. case HORIZONTAL_ALIGNMENT_FILL:
  2685. case HORIZONTAL_ALIGNMENT_CENTER: {
  2686. offset.x = -line_width / 2.0;
  2687. } break;
  2688. case HORIZONTAL_ALIGNMENT_RIGHT: {
  2689. offset.x = -line_width;
  2690. } break;
  2691. }
  2692. offset.x += lbl_offset.x * pixel_size;
  2693. offset.y -= TS->shaped_text_get_ascent(lines_rid[i]) * pixel_size;
  2694. bool has_depth = !Math::is_zero_approx(depth);
  2695. for (int j = 0; j < gl_size; j++) {
  2696. if (glyphs[j].index == 0) {
  2697. offset.x += glyphs[j].advance * pixel_size * glyphs[j].repeat;
  2698. continue;
  2699. }
  2700. if (glyphs[j].font_rid != RID()) {
  2701. GlyphMeshKey key = GlyphMeshKey(glyphs[j].font_rid.get_id(), glyphs[j].index);
  2702. _generate_glyph_mesh_data(key, glyphs[j]);
  2703. GlyphMeshData &gl_data = cache[key];
  2704. const Vector2 gl_of = Vector2(glyphs[j].x_off, glyphs[j].y_off) * pixel_size;
  2705. p_size += glyphs[j].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1);
  2706. i_size += glyphs[j].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1);
  2707. if (has_depth) {
  2708. for (int k = 0; k < gl_data.contours.size(); k++) {
  2709. p_size += glyphs[j].repeat * gl_data.contours[k].size() * 4;
  2710. i_size += glyphs[j].repeat * gl_data.contours[k].size() * 6;
  2711. }
  2712. }
  2713. for (int r = 0; r < glyphs[j].repeat; r++) {
  2714. min_p.x = MIN(gl_data.min_p.x + offset.x + gl_of.x, min_p.x);
  2715. min_p.y = MIN(gl_data.min_p.y - offset.y + gl_of.y, min_p.y);
  2716. max_p.x = MAX(gl_data.max_p.x + offset.x + gl_of.x, max_p.x);
  2717. max_p.y = MAX(gl_data.max_p.y - offset.y + gl_of.y, max_p.y);
  2718. offset.x += glyphs[j].advance * pixel_size;
  2719. }
  2720. } else {
  2721. p_size += glyphs[j].repeat * 4;
  2722. i_size += glyphs[j].repeat * 6;
  2723. offset.x += glyphs[j].advance * pixel_size * glyphs[j].repeat;
  2724. }
  2725. }
  2726. offset.y -= (TS->shaped_text_get_descent(lines_rid[i]) + line_spacing) * pixel_size;
  2727. }
  2728. vertices.resize(p_size);
  2729. normals.resize(p_size);
  2730. uvs.resize(p_size);
  2731. tangents.resize(p_size * 4);
  2732. indices.resize(i_size);
  2733. Vector3 *vertices_ptr = vertices.ptrw();
  2734. Vector3 *normals_ptr = normals.ptrw();
  2735. float *tangents_ptr = tangents.ptrw();
  2736. Vector2 *uvs_ptr = uvs.ptrw();
  2737. int32_t *indices_ptr = indices.ptrw();
  2738. // Generate mesh.
  2739. int32_t p_idx = 0;
  2740. int32_t i_idx = 0;
  2741. offset = Vector2(0, vbegin + lbl_offset.y * pixel_size);
  2742. for (int i = 0; i < lines_rid.size(); i++) {
  2743. const Glyph *glyphs = TS->shaped_text_get_glyphs(lines_rid[i]);
  2744. int gl_size = TS->shaped_text_get_glyph_count(lines_rid[i]);
  2745. float line_width = TS->shaped_text_get_width(lines_rid[i]) * pixel_size;
  2746. switch (horizontal_alignment) {
  2747. case HORIZONTAL_ALIGNMENT_LEFT:
  2748. offset.x = 0.0;
  2749. break;
  2750. case HORIZONTAL_ALIGNMENT_FILL:
  2751. case HORIZONTAL_ALIGNMENT_CENTER: {
  2752. offset.x = -line_width / 2.0;
  2753. } break;
  2754. case HORIZONTAL_ALIGNMENT_RIGHT: {
  2755. offset.x = -line_width;
  2756. } break;
  2757. }
  2758. offset.x += lbl_offset.x * pixel_size;
  2759. offset.y -= TS->shaped_text_get_ascent(lines_rid[i]) * pixel_size;
  2760. bool has_depth = !Math::is_zero_approx(depth);
  2761. // Generate glyph data, precalculate size of the arrays and mesh bounds for UV.
  2762. for (int j = 0; j < gl_size; j++) {
  2763. if (glyphs[j].index == 0) {
  2764. offset.x += glyphs[j].advance * pixel_size * glyphs[j].repeat;
  2765. continue;
  2766. }
  2767. if (glyphs[j].font_rid != RID()) {
  2768. GlyphMeshKey key = GlyphMeshKey(glyphs[j].font_rid.get_id(), glyphs[j].index);
  2769. _generate_glyph_mesh_data(key, glyphs[j]);
  2770. const GlyphMeshData &gl_data = cache[key];
  2771. int64_t ts = gl_data.triangles.size();
  2772. const Vector2 *ts_ptr = gl_data.triangles.ptr();
  2773. const Vector2 gl_of = Vector2(glyphs[j].x_off, glyphs[j].y_off) * pixel_size;
  2774. for (int r = 0; r < glyphs[j].repeat; r++) {
  2775. for (int k = 0; k < ts; k += 3) {
  2776. // Add front face.
  2777. for (int l = 0; l < 3; l++) {
  2778. Vector3 point = Vector3(ts_ptr[k + l].x + offset.x + gl_of.x, -ts_ptr[k + l].y + offset.y - gl_of.y, depth / 2.0);
  2779. vertices_ptr[p_idx] = point;
  2780. normals_ptr[p_idx] = Vector3(0.0, 0.0, 1.0);
  2781. if (has_depth) {
  2782. uvs_ptr[p_idx] = Vector2(Math::remap(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::remap(point.y, -max_p.y, -min_p.y, real_t(0.4), real_t(0.0)));
  2783. } else {
  2784. uvs_ptr[p_idx] = Vector2(Math::remap(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::remap(point.y, -max_p.y, -min_p.y, real_t(1.0), real_t(0.0)));
  2785. }
  2786. tangents_ptr[p_idx * 4 + 0] = 1.0;
  2787. tangents_ptr[p_idx * 4 + 1] = 0.0;
  2788. tangents_ptr[p_idx * 4 + 2] = 0.0;
  2789. tangents_ptr[p_idx * 4 + 3] = 1.0;
  2790. indices_ptr[i_idx++] = p_idx;
  2791. p_idx++;
  2792. }
  2793. if (has_depth) {
  2794. // Add back face.
  2795. for (int l = 2; l >= 0; l--) {
  2796. Vector3 point = Vector3(ts_ptr[k + l].x + offset.x + gl_of.x, -ts_ptr[k + l].y + offset.y - gl_of.y, -depth / 2.0);
  2797. vertices_ptr[p_idx] = point;
  2798. normals_ptr[p_idx] = Vector3(0.0, 0.0, -1.0);
  2799. uvs_ptr[p_idx] = Vector2(Math::remap(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::remap(point.y, -max_p.y, -min_p.y, real_t(0.8), real_t(0.4)));
  2800. tangents_ptr[p_idx * 4 + 0] = -1.0;
  2801. tangents_ptr[p_idx * 4 + 1] = 0.0;
  2802. tangents_ptr[p_idx * 4 + 2] = 0.0;
  2803. tangents_ptr[p_idx * 4 + 3] = 1.0;
  2804. indices_ptr[i_idx++] = p_idx;
  2805. p_idx++;
  2806. }
  2807. }
  2808. }
  2809. // Add sides.
  2810. if (has_depth) {
  2811. for (int k = 0; k < gl_data.contours.size(); k++) {
  2812. int64_t ps = gl_data.contours[k].size();
  2813. const ContourPoint *ps_ptr = gl_data.contours[k].ptr();
  2814. const ContourInfo &ps_info = gl_data.contours_info[k];
  2815. real_t length = 0.0;
  2816. for (int l = 0; l < ps; l++) {
  2817. int prev = (l == 0) ? (ps - 1) : (l - 1);
  2818. int next = (l + 1 == ps) ? 0 : (l + 1);
  2819. Vector2 d1;
  2820. Vector2 d2 = (ps_ptr[next].point - ps_ptr[l].point).normalized();
  2821. if (ps_ptr[l].sharp) {
  2822. d1 = d2;
  2823. } else {
  2824. d1 = (ps_ptr[l].point - ps_ptr[prev].point).normalized();
  2825. }
  2826. real_t seg_len = (ps_ptr[next].point - ps_ptr[l].point).length();
  2827. Vector3 quad_faces[4] = {
  2828. Vector3(ps_ptr[l].point.x + offset.x + gl_of.x, -ps_ptr[l].point.y + offset.y - gl_of.y, -depth / 2.0),
  2829. Vector3(ps_ptr[next].point.x + offset.x + gl_of.x, -ps_ptr[next].point.y + offset.y - gl_of.y, -depth / 2.0),
  2830. Vector3(ps_ptr[l].point.x + offset.x + gl_of.x, -ps_ptr[l].point.y + offset.y - gl_of.y, depth / 2.0),
  2831. Vector3(ps_ptr[next].point.x + offset.x + gl_of.x, -ps_ptr[next].point.y + offset.y - gl_of.y, depth / 2.0),
  2832. };
  2833. for (int m = 0; m < 4; m++) {
  2834. const Vector2 &d = ((m % 2) == 0) ? d1 : d2;
  2835. real_t u_pos = ((m % 2) == 0) ? length : length + seg_len;
  2836. vertices_ptr[p_idx + m] = quad_faces[m];
  2837. normals_ptr[p_idx + m] = Vector3(d.y, d.x, 0.0);
  2838. if (m < 2) {
  2839. uvs_ptr[p_idx + m] = Vector2(Math::remap(u_pos, 0, ps_info.length, real_t(0.0), real_t(1.0)), (ps_info.ccw) ? 0.8 : 0.9);
  2840. } else {
  2841. uvs_ptr[p_idx + m] = Vector2(Math::remap(u_pos, 0, ps_info.length, real_t(0.0), real_t(1.0)), (ps_info.ccw) ? 0.9 : 1.0);
  2842. }
  2843. tangents_ptr[(p_idx + m) * 4 + 0] = d.x;
  2844. tangents_ptr[(p_idx + m) * 4 + 1] = -d.y;
  2845. tangents_ptr[(p_idx + m) * 4 + 2] = 0.0;
  2846. tangents_ptr[(p_idx + m) * 4 + 3] = 1.0;
  2847. }
  2848. indices_ptr[i_idx++] = p_idx;
  2849. indices_ptr[i_idx++] = p_idx + 1;
  2850. indices_ptr[i_idx++] = p_idx + 2;
  2851. indices_ptr[i_idx++] = p_idx + 1;
  2852. indices_ptr[i_idx++] = p_idx + 3;
  2853. indices_ptr[i_idx++] = p_idx + 2;
  2854. length += seg_len;
  2855. p_idx += 4;
  2856. }
  2857. }
  2858. }
  2859. offset.x += glyphs[j].advance * pixel_size;
  2860. }
  2861. } else {
  2862. // Add fallback quad for missing glyphs.
  2863. for (int r = 0; r < glyphs[j].repeat; r++) {
  2864. Size2 sz = TS->get_hex_code_box_size(glyphs[j].font_size, glyphs[j].index) * pixel_size;
  2865. Vector3 quad_faces[4] = {
  2866. Vector3(offset.x, offset.y, 0.0),
  2867. Vector3(offset.x, sz.y + offset.y, 0.0),
  2868. Vector3(sz.x + offset.x, sz.y + offset.y, 0.0),
  2869. Vector3(sz.x + offset.x, offset.y, 0.0),
  2870. };
  2871. for (int k = 0; k < 4; k++) {
  2872. vertices_ptr[p_idx + k] = quad_faces[k];
  2873. normals_ptr[p_idx + k] = Vector3(0.0, 0.0, 1.0);
  2874. if (has_depth) {
  2875. uvs_ptr[p_idx + k] = Vector2(Math::remap(quad_faces[k].x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::remap(quad_faces[k].y, -max_p.y, -min_p.y, real_t(0.4), real_t(0.0)));
  2876. } else {
  2877. uvs_ptr[p_idx + k] = Vector2(Math::remap(quad_faces[k].x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::remap(quad_faces[k].y, -max_p.y, -min_p.y, real_t(1.0), real_t(0.0)));
  2878. }
  2879. tangents_ptr[(p_idx + k) * 4 + 0] = 1.0;
  2880. tangents_ptr[(p_idx + k) * 4 + 1] = 0.0;
  2881. tangents_ptr[(p_idx + k) * 4 + 2] = 0.0;
  2882. tangents_ptr[(p_idx + k) * 4 + 3] = 1.0;
  2883. }
  2884. indices_ptr[i_idx++] = p_idx;
  2885. indices_ptr[i_idx++] = p_idx + 1;
  2886. indices_ptr[i_idx++] = p_idx + 2;
  2887. indices_ptr[i_idx++] = p_idx + 0;
  2888. indices_ptr[i_idx++] = p_idx + 2;
  2889. indices_ptr[i_idx++] = p_idx + 3;
  2890. p_idx += 4;
  2891. offset.x += glyphs[j].advance * pixel_size;
  2892. }
  2893. }
  2894. }
  2895. offset.y -= (TS->shaped_text_get_descent(lines_rid[i]) + line_spacing) * pixel_size;
  2896. }
  2897. if (indices.is_empty()) {
  2898. // If empty, add single triangle to suppress errors.
  2899. vertices.push_back(Vector3());
  2900. normals.push_back(Vector3());
  2901. uvs.push_back(Vector2());
  2902. tangents.push_back(1.0);
  2903. tangents.push_back(0.0);
  2904. tangents.push_back(0.0);
  2905. tangents.push_back(1.0);
  2906. indices.push_back(0);
  2907. indices.push_back(0);
  2908. indices.push_back(0);
  2909. }
  2910. p_arr[RS::ARRAY_VERTEX] = vertices;
  2911. p_arr[RS::ARRAY_NORMAL] = normals;
  2912. p_arr[RS::ARRAY_TANGENT] = tangents;
  2913. p_arr[RS::ARRAY_TEX_UV] = uvs;
  2914. p_arr[RS::ARRAY_INDEX] = indices;
  2915. }
  2916. void TextMesh::_bind_methods() {
  2917. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &TextMesh::set_horizontal_alignment);
  2918. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &TextMesh::get_horizontal_alignment);
  2919. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &TextMesh::set_vertical_alignment);
  2920. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &TextMesh::get_vertical_alignment);
  2921. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextMesh::set_text);
  2922. ClassDB::bind_method(D_METHOD("get_text"), &TextMesh::get_text);
  2923. ClassDB::bind_method(D_METHOD("set_font", "font"), &TextMesh::set_font);
  2924. ClassDB::bind_method(D_METHOD("get_font"), &TextMesh::get_font);
  2925. ClassDB::bind_method(D_METHOD("set_font_size", "font_size"), &TextMesh::set_font_size);
  2926. ClassDB::bind_method(D_METHOD("get_font_size"), &TextMesh::get_font_size);
  2927. ClassDB::bind_method(D_METHOD("set_line_spacing", "line_spacing"), &TextMesh::set_line_spacing);
  2928. ClassDB::bind_method(D_METHOD("get_line_spacing"), &TextMesh::get_line_spacing);
  2929. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &TextMesh::set_autowrap_mode);
  2930. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &TextMesh::get_autowrap_mode);
  2931. ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &TextMesh::set_justification_flags);
  2932. ClassDB::bind_method(D_METHOD("get_justification_flags"), &TextMesh::get_justification_flags);
  2933. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &TextMesh::set_depth);
  2934. ClassDB::bind_method(D_METHOD("get_depth"), &TextMesh::get_depth);
  2935. ClassDB::bind_method(D_METHOD("set_width", "width"), &TextMesh::set_width);
  2936. ClassDB::bind_method(D_METHOD("get_width"), &TextMesh::get_width);
  2937. ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &TextMesh::set_pixel_size);
  2938. ClassDB::bind_method(D_METHOD("get_pixel_size"), &TextMesh::get_pixel_size);
  2939. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &TextMesh::set_offset);
  2940. ClassDB::bind_method(D_METHOD("get_offset"), &TextMesh::get_offset);
  2941. ClassDB::bind_method(D_METHOD("set_curve_step", "curve_step"), &TextMesh::set_curve_step);
  2942. ClassDB::bind_method(D_METHOD("get_curve_step"), &TextMesh::get_curve_step);
  2943. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &TextMesh::set_text_direction);
  2944. ClassDB::bind_method(D_METHOD("get_text_direction"), &TextMesh::get_text_direction);
  2945. ClassDB::bind_method(D_METHOD("set_language", "language"), &TextMesh::set_language);
  2946. ClassDB::bind_method(D_METHOD("get_language"), &TextMesh::get_language);
  2947. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &TextMesh::set_structured_text_bidi_override);
  2948. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &TextMesh::get_structured_text_bidi_override);
  2949. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &TextMesh::set_structured_text_bidi_override_options);
  2950. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &TextMesh::get_structured_text_bidi_override_options);
  2951. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &TextMesh::set_uppercase);
  2952. ClassDB::bind_method(D_METHOD("is_uppercase"), &TextMesh::is_uppercase);
  2953. ADD_GROUP("Text", "");
  2954. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, ""), "set_text", "get_text");
  2955. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_font", "get_font");
  2956. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_size", PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px"), "set_font_size", "get_font_size");
  2957. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  2958. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom"), "set_vertical_alignment", "get_vertical_alignment");
  2959. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  2960. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_line_spacing", "get_line_spacing");
  2961. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  2962. ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Justify Only After Last Tab:8,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
  2963. ADD_GROUP("Mesh", "");
  2964. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001,suffix:m"), "set_pixel_size", "get_pixel_size");
  2965. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "curve_step", PROPERTY_HINT_RANGE, "0.1,10,0.1,suffix:px"), "set_curve_step", "get_curve_step");
  2966. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.0,100.0,0.001,or_greater,suffix:m"), "set_depth", "get_depth");
  2967. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width", PROPERTY_HINT_NONE, "suffix:px"), "set_width", "get_width");
  2968. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  2969. ADD_GROUP("BiDi", "");
  2970. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left"), "set_text_direction", "get_text_direction");
  2971. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  2972. ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
  2973. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  2974. }
  2975. void TextMesh::_notification(int p_what) {
  2976. switch (p_what) {
  2977. case MainLoop::NOTIFICATION_TRANSLATION_CHANGED: {
  2978. String new_text = tr(text);
  2979. if (new_text == xl_text) {
  2980. return; // Nothing new.
  2981. }
  2982. xl_text = new_text;
  2983. dirty_text = true;
  2984. request_update();
  2985. } break;
  2986. }
  2987. }
  2988. TextMesh::TextMesh() {
  2989. primitive_type = PRIMITIVE_TRIANGLES;
  2990. text_rid = TS->create_shaped_text();
  2991. }
  2992. TextMesh::~TextMesh() {
  2993. for (int i = 0; i < lines_rid.size(); i++) {
  2994. TS->free_rid(lines_rid[i]);
  2995. }
  2996. lines_rid.clear();
  2997. TS->free_rid(text_rid);
  2998. }
  2999. void TextMesh::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  3000. ERR_FAIL_INDEX((int)p_alignment, 4);
  3001. if (horizontal_alignment != p_alignment) {
  3002. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  3003. dirty_lines = true;
  3004. }
  3005. horizontal_alignment = p_alignment;
  3006. request_update();
  3007. }
  3008. }
  3009. HorizontalAlignment TextMesh::get_horizontal_alignment() const {
  3010. return horizontal_alignment;
  3011. }
  3012. void TextMesh::set_vertical_alignment(VerticalAlignment p_alignment) {
  3013. ERR_FAIL_INDEX((int)p_alignment, 4);
  3014. if (vertical_alignment != p_alignment) {
  3015. vertical_alignment = p_alignment;
  3016. request_update();
  3017. }
  3018. }
  3019. VerticalAlignment TextMesh::get_vertical_alignment() const {
  3020. return vertical_alignment;
  3021. }
  3022. void TextMesh::set_text(const String &p_string) {
  3023. if (text != p_string) {
  3024. text = p_string;
  3025. xl_text = tr(text);
  3026. dirty_text = true;
  3027. request_update();
  3028. }
  3029. }
  3030. String TextMesh::get_text() const {
  3031. return text;
  3032. }
  3033. void TextMesh::_font_changed() {
  3034. dirty_font = true;
  3035. dirty_cache = true;
  3036. callable_mp(static_cast<PrimitiveMesh *>(this), &PrimitiveMesh::request_update).call_deferred();
  3037. }
  3038. void TextMesh::set_font(const Ref<Font> &p_font) {
  3039. if (font_override != p_font) {
  3040. const Callable font_changed = callable_mp(this, &TextMesh::_font_changed);
  3041. if (font_override.is_valid()) {
  3042. font_override->disconnect_changed(font_changed);
  3043. }
  3044. font_override = p_font;
  3045. dirty_font = true;
  3046. dirty_cache = true;
  3047. if (font_override.is_valid()) {
  3048. font_override->connect_changed(font_changed);
  3049. }
  3050. request_update();
  3051. }
  3052. }
  3053. Ref<Font> TextMesh::get_font() const {
  3054. return font_override;
  3055. }
  3056. Ref<Font> TextMesh::_get_font_or_default() const {
  3057. // Similar code taken from `FontVariation::_get_base_font_or_default`.
  3058. if (font_override.is_valid()) {
  3059. return font_override;
  3060. }
  3061. StringName theme_name = "font";
  3062. Vector<StringName> theme_types;
  3063. ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);
  3064. ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
  3065. Vector<Ref<Theme>> themes = global_context->get_themes();
  3066. if (Engine::get_singleton()->is_editor_hint()) {
  3067. themes.insert(0, ThemeDB::get_singleton()->get_project_theme());
  3068. }
  3069. for (const Ref<Theme> &theme : themes) {
  3070. if (theme.is_null()) {
  3071. continue;
  3072. }
  3073. for (const StringName &E : theme_types) {
  3074. if (theme->has_font(theme_name, E)) {
  3075. return theme->get_font(theme_name, E);
  3076. }
  3077. }
  3078. }
  3079. return global_context->get_fallback_theme()->get_font(theme_name, StringName());
  3080. }
  3081. void TextMesh::set_font_size(int p_size) {
  3082. if (font_size != p_size) {
  3083. font_size = CLAMP(p_size, 1, 127);
  3084. dirty_font = true;
  3085. dirty_cache = true;
  3086. request_update();
  3087. }
  3088. }
  3089. int TextMesh::get_font_size() const {
  3090. return font_size;
  3091. }
  3092. void TextMesh::set_line_spacing(float p_line_spacing) {
  3093. if (line_spacing != p_line_spacing) {
  3094. line_spacing = p_line_spacing;
  3095. request_update();
  3096. }
  3097. }
  3098. float TextMesh::get_line_spacing() const {
  3099. return line_spacing;
  3100. }
  3101. void TextMesh::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  3102. if (autowrap_mode != p_mode) {
  3103. autowrap_mode = p_mode;
  3104. dirty_lines = true;
  3105. request_update();
  3106. }
  3107. }
  3108. TextServer::AutowrapMode TextMesh::get_autowrap_mode() const {
  3109. return autowrap_mode;
  3110. }
  3111. void TextMesh::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
  3112. if (jst_flags != p_flags) {
  3113. jst_flags = p_flags;
  3114. dirty_lines = true;
  3115. request_update();
  3116. }
  3117. }
  3118. BitField<TextServer::JustificationFlag> TextMesh::get_justification_flags() const {
  3119. return jst_flags;
  3120. }
  3121. void TextMesh::set_depth(real_t p_depth) {
  3122. if (depth != p_depth) {
  3123. depth = MAX(p_depth, 0.0);
  3124. request_update();
  3125. }
  3126. }
  3127. real_t TextMesh::get_depth() const {
  3128. return depth;
  3129. }
  3130. void TextMesh::set_width(real_t p_width) {
  3131. if (width != p_width) {
  3132. width = p_width;
  3133. dirty_lines = true;
  3134. request_update();
  3135. }
  3136. }
  3137. real_t TextMesh::get_width() const {
  3138. return width;
  3139. }
  3140. void TextMesh::set_pixel_size(real_t p_amount) {
  3141. if (pixel_size != p_amount) {
  3142. pixel_size = CLAMP(p_amount, 0.0001, 128.0);
  3143. dirty_cache = true;
  3144. request_update();
  3145. }
  3146. }
  3147. real_t TextMesh::get_pixel_size() const {
  3148. return pixel_size;
  3149. }
  3150. void TextMesh::set_offset(const Point2 &p_offset) {
  3151. if (lbl_offset != p_offset) {
  3152. lbl_offset = p_offset;
  3153. request_update();
  3154. }
  3155. }
  3156. Point2 TextMesh::get_offset() const {
  3157. return lbl_offset;
  3158. }
  3159. void TextMesh::set_curve_step(real_t p_step) {
  3160. if (curve_step != p_step) {
  3161. curve_step = CLAMP(p_step, 0.1, 10.0);
  3162. dirty_cache = true;
  3163. request_update();
  3164. }
  3165. }
  3166. real_t TextMesh::get_curve_step() const {
  3167. return curve_step;
  3168. }
  3169. void TextMesh::set_text_direction(TextServer::Direction p_text_direction) {
  3170. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  3171. if (text_direction != p_text_direction) {
  3172. text_direction = p_text_direction;
  3173. dirty_text = true;
  3174. request_update();
  3175. }
  3176. }
  3177. TextServer::Direction TextMesh::get_text_direction() const {
  3178. return text_direction;
  3179. }
  3180. void TextMesh::set_language(const String &p_language) {
  3181. if (language != p_language) {
  3182. language = p_language;
  3183. dirty_text = true;
  3184. request_update();
  3185. }
  3186. }
  3187. String TextMesh::get_language() const {
  3188. return language;
  3189. }
  3190. void TextMesh::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  3191. if (st_parser != p_parser) {
  3192. st_parser = p_parser;
  3193. dirty_text = true;
  3194. request_update();
  3195. }
  3196. }
  3197. TextServer::StructuredTextParser TextMesh::get_structured_text_bidi_override() const {
  3198. return st_parser;
  3199. }
  3200. void TextMesh::set_structured_text_bidi_override_options(Array p_args) {
  3201. if (st_args != p_args) {
  3202. st_args = p_args;
  3203. dirty_text = true;
  3204. request_update();
  3205. }
  3206. }
  3207. Array TextMesh::get_structured_text_bidi_override_options() const {
  3208. return st_args;
  3209. }
  3210. void TextMesh::set_uppercase(bool p_uppercase) {
  3211. if (uppercase != p_uppercase) {
  3212. uppercase = p_uppercase;
  3213. dirty_text = true;
  3214. request_update();
  3215. }
  3216. }
  3217. bool TextMesh::is_uppercase() const {
  3218. return uppercase;
  3219. }