primitive_meshes.cpp 123 KB

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