primitive_meshes.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983
  1. /*************************************************************************/
  2. /* primitive_meshes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/core_string_names.h"
  32. #include "scene/resources/theme.h"
  33. #include "servers/rendering_server.h"
  34. #include "thirdparty/misc/clipper.hpp"
  35. #include "thirdparty/misc/polypartition.h"
  36. /**
  37. PrimitiveMesh
  38. */
  39. void PrimitiveMesh::_update() const {
  40. Array arr;
  41. if (GDVIRTUAL_CALL(_create_mesh_array, arr)) {
  42. ERR_FAIL_COND_MSG(arr.size() != RS::ARRAY_MAX, "_create_mesh_array must return an array of Mesh.ARRAY_MAX elements.");
  43. } else {
  44. arr.resize(RS::ARRAY_MAX);
  45. _create_mesh_array(arr);
  46. }
  47. Vector<Vector3> points = arr[RS::ARRAY_VERTEX];
  48. ERR_FAIL_COND_MSG(points.size() == 0, "_create_mesh_array must return at least a vertex array.");
  49. aabb = AABB();
  50. int pc = points.size();
  51. ERR_FAIL_COND(pc == 0);
  52. {
  53. const Vector3 *r = points.ptr();
  54. for (int i = 0; i < pc; i++) {
  55. if (i == 0) {
  56. aabb.position = r[i];
  57. } else {
  58. aabb.expand_to(r[i]);
  59. }
  60. }
  61. }
  62. Vector<int> indices = arr[RS::ARRAY_INDEX];
  63. if (flip_faces) {
  64. Vector<Vector3> normals = arr[RS::ARRAY_NORMAL];
  65. if (normals.size() && indices.size()) {
  66. {
  67. int nc = normals.size();
  68. Vector3 *w = normals.ptrw();
  69. for (int i = 0; i < nc; i++) {
  70. w[i] = -w[i];
  71. }
  72. }
  73. {
  74. int ic = indices.size();
  75. int *w = indices.ptrw();
  76. for (int i = 0; i < ic; i += 3) {
  77. SWAP(w[i + 0], w[i + 1]);
  78. }
  79. }
  80. arr[RS::ARRAY_NORMAL] = normals;
  81. arr[RS::ARRAY_INDEX] = indices;
  82. }
  83. }
  84. array_len = pc;
  85. index_array_len = indices.size();
  86. // in with the new
  87. RenderingServer::get_singleton()->mesh_clear(mesh);
  88. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(mesh, (RenderingServer::PrimitiveType)primitive_type, arr);
  89. RenderingServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
  90. pending_request = false;
  91. clear_cache();
  92. const_cast<PrimitiveMesh *>(this)->emit_changed();
  93. }
  94. void PrimitiveMesh::_request_update() {
  95. if (pending_request) {
  96. return;
  97. }
  98. _update();
  99. }
  100. int PrimitiveMesh::get_surface_count() const {
  101. if (pending_request) {
  102. _update();
  103. }
  104. return 1;
  105. }
  106. int PrimitiveMesh::surface_get_array_len(int p_idx) const {
  107. ERR_FAIL_INDEX_V(p_idx, 1, -1);
  108. if (pending_request) {
  109. _update();
  110. }
  111. return array_len;
  112. }
  113. int PrimitiveMesh::surface_get_array_index_len(int p_idx) const {
  114. ERR_FAIL_INDEX_V(p_idx, 1, -1);
  115. if (pending_request) {
  116. _update();
  117. }
  118. return index_array_len;
  119. }
  120. Array PrimitiveMesh::surface_get_arrays(int p_surface) const {
  121. ERR_FAIL_INDEX_V(p_surface, 1, Array());
  122. if (pending_request) {
  123. _update();
  124. }
  125. return RenderingServer::get_singleton()->mesh_surface_get_arrays(mesh, 0);
  126. }
  127. Dictionary PrimitiveMesh::surface_get_lods(int p_surface) const {
  128. return Dictionary(); //not really supported
  129. }
  130. Array PrimitiveMesh::surface_get_blend_shape_arrays(int p_surface) const {
  131. return Array(); //not really supported
  132. }
  133. uint32_t PrimitiveMesh::surface_get_format(int p_idx) const {
  134. ERR_FAIL_INDEX_V(p_idx, 1, 0);
  135. return RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_FORMAT_NORMAL | RS::ARRAY_FORMAT_TANGENT | RS::ARRAY_FORMAT_TEX_UV | RS::ARRAY_FORMAT_INDEX;
  136. }
  137. Mesh::PrimitiveType PrimitiveMesh::surface_get_primitive_type(int p_idx) const {
  138. return primitive_type;
  139. }
  140. void PrimitiveMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  141. ERR_FAIL_INDEX(p_idx, 1);
  142. set_material(p_material);
  143. }
  144. Ref<Material> PrimitiveMesh::surface_get_material(int p_idx) const {
  145. ERR_FAIL_INDEX_V(p_idx, 1, nullptr);
  146. return material;
  147. }
  148. int PrimitiveMesh::get_blend_shape_count() const {
  149. return 0;
  150. }
  151. StringName PrimitiveMesh::get_blend_shape_name(int p_index) const {
  152. return StringName();
  153. }
  154. void PrimitiveMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
  155. }
  156. AABB PrimitiveMesh::get_aabb() const {
  157. if (pending_request) {
  158. _update();
  159. }
  160. return aabb;
  161. }
  162. RID PrimitiveMesh::get_rid() const {
  163. if (pending_request) {
  164. _update();
  165. }
  166. return mesh;
  167. }
  168. void PrimitiveMesh::_bind_methods() {
  169. ClassDB::bind_method(D_METHOD("_update"), &PrimitiveMesh::_update);
  170. ClassDB::bind_method(D_METHOD("set_material", "material"), &PrimitiveMesh::set_material);
  171. ClassDB::bind_method(D_METHOD("get_material"), &PrimitiveMesh::get_material);
  172. ClassDB::bind_method(D_METHOD("get_mesh_arrays"), &PrimitiveMesh::get_mesh_arrays);
  173. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &PrimitiveMesh::set_custom_aabb);
  174. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &PrimitiveMesh::get_custom_aabb);
  175. ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &PrimitiveMesh::set_flip_faces);
  176. ClassDB::bind_method(D_METHOD("get_flip_faces"), &PrimitiveMesh::get_flip_faces);
  177. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial"), "set_material", "get_material");
  178. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
  179. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
  180. GDVIRTUAL_BIND(_create_mesh_array);
  181. }
  182. void PrimitiveMesh::set_material(const Ref<Material> &p_material) {
  183. material = p_material;
  184. if (!pending_request) {
  185. // just apply it, else it'll happen when _update is called.
  186. RenderingServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
  187. notify_property_list_changed();
  188. emit_changed();
  189. };
  190. }
  191. Ref<Material> PrimitiveMesh::get_material() const {
  192. return material;
  193. }
  194. Array PrimitiveMesh::get_mesh_arrays() const {
  195. return surface_get_arrays(0);
  196. }
  197. void PrimitiveMesh::set_custom_aabb(const AABB &p_custom) {
  198. custom_aabb = p_custom;
  199. RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  200. emit_changed();
  201. }
  202. AABB PrimitiveMesh::get_custom_aabb() const {
  203. return custom_aabb;
  204. }
  205. void PrimitiveMesh::set_flip_faces(bool p_enable) {
  206. flip_faces = p_enable;
  207. _request_update();
  208. }
  209. bool PrimitiveMesh::get_flip_faces() const {
  210. return flip_faces;
  211. }
  212. PrimitiveMesh::PrimitiveMesh() {
  213. mesh = RenderingServer::get_singleton()->mesh_create();
  214. }
  215. PrimitiveMesh::~PrimitiveMesh() {
  216. RenderingServer::get_singleton()->free(mesh);
  217. }
  218. /**
  219. CapsuleMesh
  220. */
  221. void CapsuleMesh::_create_mesh_array(Array &p_arr) const {
  222. create_mesh_array(p_arr, radius, height, radial_segments, rings);
  223. }
  224. void CapsuleMesh::create_mesh_array(Array &p_arr, const float radius, const float height, const int radial_segments, const int rings) {
  225. int i, j, prevrow, thisrow, point;
  226. float x, y, z, u, v, w;
  227. float onethird = 1.0 / 3.0;
  228. float twothirds = 2.0 / 3.0;
  229. // note, this has been aligned with our collision shape but I've left the descriptions as top/middle/bottom
  230. Vector<Vector3> points;
  231. Vector<Vector3> normals;
  232. Vector<float> tangents;
  233. Vector<Vector2> uvs;
  234. Vector<int> indices;
  235. point = 0;
  236. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  237. tangents.push_back(m_x); \
  238. tangents.push_back(m_y); \
  239. tangents.push_back(m_z); \
  240. tangents.push_back(m_d);
  241. /* top hemisphere */
  242. thisrow = 0;
  243. prevrow = 0;
  244. for (j = 0; j <= (rings + 1); j++) {
  245. v = j;
  246. v /= (rings + 1);
  247. w = sin(0.5 * Math_PI * v);
  248. y = radius * cos(0.5 * Math_PI * v);
  249. for (i = 0; i <= radial_segments; i++) {
  250. u = i;
  251. u /= radial_segments;
  252. x = -sin(u * Math_TAU);
  253. z = cos(u * Math_TAU);
  254. Vector3 p = Vector3(x * radius * w, y, -z * radius * w);
  255. points.push_back(p + Vector3(0.0, 0.5 * height - radius, 0.0));
  256. normals.push_back(p.normalized());
  257. ADD_TANGENT(-z, 0.0, -x, 1.0)
  258. uvs.push_back(Vector2(u, v * onethird));
  259. point++;
  260. if (i > 0 && j > 0) {
  261. indices.push_back(prevrow + i - 1);
  262. indices.push_back(prevrow + i);
  263. indices.push_back(thisrow + i - 1);
  264. indices.push_back(prevrow + i);
  265. indices.push_back(thisrow + i);
  266. indices.push_back(thisrow + i - 1);
  267. };
  268. };
  269. prevrow = thisrow;
  270. thisrow = point;
  271. };
  272. /* cylinder */
  273. thisrow = point;
  274. prevrow = 0;
  275. for (j = 0; j <= (rings + 1); j++) {
  276. v = j;
  277. v /= (rings + 1);
  278. y = (height - 2.0 * radius) * v;
  279. y = (0.5 * height - radius) - y;
  280. for (i = 0; i <= radial_segments; i++) {
  281. u = i;
  282. u /= radial_segments;
  283. x = -sin(u * Math_TAU);
  284. z = cos(u * Math_TAU);
  285. Vector3 p = Vector3(x * radius, y, -z * radius);
  286. points.push_back(p);
  287. normals.push_back(Vector3(x, 0.0, -z));
  288. ADD_TANGENT(-z, 0.0, -x, 1.0)
  289. uvs.push_back(Vector2(u, onethird + (v * onethird)));
  290. point++;
  291. if (i > 0 && j > 0) {
  292. indices.push_back(prevrow + i - 1);
  293. indices.push_back(prevrow + i);
  294. indices.push_back(thisrow + i - 1);
  295. indices.push_back(prevrow + i);
  296. indices.push_back(thisrow + i);
  297. indices.push_back(thisrow + i - 1);
  298. };
  299. };
  300. prevrow = thisrow;
  301. thisrow = point;
  302. };
  303. /* bottom hemisphere */
  304. thisrow = point;
  305. prevrow = 0;
  306. for (j = 0; j <= (rings + 1); j++) {
  307. v = j;
  308. v /= (rings + 1);
  309. v += 1.0;
  310. w = sin(0.5 * Math_PI * v);
  311. y = radius * cos(0.5 * Math_PI * v);
  312. for (i = 0; i <= radial_segments; i++) {
  313. float u2 = i;
  314. u2 /= radial_segments;
  315. x = -sin(u2 * Math_TAU);
  316. z = cos(u2 * Math_TAU);
  317. Vector3 p = Vector3(x * radius * w, y, -z * radius * w);
  318. points.push_back(p + Vector3(0.0, -0.5 * height + radius, 0.0));
  319. normals.push_back(p.normalized());
  320. ADD_TANGENT(-z, 0.0, -x, 1.0)
  321. uvs.push_back(Vector2(u2, twothirds + ((v - 1.0) * onethird)));
  322. point++;
  323. if (i > 0 && j > 0) {
  324. indices.push_back(prevrow + i - 1);
  325. indices.push_back(prevrow + i);
  326. indices.push_back(thisrow + i - 1);
  327. indices.push_back(prevrow + i);
  328. indices.push_back(thisrow + i);
  329. indices.push_back(thisrow + i - 1);
  330. };
  331. };
  332. prevrow = thisrow;
  333. thisrow = point;
  334. };
  335. p_arr[RS::ARRAY_VERTEX] = points;
  336. p_arr[RS::ARRAY_NORMAL] = normals;
  337. p_arr[RS::ARRAY_TANGENT] = tangents;
  338. p_arr[RS::ARRAY_TEX_UV] = uvs;
  339. p_arr[RS::ARRAY_INDEX] = indices;
  340. }
  341. void CapsuleMesh::_bind_methods() {
  342. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleMesh::set_radius);
  343. ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleMesh::get_radius);
  344. ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleMesh::set_height);
  345. ClassDB::bind_method(D_METHOD("get_height"), &CapsuleMesh::get_height);
  346. ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CapsuleMesh::set_radial_segments);
  347. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CapsuleMesh::get_radial_segments);
  348. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CapsuleMesh::set_rings);
  349. ClassDB::bind_method(D_METHOD("get_rings"), &CapsuleMesh::get_rings);
  350. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  351. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_height", "get_height");
  352. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  353. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
  354. }
  355. void CapsuleMesh::set_radius(const float p_radius) {
  356. radius = p_radius;
  357. if (radius > height * 0.5) {
  358. radius = height * 0.5;
  359. }
  360. _request_update();
  361. }
  362. float CapsuleMesh::get_radius() const {
  363. return radius;
  364. }
  365. void CapsuleMesh::set_height(const float p_height) {
  366. height = p_height;
  367. if (radius > height * 0.5) {
  368. height = radius * 2;
  369. }
  370. _request_update();
  371. }
  372. float CapsuleMesh::get_height() const {
  373. return height;
  374. }
  375. void CapsuleMesh::set_radial_segments(const int p_segments) {
  376. radial_segments = p_segments > 4 ? p_segments : 4;
  377. _request_update();
  378. }
  379. int CapsuleMesh::get_radial_segments() const {
  380. return radial_segments;
  381. }
  382. void CapsuleMesh::set_rings(const int p_rings) {
  383. rings = p_rings > 1 ? p_rings : 1;
  384. _request_update();
  385. }
  386. int CapsuleMesh::get_rings() const {
  387. return rings;
  388. }
  389. CapsuleMesh::CapsuleMesh() {}
  390. /**
  391. BoxMesh
  392. */
  393. void BoxMesh::_create_mesh_array(Array &p_arr) const {
  394. BoxMesh::create_mesh_array(p_arr, size, subdivide_w, subdivide_h, subdivide_d);
  395. }
  396. void BoxMesh::create_mesh_array(Array &p_arr, Vector3 size, int subdivide_w, int subdivide_h, int subdivide_d) {
  397. int i, j, prevrow, thisrow, point;
  398. float x, y, z;
  399. float onethird = 1.0 / 3.0;
  400. float twothirds = 2.0 / 3.0;
  401. Vector3 start_pos = size * -0.5;
  402. // set our bounding box
  403. Vector<Vector3> points;
  404. Vector<Vector3> normals;
  405. Vector<float> tangents;
  406. Vector<Vector2> uvs;
  407. Vector<int> indices;
  408. point = 0;
  409. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  410. tangents.push_back(m_x); \
  411. tangents.push_back(m_y); \
  412. tangents.push_back(m_z); \
  413. tangents.push_back(m_d);
  414. // front + back
  415. y = start_pos.y;
  416. thisrow = point;
  417. prevrow = 0;
  418. for (j = 0; j <= subdivide_h + 1; j++) {
  419. x = start_pos.x;
  420. for (i = 0; i <= subdivide_w + 1; i++) {
  421. float u = i;
  422. float v = j;
  423. u /= (3.0 * (subdivide_w + 1.0));
  424. v /= (2.0 * (subdivide_h + 1.0));
  425. // front
  426. points.push_back(Vector3(x, -y, -start_pos.z)); // double negative on the Z!
  427. normals.push_back(Vector3(0.0, 0.0, 1.0));
  428. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  429. uvs.push_back(Vector2(u, v));
  430. point++;
  431. // back
  432. points.push_back(Vector3(-x, -y, start_pos.z));
  433. normals.push_back(Vector3(0.0, 0.0, -1.0));
  434. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  435. uvs.push_back(Vector2(twothirds + u, v));
  436. point++;
  437. if (i > 0 && j > 0) {
  438. int i2 = i * 2;
  439. // front
  440. indices.push_back(prevrow + i2 - 2);
  441. indices.push_back(prevrow + i2);
  442. indices.push_back(thisrow + i2 - 2);
  443. indices.push_back(prevrow + i2);
  444. indices.push_back(thisrow + i2);
  445. indices.push_back(thisrow + i2 - 2);
  446. // back
  447. indices.push_back(prevrow + i2 - 1);
  448. indices.push_back(prevrow + i2 + 1);
  449. indices.push_back(thisrow + i2 - 1);
  450. indices.push_back(prevrow + i2 + 1);
  451. indices.push_back(thisrow + i2 + 1);
  452. indices.push_back(thisrow + i2 - 1);
  453. };
  454. x += size.x / (subdivide_w + 1.0);
  455. };
  456. y += size.y / (subdivide_h + 1.0);
  457. prevrow = thisrow;
  458. thisrow = point;
  459. };
  460. // left + right
  461. y = start_pos.y;
  462. thisrow = point;
  463. prevrow = 0;
  464. for (j = 0; j <= (subdivide_h + 1); j++) {
  465. z = start_pos.z;
  466. for (i = 0; i <= (subdivide_d + 1); i++) {
  467. float u = i;
  468. float v = j;
  469. u /= (3.0 * (subdivide_d + 1.0));
  470. v /= (2.0 * (subdivide_h + 1.0));
  471. // right
  472. points.push_back(Vector3(-start_pos.x, -y, -z));
  473. normals.push_back(Vector3(1.0, 0.0, 0.0));
  474. ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
  475. uvs.push_back(Vector2(onethird + u, v));
  476. point++;
  477. // left
  478. points.push_back(Vector3(start_pos.x, -y, z));
  479. normals.push_back(Vector3(-1.0, 0.0, 0.0));
  480. ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
  481. uvs.push_back(Vector2(u, 0.5 + v));
  482. point++;
  483. if (i > 0 && j > 0) {
  484. int i2 = i * 2;
  485. // right
  486. indices.push_back(prevrow + i2 - 2);
  487. indices.push_back(prevrow + i2);
  488. indices.push_back(thisrow + i2 - 2);
  489. indices.push_back(prevrow + i2);
  490. indices.push_back(thisrow + i2);
  491. indices.push_back(thisrow + i2 - 2);
  492. // left
  493. indices.push_back(prevrow + i2 - 1);
  494. indices.push_back(prevrow + i2 + 1);
  495. indices.push_back(thisrow + i2 - 1);
  496. indices.push_back(prevrow + i2 + 1);
  497. indices.push_back(thisrow + i2 + 1);
  498. indices.push_back(thisrow + i2 - 1);
  499. };
  500. z += size.z / (subdivide_d + 1.0);
  501. };
  502. y += size.y / (subdivide_h + 1.0);
  503. prevrow = thisrow;
  504. thisrow = point;
  505. };
  506. // top + bottom
  507. z = start_pos.z;
  508. thisrow = point;
  509. prevrow = 0;
  510. for (j = 0; j <= (subdivide_d + 1); j++) {
  511. x = start_pos.x;
  512. for (i = 0; i <= (subdivide_w + 1); i++) {
  513. float u = i;
  514. float v = j;
  515. u /= (3.0 * (subdivide_w + 1.0));
  516. v /= (2.0 * (subdivide_d + 1.0));
  517. // top
  518. points.push_back(Vector3(-x, -start_pos.y, -z));
  519. normals.push_back(Vector3(0.0, 1.0, 0.0));
  520. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  521. uvs.push_back(Vector2(onethird + u, 0.5 + v));
  522. point++;
  523. // bottom
  524. points.push_back(Vector3(x, start_pos.y, -z));
  525. normals.push_back(Vector3(0.0, -1.0, 0.0));
  526. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  527. uvs.push_back(Vector2(twothirds + u, 0.5 + v));
  528. point++;
  529. if (i > 0 && j > 0) {
  530. int i2 = i * 2;
  531. // top
  532. indices.push_back(prevrow + i2 - 2);
  533. indices.push_back(prevrow + i2);
  534. indices.push_back(thisrow + i2 - 2);
  535. indices.push_back(prevrow + i2);
  536. indices.push_back(thisrow + i2);
  537. indices.push_back(thisrow + i2 - 2);
  538. // bottom
  539. indices.push_back(prevrow + i2 - 1);
  540. indices.push_back(prevrow + i2 + 1);
  541. indices.push_back(thisrow + i2 - 1);
  542. indices.push_back(prevrow + i2 + 1);
  543. indices.push_back(thisrow + i2 + 1);
  544. indices.push_back(thisrow + i2 - 1);
  545. };
  546. x += size.x / (subdivide_w + 1.0);
  547. };
  548. z += size.z / (subdivide_d + 1.0);
  549. prevrow = thisrow;
  550. thisrow = point;
  551. };
  552. p_arr[RS::ARRAY_VERTEX] = points;
  553. p_arr[RS::ARRAY_NORMAL] = normals;
  554. p_arr[RS::ARRAY_TANGENT] = tangents;
  555. p_arr[RS::ARRAY_TEX_UV] = uvs;
  556. p_arr[RS::ARRAY_INDEX] = indices;
  557. }
  558. void BoxMesh::_bind_methods() {
  559. ClassDB::bind_method(D_METHOD("set_size", "size"), &BoxMesh::set_size);
  560. ClassDB::bind_method(D_METHOD("get_size"), &BoxMesh::get_size);
  561. ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &BoxMesh::set_subdivide_width);
  562. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &BoxMesh::get_subdivide_width);
  563. ClassDB::bind_method(D_METHOD("set_subdivide_height", "divisions"), &BoxMesh::set_subdivide_height);
  564. ClassDB::bind_method(D_METHOD("get_subdivide_height"), &BoxMesh::get_subdivide_height);
  565. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "divisions"), &BoxMesh::set_subdivide_depth);
  566. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &BoxMesh::get_subdivide_depth);
  567. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  568. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  569. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
  570. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  571. }
  572. void BoxMesh::set_size(const Vector3 &p_size) {
  573. size = p_size;
  574. _request_update();
  575. }
  576. Vector3 BoxMesh::get_size() const {
  577. return size;
  578. }
  579. void BoxMesh::set_subdivide_width(const int p_divisions) {
  580. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  581. _request_update();
  582. }
  583. int BoxMesh::get_subdivide_width() const {
  584. return subdivide_w;
  585. }
  586. void BoxMesh::set_subdivide_height(const int p_divisions) {
  587. subdivide_h = p_divisions > 0 ? p_divisions : 0;
  588. _request_update();
  589. }
  590. int BoxMesh::get_subdivide_height() const {
  591. return subdivide_h;
  592. }
  593. void BoxMesh::set_subdivide_depth(const int p_divisions) {
  594. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  595. _request_update();
  596. }
  597. int BoxMesh::get_subdivide_depth() const {
  598. return subdivide_d;
  599. }
  600. BoxMesh::BoxMesh() {}
  601. /**
  602. CylinderMesh
  603. */
  604. void CylinderMesh::_create_mesh_array(Array &p_arr) const {
  605. create_mesh_array(p_arr, top_radius, bottom_radius, height, radial_segments, rings);
  606. }
  607. void CylinderMesh::create_mesh_array(Array &p_arr, float top_radius, float bottom_radius, float height, int radial_segments, int rings) {
  608. int i, j, prevrow, thisrow, point;
  609. float x, y, z, u, v, radius;
  610. Vector<Vector3> points;
  611. Vector<Vector3> normals;
  612. Vector<float> tangents;
  613. Vector<Vector2> uvs;
  614. Vector<int> indices;
  615. point = 0;
  616. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  617. tangents.push_back(m_x); \
  618. tangents.push_back(m_y); \
  619. tangents.push_back(m_z); \
  620. tangents.push_back(m_d);
  621. thisrow = 0;
  622. prevrow = 0;
  623. for (j = 0; j <= (rings + 1); j++) {
  624. v = j;
  625. v /= (rings + 1);
  626. radius = top_radius + ((bottom_radius - top_radius) * v);
  627. y = height * v;
  628. y = (height * 0.5) - y;
  629. for (i = 0; i <= radial_segments; i++) {
  630. u = i;
  631. u /= radial_segments;
  632. x = sin(u * Math_TAU);
  633. z = cos(u * Math_TAU);
  634. Vector3 p = Vector3(x * radius, y, z * radius);
  635. points.push_back(p);
  636. normals.push_back(Vector3(x, 0.0, z));
  637. ADD_TANGENT(z, 0.0, -x, 1.0)
  638. uvs.push_back(Vector2(u, v * 0.5));
  639. point++;
  640. if (i > 0 && j > 0) {
  641. indices.push_back(prevrow + i - 1);
  642. indices.push_back(prevrow + i);
  643. indices.push_back(thisrow + i - 1);
  644. indices.push_back(prevrow + i);
  645. indices.push_back(thisrow + i);
  646. indices.push_back(thisrow + i - 1);
  647. };
  648. };
  649. prevrow = thisrow;
  650. thisrow = point;
  651. };
  652. // add top
  653. if (top_radius > 0.0) {
  654. y = height * 0.5;
  655. thisrow = point;
  656. points.push_back(Vector3(0.0, y, 0.0));
  657. normals.push_back(Vector3(0.0, 1.0, 0.0));
  658. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  659. uvs.push_back(Vector2(0.25, 0.75));
  660. point++;
  661. for (i = 0; i <= radial_segments; i++) {
  662. float r = i;
  663. r /= radial_segments;
  664. x = sin(r * Math_TAU);
  665. z = cos(r * Math_TAU);
  666. u = ((x + 1.0) * 0.25);
  667. v = 0.5 + ((z + 1.0) * 0.25);
  668. Vector3 p = Vector3(x * top_radius, y, z * top_radius);
  669. points.push_back(p);
  670. normals.push_back(Vector3(0.0, 1.0, 0.0));
  671. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  672. uvs.push_back(Vector2(u, v));
  673. point++;
  674. if (i > 0) {
  675. indices.push_back(thisrow);
  676. indices.push_back(point - 1);
  677. indices.push_back(point - 2);
  678. };
  679. };
  680. };
  681. // add bottom
  682. if (bottom_radius > 0.0) {
  683. y = height * -0.5;
  684. thisrow = point;
  685. points.push_back(Vector3(0.0, y, 0.0));
  686. normals.push_back(Vector3(0.0, -1.0, 0.0));
  687. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  688. uvs.push_back(Vector2(0.75, 0.75));
  689. point++;
  690. for (i = 0; i <= radial_segments; i++) {
  691. float r = i;
  692. r /= radial_segments;
  693. x = sin(r * Math_TAU);
  694. z = cos(r * Math_TAU);
  695. u = 0.5 + ((x + 1.0) * 0.25);
  696. v = 1.0 - ((z + 1.0) * 0.25);
  697. Vector3 p = Vector3(x * bottom_radius, y, z * bottom_radius);
  698. points.push_back(p);
  699. normals.push_back(Vector3(0.0, -1.0, 0.0));
  700. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  701. uvs.push_back(Vector2(u, v));
  702. point++;
  703. if (i > 0) {
  704. indices.push_back(thisrow);
  705. indices.push_back(point - 2);
  706. indices.push_back(point - 1);
  707. };
  708. };
  709. };
  710. p_arr[RS::ARRAY_VERTEX] = points;
  711. p_arr[RS::ARRAY_NORMAL] = normals;
  712. p_arr[RS::ARRAY_TANGENT] = tangents;
  713. p_arr[RS::ARRAY_TEX_UV] = uvs;
  714. p_arr[RS::ARRAY_INDEX] = indices;
  715. }
  716. void CylinderMesh::_bind_methods() {
  717. ClassDB::bind_method(D_METHOD("set_top_radius", "radius"), &CylinderMesh::set_top_radius);
  718. ClassDB::bind_method(D_METHOD("get_top_radius"), &CylinderMesh::get_top_radius);
  719. ClassDB::bind_method(D_METHOD("set_bottom_radius", "radius"), &CylinderMesh::set_bottom_radius);
  720. ClassDB::bind_method(D_METHOD("get_bottom_radius"), &CylinderMesh::get_bottom_radius);
  721. ClassDB::bind_method(D_METHOD("set_height", "height"), &CylinderMesh::set_height);
  722. ClassDB::bind_method(D_METHOD("get_height"), &CylinderMesh::get_height);
  723. ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CylinderMesh::set_radial_segments);
  724. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CylinderMesh::get_radial_segments);
  725. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CylinderMesh::set_rings);
  726. ClassDB::bind_method(D_METHOD("get_rings"), &CylinderMesh::get_rings);
  727. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "top_radius", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater,suffix:m"), "set_top_radius", "get_top_radius");
  728. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bottom_radius", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater,suffix:m"), "set_bottom_radius", "get_bottom_radius");
  729. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_height", "get_height");
  730. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  731. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
  732. }
  733. void CylinderMesh::set_top_radius(const float p_radius) {
  734. top_radius = p_radius;
  735. _request_update();
  736. }
  737. float CylinderMesh::get_top_radius() const {
  738. return top_radius;
  739. }
  740. void CylinderMesh::set_bottom_radius(const float p_radius) {
  741. bottom_radius = p_radius;
  742. _request_update();
  743. }
  744. float CylinderMesh::get_bottom_radius() const {
  745. return bottom_radius;
  746. }
  747. void CylinderMesh::set_height(const float p_height) {
  748. height = p_height;
  749. _request_update();
  750. }
  751. float CylinderMesh::get_height() const {
  752. return height;
  753. }
  754. void CylinderMesh::set_radial_segments(const int p_segments) {
  755. radial_segments = p_segments > 4 ? p_segments : 4;
  756. _request_update();
  757. }
  758. int CylinderMesh::get_radial_segments() const {
  759. return radial_segments;
  760. }
  761. void CylinderMesh::set_rings(const int p_rings) {
  762. rings = p_rings > 0 ? p_rings : 0;
  763. _request_update();
  764. }
  765. int CylinderMesh::get_rings() const {
  766. return rings;
  767. }
  768. CylinderMesh::CylinderMesh() {}
  769. /**
  770. PlaneMesh
  771. */
  772. void PlaneMesh::_create_mesh_array(Array &p_arr) const {
  773. int i, j, prevrow, thisrow, point;
  774. float x, z;
  775. Size2 start_pos = size * -0.5;
  776. Vector<Vector3> points;
  777. Vector<Vector3> normals;
  778. Vector<float> tangents;
  779. Vector<Vector2> uvs;
  780. Vector<int> indices;
  781. point = 0;
  782. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  783. tangents.push_back(m_x); \
  784. tangents.push_back(m_y); \
  785. tangents.push_back(m_z); \
  786. tangents.push_back(m_d);
  787. /* top + bottom */
  788. z = start_pos.y;
  789. thisrow = point;
  790. prevrow = 0;
  791. for (j = 0; j <= (subdivide_d + 1); j++) {
  792. x = start_pos.x;
  793. for (i = 0; i <= (subdivide_w + 1); i++) {
  794. float u = i;
  795. float v = j;
  796. u /= (subdivide_w + 1.0);
  797. v /= (subdivide_d + 1.0);
  798. points.push_back(Vector3(-x, 0.0, -z) + center_offset);
  799. normals.push_back(Vector3(0.0, 1.0, 0.0));
  800. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  801. uvs.push_back(Vector2(1.0 - u, 1.0 - v)); /* 1.0 - uv to match orientation with Quad */
  802. point++;
  803. if (i > 0 && j > 0) {
  804. indices.push_back(prevrow + i - 1);
  805. indices.push_back(prevrow + i);
  806. indices.push_back(thisrow + i - 1);
  807. indices.push_back(prevrow + i);
  808. indices.push_back(thisrow + i);
  809. indices.push_back(thisrow + i - 1);
  810. };
  811. x += size.x / (subdivide_w + 1.0);
  812. };
  813. z += size.y / (subdivide_d + 1.0);
  814. prevrow = thisrow;
  815. thisrow = point;
  816. };
  817. p_arr[RS::ARRAY_VERTEX] = points;
  818. p_arr[RS::ARRAY_NORMAL] = normals;
  819. p_arr[RS::ARRAY_TANGENT] = tangents;
  820. p_arr[RS::ARRAY_TEX_UV] = uvs;
  821. p_arr[RS::ARRAY_INDEX] = indices;
  822. }
  823. void PlaneMesh::_bind_methods() {
  824. ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaneMesh::set_size);
  825. ClassDB::bind_method(D_METHOD("get_size"), &PlaneMesh::get_size);
  826. ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &PlaneMesh::set_subdivide_width);
  827. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PlaneMesh::get_subdivide_width);
  828. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "subdivide"), &PlaneMesh::set_subdivide_depth);
  829. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PlaneMesh::get_subdivide_depth);
  830. ClassDB::bind_method(D_METHOD("set_center_offset", "offset"), &PlaneMesh::set_center_offset);
  831. ClassDB::bind_method(D_METHOD("get_center_offset"), &PlaneMesh::get_center_offset);
  832. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  833. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  834. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  835. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_offset"), "set_center_offset", "get_center_offset");
  836. }
  837. void PlaneMesh::set_size(const Size2 &p_size) {
  838. size = p_size;
  839. _request_update();
  840. }
  841. Size2 PlaneMesh::get_size() const {
  842. return size;
  843. }
  844. void PlaneMesh::set_subdivide_width(const int p_divisions) {
  845. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  846. _request_update();
  847. }
  848. int PlaneMesh::get_subdivide_width() const {
  849. return subdivide_w;
  850. }
  851. void PlaneMesh::set_subdivide_depth(const int p_divisions) {
  852. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  853. _request_update();
  854. }
  855. int PlaneMesh::get_subdivide_depth() const {
  856. return subdivide_d;
  857. }
  858. void PlaneMesh::set_center_offset(const Vector3 p_offset) {
  859. center_offset = p_offset;
  860. _request_update();
  861. }
  862. Vector3 PlaneMesh::get_center_offset() const {
  863. return center_offset;
  864. }
  865. PlaneMesh::PlaneMesh() {}
  866. /**
  867. PrismMesh
  868. */
  869. void PrismMesh::_create_mesh_array(Array &p_arr) const {
  870. int i, j, prevrow, thisrow, point;
  871. float x, y, z;
  872. float onethird = 1.0 / 3.0;
  873. float twothirds = 2.0 / 3.0;
  874. Vector3 start_pos = size * -0.5;
  875. // set our bounding box
  876. Vector<Vector3> points;
  877. Vector<Vector3> normals;
  878. Vector<float> tangents;
  879. Vector<Vector2> uvs;
  880. Vector<int> indices;
  881. point = 0;
  882. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  883. tangents.push_back(m_x); \
  884. tangents.push_back(m_y); \
  885. tangents.push_back(m_z); \
  886. tangents.push_back(m_d);
  887. /* front + back */
  888. y = start_pos.y;
  889. thisrow = point;
  890. prevrow = 0;
  891. for (j = 0; j <= (subdivide_h + 1); j++) {
  892. float scale = (y - start_pos.y) / size.y;
  893. float scaled_size_x = size.x * scale;
  894. float start_x = start_pos.x + (1.0 - scale) * size.x * left_to_right;
  895. float offset_front = (1.0 - scale) * onethird * left_to_right;
  896. float offset_back = (1.0 - scale) * onethird * (1.0 - left_to_right);
  897. x = 0.0;
  898. for (i = 0; i <= (subdivide_w + 1); i++) {
  899. float u = i;
  900. float v = j;
  901. u /= (3.0 * (subdivide_w + 1.0));
  902. v /= (2.0 * (subdivide_h + 1.0));
  903. u *= scale;
  904. /* front */
  905. points.push_back(Vector3(start_x + x, -y, -start_pos.z)); // double negative on the Z!
  906. normals.push_back(Vector3(0.0, 0.0, 1.0));
  907. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  908. uvs.push_back(Vector2(offset_front + u, v));
  909. point++;
  910. /* back */
  911. points.push_back(Vector3(start_x + scaled_size_x - x, -y, start_pos.z));
  912. normals.push_back(Vector3(0.0, 0.0, -1.0));
  913. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  914. uvs.push_back(Vector2(twothirds + offset_back + u, v));
  915. point++;
  916. if (i > 0 && j == 1) {
  917. int i2 = i * 2;
  918. /* front */
  919. indices.push_back(prevrow + i2);
  920. indices.push_back(thisrow + i2);
  921. indices.push_back(thisrow + i2 - 2);
  922. /* back */
  923. indices.push_back(prevrow + i2 + 1);
  924. indices.push_back(thisrow + i2 + 1);
  925. indices.push_back(thisrow + i2 - 1);
  926. } else if (i > 0 && j > 0) {
  927. int i2 = i * 2;
  928. /* front */
  929. indices.push_back(prevrow + i2 - 2);
  930. indices.push_back(prevrow + i2);
  931. indices.push_back(thisrow + i2 - 2);
  932. indices.push_back(prevrow + i2);
  933. indices.push_back(thisrow + i2);
  934. indices.push_back(thisrow + i2 - 2);
  935. /* back */
  936. indices.push_back(prevrow + i2 - 1);
  937. indices.push_back(prevrow + i2 + 1);
  938. indices.push_back(thisrow + i2 - 1);
  939. indices.push_back(prevrow + i2 + 1);
  940. indices.push_back(thisrow + i2 + 1);
  941. indices.push_back(thisrow + i2 - 1);
  942. };
  943. x += scale * size.x / (subdivide_w + 1.0);
  944. };
  945. y += size.y / (subdivide_h + 1.0);
  946. prevrow = thisrow;
  947. thisrow = point;
  948. };
  949. /* left + right */
  950. Vector3 normal_left, normal_right;
  951. normal_left = Vector3(-size.y, size.x * left_to_right, 0.0);
  952. normal_right = Vector3(size.y, size.x * (1.0 - left_to_right), 0.0);
  953. normal_left.normalize();
  954. normal_right.normalize();
  955. y = start_pos.y;
  956. thisrow = point;
  957. prevrow = 0;
  958. for (j = 0; j <= (subdivide_h + 1); j++) {
  959. float left, right;
  960. float scale = (y - start_pos.y) / size.y;
  961. left = start_pos.x + (size.x * (1.0 - scale) * left_to_right);
  962. right = left + (size.x * scale);
  963. z = start_pos.z;
  964. for (i = 0; i <= (subdivide_d + 1); i++) {
  965. float u = i;
  966. float v = j;
  967. u /= (3.0 * (subdivide_d + 1.0));
  968. v /= (2.0 * (subdivide_h + 1.0));
  969. /* right */
  970. points.push_back(Vector3(right, -y, -z));
  971. normals.push_back(normal_right);
  972. ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
  973. uvs.push_back(Vector2(onethird + u, v));
  974. point++;
  975. /* left */
  976. points.push_back(Vector3(left, -y, z));
  977. normals.push_back(normal_left);
  978. ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
  979. uvs.push_back(Vector2(u, 0.5 + v));
  980. point++;
  981. if (i > 0 && j > 0) {
  982. int i2 = i * 2;
  983. /* right */
  984. indices.push_back(prevrow + i2 - 2);
  985. indices.push_back(prevrow + i2);
  986. indices.push_back(thisrow + i2 - 2);
  987. indices.push_back(prevrow + i2);
  988. indices.push_back(thisrow + i2);
  989. indices.push_back(thisrow + i2 - 2);
  990. /* left */
  991. indices.push_back(prevrow + i2 - 1);
  992. indices.push_back(prevrow + i2 + 1);
  993. indices.push_back(thisrow + i2 - 1);
  994. indices.push_back(prevrow + i2 + 1);
  995. indices.push_back(thisrow + i2 + 1);
  996. indices.push_back(thisrow + i2 - 1);
  997. };
  998. z += size.z / (subdivide_d + 1.0);
  999. };
  1000. y += size.y / (subdivide_h + 1.0);
  1001. prevrow = thisrow;
  1002. thisrow = point;
  1003. };
  1004. /* bottom */
  1005. z = start_pos.z;
  1006. thisrow = point;
  1007. prevrow = 0;
  1008. for (j = 0; j <= (subdivide_d + 1); j++) {
  1009. x = start_pos.x;
  1010. for (i = 0; i <= (subdivide_w + 1); i++) {
  1011. float u = i;
  1012. float v = j;
  1013. u /= (3.0 * (subdivide_w + 1.0));
  1014. v /= (2.0 * (subdivide_d + 1.0));
  1015. /* bottom */
  1016. points.push_back(Vector3(x, start_pos.y, -z));
  1017. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1018. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  1019. uvs.push_back(Vector2(twothirds + u, 0.5 + v));
  1020. point++;
  1021. if (i > 0 && j > 0) {
  1022. /* bottom */
  1023. indices.push_back(prevrow + i - 1);
  1024. indices.push_back(prevrow + i);
  1025. indices.push_back(thisrow + i - 1);
  1026. indices.push_back(prevrow + i);
  1027. indices.push_back(thisrow + i);
  1028. indices.push_back(thisrow + i - 1);
  1029. };
  1030. x += size.x / (subdivide_w + 1.0);
  1031. };
  1032. z += size.z / (subdivide_d + 1.0);
  1033. prevrow = thisrow;
  1034. thisrow = point;
  1035. };
  1036. p_arr[RS::ARRAY_VERTEX] = points;
  1037. p_arr[RS::ARRAY_NORMAL] = normals;
  1038. p_arr[RS::ARRAY_TANGENT] = tangents;
  1039. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1040. p_arr[RS::ARRAY_INDEX] = indices;
  1041. }
  1042. void PrismMesh::_bind_methods() {
  1043. ClassDB::bind_method(D_METHOD("set_left_to_right", "left_to_right"), &PrismMesh::set_left_to_right);
  1044. ClassDB::bind_method(D_METHOD("get_left_to_right"), &PrismMesh::get_left_to_right);
  1045. ClassDB::bind_method(D_METHOD("set_size", "size"), &PrismMesh::set_size);
  1046. ClassDB::bind_method(D_METHOD("get_size"), &PrismMesh::get_size);
  1047. ClassDB::bind_method(D_METHOD("set_subdivide_width", "segments"), &PrismMesh::set_subdivide_width);
  1048. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PrismMesh::get_subdivide_width);
  1049. ClassDB::bind_method(D_METHOD("set_subdivide_height", "segments"), &PrismMesh::set_subdivide_height);
  1050. ClassDB::bind_method(D_METHOD("get_subdivide_height"), &PrismMesh::get_subdivide_height);
  1051. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "segments"), &PrismMesh::set_subdivide_depth);
  1052. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PrismMesh::get_subdivide_depth);
  1053. 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");
  1054. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1055. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  1056. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
  1057. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  1058. }
  1059. void PrismMesh::set_left_to_right(const float p_left_to_right) {
  1060. left_to_right = p_left_to_right;
  1061. _request_update();
  1062. }
  1063. float PrismMesh::get_left_to_right() const {
  1064. return left_to_right;
  1065. }
  1066. void PrismMesh::set_size(const Vector3 &p_size) {
  1067. size = p_size;
  1068. _request_update();
  1069. }
  1070. Vector3 PrismMesh::get_size() const {
  1071. return size;
  1072. }
  1073. void PrismMesh::set_subdivide_width(const int p_divisions) {
  1074. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  1075. _request_update();
  1076. }
  1077. int PrismMesh::get_subdivide_width() const {
  1078. return subdivide_w;
  1079. }
  1080. void PrismMesh::set_subdivide_height(const int p_divisions) {
  1081. subdivide_h = p_divisions > 0 ? p_divisions : 0;
  1082. _request_update();
  1083. }
  1084. int PrismMesh::get_subdivide_height() const {
  1085. return subdivide_h;
  1086. }
  1087. void PrismMesh::set_subdivide_depth(const int p_divisions) {
  1088. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  1089. _request_update();
  1090. }
  1091. int PrismMesh::get_subdivide_depth() const {
  1092. return subdivide_d;
  1093. }
  1094. PrismMesh::PrismMesh() {}
  1095. /**
  1096. QuadMesh
  1097. */
  1098. void QuadMesh::_create_mesh_array(Array &p_arr) const {
  1099. Vector<Vector3> faces;
  1100. Vector<Vector3> normals;
  1101. Vector<float> tangents;
  1102. Vector<Vector2> uvs;
  1103. faces.resize(6);
  1104. normals.resize(6);
  1105. tangents.resize(6 * 4);
  1106. uvs.resize(6);
  1107. Vector2 _size = Vector2(size.x / 2.0f, size.y / 2.0f);
  1108. Vector3 quad_faces[4] = {
  1109. Vector3(-_size.x, -_size.y, 0) + center_offset,
  1110. Vector3(-_size.x, _size.y, 0) + center_offset,
  1111. Vector3(_size.x, _size.y, 0) + center_offset,
  1112. Vector3(_size.x, -_size.y, 0) + center_offset,
  1113. };
  1114. static const int indices[6] = {
  1115. 0, 1, 2,
  1116. 0, 2, 3
  1117. };
  1118. for (int i = 0; i < 6; i++) {
  1119. int j = indices[i];
  1120. faces.set(i, quad_faces[j]);
  1121. normals.set(i, Vector3(0, 0, 1));
  1122. tangents.set(i * 4 + 0, 1.0);
  1123. tangents.set(i * 4 + 1, 0.0);
  1124. tangents.set(i * 4 + 2, 0.0);
  1125. tangents.set(i * 4 + 3, 1.0);
  1126. static const Vector2 quad_uv[4] = {
  1127. Vector2(0, 1),
  1128. Vector2(0, 0),
  1129. Vector2(1, 0),
  1130. Vector2(1, 1),
  1131. };
  1132. uvs.set(i, quad_uv[j]);
  1133. }
  1134. p_arr[RS::ARRAY_VERTEX] = faces;
  1135. p_arr[RS::ARRAY_NORMAL] = normals;
  1136. p_arr[RS::ARRAY_TANGENT] = tangents;
  1137. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1138. }
  1139. void QuadMesh::_bind_methods() {
  1140. ClassDB::bind_method(D_METHOD("set_size", "size"), &QuadMesh::set_size);
  1141. ClassDB::bind_method(D_METHOD("get_size"), &QuadMesh::get_size);
  1142. ClassDB::bind_method(D_METHOD("set_center_offset", "center_offset"), &QuadMesh::set_center_offset);
  1143. ClassDB::bind_method(D_METHOD("get_center_offset"), &QuadMesh::get_center_offset);
  1144. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  1145. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_center_offset", "get_center_offset");
  1146. }
  1147. uint32_t QuadMesh::surface_get_format(int p_idx) const {
  1148. ERR_FAIL_INDEX_V(p_idx, 1, 0);
  1149. return RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_FORMAT_NORMAL | RS::ARRAY_FORMAT_TANGENT | RS::ARRAY_FORMAT_TEX_UV;
  1150. }
  1151. QuadMesh::QuadMesh() {
  1152. primitive_type = PRIMITIVE_TRIANGLES;
  1153. }
  1154. void QuadMesh::set_size(const Size2 &p_size) {
  1155. size = p_size;
  1156. _request_update();
  1157. }
  1158. Size2 QuadMesh::get_size() const {
  1159. return size;
  1160. }
  1161. void QuadMesh::set_center_offset(Vector3 p_center_offset) {
  1162. center_offset = p_center_offset;
  1163. _request_update();
  1164. }
  1165. Vector3 QuadMesh::get_center_offset() const {
  1166. return center_offset;
  1167. }
  1168. /**
  1169. SphereMesh
  1170. */
  1171. void SphereMesh::_create_mesh_array(Array &p_arr) const {
  1172. create_mesh_array(p_arr, radius, height, radial_segments, rings, is_hemisphere);
  1173. }
  1174. void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int radial_segments, int rings, bool is_hemisphere) {
  1175. int i, j, prevrow, thisrow, point;
  1176. float x, y, z;
  1177. float scale = height * (is_hemisphere ? 1.0 : 0.5);
  1178. // set our bounding box
  1179. Vector<Vector3> points;
  1180. Vector<Vector3> normals;
  1181. Vector<float> tangents;
  1182. Vector<Vector2> uvs;
  1183. Vector<int> indices;
  1184. point = 0;
  1185. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1186. tangents.push_back(m_x); \
  1187. tangents.push_back(m_y); \
  1188. tangents.push_back(m_z); \
  1189. tangents.push_back(m_d);
  1190. thisrow = 0;
  1191. prevrow = 0;
  1192. for (j = 0; j <= (rings + 1); j++) {
  1193. float v = j;
  1194. float w;
  1195. v /= (rings + 1);
  1196. w = sin(Math_PI * v);
  1197. y = scale * cos(Math_PI * v);
  1198. for (i = 0; i <= radial_segments; i++) {
  1199. float u = i;
  1200. u /= radial_segments;
  1201. x = sin(u * Math_TAU);
  1202. z = cos(u * Math_TAU);
  1203. if (is_hemisphere && y < 0.0) {
  1204. points.push_back(Vector3(x * radius * w, 0.0, z * radius * w));
  1205. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1206. } else {
  1207. Vector3 p = Vector3(x * radius * w, y, z * radius * w);
  1208. points.push_back(p);
  1209. Vector3 normal = Vector3(x * w * scale, radius * (y / scale), z * w * scale);
  1210. normals.push_back(normal.normalized());
  1211. };
  1212. ADD_TANGENT(z, 0.0, -x, 1.0)
  1213. uvs.push_back(Vector2(u, v));
  1214. point++;
  1215. if (i > 0 && j > 0) {
  1216. indices.push_back(prevrow + i - 1);
  1217. indices.push_back(prevrow + i);
  1218. indices.push_back(thisrow + i - 1);
  1219. indices.push_back(prevrow + i);
  1220. indices.push_back(thisrow + i);
  1221. indices.push_back(thisrow + i - 1);
  1222. };
  1223. };
  1224. prevrow = thisrow;
  1225. thisrow = point;
  1226. };
  1227. p_arr[RS::ARRAY_VERTEX] = points;
  1228. p_arr[RS::ARRAY_NORMAL] = normals;
  1229. p_arr[RS::ARRAY_TANGENT] = tangents;
  1230. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1231. p_arr[RS::ARRAY_INDEX] = indices;
  1232. }
  1233. void SphereMesh::_bind_methods() {
  1234. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SphereMesh::set_radius);
  1235. ClassDB::bind_method(D_METHOD("get_radius"), &SphereMesh::get_radius);
  1236. ClassDB::bind_method(D_METHOD("set_height", "height"), &SphereMesh::set_height);
  1237. ClassDB::bind_method(D_METHOD("get_height"), &SphereMesh::get_height);
  1238. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &SphereMesh::set_radial_segments);
  1239. ClassDB::bind_method(D_METHOD("get_radial_segments"), &SphereMesh::get_radial_segments);
  1240. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &SphereMesh::set_rings);
  1241. ClassDB::bind_method(D_METHOD("get_rings"), &SphereMesh::get_rings);
  1242. ClassDB::bind_method(D_METHOD("set_is_hemisphere", "is_hemisphere"), &SphereMesh::set_is_hemisphere);
  1243. ClassDB::bind_method(D_METHOD("get_is_hemisphere"), &SphereMesh::get_is_hemisphere);
  1244. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  1245. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_height", "get_height");
  1246. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  1247. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
  1248. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_hemisphere"), "set_is_hemisphere", "get_is_hemisphere");
  1249. }
  1250. void SphereMesh::set_radius(const float p_radius) {
  1251. radius = p_radius;
  1252. _request_update();
  1253. }
  1254. float SphereMesh::get_radius() const {
  1255. return radius;
  1256. }
  1257. void SphereMesh::set_height(const float p_height) {
  1258. height = p_height;
  1259. _request_update();
  1260. }
  1261. float SphereMesh::get_height() const {
  1262. return height;
  1263. }
  1264. void SphereMesh::set_radial_segments(const int p_radial_segments) {
  1265. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  1266. _request_update();
  1267. }
  1268. int SphereMesh::get_radial_segments() const {
  1269. return radial_segments;
  1270. }
  1271. void SphereMesh::set_rings(const int p_rings) {
  1272. rings = p_rings > 1 ? p_rings : 1;
  1273. _request_update();
  1274. }
  1275. int SphereMesh::get_rings() const {
  1276. return rings;
  1277. }
  1278. void SphereMesh::set_is_hemisphere(const bool p_is_hemisphere) {
  1279. is_hemisphere = p_is_hemisphere;
  1280. _request_update();
  1281. }
  1282. bool SphereMesh::get_is_hemisphere() const {
  1283. return is_hemisphere;
  1284. }
  1285. SphereMesh::SphereMesh() {}
  1286. /**
  1287. PointMesh
  1288. */
  1289. void PointMesh::_create_mesh_array(Array &p_arr) const {
  1290. Vector<Vector3> faces;
  1291. faces.resize(1);
  1292. faces.set(0, Vector3(0.0, 0.0, 0.0));
  1293. p_arr[RS::ARRAY_VERTEX] = faces;
  1294. }
  1295. PointMesh::PointMesh() {
  1296. primitive_type = PRIMITIVE_POINTS;
  1297. }
  1298. // TUBE TRAIL
  1299. void TubeTrailMesh::set_radius(const float p_radius) {
  1300. radius = p_radius;
  1301. _request_update();
  1302. }
  1303. float TubeTrailMesh::get_radius() const {
  1304. return radius;
  1305. }
  1306. void TubeTrailMesh::set_radial_steps(const int p_radial_steps) {
  1307. ERR_FAIL_COND(p_radial_steps < 3 || p_radial_steps > 128);
  1308. radial_steps = p_radial_steps;
  1309. _request_update();
  1310. }
  1311. int TubeTrailMesh::get_radial_steps() const {
  1312. return radial_steps;
  1313. }
  1314. void TubeTrailMesh::set_sections(const int p_sections) {
  1315. ERR_FAIL_COND(p_sections < 2 || p_sections > 128);
  1316. sections = p_sections;
  1317. _request_update();
  1318. }
  1319. int TubeTrailMesh::get_sections() const {
  1320. return sections;
  1321. }
  1322. void TubeTrailMesh::set_section_length(float p_section_length) {
  1323. section_length = p_section_length;
  1324. _request_update();
  1325. }
  1326. float TubeTrailMesh::get_section_length() const {
  1327. return section_length;
  1328. }
  1329. void TubeTrailMesh::set_section_rings(const int p_section_rings) {
  1330. ERR_FAIL_COND(p_section_rings < 1 || p_section_rings > 1024);
  1331. section_rings = p_section_rings;
  1332. _request_update();
  1333. }
  1334. int TubeTrailMesh::get_section_rings() const {
  1335. return section_rings;
  1336. }
  1337. void TubeTrailMesh::set_curve(const Ref<Curve> &p_curve) {
  1338. if (curve == p_curve) {
  1339. return;
  1340. }
  1341. if (curve.is_valid()) {
  1342. curve->disconnect("changed", callable_mp(this, &TubeTrailMesh::_curve_changed));
  1343. }
  1344. curve = p_curve;
  1345. if (curve.is_valid()) {
  1346. curve->connect("changed", callable_mp(this, &TubeTrailMesh::_curve_changed));
  1347. }
  1348. _request_update();
  1349. }
  1350. Ref<Curve> TubeTrailMesh::get_curve() const {
  1351. return curve;
  1352. }
  1353. void TubeTrailMesh::_curve_changed() {
  1354. _request_update();
  1355. }
  1356. int TubeTrailMesh::get_builtin_bind_pose_count() const {
  1357. return sections + 1;
  1358. }
  1359. Transform3D TubeTrailMesh::get_builtin_bind_pose(int p_index) const {
  1360. float depth = section_length * sections;
  1361. Transform3D xform;
  1362. xform.origin.y = depth / 2.0 - section_length * float(p_index);
  1363. xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y
  1364. return xform;
  1365. }
  1366. void TubeTrailMesh::_create_mesh_array(Array &p_arr) const {
  1367. PackedVector3Array points;
  1368. PackedVector3Array normals;
  1369. PackedFloat32Array tangents;
  1370. PackedVector2Array uvs;
  1371. PackedInt32Array bone_indices;
  1372. PackedFloat32Array bone_weights;
  1373. PackedInt32Array indices;
  1374. int point = 0;
  1375. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1376. tangents.push_back(m_x); \
  1377. tangents.push_back(m_y); \
  1378. tangents.push_back(m_z); \
  1379. tangents.push_back(m_d);
  1380. int thisrow = 0;
  1381. int prevrow = 0;
  1382. int total_rings = section_rings * sections;
  1383. float depth = section_length * sections;
  1384. for (int j = 0; j <= total_rings; j++) {
  1385. float v = j;
  1386. v /= total_rings;
  1387. float y = depth * v;
  1388. y = (depth * 0.5) - y;
  1389. int bone = j / section_rings;
  1390. float blend = 1.0 - float(j % section_rings) / float(section_rings);
  1391. for (int i = 0; i <= radial_steps; i++) {
  1392. float u = i;
  1393. u /= radial_steps;
  1394. float r = radius;
  1395. if (curve.is_valid() && curve->get_point_count() > 0) {
  1396. r *= curve->interpolate_baked(v);
  1397. }
  1398. float x = sin(u * Math_TAU);
  1399. float z = cos(u * Math_TAU);
  1400. Vector3 p = Vector3(x * r, y, z * r);
  1401. points.push_back(p);
  1402. normals.push_back(Vector3(x, 0, z));
  1403. ADD_TANGENT(z, 0.0, -x, 1.0)
  1404. uvs.push_back(Vector2(u, v * 0.5));
  1405. point++;
  1406. {
  1407. bone_indices.push_back(bone);
  1408. bone_indices.push_back(MIN(sections, bone + 1));
  1409. bone_indices.push_back(0);
  1410. bone_indices.push_back(0);
  1411. bone_weights.push_back(blend);
  1412. bone_weights.push_back(1.0 - blend);
  1413. bone_weights.push_back(0);
  1414. bone_weights.push_back(0);
  1415. }
  1416. if (i > 0 && j > 0) {
  1417. indices.push_back(prevrow + i - 1);
  1418. indices.push_back(prevrow + i);
  1419. indices.push_back(thisrow + i - 1);
  1420. indices.push_back(prevrow + i);
  1421. indices.push_back(thisrow + i);
  1422. indices.push_back(thisrow + i - 1);
  1423. }
  1424. }
  1425. prevrow = thisrow;
  1426. thisrow = point;
  1427. }
  1428. // add top
  1429. float scale_pos = 1.0;
  1430. if (curve.is_valid() && curve->get_point_count() > 0) {
  1431. scale_pos = curve->interpolate_baked(0);
  1432. }
  1433. if (scale_pos > CMP_EPSILON) {
  1434. float y = depth * 0.5;
  1435. thisrow = point;
  1436. points.push_back(Vector3(0.0, y, 0));
  1437. normals.push_back(Vector3(0.0, 1.0, 0.0));
  1438. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  1439. uvs.push_back(Vector2(0.25, 0.75));
  1440. point++;
  1441. bone_indices.push_back(0);
  1442. bone_indices.push_back(0);
  1443. bone_indices.push_back(0);
  1444. bone_indices.push_back(0);
  1445. bone_weights.push_back(1.0);
  1446. bone_weights.push_back(0);
  1447. bone_weights.push_back(0);
  1448. bone_weights.push_back(0);
  1449. float rm = radius * scale_pos;
  1450. for (int i = 0; i <= radial_steps; i++) {
  1451. float r = i;
  1452. r /= radial_steps;
  1453. float x = sin(r * Math_TAU);
  1454. float z = cos(r * Math_TAU);
  1455. float u = ((x + 1.0) * 0.25);
  1456. float v = 0.5 + ((z + 1.0) * 0.25);
  1457. Vector3 p = Vector3(x * rm, y, z * rm);
  1458. points.push_back(p);
  1459. normals.push_back(Vector3(0.0, 1.0, 0.0));
  1460. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  1461. uvs.push_back(Vector2(u, v));
  1462. point++;
  1463. bone_indices.push_back(0);
  1464. bone_indices.push_back(0);
  1465. bone_indices.push_back(0);
  1466. bone_indices.push_back(0);
  1467. bone_weights.push_back(1.0);
  1468. bone_weights.push_back(0);
  1469. bone_weights.push_back(0);
  1470. bone_weights.push_back(0);
  1471. if (i > 0) {
  1472. indices.push_back(thisrow);
  1473. indices.push_back(point - 1);
  1474. indices.push_back(point - 2);
  1475. };
  1476. };
  1477. };
  1478. float scale_neg = 1.0;
  1479. if (curve.is_valid() && curve->get_point_count() > 0) {
  1480. scale_neg = curve->interpolate_baked(1.0);
  1481. }
  1482. // add bottom
  1483. if (scale_neg > CMP_EPSILON) {
  1484. float y = depth * -0.5;
  1485. thisrow = point;
  1486. points.push_back(Vector3(0.0, y, 0.0));
  1487. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1488. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  1489. uvs.push_back(Vector2(0.75, 0.75));
  1490. point++;
  1491. bone_indices.push_back(sections);
  1492. bone_indices.push_back(0);
  1493. bone_indices.push_back(0);
  1494. bone_indices.push_back(0);
  1495. bone_weights.push_back(1.0);
  1496. bone_weights.push_back(0);
  1497. bone_weights.push_back(0);
  1498. bone_weights.push_back(0);
  1499. float rm = radius * scale_neg;
  1500. for (int i = 0; i <= radial_steps; i++) {
  1501. float r = i;
  1502. r /= radial_steps;
  1503. float x = sin(r * Math_TAU);
  1504. float z = cos(r * Math_TAU);
  1505. float u = 0.5 + ((x + 1.0) * 0.25);
  1506. float v = 1.0 - ((z + 1.0) * 0.25);
  1507. Vector3 p = Vector3(x * rm, y, z * rm);
  1508. points.push_back(p);
  1509. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1510. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  1511. uvs.push_back(Vector2(u, v));
  1512. point++;
  1513. bone_indices.push_back(sections);
  1514. bone_indices.push_back(0);
  1515. bone_indices.push_back(0);
  1516. bone_indices.push_back(0);
  1517. bone_weights.push_back(1.0);
  1518. bone_weights.push_back(0);
  1519. bone_weights.push_back(0);
  1520. bone_weights.push_back(0);
  1521. if (i > 0) {
  1522. indices.push_back(thisrow);
  1523. indices.push_back(point - 2);
  1524. indices.push_back(point - 1);
  1525. };
  1526. };
  1527. };
  1528. p_arr[RS::ARRAY_VERTEX] = points;
  1529. p_arr[RS::ARRAY_NORMAL] = normals;
  1530. p_arr[RS::ARRAY_TANGENT] = tangents;
  1531. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1532. p_arr[RS::ARRAY_BONES] = bone_indices;
  1533. p_arr[RS::ARRAY_WEIGHTS] = bone_weights;
  1534. p_arr[RS::ARRAY_INDEX] = indices;
  1535. }
  1536. void TubeTrailMesh::_bind_methods() {
  1537. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &TubeTrailMesh::set_radius);
  1538. ClassDB::bind_method(D_METHOD("get_radius"), &TubeTrailMesh::get_radius);
  1539. ClassDB::bind_method(D_METHOD("set_radial_steps", "radial_steps"), &TubeTrailMesh::set_radial_steps);
  1540. ClassDB::bind_method(D_METHOD("get_radial_steps"), &TubeTrailMesh::get_radial_steps);
  1541. ClassDB::bind_method(D_METHOD("set_sections", "sections"), &TubeTrailMesh::set_sections);
  1542. ClassDB::bind_method(D_METHOD("get_sections"), &TubeTrailMesh::get_sections);
  1543. ClassDB::bind_method(D_METHOD("set_section_length", "section_length"), &TubeTrailMesh::set_section_length);
  1544. ClassDB::bind_method(D_METHOD("get_section_length"), &TubeTrailMesh::get_section_length);
  1545. ClassDB::bind_method(D_METHOD("set_section_rings", "section_rings"), &TubeTrailMesh::set_section_rings);
  1546. ClassDB::bind_method(D_METHOD("get_section_rings"), &TubeTrailMesh::get_section_rings);
  1547. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &TubeTrailMesh::set_curve);
  1548. ClassDB::bind_method(D_METHOD("get_curve"), &TubeTrailMesh::get_curve);
  1549. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  1550. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_steps", PROPERTY_HINT_RANGE, "3,128,1"), "set_radial_steps", "get_radial_steps");
  1551. ADD_PROPERTY(PropertyInfo(Variant::INT, "sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_sections", "get_sections");
  1552. 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");
  1553. ADD_PROPERTY(PropertyInfo(Variant::INT, "section_rings", PROPERTY_HINT_RANGE, "1,128,1"), "set_section_rings", "get_section_rings");
  1554. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  1555. }
  1556. TubeTrailMesh::TubeTrailMesh() {
  1557. }
  1558. // TUBE TRAIL
  1559. void RibbonTrailMesh::set_shape(Shape p_shape) {
  1560. shape = p_shape;
  1561. _request_update();
  1562. }
  1563. RibbonTrailMesh::Shape RibbonTrailMesh::get_shape() const {
  1564. return shape;
  1565. }
  1566. void RibbonTrailMesh::set_size(const float p_size) {
  1567. size = p_size;
  1568. _request_update();
  1569. }
  1570. float RibbonTrailMesh::get_size() const {
  1571. return size;
  1572. }
  1573. void RibbonTrailMesh::set_sections(const int p_sections) {
  1574. ERR_FAIL_COND(p_sections < 2 || p_sections > 128);
  1575. sections = p_sections;
  1576. _request_update();
  1577. }
  1578. int RibbonTrailMesh::get_sections() const {
  1579. return sections;
  1580. }
  1581. void RibbonTrailMesh::set_section_length(float p_section_length) {
  1582. section_length = p_section_length;
  1583. _request_update();
  1584. }
  1585. float RibbonTrailMesh::get_section_length() const {
  1586. return section_length;
  1587. }
  1588. void RibbonTrailMesh::set_section_segments(const int p_section_segments) {
  1589. ERR_FAIL_COND(p_section_segments < 1 || p_section_segments > 1024);
  1590. section_segments = p_section_segments;
  1591. _request_update();
  1592. }
  1593. int RibbonTrailMesh::get_section_segments() const {
  1594. return section_segments;
  1595. }
  1596. void RibbonTrailMesh::set_curve(const Ref<Curve> &p_curve) {
  1597. if (curve == p_curve) {
  1598. return;
  1599. }
  1600. if (curve.is_valid()) {
  1601. curve->disconnect("changed", callable_mp(this, &RibbonTrailMesh::_curve_changed));
  1602. }
  1603. curve = p_curve;
  1604. if (curve.is_valid()) {
  1605. curve->connect("changed", callable_mp(this, &RibbonTrailMesh::_curve_changed));
  1606. }
  1607. _request_update();
  1608. }
  1609. Ref<Curve> RibbonTrailMesh::get_curve() const {
  1610. return curve;
  1611. }
  1612. void RibbonTrailMesh::_curve_changed() {
  1613. _request_update();
  1614. }
  1615. int RibbonTrailMesh::get_builtin_bind_pose_count() const {
  1616. return sections + 1;
  1617. }
  1618. Transform3D RibbonTrailMesh::get_builtin_bind_pose(int p_index) const {
  1619. float depth = section_length * sections;
  1620. Transform3D xform;
  1621. xform.origin.y = depth / 2.0 - section_length * float(p_index);
  1622. xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y
  1623. return xform;
  1624. }
  1625. void RibbonTrailMesh::_create_mesh_array(Array &p_arr) const {
  1626. PackedVector3Array points;
  1627. PackedVector3Array normals;
  1628. PackedFloat32Array tangents;
  1629. PackedVector2Array uvs;
  1630. PackedInt32Array bone_indices;
  1631. PackedFloat32Array bone_weights;
  1632. PackedInt32Array indices;
  1633. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1634. tangents.push_back(m_x); \
  1635. tangents.push_back(m_y); \
  1636. tangents.push_back(m_z); \
  1637. tangents.push_back(m_d);
  1638. int total_segments = section_segments * sections;
  1639. float depth = section_length * sections;
  1640. for (int j = 0; j <= total_segments; j++) {
  1641. float v = j;
  1642. v /= total_segments;
  1643. float y = depth * v;
  1644. y = (depth * 0.5) - y;
  1645. int bone = j / section_segments;
  1646. float blend = 1.0 - float(j % section_segments) / float(section_segments);
  1647. float s = size;
  1648. if (curve.is_valid() && curve->get_point_count() > 0) {
  1649. s *= curve->interpolate_baked(v);
  1650. }
  1651. points.push_back(Vector3(-s * 0.5, y, 0));
  1652. points.push_back(Vector3(+s * 0.5, y, 0));
  1653. if (shape == SHAPE_CROSS) {
  1654. points.push_back(Vector3(0, y, -s * 0.5));
  1655. points.push_back(Vector3(0, y, +s * 0.5));
  1656. }
  1657. normals.push_back(Vector3(0, 0, 1));
  1658. normals.push_back(Vector3(0, 0, 1));
  1659. if (shape == SHAPE_CROSS) {
  1660. normals.push_back(Vector3(1, 0, 0));
  1661. normals.push_back(Vector3(1, 0, 0));
  1662. }
  1663. uvs.push_back(Vector2(0, v));
  1664. uvs.push_back(Vector2(1, v));
  1665. if (shape == SHAPE_CROSS) {
  1666. uvs.push_back(Vector2(0, v));
  1667. uvs.push_back(Vector2(1, v));
  1668. }
  1669. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  1670. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  1671. if (shape == SHAPE_CROSS) {
  1672. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  1673. ADD_TANGENT(0.0, 1.0, 0.0, 1.0)
  1674. }
  1675. for (int i = 0; i < (shape == SHAPE_CROSS ? 4 : 2); i++) {
  1676. bone_indices.push_back(bone);
  1677. bone_indices.push_back(MIN(sections, bone + 1));
  1678. bone_indices.push_back(0);
  1679. bone_indices.push_back(0);
  1680. bone_weights.push_back(blend);
  1681. bone_weights.push_back(1.0 - blend);
  1682. bone_weights.push_back(0);
  1683. bone_weights.push_back(0);
  1684. }
  1685. if (j > 0) {
  1686. if (shape == SHAPE_CROSS) {
  1687. int base = j * 4 - 4;
  1688. indices.push_back(base + 0);
  1689. indices.push_back(base + 1);
  1690. indices.push_back(base + 4);
  1691. indices.push_back(base + 1);
  1692. indices.push_back(base + 5);
  1693. indices.push_back(base + 4);
  1694. indices.push_back(base + 2);
  1695. indices.push_back(base + 3);
  1696. indices.push_back(base + 6);
  1697. indices.push_back(base + 3);
  1698. indices.push_back(base + 7);
  1699. indices.push_back(base + 6);
  1700. } else {
  1701. int base = j * 2 - 2;
  1702. indices.push_back(base + 0);
  1703. indices.push_back(base + 1);
  1704. indices.push_back(base + 2);
  1705. indices.push_back(base + 1);
  1706. indices.push_back(base + 3);
  1707. indices.push_back(base + 2);
  1708. }
  1709. }
  1710. }
  1711. p_arr[RS::ARRAY_VERTEX] = points;
  1712. p_arr[RS::ARRAY_NORMAL] = normals;
  1713. p_arr[RS::ARRAY_TANGENT] = tangents;
  1714. p_arr[RS::ARRAY_TEX_UV] = uvs;
  1715. p_arr[RS::ARRAY_BONES] = bone_indices;
  1716. p_arr[RS::ARRAY_WEIGHTS] = bone_weights;
  1717. p_arr[RS::ARRAY_INDEX] = indices;
  1718. }
  1719. void RibbonTrailMesh::_bind_methods() {
  1720. ClassDB::bind_method(D_METHOD("set_size", "size"), &RibbonTrailMesh::set_size);
  1721. ClassDB::bind_method(D_METHOD("get_size"), &RibbonTrailMesh::get_size);
  1722. ClassDB::bind_method(D_METHOD("set_sections", "sections"), &RibbonTrailMesh::set_sections);
  1723. ClassDB::bind_method(D_METHOD("get_sections"), &RibbonTrailMesh::get_sections);
  1724. ClassDB::bind_method(D_METHOD("set_section_length", "section_length"), &RibbonTrailMesh::set_section_length);
  1725. ClassDB::bind_method(D_METHOD("get_section_length"), &RibbonTrailMesh::get_section_length);
  1726. ClassDB::bind_method(D_METHOD("set_section_segments", "section_segments"), &RibbonTrailMesh::set_section_segments);
  1727. ClassDB::bind_method(D_METHOD("get_section_segments"), &RibbonTrailMesh::get_section_segments);
  1728. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &RibbonTrailMesh::set_curve);
  1729. ClassDB::bind_method(D_METHOD("get_curve"), &RibbonTrailMesh::get_curve);
  1730. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &RibbonTrailMesh::set_shape);
  1731. ClassDB::bind_method(D_METHOD("get_shape"), &RibbonTrailMesh::get_shape);
  1732. ADD_PROPERTY(PropertyInfo(Variant::INT, "shape", PROPERTY_HINT_ENUM, "Flat,Cross"), "set_shape", "get_shape");
  1733. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater,suffix:m"), "set_size", "get_size");
  1734. ADD_PROPERTY(PropertyInfo(Variant::INT, "sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_sections", "get_sections");
  1735. 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");
  1736. ADD_PROPERTY(PropertyInfo(Variant::INT, "section_segments", PROPERTY_HINT_RANGE, "1,128,1"), "set_section_segments", "get_section_segments");
  1737. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  1738. BIND_ENUM_CONSTANT(SHAPE_FLAT)
  1739. BIND_ENUM_CONSTANT(SHAPE_CROSS)
  1740. }
  1741. RibbonTrailMesh::RibbonTrailMesh() {
  1742. }
  1743. /*************************************************************************/
  1744. /* TextMesh */
  1745. /*************************************************************************/
  1746. void TextMesh::_generate_glyph_mesh_data(uint32_t p_hash, const Glyph &p_gl) const {
  1747. if (cache.has(p_hash)) {
  1748. return;
  1749. }
  1750. GlyphMeshData &gl_data = cache[p_hash];
  1751. Dictionary d = TS->font_get_glyph_contours(p_gl.font_rid, p_gl.font_size, p_gl.index);
  1752. Vector2 origin = Vector2(p_gl.x_off, p_gl.y_off) * pixel_size;
  1753. PackedVector3Array points = d["points"];
  1754. PackedInt32Array contours = d["contours"];
  1755. bool orientation = d["orientation"];
  1756. if (points.size() < 3 || contours.size() < 1) {
  1757. return; // No full contours, only glyph control points (or nothing), ignore.
  1758. }
  1759. // Approximate Bezier curves as polygons.
  1760. // See https://freetype.org/freetype2/docs/glyphs/glyphs-6.html, for more info.
  1761. for (int i = 0; i < contours.size(); i++) {
  1762. int32_t start = (i == 0) ? 0 : (contours[i - 1] + 1);
  1763. int32_t end = contours[i];
  1764. Vector<ContourPoint> polygon;
  1765. for (int32_t j = start; j <= end; j++) {
  1766. if (points[j].z == TextServer::CONTOUR_CURVE_TAG_ON) {
  1767. // Point on the curve.
  1768. Vector2 p = Vector2(points[j].x, points[j].y) * pixel_size + origin;
  1769. polygon.push_back(ContourPoint(p, true));
  1770. } else if (points[j].z == TextServer::CONTOUR_CURVE_TAG_OFF_CONIC) {
  1771. // Conic Bezier arc.
  1772. int32_t next = (j == end) ? start : (j + 1);
  1773. int32_t prev = (j == start) ? end : (j - 1);
  1774. Vector2 p0;
  1775. Vector2 p1 = Vector2(points[j].x, points[j].y);
  1776. Vector2 p2;
  1777. // For successive conic OFF points add a virtual ON point in the middle.
  1778. if (points[prev].z == TextServer::CONTOUR_CURVE_TAG_OFF_CONIC) {
  1779. p0 = (Vector2(points[prev].x, points[prev].y) + Vector2(points[j].x, points[j].y)) / 2.0;
  1780. } else if (points[prev].z == TextServer::CONTOUR_CURVE_TAG_ON) {
  1781. p0 = Vector2(points[prev].x, points[prev].y);
  1782. } else {
  1783. ERR_FAIL_MSG(vformat("Invalid conic arc point sequence at %d:%d", i, j));
  1784. }
  1785. if (points[next].z == TextServer::CONTOUR_CURVE_TAG_OFF_CONIC) {
  1786. p2 = (Vector2(points[j].x, points[j].y) + Vector2(points[next].x, points[next].y)) / 2.0;
  1787. } else if (points[next].z == TextServer::CONTOUR_CURVE_TAG_ON) {
  1788. p2 = Vector2(points[next].x, points[next].y);
  1789. } else {
  1790. ERR_FAIL_MSG(vformat("Invalid conic arc point sequence at %d:%d", i, j));
  1791. }
  1792. real_t step = CLAMP(curve_step / (p0 - p2).length(), 0.01, 0.5);
  1793. real_t t = step;
  1794. while (t < 1.0) {
  1795. real_t omt = (1.0 - t);
  1796. real_t omt2 = omt * omt;
  1797. real_t t2 = t * t;
  1798. Vector2 point = p1 + omt2 * (p0 - p1) + t2 * (p2 - p1);
  1799. Vector2 p = point * pixel_size + origin;
  1800. polygon.push_back(ContourPoint(p, false));
  1801. t += step;
  1802. }
  1803. } else if (points[j].z == TextServer::CONTOUR_CURVE_TAG_OFF_CUBIC) {
  1804. // Cubic Bezier arc.
  1805. int32_t cur = j;
  1806. int32_t next1 = (j == end) ? start : (j + 1);
  1807. int32_t next2 = (next1 == end) ? start : (next1 + 1);
  1808. int32_t prev = (j == start) ? end : (j - 1);
  1809. // There must be exactly two OFF points and two ON points for each cubic arc.
  1810. if (points[prev].z != TextServer::CONTOUR_CURVE_TAG_ON) {
  1811. cur = (cur == 0) ? end : cur - 1;
  1812. next1 = (next1 == 0) ? end : next1 - 1;
  1813. next2 = (next2 == 0) ? end : next2 - 1;
  1814. prev = (prev == 0) ? end : prev - 1;
  1815. } else {
  1816. j++;
  1817. }
  1818. ERR_FAIL_COND_MSG(points[prev].z != TextServer::CONTOUR_CURVE_TAG_ON, vformat("Invalid cubic arc point sequence at %d:%d", i, prev));
  1819. ERR_FAIL_COND_MSG(points[cur].z != TextServer::CONTOUR_CURVE_TAG_OFF_CUBIC, vformat("Invalid cubic arc point sequence at %d:%d", i, cur));
  1820. ERR_FAIL_COND_MSG(points[next1].z != TextServer::CONTOUR_CURVE_TAG_OFF_CUBIC, vformat("Invalid cubic arc point sequence at %d:%d", i, next1));
  1821. ERR_FAIL_COND_MSG(points[next2].z != TextServer::CONTOUR_CURVE_TAG_ON, vformat("Invalid cubic arc point sequence at %d:%d", i, next2));
  1822. Vector2 p0 = Vector2(points[prev].x, points[prev].y);
  1823. Vector2 p1 = Vector2(points[cur].x, points[cur].y);
  1824. Vector2 p2 = Vector2(points[next1].x, points[next1].y);
  1825. Vector2 p3 = Vector2(points[next2].x, points[next2].y);
  1826. real_t step = CLAMP(curve_step / (p0 - p3).length(), 0.01, 0.5);
  1827. real_t t = step;
  1828. while (t < 1.0) {
  1829. real_t omt = (1.0 - t);
  1830. real_t omt2 = omt * omt;
  1831. real_t omt3 = omt2 * omt;
  1832. real_t t2 = t * t;
  1833. real_t t3 = t2 * t;
  1834. Vector2 point = p0 * omt3 + p1 * omt2 * t * 3.0 + p2 * omt * t2 * 3.0 + p3 * t3;
  1835. Vector2 p = point * pixel_size + origin;
  1836. polygon.push_back(ContourPoint(p, false));
  1837. t += step;
  1838. }
  1839. } else {
  1840. ERR_FAIL_MSG(vformat("Unknown point tag at %d:%d", i, j));
  1841. }
  1842. }
  1843. if (polygon.size() < 3) {
  1844. continue; // Skip glyph control points.
  1845. }
  1846. if (!orientation) {
  1847. polygon.reverse();
  1848. }
  1849. gl_data.contours.push_back(polygon);
  1850. }
  1851. // Calculate bounds.
  1852. List<TPPLPoly> in_poly;
  1853. for (int i = 0; i < gl_data.contours.size(); i++) {
  1854. TPPLPoly inp;
  1855. inp.Init(gl_data.contours[i].size());
  1856. real_t length = 0.0;
  1857. for (int j = 0; j < gl_data.contours[i].size(); j++) {
  1858. int next = (j + 1 == gl_data.contours[i].size()) ? 0 : (j + 1);
  1859. gl_data.min_p.x = MIN(gl_data.min_p.x, gl_data.contours[i][j].point.x);
  1860. gl_data.min_p.y = MIN(gl_data.min_p.y, gl_data.contours[i][j].point.y);
  1861. gl_data.max_p.x = MAX(gl_data.max_p.x, gl_data.contours[i][j].point.x);
  1862. gl_data.max_p.y = MAX(gl_data.max_p.y, gl_data.contours[i][j].point.y);
  1863. length += (gl_data.contours[i][next].point - gl_data.contours[i][j].point).length();
  1864. inp.GetPoint(j) = gl_data.contours[i][j].point;
  1865. }
  1866. TPPLOrientation poly_orient = inp.GetOrientation();
  1867. if (poly_orient == TPPL_ORIENTATION_CW) {
  1868. inp.SetHole(true);
  1869. }
  1870. in_poly.push_back(inp);
  1871. gl_data.contours_info.push_back(ContourInfo(length, poly_orient == TPPL_ORIENTATION_CCW));
  1872. }
  1873. TPPLPartition tpart;
  1874. //Decompose and triangulate.
  1875. List<TPPLPoly> out_poly;
  1876. if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) {
  1877. ERR_FAIL_MSG("Convex decomposing failed!");
  1878. }
  1879. List<TPPLPoly> out_tris;
  1880. for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
  1881. if (tpart.Triangulate_OPT(&(I->get()), &out_tris) == 0) {
  1882. ERR_FAIL_MSG("Triangulation failed!");
  1883. }
  1884. }
  1885. for (List<TPPLPoly>::Element *I = out_tris.front(); I; I = I->next()) {
  1886. TPPLPoly &tp = I->get();
  1887. ERR_FAIL_COND(tp.GetNumPoints() != 3); // Trianges only.
  1888. for (int i = 0; i < 3; i++) {
  1889. gl_data.triangles.push_back(Vector2(tp.GetPoint(i).x, tp.GetPoint(i).y));
  1890. }
  1891. }
  1892. }
  1893. void TextMesh::_create_mesh_array(Array &p_arr) const {
  1894. Ref<Font> font = _get_font_or_default();
  1895. ERR_FAIL_COND(font.is_null());
  1896. if (dirty_cache) {
  1897. cache.clear();
  1898. dirty_cache = false;
  1899. }
  1900. // Update text buffer.
  1901. if (dirty_text) {
  1902. TS->shaped_text_clear(text_rid);
  1903. TS->shaped_text_set_direction(text_rid, text_direction);
  1904. String text = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
  1905. TS->shaped_text_add_string(text_rid, text, font->get_rids(), font_size, opentype_features, language);
  1906. Array stt;
  1907. if (st_parser == TextServer::STRUCTURED_TEXT_CUSTOM) {
  1908. GDVIRTUAL_CALL(_structured_text_parser, st_args, text, stt);
  1909. } else {
  1910. stt = TS->parse_structured_text(st_parser, st_args, text);
  1911. }
  1912. TS->shaped_text_set_bidi_override(text_rid, stt);
  1913. dirty_text = false;
  1914. dirty_font = false;
  1915. } else if (dirty_font) {
  1916. int spans = TS->shaped_get_span_count(text_rid);
  1917. for (int i = 0; i < spans; i++) {
  1918. TS->shaped_set_span_update_font(text_rid, i, font->get_rids(), font_size, opentype_features);
  1919. }
  1920. dirty_font = false;
  1921. }
  1922. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  1923. TS->shaped_text_fit_to_width(text_rid, width, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  1924. } else {
  1925. TS->shaped_text_fit_to_width(text_rid, -1, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  1926. }
  1927. Vector2 offset;
  1928. const Glyph *glyphs = TS->shaped_text_get_glyphs(text_rid);
  1929. int gl_size = TS->shaped_text_get_glyph_count(text_rid);
  1930. float line_width = TS->shaped_text_get_width(text_rid) * pixel_size;
  1931. switch (horizontal_alignment) {
  1932. case HORIZONTAL_ALIGNMENT_LEFT:
  1933. offset.x = 0.0;
  1934. break;
  1935. case HORIZONTAL_ALIGNMENT_FILL:
  1936. case HORIZONTAL_ALIGNMENT_CENTER: {
  1937. offset.x = -line_width / 2.0;
  1938. } break;
  1939. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1940. offset.x = -line_width;
  1941. } break;
  1942. }
  1943. bool has_depth = !Math::is_zero_approx(depth);
  1944. // Generate glyph data, precalculate size of the arrays and mesh bounds for UV.
  1945. int64_t p_size = 0;
  1946. int64_t i_size = 0;
  1947. Vector2 min_p = Vector2(INFINITY, INFINITY);
  1948. Vector2 max_p = Vector2(-INFINITY, -INFINITY);
  1949. Vector2 offset_pre = offset;
  1950. for (int i = 0; i < gl_size; i++) {
  1951. if (glyphs[i].font_rid != RID()) {
  1952. uint32_t hash = hash_one_uint64(glyphs[i].font_rid.get_id());
  1953. hash = hash_djb2_one_32(glyphs[i].index, hash);
  1954. _generate_glyph_mesh_data(hash, glyphs[i]);
  1955. GlyphMeshData &gl_data = cache[hash];
  1956. p_size += glyphs[i].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1);
  1957. i_size += glyphs[i].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1);
  1958. if (has_depth) {
  1959. for (int j = 0; j < gl_data.contours.size(); j++) {
  1960. p_size += glyphs[i].repeat * gl_data.contours[j].size() * 4;
  1961. i_size += glyphs[i].repeat * gl_data.contours[j].size() * 6;
  1962. }
  1963. }
  1964. for (int j = 0; j < glyphs[i].repeat; j++) {
  1965. min_p.x = MIN(gl_data.min_p.x + offset_pre.x, min_p.x);
  1966. min_p.y = MIN(gl_data.min_p.y + offset_pre.y, min_p.y);
  1967. max_p.x = MAX(gl_data.max_p.x + offset_pre.x, max_p.x);
  1968. max_p.y = MAX(gl_data.max_p.y + offset_pre.y, max_p.y);
  1969. offset_pre.x += glyphs[i].advance * pixel_size;
  1970. }
  1971. } else {
  1972. p_size += glyphs[i].repeat * 4;
  1973. i_size += glyphs[i].repeat * 6;
  1974. offset_pre.x += glyphs[i].advance * pixel_size * glyphs[i].repeat;
  1975. }
  1976. }
  1977. Vector<Vector3> vertices;
  1978. Vector<Vector3> normals;
  1979. Vector<float> tangents;
  1980. Vector<Vector2> uvs;
  1981. Vector<int32_t> indices;
  1982. vertices.resize(p_size);
  1983. normals.resize(p_size);
  1984. uvs.resize(p_size);
  1985. tangents.resize(p_size * 4);
  1986. indices.resize(i_size);
  1987. Vector3 *vertices_ptr = vertices.ptrw();
  1988. Vector3 *normals_ptr = normals.ptrw();
  1989. float *tangents_ptr = tangents.ptrw();
  1990. Vector2 *uvs_ptr = uvs.ptrw();
  1991. int32_t *indices_ptr = indices.ptrw();
  1992. // Generate mesh.
  1993. int32_t p_idx = 0;
  1994. int32_t i_idx = 0;
  1995. for (int i = 0; i < gl_size; i++) {
  1996. if (glyphs[i].font_rid != RID()) {
  1997. uint32_t hash = hash_one_uint64(glyphs[i].font_rid.get_id());
  1998. hash = hash_djb2_one_32(glyphs[i].index, hash);
  1999. const GlyphMeshData &gl_data = cache[hash];
  2000. int64_t ts = gl_data.triangles.size();
  2001. const Vector2 *ts_ptr = gl_data.triangles.ptr();
  2002. for (int j = 0; j < glyphs[i].repeat; j++) {
  2003. for (int k = 0; k < ts; k += 3) {
  2004. // Add front face.
  2005. for (int l = 0; l < 3; l++) {
  2006. Vector3 point = Vector3(ts_ptr[k + l].x + offset.x, -ts_ptr[k + l].y + offset.y, depth / 2.0);
  2007. vertices_ptr[p_idx] = point;
  2008. normals_ptr[p_idx] = Vector3(0.0, 0.0, 1.0);
  2009. if (has_depth) {
  2010. uvs_ptr[p_idx] = Vector2(Math::range_lerp(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(point.y, -min_p.y, -max_p.y, real_t(0.0), real_t(0.4)));
  2011. } else {
  2012. uvs_ptr[p_idx] = Vector2(Math::range_lerp(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(point.y, -min_p.y, -max_p.y, real_t(0.0), real_t(1.0)));
  2013. }
  2014. tangents_ptr[p_idx * 4 + 0] = 1.0;
  2015. tangents_ptr[p_idx * 4 + 1] = 0.0;
  2016. tangents_ptr[p_idx * 4 + 2] = 0.0;
  2017. tangents_ptr[p_idx * 4 + 3] = 1.0;
  2018. indices_ptr[i_idx++] = p_idx;
  2019. p_idx++;
  2020. }
  2021. if (has_depth) {
  2022. // Add back face.
  2023. for (int l = 2; l >= 0; l--) {
  2024. Vector3 point = Vector3(ts_ptr[k + l].x + offset.x, -ts_ptr[k + l].y + offset.y, -depth / 2.0);
  2025. vertices_ptr[p_idx] = point;
  2026. normals_ptr[p_idx] = Vector3(0.0, 0.0, -1.0);
  2027. uvs_ptr[p_idx] = Vector2(Math::range_lerp(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(point.y, -min_p.y, -max_p.y, real_t(0.4), real_t(0.8)));
  2028. tangents_ptr[p_idx * 4 + 0] = -1.0;
  2029. tangents_ptr[p_idx * 4 + 1] = 0.0;
  2030. tangents_ptr[p_idx * 4 + 2] = 0.0;
  2031. tangents_ptr[p_idx * 4 + 3] = 1.0;
  2032. indices_ptr[i_idx++] = p_idx;
  2033. p_idx++;
  2034. }
  2035. }
  2036. }
  2037. // Add sides.
  2038. if (has_depth) {
  2039. for (int k = 0; k < gl_data.contours.size(); k++) {
  2040. int64_t ps = gl_data.contours[k].size();
  2041. const ContourPoint *ps_ptr = gl_data.contours[k].ptr();
  2042. const ContourInfo &ps_info = gl_data.contours_info[k];
  2043. real_t length = 0.0;
  2044. for (int l = 0; l < ps; l++) {
  2045. int prev = (l == 0) ? (ps - 1) : (l - 1);
  2046. int next = (l + 1 == ps) ? 0 : (l + 1);
  2047. Vector2 d1;
  2048. Vector2 d2 = (ps_ptr[next].point - ps_ptr[l].point).normalized();
  2049. if (ps_ptr[l].sharp) {
  2050. d1 = d2;
  2051. } else {
  2052. d1 = (ps_ptr[l].point - ps_ptr[prev].point).normalized();
  2053. }
  2054. real_t seg_len = (ps_ptr[next].point - ps_ptr[l].point).length();
  2055. Vector3 quad_faces[4] = {
  2056. Vector3(ps_ptr[l].point.x + offset.x, -ps_ptr[l].point.y + offset.y, -depth / 2.0),
  2057. Vector3(ps_ptr[next].point.x + offset.x, -ps_ptr[next].point.y + offset.y, -depth / 2.0),
  2058. Vector3(ps_ptr[l].point.x + offset.x, -ps_ptr[l].point.y + offset.y, depth / 2.0),
  2059. Vector3(ps_ptr[next].point.x + offset.x, -ps_ptr[next].point.y + offset.y, depth / 2.0),
  2060. };
  2061. for (int m = 0; m < 4; m++) {
  2062. const Vector2 &d = ((m % 2) == 0) ? d1 : d2;
  2063. real_t u_pos = ((m % 2) == 0) ? length : length + seg_len;
  2064. vertices_ptr[p_idx + m] = quad_faces[m];
  2065. normals_ptr[p_idx + m] = Vector3(d.y, d.x, 0.0);
  2066. if (m < 2) {
  2067. uvs_ptr[p_idx + m] = Vector2(Math::range_lerp(u_pos, 0, ps_info.length, real_t(0.0), real_t(1.0)), (ps_info.ccw) ? 0.8 : 0.9);
  2068. } else {
  2069. uvs_ptr[p_idx + m] = Vector2(Math::range_lerp(u_pos, 0, ps_info.length, real_t(0.0), real_t(1.0)), (ps_info.ccw) ? 0.9 : 1.0);
  2070. }
  2071. tangents_ptr[(p_idx + m) * 4 + 0] = d.x;
  2072. tangents_ptr[(p_idx + m) * 4 + 1] = -d.y;
  2073. tangents_ptr[(p_idx + m) * 4 + 2] = 0.0;
  2074. tangents_ptr[(p_idx + m) * 4 + 3] = 1.0;
  2075. }
  2076. indices_ptr[i_idx++] = p_idx;
  2077. indices_ptr[i_idx++] = p_idx + 1;
  2078. indices_ptr[i_idx++] = p_idx + 2;
  2079. indices_ptr[i_idx++] = p_idx + 1;
  2080. indices_ptr[i_idx++] = p_idx + 3;
  2081. indices_ptr[i_idx++] = p_idx + 2;
  2082. length += seg_len;
  2083. p_idx += 4;
  2084. }
  2085. }
  2086. }
  2087. offset.x += glyphs[i].advance * pixel_size;
  2088. }
  2089. } else {
  2090. // Add fallback quad for missing glyphs.
  2091. for (int j = 0; j < glyphs[i].repeat; j++) {
  2092. Size2 sz = TS->get_hex_code_box_size(glyphs[i].font_size, glyphs[i].index) * pixel_size;
  2093. Vector3 quad_faces[4] = {
  2094. Vector3(offset.x, offset.y, 0.0),
  2095. Vector3(offset.x, sz.y + offset.y, 0.0),
  2096. Vector3(sz.x + offset.x, sz.y + offset.y, 0.0),
  2097. Vector3(sz.x + offset.x, offset.y, 0.0),
  2098. };
  2099. for (int k = 0; k < 4; k++) {
  2100. vertices_ptr[p_idx + k] = quad_faces[k];
  2101. normals_ptr[p_idx + k] = Vector3(0.0, 0.0, 1.0);
  2102. if (has_depth) {
  2103. uvs_ptr[p_idx + k] = Vector2(Math::range_lerp(quad_faces[k].x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(quad_faces[k].y, -min_p.y, -max_p.y, real_t(0.0), real_t(0.4)));
  2104. } else {
  2105. uvs_ptr[p_idx + k] = Vector2(Math::range_lerp(quad_faces[k].x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(quad_faces[k].y, -min_p.y, -max_p.y, real_t(0.0), real_t(1.0)));
  2106. }
  2107. tangents_ptr[(p_idx + k) * 4 + 0] = 1.0;
  2108. tangents_ptr[(p_idx + k) * 4 + 1] = 0.0;
  2109. tangents_ptr[(p_idx + k) * 4 + 2] = 0.0;
  2110. tangents_ptr[(p_idx + k) * 4 + 3] = 1.0;
  2111. }
  2112. indices_ptr[i_idx++] = p_idx;
  2113. indices_ptr[i_idx++] = p_idx + 1;
  2114. indices_ptr[i_idx++] = p_idx + 2;
  2115. indices_ptr[i_idx++] = p_idx + 0;
  2116. indices_ptr[i_idx++] = p_idx + 2;
  2117. indices_ptr[i_idx++] = p_idx + 3;
  2118. p_idx += 4;
  2119. offset.x += glyphs[i].advance * pixel_size;
  2120. }
  2121. }
  2122. }
  2123. if (p_size == 0) {
  2124. // If empty, add single trinagle to suppress errors.
  2125. vertices.push_back(Vector3());
  2126. normals.push_back(Vector3());
  2127. uvs.push_back(Vector2());
  2128. tangents.push_back(1.0);
  2129. tangents.push_back(0.0);
  2130. tangents.push_back(0.0);
  2131. tangents.push_back(1.0);
  2132. indices.push_back(0);
  2133. indices.push_back(0);
  2134. indices.push_back(0);
  2135. }
  2136. p_arr[RS::ARRAY_VERTEX] = vertices;
  2137. p_arr[RS::ARRAY_NORMAL] = normals;
  2138. p_arr[RS::ARRAY_TANGENT] = tangents;
  2139. p_arr[RS::ARRAY_TEX_UV] = uvs;
  2140. p_arr[RS::ARRAY_INDEX] = indices;
  2141. }
  2142. void TextMesh::_bind_methods() {
  2143. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &TextMesh::set_horizontal_alignment);
  2144. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &TextMesh::get_horizontal_alignment);
  2145. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextMesh::set_text);
  2146. ClassDB::bind_method(D_METHOD("get_text"), &TextMesh::get_text);
  2147. ClassDB::bind_method(D_METHOD("set_font", "font"), &TextMesh::set_font);
  2148. ClassDB::bind_method(D_METHOD("get_font"), &TextMesh::get_font);
  2149. ClassDB::bind_method(D_METHOD("set_font_size", "font_size"), &TextMesh::set_font_size);
  2150. ClassDB::bind_method(D_METHOD("get_font_size"), &TextMesh::get_font_size);
  2151. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &TextMesh::set_depth);
  2152. ClassDB::bind_method(D_METHOD("get_depth"), &TextMesh::get_depth);
  2153. ClassDB::bind_method(D_METHOD("set_width", "width"), &TextMesh::set_width);
  2154. ClassDB::bind_method(D_METHOD("get_width"), &TextMesh::get_width);
  2155. ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &TextMesh::set_pixel_size);
  2156. ClassDB::bind_method(D_METHOD("get_pixel_size"), &TextMesh::get_pixel_size);
  2157. ClassDB::bind_method(D_METHOD("set_curve_step", "curve_step"), &TextMesh::set_curve_step);
  2158. ClassDB::bind_method(D_METHOD("get_curve_step"), &TextMesh::get_curve_step);
  2159. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &TextMesh::set_text_direction);
  2160. ClassDB::bind_method(D_METHOD("get_text_direction"), &TextMesh::get_text_direction);
  2161. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &TextMesh::set_opentype_feature);
  2162. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &TextMesh::get_opentype_feature);
  2163. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &TextMesh::clear_opentype_features);
  2164. ClassDB::bind_method(D_METHOD("set_language", "language"), &TextMesh::set_language);
  2165. ClassDB::bind_method(D_METHOD("get_language"), &TextMesh::get_language);
  2166. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &TextMesh::set_structured_text_bidi_override);
  2167. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &TextMesh::get_structured_text_bidi_override);
  2168. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &TextMesh::set_structured_text_bidi_override_options);
  2169. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &TextMesh::get_structured_text_bidi_override_options);
  2170. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &TextMesh::set_uppercase);
  2171. ClassDB::bind_method(D_METHOD("is_uppercase"), &TextMesh::is_uppercase);
  2172. ClassDB::bind_method(D_METHOD("_font_changed"), &TextMesh::_font_changed);
  2173. ClassDB::bind_method(D_METHOD("_request_update"), &TextMesh::_request_update);
  2174. ADD_GROUP("Text", "");
  2175. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  2176. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_font", "get_font");
  2177. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_size", PROPERTY_HINT_RANGE, "1,127,1"), "set_font_size", "get_font_size");
  2178. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  2179. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  2180. 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");
  2181. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  2182. ADD_GROUP("Mesh", "");
  2183. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001"), "set_pixel_size", "get_pixel_size");
  2184. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "curve_step", PROPERTY_HINT_RANGE, "0.1,10,0.1"), "set_curve_step", "get_curve_step");
  2185. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.0,100.0,0.001,or_greater"), "set_depth", "get_depth");
  2186. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width"), "set_width", "get_width");
  2187. ADD_GROUP("Locale", "");
  2188. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left"), "set_text_direction", "get_text_direction");
  2189. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  2190. }
  2191. void TextMesh::_notification(int p_what) {
  2192. switch (p_what) {
  2193. case MainLoop::NOTIFICATION_TRANSLATION_CHANGED: {
  2194. String new_text = tr(text);
  2195. if (new_text == xl_text) {
  2196. return; // Nothing new.
  2197. }
  2198. xl_text = new_text;
  2199. dirty_text = true;
  2200. _request_update();
  2201. } break;
  2202. }
  2203. }
  2204. bool TextMesh::_set(const StringName &p_name, const Variant &p_value) {
  2205. String str = p_name;
  2206. if (str.begins_with("opentype_features/")) {
  2207. String name = str.get_slicec('/', 1);
  2208. int32_t tag = TS->name_to_tag(name);
  2209. int value = p_value;
  2210. if (value == -1) {
  2211. if (opentype_features.has(tag)) {
  2212. opentype_features.erase(tag);
  2213. dirty_font = true;
  2214. _request_update();
  2215. }
  2216. } else {
  2217. if (!opentype_features.has(tag) || (int)opentype_features[tag] != value) {
  2218. opentype_features[tag] = value;
  2219. dirty_font = true;
  2220. _request_update();
  2221. }
  2222. }
  2223. notify_property_list_changed();
  2224. return true;
  2225. }
  2226. return false;
  2227. }
  2228. bool TextMesh::_get(const StringName &p_name, Variant &r_ret) const {
  2229. String str = p_name;
  2230. if (str.begins_with("opentype_features/")) {
  2231. String name = str.get_slicec('/', 1);
  2232. int32_t tag = TS->name_to_tag(name);
  2233. if (opentype_features.has(tag)) {
  2234. r_ret = opentype_features[tag];
  2235. return true;
  2236. } else {
  2237. r_ret = -1;
  2238. return true;
  2239. }
  2240. }
  2241. return false;
  2242. }
  2243. void TextMesh::_get_property_list(List<PropertyInfo> *p_list) const {
  2244. for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
  2245. String name = TS->tag_to_name(*ftr);
  2246. p_list->push_back(PropertyInfo(Variant::INT, "opentype_features/" + name));
  2247. }
  2248. p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  2249. }
  2250. TextMesh::TextMesh() {
  2251. primitive_type = PRIMITIVE_TRIANGLES;
  2252. text_rid = TS->create_shaped_text();
  2253. }
  2254. TextMesh::~TextMesh() {
  2255. TS->free_rid(text_rid);
  2256. }
  2257. void TextMesh::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  2258. ERR_FAIL_INDEX((int)p_alignment, 4);
  2259. if (horizontal_alignment != p_alignment) {
  2260. horizontal_alignment = p_alignment;
  2261. _request_update();
  2262. }
  2263. }
  2264. HorizontalAlignment TextMesh::get_horizontal_alignment() const {
  2265. return horizontal_alignment;
  2266. }
  2267. void TextMesh::set_text(const String &p_string) {
  2268. if (text != p_string) {
  2269. text = p_string;
  2270. xl_text = tr(text);
  2271. dirty_text = true;
  2272. _request_update();
  2273. }
  2274. }
  2275. String TextMesh::get_text() const {
  2276. return text;
  2277. }
  2278. void TextMesh::_font_changed() {
  2279. dirty_font = true;
  2280. dirty_cache = true;
  2281. call_deferred(SNAME("_request_update"));
  2282. }
  2283. void TextMesh::set_font(const Ref<Font> &p_font) {
  2284. if (font_override != p_font) {
  2285. if (font_override.is_valid()) {
  2286. font_override->disconnect(CoreStringNames::get_singleton()->changed, Callable(this, "_font_changed"));
  2287. }
  2288. font_override = p_font;
  2289. dirty_font = true;
  2290. dirty_cache = true;
  2291. if (font_override.is_valid()) {
  2292. font_override->connect(CoreStringNames::get_singleton()->changed, Callable(this, "_font_changed"));
  2293. }
  2294. _request_update();
  2295. }
  2296. }
  2297. Ref<Font> TextMesh::get_font() const {
  2298. return font_override;
  2299. }
  2300. Ref<Font> TextMesh::_get_font_or_default() const {
  2301. if (font_override.is_valid() && font_override->get_data_count() > 0) {
  2302. return font_override;
  2303. }
  2304. // Check the project-defined Theme resource.
  2305. if (Theme::get_project_default().is_valid()) {
  2306. List<StringName> theme_types;
  2307. Theme::get_project_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
  2308. for (const StringName &E : theme_types) {
  2309. if (Theme::get_project_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) {
  2310. return Theme::get_project_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E);
  2311. }
  2312. }
  2313. }
  2314. // Lastly, fall back on the items defined in the default Theme, if they exist.
  2315. {
  2316. List<StringName> theme_types;
  2317. Theme::get_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
  2318. for (const StringName &E : theme_types) {
  2319. if (Theme::get_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) {
  2320. return Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E);
  2321. }
  2322. }
  2323. }
  2324. // If they don't exist, use any type to return the default/empty value.
  2325. return Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName());
  2326. }
  2327. void TextMesh::set_font_size(int p_size) {
  2328. if (font_size != p_size) {
  2329. font_size = CLAMP(p_size, 1, 127);
  2330. dirty_font = true;
  2331. dirty_cache = true;
  2332. _request_update();
  2333. }
  2334. }
  2335. int TextMesh::get_font_size() const {
  2336. return font_size;
  2337. }
  2338. void TextMesh::set_depth(real_t p_depth) {
  2339. if (depth != p_depth) {
  2340. depth = MAX(p_depth, 0.0);
  2341. _request_update();
  2342. }
  2343. }
  2344. real_t TextMesh::get_depth() const {
  2345. return depth;
  2346. }
  2347. void TextMesh::set_width(real_t p_width) {
  2348. if (width != p_width) {
  2349. width = p_width;
  2350. _request_update();
  2351. }
  2352. }
  2353. real_t TextMesh::get_width() const {
  2354. return width;
  2355. }
  2356. void TextMesh::set_pixel_size(real_t p_amount) {
  2357. if (pixel_size != p_amount) {
  2358. pixel_size = CLAMP(p_amount, 0.0001, 128.0);
  2359. dirty_cache = true;
  2360. _request_update();
  2361. }
  2362. }
  2363. real_t TextMesh::get_pixel_size() const {
  2364. return pixel_size;
  2365. }
  2366. void TextMesh::set_curve_step(real_t p_step) {
  2367. if (curve_step != p_step) {
  2368. curve_step = CLAMP(p_step, 0.1, 10.0);
  2369. dirty_cache = true;
  2370. _request_update();
  2371. }
  2372. }
  2373. real_t TextMesh::get_curve_step() const {
  2374. return curve_step;
  2375. }
  2376. void TextMesh::set_text_direction(TextServer::Direction p_text_direction) {
  2377. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  2378. if (text_direction != p_text_direction) {
  2379. text_direction = p_text_direction;
  2380. dirty_text = true;
  2381. _request_update();
  2382. }
  2383. }
  2384. TextServer::Direction TextMesh::get_text_direction() const {
  2385. return text_direction;
  2386. }
  2387. void TextMesh::clear_opentype_features() {
  2388. opentype_features.clear();
  2389. dirty_font = true;
  2390. _request_update();
  2391. }
  2392. void TextMesh::set_opentype_feature(const String &p_name, int p_value) {
  2393. int32_t tag = TS->name_to_tag(p_name);
  2394. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  2395. opentype_features[tag] = p_value;
  2396. dirty_font = true;
  2397. _request_update();
  2398. }
  2399. }
  2400. int TextMesh::get_opentype_feature(const String &p_name) const {
  2401. int32_t tag = TS->name_to_tag(p_name);
  2402. if (!opentype_features.has(tag)) {
  2403. return -1;
  2404. }
  2405. return opentype_features[tag];
  2406. }
  2407. void TextMesh::set_language(const String &p_language) {
  2408. if (language != p_language) {
  2409. language = p_language;
  2410. dirty_text = true;
  2411. _request_update();
  2412. }
  2413. }
  2414. String TextMesh::get_language() const {
  2415. return language;
  2416. }
  2417. void TextMesh::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  2418. if (st_parser != p_parser) {
  2419. st_parser = p_parser;
  2420. dirty_text = true;
  2421. _request_update();
  2422. }
  2423. }
  2424. TextServer::StructuredTextParser TextMesh::get_structured_text_bidi_override() const {
  2425. return st_parser;
  2426. }
  2427. void TextMesh::set_structured_text_bidi_override_options(Array p_args) {
  2428. if (st_args != p_args) {
  2429. st_args = p_args;
  2430. dirty_text = true;
  2431. _request_update();
  2432. }
  2433. }
  2434. Array TextMesh::get_structured_text_bidi_override_options() const {
  2435. return st_args;
  2436. }
  2437. void TextMesh::set_uppercase(bool p_uppercase) {
  2438. if (uppercase != p_uppercase) {
  2439. uppercase = p_uppercase;
  2440. dirty_text = true;
  2441. _request_update();
  2442. }
  2443. }
  2444. bool TextMesh::is_uppercase() const {
  2445. return uppercase;
  2446. }