primitive_meshes.cpp 113 KB

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