primitive_meshes.cpp 116 KB

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