primitive_meshes.cpp 113 KB

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