mesh.cpp 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  1. /**************************************************************************/
  2. /* mesh.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "mesh.h"
  31. #include "core/math/convex_hull.h"
  32. #include "core/templates/pair.h"
  33. #include "scene/resources/surface_tool.h"
  34. #ifndef _3D_DISABLED
  35. #include "scene/resources/3d/concave_polygon_shape_3d.h"
  36. #include "scene/resources/3d/convex_polygon_shape_3d.h"
  37. #endif // _3D_DISABLED
  38. void MeshConvexDecompositionSettings::set_max_concavity(real_t p_max_concavity) {
  39. max_concavity = CLAMP(p_max_concavity, 0.001, 1.0);
  40. }
  41. real_t MeshConvexDecompositionSettings::get_max_concavity() const {
  42. return max_concavity;
  43. }
  44. void MeshConvexDecompositionSettings::set_symmetry_planes_clipping_bias(real_t p_symmetry_planes_clipping_bias) {
  45. symmetry_planes_clipping_bias = CLAMP(p_symmetry_planes_clipping_bias, 0.0, 1.0);
  46. }
  47. real_t MeshConvexDecompositionSettings::get_symmetry_planes_clipping_bias() const {
  48. return symmetry_planes_clipping_bias;
  49. }
  50. void MeshConvexDecompositionSettings::set_revolution_axes_clipping_bias(real_t p_revolution_axes_clipping_bias) {
  51. revolution_axes_clipping_bias = CLAMP(p_revolution_axes_clipping_bias, 0.0, 1.0);
  52. }
  53. real_t MeshConvexDecompositionSettings::get_revolution_axes_clipping_bias() const {
  54. return revolution_axes_clipping_bias;
  55. }
  56. void MeshConvexDecompositionSettings::set_min_volume_per_convex_hull(real_t p_min_volume_per_convex_hull) {
  57. min_volume_per_convex_hull = CLAMP(p_min_volume_per_convex_hull, 0.0001, 0.01);
  58. }
  59. real_t MeshConvexDecompositionSettings::get_min_volume_per_convex_hull() const {
  60. return min_volume_per_convex_hull;
  61. }
  62. void MeshConvexDecompositionSettings::set_resolution(uint32_t p_resolution) {
  63. resolution = p_resolution < 10'000 ? 10'000 : (p_resolution > 100'000 ? 100'000 : p_resolution);
  64. }
  65. uint32_t MeshConvexDecompositionSettings::get_resolution() const {
  66. return resolution;
  67. }
  68. void MeshConvexDecompositionSettings::set_max_num_vertices_per_convex_hull(uint32_t p_max_num_vertices_per_convex_hull) {
  69. max_num_vertices_per_convex_hull = p_max_num_vertices_per_convex_hull < 4 ? 4 : (p_max_num_vertices_per_convex_hull > 1024 ? 1024 : p_max_num_vertices_per_convex_hull);
  70. }
  71. uint32_t MeshConvexDecompositionSettings::get_max_num_vertices_per_convex_hull() const {
  72. return max_num_vertices_per_convex_hull;
  73. }
  74. void MeshConvexDecompositionSettings::set_plane_downsampling(uint32_t p_plane_downsampling) {
  75. plane_downsampling = p_plane_downsampling < 1 ? 1 : (p_plane_downsampling > 16 ? 16 : p_plane_downsampling);
  76. }
  77. uint32_t MeshConvexDecompositionSettings::get_plane_downsampling() const {
  78. return plane_downsampling;
  79. }
  80. void MeshConvexDecompositionSettings::set_convex_hull_downsampling(uint32_t p_convex_hull_downsampling) {
  81. convex_hull_downsampling = p_convex_hull_downsampling < 1 ? 1 : (p_convex_hull_downsampling > 16 ? 16 : p_convex_hull_downsampling);
  82. }
  83. uint32_t MeshConvexDecompositionSettings::get_convex_hull_downsampling() const {
  84. return convex_hull_downsampling;
  85. }
  86. void MeshConvexDecompositionSettings::set_normalize_mesh(bool p_normalize_mesh) {
  87. normalize_mesh = p_normalize_mesh;
  88. }
  89. bool MeshConvexDecompositionSettings::get_normalize_mesh() const {
  90. return normalize_mesh;
  91. }
  92. void MeshConvexDecompositionSettings::set_mode(Mode p_mode) {
  93. mode = p_mode;
  94. }
  95. MeshConvexDecompositionSettings::Mode MeshConvexDecompositionSettings::get_mode() const {
  96. return mode;
  97. }
  98. void MeshConvexDecompositionSettings::set_convex_hull_approximation(bool p_convex_hull_approximation) {
  99. convex_hull_approximation = p_convex_hull_approximation;
  100. }
  101. bool MeshConvexDecompositionSettings::get_convex_hull_approximation() const {
  102. return convex_hull_approximation;
  103. }
  104. void MeshConvexDecompositionSettings::set_max_convex_hulls(uint32_t p_max_convex_hulls) {
  105. max_convex_hulls = p_max_convex_hulls < 1 ? 1 : (p_max_convex_hulls > 32 ? 32 : p_max_convex_hulls);
  106. }
  107. uint32_t MeshConvexDecompositionSettings::get_max_convex_hulls() const {
  108. return max_convex_hulls;
  109. }
  110. void MeshConvexDecompositionSettings::set_project_hull_vertices(bool p_project_hull_vertices) {
  111. project_hull_vertices = p_project_hull_vertices;
  112. }
  113. bool MeshConvexDecompositionSettings::get_project_hull_vertices() const {
  114. return project_hull_vertices;
  115. }
  116. void MeshConvexDecompositionSettings::_bind_methods() {
  117. ClassDB::bind_method(D_METHOD("set_max_concavity", "max_concavity"), &MeshConvexDecompositionSettings::set_max_concavity);
  118. ClassDB::bind_method(D_METHOD("get_max_concavity"), &MeshConvexDecompositionSettings::get_max_concavity);
  119. ClassDB::bind_method(D_METHOD("set_symmetry_planes_clipping_bias", "symmetry_planes_clipping_bias"), &MeshConvexDecompositionSettings::set_symmetry_planes_clipping_bias);
  120. ClassDB::bind_method(D_METHOD("get_symmetry_planes_clipping_bias"), &MeshConvexDecompositionSettings::get_symmetry_planes_clipping_bias);
  121. ClassDB::bind_method(D_METHOD("set_revolution_axes_clipping_bias", "revolution_axes_clipping_bias"), &MeshConvexDecompositionSettings::set_revolution_axes_clipping_bias);
  122. ClassDB::bind_method(D_METHOD("get_revolution_axes_clipping_bias"), &MeshConvexDecompositionSettings::get_revolution_axes_clipping_bias);
  123. ClassDB::bind_method(D_METHOD("set_min_volume_per_convex_hull", "min_volume_per_convex_hull"), &MeshConvexDecompositionSettings::set_min_volume_per_convex_hull);
  124. ClassDB::bind_method(D_METHOD("get_min_volume_per_convex_hull"), &MeshConvexDecompositionSettings::get_min_volume_per_convex_hull);
  125. ClassDB::bind_method(D_METHOD("set_resolution", "min_volume_per_convex_hull"), &MeshConvexDecompositionSettings::set_resolution);
  126. ClassDB::bind_method(D_METHOD("get_resolution"), &MeshConvexDecompositionSettings::get_resolution);
  127. ClassDB::bind_method(D_METHOD("set_max_num_vertices_per_convex_hull", "max_num_vertices_per_convex_hull"), &MeshConvexDecompositionSettings::set_max_num_vertices_per_convex_hull);
  128. ClassDB::bind_method(D_METHOD("get_max_num_vertices_per_convex_hull"), &MeshConvexDecompositionSettings::get_max_num_vertices_per_convex_hull);
  129. ClassDB::bind_method(D_METHOD("set_plane_downsampling", "plane_downsampling"), &MeshConvexDecompositionSettings::set_plane_downsampling);
  130. ClassDB::bind_method(D_METHOD("get_plane_downsampling"), &MeshConvexDecompositionSettings::get_plane_downsampling);
  131. ClassDB::bind_method(D_METHOD("set_convex_hull_downsampling", "convex_hull_downsampling"), &MeshConvexDecompositionSettings::set_convex_hull_downsampling);
  132. ClassDB::bind_method(D_METHOD("get_convex_hull_downsampling"), &MeshConvexDecompositionSettings::get_convex_hull_downsampling);
  133. ClassDB::bind_method(D_METHOD("set_normalize_mesh", "normalize_mesh"), &MeshConvexDecompositionSettings::set_normalize_mesh);
  134. ClassDB::bind_method(D_METHOD("get_normalize_mesh"), &MeshConvexDecompositionSettings::get_normalize_mesh);
  135. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &MeshConvexDecompositionSettings::set_mode);
  136. ClassDB::bind_method(D_METHOD("get_mode"), &MeshConvexDecompositionSettings::get_mode);
  137. ClassDB::bind_method(D_METHOD("set_convex_hull_approximation", "convex_hull_approximation"), &MeshConvexDecompositionSettings::set_convex_hull_approximation);
  138. ClassDB::bind_method(D_METHOD("get_convex_hull_approximation"), &MeshConvexDecompositionSettings::get_convex_hull_approximation);
  139. ClassDB::bind_method(D_METHOD("set_max_convex_hulls", "max_convex_hulls"), &MeshConvexDecompositionSettings::set_max_convex_hulls);
  140. ClassDB::bind_method(D_METHOD("get_max_convex_hulls"), &MeshConvexDecompositionSettings::get_max_convex_hulls);
  141. ClassDB::bind_method(D_METHOD("set_project_hull_vertices", "project_hull_vertices"), &MeshConvexDecompositionSettings::set_project_hull_vertices);
  142. ClassDB::bind_method(D_METHOD("get_project_hull_vertices"), &MeshConvexDecompositionSettings::get_project_hull_vertices);
  143. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_concavity", PROPERTY_HINT_RANGE, "0.001,1.0,0.001"), "set_max_concavity", "get_max_concavity");
  144. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "symmetry_planes_clipping_bias", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_symmetry_planes_clipping_bias", "get_symmetry_planes_clipping_bias");
  145. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "revolution_axes_clipping_bias", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_revolution_axes_clipping_bias", "get_revolution_axes_clipping_bias");
  146. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_volume_per_convex_hull", PROPERTY_HINT_RANGE, "0.0001,0.01,0.0001"), "set_min_volume_per_convex_hull", "get_min_volume_per_convex_hull");
  147. ADD_PROPERTY(PropertyInfo(Variant::INT, "resolution"), "set_resolution", "get_resolution");
  148. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_num_vertices_per_convex_hull"), "set_max_num_vertices_per_convex_hull", "get_max_num_vertices_per_convex_hull");
  149. ADD_PROPERTY(PropertyInfo(Variant::INT, "plane_downsampling", PROPERTY_HINT_RANGE, "1,16,1"), "set_plane_downsampling", "get_plane_downsampling");
  150. ADD_PROPERTY(PropertyInfo(Variant::INT, "convex_hull_downsampling", PROPERTY_HINT_RANGE, "1,16,1"), "set_convex_hull_downsampling", "get_convex_hull_downsampling");
  151. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize_mesh"), "set_normalize_mesh", "get_normalize_mesh");
  152. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Voxel,Tetrahedron"), "set_mode", "get_mode");
  153. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "convex_hull_approximation"), "set_convex_hull_approximation", "get_convex_hull_approximation");
  154. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_convex_hulls"), "set_max_convex_hulls", "get_max_convex_hulls");
  155. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "project_hull_vertices"), "set_project_hull_vertices", "get_project_hull_vertices");
  156. BIND_ENUM_CONSTANT(CONVEX_DECOMPOSITION_MODE_VOXEL);
  157. BIND_ENUM_CONSTANT(CONVEX_DECOMPOSITION_MODE_TETRAHEDRON);
  158. }
  159. Mesh::ConvexDecompositionFunc Mesh::convex_decomposition_function = nullptr;
  160. int Mesh::get_surface_count() const {
  161. int ret = 0;
  162. GDVIRTUAL_CALL(_get_surface_count, ret);
  163. return ret;
  164. }
  165. int Mesh::surface_get_array_len(int p_idx) const {
  166. int ret = 0;
  167. GDVIRTUAL_CALL(_surface_get_array_len, p_idx, ret);
  168. return ret;
  169. }
  170. int Mesh::surface_get_array_index_len(int p_idx) const {
  171. int ret = 0;
  172. GDVIRTUAL_CALL(_surface_get_array_index_len, p_idx, ret);
  173. return ret;
  174. }
  175. Array Mesh::surface_get_arrays(int p_surface) const {
  176. Array ret;
  177. GDVIRTUAL_CALL(_surface_get_arrays, p_surface, ret);
  178. return ret;
  179. }
  180. TypedArray<Array> Mesh::surface_get_blend_shape_arrays(int p_surface) const {
  181. TypedArray<Array> ret;
  182. GDVIRTUAL_CALL(_surface_get_blend_shape_arrays, p_surface, ret);
  183. return ret;
  184. }
  185. Dictionary Mesh::surface_get_lods(int p_surface) const {
  186. Dictionary ret;
  187. GDVIRTUAL_CALL(_surface_get_lods, p_surface, ret);
  188. return ret;
  189. }
  190. BitField<Mesh::ArrayFormat> Mesh::surface_get_format(int p_idx) const {
  191. uint32_t ret = 0;
  192. GDVIRTUAL_CALL(_surface_get_format, p_idx, ret);
  193. return ret;
  194. }
  195. Mesh::PrimitiveType Mesh::surface_get_primitive_type(int p_idx) const {
  196. uint32_t ret = PRIMITIVE_MAX;
  197. GDVIRTUAL_CALL(_surface_get_primitive_type, p_idx, ret);
  198. return (Mesh::PrimitiveType)ret;
  199. }
  200. void Mesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  201. GDVIRTUAL_CALL(_surface_set_material, p_idx, p_material);
  202. }
  203. Ref<Material> Mesh::surface_get_material(int p_idx) const {
  204. Ref<Material> ret;
  205. GDVIRTUAL_CALL(_surface_get_material, p_idx, ret);
  206. return ret;
  207. }
  208. int Mesh::get_blend_shape_count() const {
  209. int ret = 0;
  210. GDVIRTUAL_CALL(_get_blend_shape_count, ret);
  211. return ret;
  212. }
  213. StringName Mesh::get_blend_shape_name(int p_index) const {
  214. StringName ret;
  215. GDVIRTUAL_CALL(_get_blend_shape_name, p_index, ret);
  216. return ret;
  217. }
  218. void Mesh::set_blend_shape_name(int p_index, const StringName &p_name) {
  219. GDVIRTUAL_CALL(_set_blend_shape_name, p_index, p_name);
  220. }
  221. AABB Mesh::get_aabb() const {
  222. AABB ret;
  223. GDVIRTUAL_CALL(_get_aabb, ret);
  224. return ret;
  225. }
  226. Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
  227. if (triangle_mesh.is_valid()) {
  228. return triangle_mesh;
  229. }
  230. int faces_size = 0;
  231. for (int i = 0; i < get_surface_count(); i++) {
  232. switch (surface_get_primitive_type(i)) {
  233. case PRIMITIVE_TRIANGLES: {
  234. int len = (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(i) : surface_get_array_len(i);
  235. // Don't error if zero, it's valid (we'll just skip it later).
  236. ERR_CONTINUE_MSG((len % 3) != 0, vformat("Ignoring surface %d, incorrect %s count: %d (for PRIMITIVE_TRIANGLES).", i, (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? "index" : "vertex", len));
  237. faces_size += len;
  238. } break;
  239. case PRIMITIVE_TRIANGLE_STRIP: {
  240. int len = (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(i) : surface_get_array_len(i);
  241. // Don't error if zero, it's valid (we'll just skip it later).
  242. ERR_CONTINUE_MSG(len != 0 && len < 3, vformat("Ignoring surface %d, incorrect %s count: %d (for PRIMITIVE_TRIANGLE_STRIP).", i, (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? "index" : "vertex", len));
  243. faces_size += (len == 0) ? 0 : (len - 2) * 3;
  244. } break;
  245. default: {
  246. } break;
  247. }
  248. }
  249. if (faces_size == 0) {
  250. return triangle_mesh;
  251. }
  252. Vector<Vector3> faces;
  253. faces.resize(faces_size);
  254. Vector<int32_t> surface_indices;
  255. surface_indices.resize(faces_size / 3);
  256. Vector3 *facesw = faces.ptrw();
  257. int32_t *surface_indicesw = surface_indices.ptrw();
  258. int widx = 0;
  259. for (int i = 0; i < get_surface_count(); i++) {
  260. Mesh::PrimitiveType primitive = surface_get_primitive_type(i);
  261. if (primitive != PRIMITIVE_TRIANGLES && primitive != PRIMITIVE_TRIANGLE_STRIP) {
  262. continue;
  263. }
  264. int len = (surface_get_format(i) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(i) : surface_get_array_len(i);
  265. if ((primitive == PRIMITIVE_TRIANGLES && (len == 0 || (len % 3) != 0)) ||
  266. (primitive == PRIMITIVE_TRIANGLE_STRIP && len < 3) ||
  267. (surface_get_format(i) & ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY)) {
  268. // Error was already shown, just skip (including zero).
  269. continue;
  270. }
  271. Array a = surface_get_arrays(i);
  272. ERR_FAIL_COND_V(a.is_empty(), Ref<TriangleMesh>());
  273. int vc = surface_get_array_len(i);
  274. Vector<Vector3> vertices = a[ARRAY_VERTEX];
  275. ERR_FAIL_COND_V(vertices.is_empty(), Ref<TriangleMesh>());
  276. const Vector3 *vr = vertices.ptr();
  277. int32_t from_index = widx / 3;
  278. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  279. int ic = surface_get_array_index_len(i);
  280. Vector<int> indices = a[ARRAY_INDEX];
  281. const int *ir = indices.ptr();
  282. if (primitive == PRIMITIVE_TRIANGLES) {
  283. for (int j = 0; j < ic; j++) {
  284. int index = ir[j];
  285. ERR_FAIL_COND_V(index >= vc, Ref<TriangleMesh>());
  286. facesw[widx++] = vr[index];
  287. }
  288. } else { // PRIMITIVE_TRIANGLE_STRIP
  289. for (int j = 2; j < ic; j++) {
  290. facesw[widx++] = vr[ir[j - 2]];
  291. facesw[widx++] = vr[ir[j - 1]];
  292. facesw[widx++] = vr[ir[j]];
  293. }
  294. }
  295. } else {
  296. if (primitive == PRIMITIVE_TRIANGLES) {
  297. for (int j = 0; j < vc; j++) {
  298. facesw[widx++] = vr[j];
  299. }
  300. } else { // PRIMITIVE_TRIANGLE_STRIP
  301. for (int j = 2; j < vc; j++) {
  302. facesw[widx++] = vr[j - 2];
  303. facesw[widx++] = vr[j - 1];
  304. facesw[widx++] = vr[j];
  305. }
  306. }
  307. }
  308. int32_t to_index = widx / 3;
  309. for (int j = from_index; j < to_index; j++) {
  310. surface_indicesw[j] = i;
  311. }
  312. }
  313. triangle_mesh.instantiate();
  314. triangle_mesh->create(faces);
  315. return triangle_mesh;
  316. }
  317. Ref<TriangleMesh> Mesh::generate_surface_triangle_mesh(int p_surface) const {
  318. ERR_FAIL_INDEX_V(p_surface, get_surface_count(), Ref<TriangleMesh>());
  319. if (surface_triangle_meshes.size() != get_surface_count()) {
  320. surface_triangle_meshes.resize(get_surface_count());
  321. }
  322. if (surface_triangle_meshes[p_surface].is_valid()) {
  323. return surface_triangle_meshes[p_surface];
  324. }
  325. int facecount = 0;
  326. if (surface_get_primitive_type(p_surface) != PRIMITIVE_TRIANGLES) {
  327. return Ref<TriangleMesh>();
  328. }
  329. if (surface_get_format(p_surface) & ARRAY_FORMAT_INDEX) {
  330. facecount += surface_get_array_index_len(p_surface);
  331. } else {
  332. facecount += surface_get_array_len(p_surface);
  333. }
  334. Vector<Vector3> faces;
  335. faces.resize(facecount);
  336. Vector3 *facesw = faces.ptrw();
  337. Array a = surface_get_arrays(p_surface);
  338. ERR_FAIL_COND_V(a.is_empty(), Ref<TriangleMesh>());
  339. int vc = surface_get_array_len(p_surface);
  340. Vector<Vector3> vertices = a[ARRAY_VERTEX];
  341. const Vector3 *vr = vertices.ptr();
  342. int widx = 0;
  343. if (surface_get_format(p_surface) & ARRAY_FORMAT_INDEX) {
  344. int ic = surface_get_array_index_len(p_surface);
  345. Vector<int> indices = a[ARRAY_INDEX];
  346. const int *ir = indices.ptr();
  347. for (int j = 0; j < ic; j++) {
  348. int index = ir[j];
  349. facesw[widx++] = vr[index];
  350. }
  351. } else {
  352. for (int j = 0; j < vc; j++) {
  353. facesw[widx++] = vr[j];
  354. }
  355. }
  356. Ref<TriangleMesh> tr_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  357. tr_mesh->create(faces);
  358. surface_triangle_meshes.set(p_surface, tr_mesh);
  359. return tr_mesh;
  360. }
  361. void Mesh::generate_debug_mesh_lines(Vector<Vector3> &r_lines) {
  362. if (debug_lines.size() > 0) {
  363. r_lines = debug_lines;
  364. return;
  365. }
  366. Ref<TriangleMesh> tm = generate_triangle_mesh();
  367. if (tm.is_null()) {
  368. return;
  369. }
  370. Vector<int> triangle_indices;
  371. tm->get_indices(&triangle_indices);
  372. const int triangles_num = tm->get_triangles().size();
  373. Vector<Vector3> vertices = tm->get_vertices();
  374. debug_lines.resize(tm->get_triangles().size() * 6); // 3 lines x 2 points each line
  375. const int *ind_r = triangle_indices.ptr();
  376. const Vector3 *ver_r = vertices.ptr();
  377. for (int j = 0, x = 0, i = 0; i < triangles_num; j += 6, x += 3, ++i) {
  378. // Triangle line 1
  379. debug_lines.write[j + 0] = ver_r[ind_r[x + 0]];
  380. debug_lines.write[j + 1] = ver_r[ind_r[x + 1]];
  381. // Triangle line 2
  382. debug_lines.write[j + 2] = ver_r[ind_r[x + 1]];
  383. debug_lines.write[j + 3] = ver_r[ind_r[x + 2]];
  384. // Triangle line 3
  385. debug_lines.write[j + 4] = ver_r[ind_r[x + 2]];
  386. debug_lines.write[j + 5] = ver_r[ind_r[x + 0]];
  387. }
  388. r_lines = debug_lines;
  389. }
  390. void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) {
  391. Ref<TriangleMesh> tm = generate_triangle_mesh();
  392. if (tm.is_null()) {
  393. return;
  394. }
  395. Vector<Vector3> vertices = tm->get_vertices();
  396. int vertices_size = vertices.size();
  397. r_points.resize(vertices_size);
  398. for (int i = 0; i < vertices_size; ++i) {
  399. r_points.write[i] = vertices[i];
  400. }
  401. }
  402. Vector<Vector3> Mesh::_get_faces() const {
  403. return Variant(get_faces());
  404. }
  405. Vector<Face3> Mesh::get_faces() const {
  406. Ref<TriangleMesh> tm = generate_triangle_mesh();
  407. if (tm.is_valid()) {
  408. return tm->get_faces();
  409. }
  410. return Vector<Face3>();
  411. }
  412. Vector<Face3> Mesh::get_surface_faces(int p_surface) const {
  413. Ref<TriangleMesh> tm = generate_surface_triangle_mesh(p_surface);
  414. if (tm.is_valid()) {
  415. return tm->get_faces();
  416. }
  417. return Vector<Face3>();
  418. }
  419. #ifndef _3D_DISABLED
  420. Ref<ConvexPolygonShape3D> Mesh::create_convex_shape(bool p_clean, bool p_simplify) const {
  421. if (p_simplify) {
  422. Ref<MeshConvexDecompositionSettings> settings = Ref<MeshConvexDecompositionSettings>();
  423. settings.instantiate();
  424. settings->set_max_convex_hulls(1);
  425. settings->set_max_concavity(1.0);
  426. Vector<Ref<Shape3D>> decomposed = convex_decompose(settings);
  427. if (decomposed.size() == 1) {
  428. return decomposed[0];
  429. } else {
  430. ERR_PRINT("Convex shape simplification failed, falling back to simpler process.");
  431. }
  432. }
  433. Vector<Vector3> vertices;
  434. for (int i = 0; i < get_surface_count(); i++) {
  435. Array a = surface_get_arrays(i);
  436. ERR_FAIL_COND_V(a.is_empty(), Ref<ConvexPolygonShape3D>());
  437. Vector<Vector3> v = a[ARRAY_VERTEX];
  438. vertices.append_array(v);
  439. }
  440. Ref<ConvexPolygonShape3D> shape = memnew(ConvexPolygonShape3D);
  441. if (p_clean) {
  442. Geometry3D::MeshData md;
  443. Error err = ConvexHullComputer::convex_hull(vertices, md);
  444. if (err == OK) {
  445. shape->set_points(md.vertices);
  446. return shape;
  447. } else {
  448. ERR_PRINT("Convex shape cleaning failed, falling back to simpler process.");
  449. }
  450. }
  451. shape->set_points(vertices);
  452. return shape;
  453. }
  454. Ref<ConcavePolygonShape3D> Mesh::create_trimesh_shape() const {
  455. Vector<Face3> faces = get_faces();
  456. if (faces.size() == 0) {
  457. return Ref<ConcavePolygonShape3D>();
  458. }
  459. Vector<Vector3> face_points;
  460. face_points.resize(faces.size() * 3);
  461. for (int i = 0; i < face_points.size(); i += 3) {
  462. Face3 f = faces.get(i / 3);
  463. face_points.set(i, f.vertex[0]);
  464. face_points.set(i + 1, f.vertex[1]);
  465. face_points.set(i + 2, f.vertex[2]);
  466. }
  467. Ref<ConcavePolygonShape3D> shape = memnew(ConcavePolygonShape3D);
  468. shape->set_faces(face_points);
  469. return shape;
  470. }
  471. #endif // _3D_DISABLED
  472. Ref<Mesh> Mesh::create_outline(float p_margin) const {
  473. Array arrays;
  474. int index_accum = 0;
  475. for (int i = 0; i < get_surface_count(); i++) {
  476. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
  477. continue;
  478. }
  479. Array a = surface_get_arrays(i);
  480. ERR_FAIL_COND_V(a.is_empty(), Ref<ArrayMesh>());
  481. if (i == 0) {
  482. arrays = a;
  483. Vector<Vector3> v = a[ARRAY_VERTEX];
  484. index_accum += v.size();
  485. } else {
  486. int vcount = 0;
  487. for (int j = 0; j < arrays.size(); j++) {
  488. if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) {
  489. //mismatch, do not use
  490. arrays[j] = Variant();
  491. continue;
  492. }
  493. switch (j) {
  494. case ARRAY_VERTEX:
  495. case ARRAY_NORMAL: {
  496. Vector<Vector3> dst = arrays[j];
  497. Vector<Vector3> src = a[j];
  498. if (j == ARRAY_VERTEX) {
  499. vcount = src.size();
  500. }
  501. if (dst.size() == 0 || src.size() == 0) {
  502. arrays[j] = Variant();
  503. continue;
  504. }
  505. dst.append_array(src);
  506. arrays[j] = dst;
  507. } break;
  508. case ARRAY_TANGENT:
  509. case ARRAY_BONES:
  510. case ARRAY_WEIGHTS: {
  511. Vector<real_t> dst = arrays[j];
  512. Vector<real_t> src = a[j];
  513. if (dst.size() == 0 || src.size() == 0) {
  514. arrays[j] = Variant();
  515. continue;
  516. }
  517. dst.append_array(src);
  518. arrays[j] = dst;
  519. } break;
  520. case ARRAY_COLOR: {
  521. Vector<Color> dst = arrays[j];
  522. Vector<Color> src = a[j];
  523. if (dst.size() == 0 || src.size() == 0) {
  524. arrays[j] = Variant();
  525. continue;
  526. }
  527. dst.append_array(src);
  528. arrays[j] = dst;
  529. } break;
  530. case ARRAY_TEX_UV:
  531. case ARRAY_TEX_UV2: {
  532. Vector<Vector2> dst = arrays[j];
  533. Vector<Vector2> src = a[j];
  534. if (dst.size() == 0 || src.size() == 0) {
  535. arrays[j] = Variant();
  536. continue;
  537. }
  538. dst.append_array(src);
  539. arrays[j] = dst;
  540. } break;
  541. case ARRAY_INDEX: {
  542. Vector<int> dst = arrays[j];
  543. Vector<int> src = a[j];
  544. if (dst.size() == 0 || src.size() == 0) {
  545. arrays[j] = Variant();
  546. continue;
  547. }
  548. {
  549. int ss = src.size();
  550. int *w = src.ptrw();
  551. for (int k = 0; k < ss; k++) {
  552. w[k] += index_accum;
  553. }
  554. }
  555. dst.append_array(src);
  556. arrays[j] = dst;
  557. index_accum += vcount;
  558. } break;
  559. }
  560. }
  561. }
  562. }
  563. ERR_FAIL_COND_V(arrays.size() != ARRAY_MAX, Ref<ArrayMesh>());
  564. {
  565. int *ir = nullptr;
  566. Vector<int> indices = arrays[ARRAY_INDEX];
  567. bool has_indices = false;
  568. Vector<Vector3> vertices = arrays[ARRAY_VERTEX];
  569. int vc = vertices.size();
  570. ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>());
  571. Vector3 *r = vertices.ptrw();
  572. if (indices.size()) {
  573. ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ArrayMesh>());
  574. vc = indices.size();
  575. ir = indices.ptrw();
  576. has_indices = true;
  577. } else {
  578. // Ensure there are enough vertices to construct at least one triangle.
  579. ERR_FAIL_COND_V(vertices.size() % 3 != 0, Ref<ArrayMesh>());
  580. }
  581. HashMap<Vector3, Vector3> normal_accum;
  582. //fill normals with triangle normals
  583. for (int i = 0; i < vc; i += 3) {
  584. Vector3 t[3];
  585. if (has_indices) {
  586. t[0] = r[ir[i + 0]];
  587. t[1] = r[ir[i + 1]];
  588. t[2] = r[ir[i + 2]];
  589. } else {
  590. t[0] = r[i + 0];
  591. t[1] = r[i + 1];
  592. t[2] = r[i + 2];
  593. }
  594. Vector3 n = Plane(t[0], t[1], t[2]).normal;
  595. for (int j = 0; j < 3; j++) {
  596. HashMap<Vector3, Vector3>::Iterator E = normal_accum.find(t[j]);
  597. if (!E) {
  598. normal_accum[t[j]] = n;
  599. } else {
  600. float d = n.dot(E->value);
  601. if (d < 1.0) {
  602. E->value += n * (1.0 - d);
  603. }
  604. //E->get()+=n;
  605. }
  606. }
  607. }
  608. //normalize
  609. for (KeyValue<Vector3, Vector3> &E : normal_accum) {
  610. E.value.normalize();
  611. }
  612. //displace normals
  613. int vc2 = vertices.size();
  614. for (int i = 0; i < vc2; i++) {
  615. Vector3 t = r[i];
  616. HashMap<Vector3, Vector3>::Iterator E = normal_accum.find(t);
  617. ERR_CONTINUE(!E);
  618. t += E->value * p_margin;
  619. r[i] = t;
  620. }
  621. arrays[ARRAY_VERTEX] = vertices;
  622. if (!has_indices) {
  623. Vector<int> new_indices;
  624. new_indices.resize(vertices.size());
  625. int *iw = new_indices.ptrw();
  626. for (int j = 0; j < vc2; j += 3) {
  627. iw[j] = j;
  628. iw[j + 1] = j + 2;
  629. iw[j + 2] = j + 1;
  630. }
  631. arrays[ARRAY_INDEX] = new_indices;
  632. } else {
  633. for (int j = 0; j < vc; j += 3) {
  634. SWAP(ir[j + 1], ir[j + 2]);
  635. }
  636. arrays[ARRAY_INDEX] = indices;
  637. }
  638. }
  639. Ref<ArrayMesh> newmesh = memnew(ArrayMesh);
  640. newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays);
  641. return newmesh;
  642. }
  643. void Mesh::set_lightmap_size_hint(const Size2i &p_size) {
  644. lightmap_size_hint = p_size;
  645. }
  646. Size2i Mesh::get_lightmap_size_hint() const {
  647. return lightmap_size_hint;
  648. }
  649. Ref<Resource> Mesh::create_placeholder() const {
  650. Ref<PlaceholderMesh> placeholder;
  651. placeholder.instantiate();
  652. placeholder->set_aabb(get_aabb());
  653. return placeholder;
  654. }
  655. void Mesh::_bind_methods() {
  656. ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &Mesh::set_lightmap_size_hint);
  657. ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &Mesh::get_lightmap_size_hint);
  658. ClassDB::bind_method(D_METHOD("get_aabb"), &Mesh::get_aabb);
  659. ClassDB::bind_method(D_METHOD("get_faces"), &Mesh::_get_faces);
  660. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "lightmap_size_hint"), "set_lightmap_size_hint", "get_lightmap_size_hint");
  661. ClassDB::bind_method(D_METHOD("get_surface_count"), &Mesh::get_surface_count);
  662. ClassDB::bind_method(D_METHOD("surface_get_arrays", "surf_idx"), &Mesh::surface_get_arrays);
  663. ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &Mesh::surface_get_blend_shape_arrays);
  664. ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material);
  665. ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material);
  666. ClassDB::bind_method(D_METHOD("create_placeholder"), &Mesh::create_placeholder);
  667. BIND_ENUM_CONSTANT(PRIMITIVE_POINTS);
  668. BIND_ENUM_CONSTANT(PRIMITIVE_LINES);
  669. BIND_ENUM_CONSTANT(PRIMITIVE_LINE_STRIP);
  670. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLES);
  671. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
  672. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  673. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  674. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  675. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  676. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  677. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  678. BIND_ENUM_CONSTANT(ARRAY_CUSTOM0);
  679. BIND_ENUM_CONSTANT(ARRAY_CUSTOM1);
  680. BIND_ENUM_CONSTANT(ARRAY_CUSTOM2);
  681. BIND_ENUM_CONSTANT(ARRAY_CUSTOM3);
  682. BIND_ENUM_CONSTANT(ARRAY_BONES);
  683. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  684. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  685. BIND_ENUM_CONSTANT(ARRAY_MAX);
  686. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_UNORM);
  687. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_SNORM);
  688. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_HALF);
  689. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_HALF);
  690. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_R_FLOAT);
  691. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_FLOAT);
  692. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGB_FLOAT);
  693. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_FLOAT);
  694. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_MAX);
  695. BIND_BITFIELD_FLAG(ARRAY_FORMAT_VERTEX);
  696. BIND_BITFIELD_FLAG(ARRAY_FORMAT_NORMAL);
  697. BIND_BITFIELD_FLAG(ARRAY_FORMAT_TANGENT);
  698. BIND_BITFIELD_FLAG(ARRAY_FORMAT_COLOR);
  699. BIND_BITFIELD_FLAG(ARRAY_FORMAT_TEX_UV);
  700. BIND_BITFIELD_FLAG(ARRAY_FORMAT_TEX_UV2);
  701. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM0);
  702. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM1);
  703. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM2);
  704. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM3);
  705. BIND_BITFIELD_FLAG(ARRAY_FORMAT_BONES);
  706. BIND_BITFIELD_FLAG(ARRAY_FORMAT_WEIGHTS);
  707. BIND_BITFIELD_FLAG(ARRAY_FORMAT_INDEX);
  708. BIND_BITFIELD_FLAG(ARRAY_FORMAT_BLEND_SHAPE_MASK);
  709. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM_BASE);
  710. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM_BITS);
  711. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM0_SHIFT);
  712. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM1_SHIFT);
  713. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM2_SHIFT);
  714. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM3_SHIFT);
  715. BIND_BITFIELD_FLAG(ARRAY_FORMAT_CUSTOM_MASK);
  716. BIND_BITFIELD_FLAG(ARRAY_COMPRESS_FLAGS_BASE);
  717. BIND_BITFIELD_FLAG(ARRAY_FLAG_USE_2D_VERTICES);
  718. BIND_BITFIELD_FLAG(ARRAY_FLAG_USE_DYNAMIC_UPDATE);
  719. BIND_BITFIELD_FLAG(ARRAY_FLAG_USE_8_BONE_WEIGHTS);
  720. BIND_BITFIELD_FLAG(ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY);
  721. BIND_BITFIELD_FLAG(ARRAY_FLAG_COMPRESS_ATTRIBUTES);
  722. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_NORMALIZED);
  723. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_RELATIVE);
  724. GDVIRTUAL_BIND(_get_surface_count)
  725. GDVIRTUAL_BIND(_surface_get_array_len, "index")
  726. GDVIRTUAL_BIND(_surface_get_array_index_len, "index")
  727. GDVIRTUAL_BIND(_surface_get_arrays, "index")
  728. GDVIRTUAL_BIND(_surface_get_blend_shape_arrays, "index")
  729. GDVIRTUAL_BIND(_surface_get_lods, "index")
  730. GDVIRTUAL_BIND(_surface_get_format, "index")
  731. GDVIRTUAL_BIND(_surface_get_primitive_type, "index")
  732. GDVIRTUAL_BIND(_surface_set_material, "index", "material")
  733. GDVIRTUAL_BIND(_surface_get_material, "index")
  734. GDVIRTUAL_BIND(_get_blend_shape_count)
  735. GDVIRTUAL_BIND(_get_blend_shape_name, "index")
  736. GDVIRTUAL_BIND(_set_blend_shape_name, "index", "name")
  737. GDVIRTUAL_BIND(_get_aabb)
  738. }
  739. void Mesh::clear_cache() const {
  740. triangle_mesh.unref();
  741. debug_lines.clear();
  742. }
  743. #ifndef _3D_DISABLED
  744. Vector<Ref<Shape3D>> Mesh::convex_decompose(const Ref<MeshConvexDecompositionSettings> &p_settings) const {
  745. ERR_FAIL_NULL_V(convex_decomposition_function, Vector<Ref<Shape3D>>());
  746. Ref<TriangleMesh> tm = generate_triangle_mesh();
  747. ERR_FAIL_COND_V(!tm.is_valid(), Vector<Ref<Shape3D>>());
  748. const Vector<TriangleMesh::Triangle> &triangles = tm->get_triangles();
  749. int triangle_count = triangles.size();
  750. Vector<uint32_t> indices;
  751. {
  752. indices.resize(triangle_count * 3);
  753. uint32_t *w = indices.ptrw();
  754. for (int i = 0; i < triangle_count; i++) {
  755. for (int j = 0; j < 3; j++) {
  756. w[i * 3 + j] = triangles[i].indices[j];
  757. }
  758. }
  759. }
  760. const Vector<Vector3> &vertices = tm->get_vertices();
  761. int vertex_count = vertices.size();
  762. Vector<Vector<Vector3>> decomposed = convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), triangle_count, p_settings, nullptr);
  763. Vector<Ref<Shape3D>> ret;
  764. for (int i = 0; i < decomposed.size(); i++) {
  765. Ref<ConvexPolygonShape3D> shape;
  766. shape.instantiate();
  767. shape->set_points(decomposed[i]);
  768. ret.push_back(shape);
  769. }
  770. return ret;
  771. }
  772. #endif // _3D_DISABLED
  773. int Mesh::get_builtin_bind_pose_count() const {
  774. return 0;
  775. }
  776. Transform3D Mesh::get_builtin_bind_pose(int p_index) const {
  777. return Transform3D();
  778. }
  779. Mesh::Mesh() {
  780. }
  781. enum OldArrayType {
  782. OLD_ARRAY_VERTEX,
  783. OLD_ARRAY_NORMAL,
  784. OLD_ARRAY_TANGENT,
  785. OLD_ARRAY_COLOR,
  786. OLD_ARRAY_TEX_UV,
  787. OLD_ARRAY_TEX_UV2,
  788. OLD_ARRAY_BONES,
  789. OLD_ARRAY_WEIGHTS,
  790. OLD_ARRAY_INDEX,
  791. OLD_ARRAY_MAX,
  792. };
  793. enum OldArrayFormat {
  794. /* OLD_ARRAY FORMAT FLAGS */
  795. OLD_ARRAY_FORMAT_VERTEX = 1 << OLD_ARRAY_VERTEX, // mandatory
  796. OLD_ARRAY_FORMAT_NORMAL = 1 << OLD_ARRAY_NORMAL,
  797. OLD_ARRAY_FORMAT_TANGENT = 1 << OLD_ARRAY_TANGENT,
  798. OLD_ARRAY_FORMAT_COLOR = 1 << OLD_ARRAY_COLOR,
  799. OLD_ARRAY_FORMAT_TEX_UV = 1 << OLD_ARRAY_TEX_UV,
  800. OLD_ARRAY_FORMAT_TEX_UV2 = 1 << OLD_ARRAY_TEX_UV2,
  801. OLD_ARRAY_FORMAT_BONES = 1 << OLD_ARRAY_BONES,
  802. OLD_ARRAY_FORMAT_WEIGHTS = 1 << OLD_ARRAY_WEIGHTS,
  803. OLD_ARRAY_FORMAT_INDEX = 1 << OLD_ARRAY_INDEX,
  804. OLD_ARRAY_COMPRESS_BASE = (OLD_ARRAY_INDEX + 1),
  805. OLD_ARRAY_COMPRESS_VERTEX = 1 << (OLD_ARRAY_VERTEX + OLD_ARRAY_COMPRESS_BASE), // mandatory
  806. OLD_ARRAY_COMPRESS_NORMAL = 1 << (OLD_ARRAY_NORMAL + OLD_ARRAY_COMPRESS_BASE),
  807. OLD_ARRAY_COMPRESS_TANGENT = 1 << (OLD_ARRAY_TANGENT + OLD_ARRAY_COMPRESS_BASE),
  808. OLD_ARRAY_COMPRESS_COLOR = 1 << (OLD_ARRAY_COLOR + OLD_ARRAY_COMPRESS_BASE),
  809. OLD_ARRAY_COMPRESS_TEX_UV = 1 << (OLD_ARRAY_TEX_UV + OLD_ARRAY_COMPRESS_BASE),
  810. OLD_ARRAY_COMPRESS_TEX_UV2 = 1 << (OLD_ARRAY_TEX_UV2 + OLD_ARRAY_COMPRESS_BASE),
  811. OLD_ARRAY_COMPRESS_BONES = 1 << (OLD_ARRAY_BONES + OLD_ARRAY_COMPRESS_BASE),
  812. OLD_ARRAY_COMPRESS_WEIGHTS = 1 << (OLD_ARRAY_WEIGHTS + OLD_ARRAY_COMPRESS_BASE),
  813. OLD_ARRAY_COMPRESS_INDEX = 1 << (OLD_ARRAY_INDEX + OLD_ARRAY_COMPRESS_BASE),
  814. OLD_ARRAY_FLAG_USE_2D_VERTICES = OLD_ARRAY_COMPRESS_INDEX << 1,
  815. OLD_ARRAY_FLAG_USE_16_BIT_BONES = OLD_ARRAY_COMPRESS_INDEX << 2,
  816. OLD_ARRAY_FLAG_USE_DYNAMIC_UPDATE = OLD_ARRAY_COMPRESS_INDEX << 3,
  817. OLD_ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION = OLD_ARRAY_COMPRESS_INDEX << 4,
  818. };
  819. #ifndef DISABLE_DEPRECATED
  820. static Array _convert_old_array(const Array &p_old) {
  821. Array new_array;
  822. new_array.resize(Mesh::ARRAY_MAX);
  823. new_array[Mesh::ARRAY_VERTEX] = p_old[OLD_ARRAY_VERTEX];
  824. new_array[Mesh::ARRAY_NORMAL] = p_old[OLD_ARRAY_NORMAL];
  825. new_array[Mesh::ARRAY_TANGENT] = p_old[OLD_ARRAY_TANGENT];
  826. new_array[Mesh::ARRAY_COLOR] = p_old[OLD_ARRAY_COLOR];
  827. new_array[Mesh::ARRAY_TEX_UV] = p_old[OLD_ARRAY_TEX_UV];
  828. new_array[Mesh::ARRAY_TEX_UV2] = p_old[OLD_ARRAY_TEX_UV2];
  829. new_array[Mesh::ARRAY_BONES] = p_old[OLD_ARRAY_BONES];
  830. new_array[Mesh::ARRAY_WEIGHTS] = p_old[OLD_ARRAY_WEIGHTS];
  831. new_array[Mesh::ARRAY_INDEX] = p_old[OLD_ARRAY_INDEX];
  832. return new_array;
  833. }
  834. static Mesh::PrimitiveType _old_primitives[7] = {
  835. Mesh::PRIMITIVE_POINTS,
  836. Mesh::PRIMITIVE_LINES,
  837. Mesh::PRIMITIVE_LINE_STRIP,
  838. Mesh::PRIMITIVE_LINES,
  839. Mesh::PRIMITIVE_TRIANGLES,
  840. Mesh::PRIMITIVE_TRIANGLE_STRIP,
  841. Mesh::PRIMITIVE_TRIANGLE_STRIP
  842. };
  843. #endif // DISABLE_DEPRECATED
  844. void _fix_array_compatibility(const Vector<uint8_t> &p_src, uint64_t p_old_format, uint64_t p_new_format, uint32_t p_elements, Vector<uint8_t> &vertex_data, Vector<uint8_t> &attribute_data, Vector<uint8_t> &skin_data) {
  845. uint32_t dst_vertex_stride;
  846. uint32_t dst_normal_tangent_stride;
  847. uint32_t dst_attribute_stride;
  848. uint32_t dst_skin_stride;
  849. uint32_t dst_offsets[Mesh::ARRAY_MAX];
  850. RenderingServer::get_singleton()->mesh_surface_make_offsets_from_format(p_new_format & (~RS::ARRAY_FORMAT_INDEX), p_elements, 0, dst_offsets, dst_vertex_stride, dst_normal_tangent_stride, dst_attribute_stride, dst_skin_stride);
  851. vertex_data.resize((dst_vertex_stride + dst_normal_tangent_stride) * p_elements);
  852. attribute_data.resize(dst_attribute_stride * p_elements);
  853. skin_data.resize(dst_skin_stride * p_elements);
  854. uint8_t *dst_vertex_ptr = vertex_data.ptrw();
  855. uint8_t *dst_attribute_ptr = attribute_data.ptrw();
  856. uint8_t *dst_skin_ptr = skin_data.ptrw();
  857. const uint8_t *src_vertex_ptr = p_src.ptr();
  858. uint32_t src_vertex_stride = p_src.size() / p_elements;
  859. uint32_t src_offset = 0;
  860. for (uint32_t j = 0; j < OLD_ARRAY_INDEX; j++) {
  861. if (!(p_old_format & (1ULL << j))) {
  862. continue;
  863. }
  864. switch (j) {
  865. case OLD_ARRAY_VERTEX: {
  866. if (p_old_format & OLD_ARRAY_FLAG_USE_2D_VERTICES) {
  867. if (p_old_format & OLD_ARRAY_COMPRESS_VERTEX) {
  868. for (uint32_t i = 0; i < p_elements; i++) {
  869. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride];
  870. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  871. dst[0] = Math::half_to_float(src[0]);
  872. dst[1] = Math::half_to_float(src[1]);
  873. }
  874. src_offset += sizeof(uint16_t) * 2;
  875. } else {
  876. for (uint32_t i = 0; i < p_elements; i++) {
  877. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride];
  878. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  879. dst[0] = src[0];
  880. dst[1] = src[1];
  881. }
  882. src_offset += sizeof(float) * 2;
  883. }
  884. } else {
  885. if (p_old_format & OLD_ARRAY_COMPRESS_VERTEX) {
  886. for (uint32_t i = 0; i < p_elements; i++) {
  887. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride];
  888. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  889. dst[0] = Math::half_to_float(src[0]);
  890. dst[1] = Math::half_to_float(src[1]);
  891. dst[2] = Math::half_to_float(src[2]);
  892. }
  893. src_offset += sizeof(uint16_t) * 4; //+pad
  894. } else {
  895. for (uint32_t i = 0; i < p_elements; i++) {
  896. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride];
  897. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  898. dst[0] = src[0];
  899. dst[1] = src[1];
  900. dst[2] = src[2];
  901. }
  902. src_offset += sizeof(float) * 3;
  903. }
  904. }
  905. } break;
  906. case OLD_ARRAY_NORMAL: {
  907. if (p_old_format & OLD_ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  908. if ((p_old_format & OLD_ARRAY_COMPRESS_NORMAL) && (p_old_format & OLD_ARRAY_FORMAT_TANGENT) && (p_old_format & OLD_ARRAY_COMPRESS_TANGENT)) {
  909. for (uint32_t i = 0; i < p_elements; i++) {
  910. const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  911. int16_t *dst = (int16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  912. dst[0] = (int16_t)CLAMP(src[0] / 127.0f * 32767, -32768, 32767);
  913. dst[1] = (int16_t)CLAMP(src[1] / 127.0f * 32767, -32768, 32767);
  914. }
  915. src_offset += sizeof(int8_t) * 2;
  916. } else {
  917. for (uint32_t i = 0; i < p_elements; i++) {
  918. const int16_t *src = (const int16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  919. int16_t *dst = (int16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  920. dst[0] = src[0];
  921. dst[1] = src[1];
  922. }
  923. src_offset += sizeof(int16_t) * 2;
  924. }
  925. } else { // No Octahedral compression
  926. if (p_old_format & OLD_ARRAY_COMPRESS_NORMAL) {
  927. for (uint32_t i = 0; i < p_elements; i++) {
  928. const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  929. const Vector3 original_normal(src[0], src[1], src[2]);
  930. Vector2 res = original_normal.octahedron_encode();
  931. uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  932. dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535);
  933. dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535);
  934. }
  935. src_offset += sizeof(uint8_t) * 4; // 1 byte padding
  936. } else {
  937. for (uint32_t i = 0; i < p_elements; i++) {
  938. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  939. const Vector3 original_normal(src[0], src[1], src[2]);
  940. Vector2 res = original_normal.octahedron_encode();
  941. uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  942. dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535);
  943. dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535);
  944. }
  945. src_offset += sizeof(float) * 3;
  946. }
  947. }
  948. } break;
  949. case OLD_ARRAY_TANGENT: {
  950. if (p_old_format & OLD_ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  951. if (p_old_format & OLD_ARRAY_COMPRESS_TANGENT) { // int8 SNORM -> uint16 UNORM
  952. for (uint32_t i = 0; i < p_elements; i++) {
  953. const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  954. uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_TANGENT]];
  955. dst[0] = (uint16_t)CLAMP((src[0] / 127.0f * .5f + .5f) * 65535, 0, 65535);
  956. dst[1] = (uint16_t)CLAMP((src[1] / 127.0f * .5f + .5f) * 65535, 0, 65535);
  957. }
  958. src_offset += sizeof(uint8_t) * 2;
  959. } else { // int16 SNORM -> uint16 UNORM
  960. for (uint32_t i = 0; i < p_elements; i++) {
  961. const int16_t *src = (const int16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  962. uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_TANGENT]];
  963. dst[0] = (uint16_t)CLAMP((src[0] / 32767.0f * .5f + .5f) * 65535, 0, 65535);
  964. dst[1] = (uint16_t)CLAMP((src[1] / 32767.0f * .5f + .5f) * 65535, 0, 65535);
  965. }
  966. src_offset += sizeof(uint16_t) * 2;
  967. }
  968. } else { // No Octahedral compression
  969. if (p_old_format & OLD_ARRAY_COMPRESS_TANGENT) {
  970. for (uint32_t i = 0; i < p_elements; i++) {
  971. const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  972. const Vector3 original_tangent(src[0], src[1], src[2]);
  973. Vector2 res = original_tangent.octahedron_tangent_encode(src[3]);
  974. uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  975. dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535);
  976. dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535);
  977. if (dst[0] == 0 && dst[1] == 65535) {
  978. // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection.
  979. // So we sanitize here.
  980. dst[0] = 65535;
  981. }
  982. }
  983. src_offset += sizeof(uint8_t) * 4;
  984. } else {
  985. for (uint32_t i = 0; i < p_elements; i++) {
  986. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  987. const Vector3 original_tangent(src[0], src[1], src[2]);
  988. Vector2 res = original_tangent.octahedron_tangent_encode(src[3]);
  989. uint16_t *dst = (uint16_t *)&dst_vertex_ptr[i * dst_normal_tangent_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  990. dst[0] = (uint16_t)CLAMP(res.x * 65535, 0, 65535);
  991. dst[1] = (uint16_t)CLAMP(res.y * 65535, 0, 65535);
  992. if (dst[0] == 0 && dst[1] == 65535) {
  993. // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection.
  994. // So we sanitize here.
  995. dst[0] = 65535;
  996. }
  997. }
  998. src_offset += sizeof(float) * 4;
  999. }
  1000. }
  1001. } break;
  1002. case OLD_ARRAY_COLOR: {
  1003. if (p_old_format & OLD_ARRAY_COMPRESS_COLOR) {
  1004. for (uint32_t i = 0; i < p_elements; i++) {
  1005. const uint32_t *src = (const uint32_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1006. uint32_t *dst = (uint32_t *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_COLOR]];
  1007. *dst = *src;
  1008. }
  1009. src_offset += sizeof(uint32_t);
  1010. } else {
  1011. for (uint32_t i = 0; i < p_elements; i++) {
  1012. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1013. uint8_t *dst = (uint8_t *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_COLOR]];
  1014. dst[0] = uint8_t(CLAMP(src[0] * 255.0, 0.0, 255.0));
  1015. dst[1] = uint8_t(CLAMP(src[1] * 255.0, 0.0, 255.0));
  1016. dst[2] = uint8_t(CLAMP(src[2] * 255.0, 0.0, 255.0));
  1017. dst[3] = uint8_t(CLAMP(src[3] * 255.0, 0.0, 255.0));
  1018. }
  1019. src_offset += sizeof(float) * 4;
  1020. }
  1021. } break;
  1022. case OLD_ARRAY_TEX_UV: {
  1023. if (p_old_format & OLD_ARRAY_COMPRESS_TEX_UV) {
  1024. for (uint32_t i = 0; i < p_elements; i++) {
  1025. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1026. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV]];
  1027. dst[0] = Math::half_to_float(src[0]);
  1028. dst[1] = Math::half_to_float(src[1]);
  1029. }
  1030. src_offset += sizeof(uint16_t) * 2;
  1031. } else {
  1032. for (uint32_t i = 0; i < p_elements; i++) {
  1033. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1034. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV]];
  1035. dst[0] = src[0];
  1036. dst[1] = src[1];
  1037. }
  1038. src_offset += sizeof(float) * 2;
  1039. }
  1040. } break;
  1041. case OLD_ARRAY_TEX_UV2: {
  1042. if (p_old_format & OLD_ARRAY_COMPRESS_TEX_UV2) {
  1043. for (uint32_t i = 0; i < p_elements; i++) {
  1044. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1045. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV2]];
  1046. dst[0] = Math::half_to_float(src[0]);
  1047. dst[1] = Math::half_to_float(src[1]);
  1048. }
  1049. src_offset += sizeof(uint16_t) * 2;
  1050. } else {
  1051. for (uint32_t i = 0; i < p_elements; i++) {
  1052. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1053. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV2]];
  1054. dst[0] = src[0];
  1055. dst[1] = src[1];
  1056. }
  1057. src_offset += sizeof(float) * 2;
  1058. }
  1059. } break;
  1060. case OLD_ARRAY_BONES: {
  1061. if (p_old_format & OLD_ARRAY_FLAG_USE_16_BIT_BONES) {
  1062. for (uint32_t i = 0; i < p_elements; i++) {
  1063. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1064. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_BONES]];
  1065. dst[0] = src[0];
  1066. dst[1] = src[1];
  1067. dst[2] = src[2];
  1068. dst[3] = src[3];
  1069. }
  1070. src_offset += sizeof(uint16_t) * 4;
  1071. } else {
  1072. for (uint32_t i = 0; i < p_elements; i++) {
  1073. const uint8_t *src = (const uint8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1074. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_BONES]];
  1075. dst[0] = src[0];
  1076. dst[1] = src[1];
  1077. dst[2] = src[2];
  1078. dst[3] = src[3];
  1079. }
  1080. src_offset += sizeof(uint8_t) * 4;
  1081. }
  1082. } break;
  1083. case OLD_ARRAY_WEIGHTS: {
  1084. if (p_old_format & OLD_ARRAY_COMPRESS_WEIGHTS) {
  1085. for (uint32_t i = 0; i < p_elements; i++) {
  1086. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1087. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_WEIGHTS]];
  1088. dst[0] = src[0];
  1089. dst[1] = src[1];
  1090. dst[2] = src[2];
  1091. dst[3] = src[3];
  1092. }
  1093. src_offset += sizeof(uint16_t) * 4;
  1094. } else {
  1095. for (uint32_t i = 0; i < p_elements; i++) {
  1096. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  1097. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_WEIGHTS]];
  1098. dst[0] = uint16_t(CLAMP(src[0] * 65535.0, 0, 65535.0));
  1099. dst[1] = uint16_t(CLAMP(src[1] * 65535.0, 0, 65535.0));
  1100. dst[2] = uint16_t(CLAMP(src[2] * 65535.0, 0, 65535.0));
  1101. dst[3] = uint16_t(CLAMP(src[3] * 65535.0, 0, 65535.0));
  1102. }
  1103. src_offset += sizeof(float) * 4;
  1104. }
  1105. } break;
  1106. default: {
  1107. }
  1108. }
  1109. }
  1110. }
  1111. bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) {
  1112. String sname = p_name;
  1113. if (sname.begins_with("surface_")) {
  1114. int sl = sname.find_char('/');
  1115. if (sl == -1) {
  1116. return false;
  1117. }
  1118. int idx = sname.substr(8, sl - 8).to_int();
  1119. String what = sname.get_slicec('/', 1);
  1120. if (what == "material") {
  1121. surface_set_material(idx, p_value);
  1122. } else if (what == "name") {
  1123. surface_set_name(idx, p_value);
  1124. }
  1125. return true;
  1126. }
  1127. #ifndef DISABLE_DEPRECATED
  1128. // Kept for compatibility from 3.x to 4.0.
  1129. if (!sname.begins_with("surfaces")) {
  1130. return false;
  1131. }
  1132. WARN_DEPRECATED_MSG(vformat(
  1133. "Mesh uses old surface format, which is deprecated (and loads slower). Consider re-importing or re-saving the scene. Path: \"%s\"",
  1134. get_path()));
  1135. int idx = sname.get_slicec('/', 1).to_int();
  1136. String what = sname.get_slicec('/', 2);
  1137. if (idx == surfaces.size()) {
  1138. //create
  1139. Dictionary d = p_value;
  1140. ERR_FAIL_COND_V(!d.has("primitive"), false);
  1141. if (d.has("arrays")) {
  1142. //oldest format (2.x)
  1143. ERR_FAIL_COND_V(!d.has("morph_arrays"), false);
  1144. Array morph_arrays = d["morph_arrays"];
  1145. for (int i = 0; i < morph_arrays.size(); i++) {
  1146. morph_arrays[i] = _convert_old_array(morph_arrays[i]);
  1147. }
  1148. add_surface_from_arrays(_old_primitives[int(d["primitive"])], _convert_old_array(d["arrays"]), morph_arrays);
  1149. } else if (d.has("array_data")) {
  1150. //print_line("array data (old style");
  1151. //older format (3.x)
  1152. Vector<uint8_t> array_data = d["array_data"];
  1153. Vector<uint8_t> array_index_data;
  1154. if (d.has("array_index_data")) {
  1155. array_index_data = d["array_index_data"];
  1156. }
  1157. ERR_FAIL_COND_V(!d.has("format"), false);
  1158. uint64_t old_format = d["format"];
  1159. uint32_t primitive = d["primitive"];
  1160. primitive = _old_primitives[primitive]; //compatibility
  1161. ERR_FAIL_COND_V(!d.has("vertex_count"), false);
  1162. int vertex_count = d["vertex_count"];
  1163. uint64_t new_format = ARRAY_FORMAT_VERTEX | ARRAY_FLAG_FORMAT_CURRENT_VERSION;
  1164. if (old_format & OLD_ARRAY_FORMAT_NORMAL) {
  1165. new_format |= ARRAY_FORMAT_NORMAL;
  1166. }
  1167. if (old_format & OLD_ARRAY_FORMAT_TANGENT) {
  1168. new_format |= ARRAY_FORMAT_TANGENT;
  1169. }
  1170. if (old_format & OLD_ARRAY_FORMAT_COLOR) {
  1171. new_format |= ARRAY_FORMAT_COLOR;
  1172. }
  1173. if (old_format & OLD_ARRAY_FORMAT_TEX_UV) {
  1174. new_format |= ARRAY_FORMAT_TEX_UV;
  1175. }
  1176. if (old_format & OLD_ARRAY_FORMAT_TEX_UV2) {
  1177. new_format |= ARRAY_FORMAT_TEX_UV2;
  1178. }
  1179. if (old_format & OLD_ARRAY_FORMAT_BONES) {
  1180. new_format |= ARRAY_FORMAT_BONES;
  1181. }
  1182. if (old_format & OLD_ARRAY_FORMAT_WEIGHTS) {
  1183. new_format |= ARRAY_FORMAT_WEIGHTS;
  1184. }
  1185. if (old_format & OLD_ARRAY_FORMAT_INDEX) {
  1186. new_format |= ARRAY_FORMAT_INDEX;
  1187. }
  1188. if (old_format & OLD_ARRAY_FLAG_USE_2D_VERTICES) {
  1189. new_format |= OLD_ARRAY_FLAG_USE_2D_VERTICES;
  1190. }
  1191. Vector<uint8_t> vertex_array;
  1192. Vector<uint8_t> attribute_array;
  1193. Vector<uint8_t> skin_array;
  1194. _fix_array_compatibility(array_data, old_format, new_format, vertex_count, vertex_array, attribute_array, skin_array);
  1195. int index_count = 0;
  1196. if (d.has("index_count")) {
  1197. index_count = d["index_count"];
  1198. }
  1199. Vector<uint8_t> blend_shapes_new;
  1200. if (d.has("blend_shape_data")) {
  1201. Array blend_shape_data = d["blend_shape_data"];
  1202. for (int i = 0; i < blend_shape_data.size(); i++) {
  1203. Vector<uint8_t> blend_vertex_array;
  1204. Vector<uint8_t> blend_attribute_array;
  1205. Vector<uint8_t> blend_skin_array;
  1206. Vector<uint8_t> shape = blend_shape_data[i];
  1207. _fix_array_compatibility(shape, old_format, new_format, vertex_count, blend_vertex_array, blend_attribute_array, blend_skin_array);
  1208. blend_shapes_new.append_array(blend_vertex_array);
  1209. }
  1210. }
  1211. //clear unused flags
  1212. print_verbose("Mesh format pre-conversion: " + itos(old_format));
  1213. print_verbose("Mesh format post-conversion: " + itos(new_format));
  1214. ERR_FAIL_COND_V(!d.has("aabb"), false);
  1215. AABB aabb_new = d["aabb"];
  1216. Vector<AABB> bone_aabb;
  1217. if (d.has("skeleton_aabb")) {
  1218. Array baabb = d["skeleton_aabb"];
  1219. bone_aabb.resize(baabb.size());
  1220. for (int i = 0; i < baabb.size(); i++) {
  1221. bone_aabb.write[i] = baabb[i];
  1222. }
  1223. }
  1224. add_surface(new_format, PrimitiveType(primitive), vertex_array, attribute_array, skin_array, vertex_count, array_index_data, index_count, aabb_new, blend_shapes_new, bone_aabb);
  1225. } else {
  1226. ERR_FAIL_V(false);
  1227. }
  1228. if (d.has("material")) {
  1229. surface_set_material(idx, d["material"]);
  1230. }
  1231. if (d.has("name")) {
  1232. surface_set_name(idx, d["name"]);
  1233. }
  1234. return true;
  1235. }
  1236. #endif // DISABLE_DEPRECATED
  1237. return false;
  1238. }
  1239. void ArrayMesh::_set_blend_shape_names(const PackedStringArray &p_names) {
  1240. ERR_FAIL_COND(surfaces.size() > 0);
  1241. blend_shapes.resize(p_names.size());
  1242. for (int i = 0; i < p_names.size(); i++) {
  1243. blend_shapes.write[i] = p_names[i];
  1244. }
  1245. if (mesh.is_valid()) {
  1246. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  1247. }
  1248. }
  1249. PackedStringArray ArrayMesh::_get_blend_shape_names() const {
  1250. PackedStringArray sarr;
  1251. sarr.resize(blend_shapes.size());
  1252. for (int i = 0; i < blend_shapes.size(); i++) {
  1253. sarr.write[i] = blend_shapes[i];
  1254. }
  1255. return sarr;
  1256. }
  1257. Array ArrayMesh::_get_surfaces() const {
  1258. if (mesh.is_null()) {
  1259. return Array();
  1260. }
  1261. Array ret;
  1262. for (int i = 0; i < surfaces.size(); i++) {
  1263. RenderingServer::SurfaceData surface = RS::get_singleton()->mesh_get_surface(mesh, i);
  1264. Dictionary data;
  1265. data["format"] = surface.format;
  1266. data["primitive"] = surface.primitive;
  1267. data["vertex_data"] = surface.vertex_data;
  1268. data["vertex_count"] = surface.vertex_count;
  1269. if (surface.skin_data.size()) {
  1270. data["skin_data"] = surface.skin_data;
  1271. }
  1272. if (surface.attribute_data.size()) {
  1273. data["attribute_data"] = surface.attribute_data;
  1274. }
  1275. data["aabb"] = surface.aabb;
  1276. data["uv_scale"] = surface.uv_scale;
  1277. if (surface.index_count) {
  1278. data["index_data"] = surface.index_data;
  1279. data["index_count"] = surface.index_count;
  1280. };
  1281. Array lods;
  1282. for (int j = 0; j < surface.lods.size(); j++) {
  1283. lods.push_back(surface.lods[j].edge_length);
  1284. lods.push_back(surface.lods[j].index_data);
  1285. }
  1286. if (lods.size()) {
  1287. data["lods"] = lods;
  1288. }
  1289. Array bone_aabbs;
  1290. for (int j = 0; j < surface.bone_aabbs.size(); j++) {
  1291. bone_aabbs.push_back(surface.bone_aabbs[j]);
  1292. }
  1293. if (bone_aabbs.size()) {
  1294. data["bone_aabbs"] = bone_aabbs;
  1295. }
  1296. if (surface.blend_shape_data.size()) {
  1297. data["blend_shapes"] = surface.blend_shape_data;
  1298. }
  1299. if (surfaces[i].material.is_valid()) {
  1300. data["material"] = surfaces[i].material;
  1301. }
  1302. if (!surfaces[i].name.is_empty()) {
  1303. data["name"] = surfaces[i].name;
  1304. }
  1305. if (surfaces[i].is_2d) {
  1306. data["2d"] = true;
  1307. }
  1308. ret.push_back(data);
  1309. }
  1310. return ret;
  1311. }
  1312. void ArrayMesh::_create_if_empty() const {
  1313. if (!mesh.is_valid()) {
  1314. mesh = RS::get_singleton()->mesh_create();
  1315. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode);
  1316. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  1317. RS::get_singleton()->mesh_set_path(mesh, get_path());
  1318. }
  1319. }
  1320. void ArrayMesh::_set_surfaces(const Array &p_surfaces) {
  1321. Vector<RS::SurfaceData> surface_data;
  1322. Vector<Ref<Material>> surface_materials;
  1323. Vector<String> surface_names;
  1324. Vector<bool> surface_2d;
  1325. for (int i = 0; i < p_surfaces.size(); i++) {
  1326. RS::SurfaceData surface;
  1327. Dictionary d = p_surfaces[i];
  1328. ERR_FAIL_COND(!d.has("format"));
  1329. ERR_FAIL_COND(!d.has("primitive"));
  1330. ERR_FAIL_COND(!d.has("vertex_data"));
  1331. ERR_FAIL_COND(!d.has("vertex_count"));
  1332. ERR_FAIL_COND(!d.has("aabb"));
  1333. surface.format = d["format"];
  1334. surface.primitive = RS::PrimitiveType(int(d["primitive"]));
  1335. surface.vertex_data = d["vertex_data"];
  1336. surface.vertex_count = d["vertex_count"];
  1337. if (d.has("attribute_data")) {
  1338. surface.attribute_data = d["attribute_data"];
  1339. }
  1340. if (d.has("skin_data")) {
  1341. surface.skin_data = d["skin_data"];
  1342. }
  1343. surface.aabb = d["aabb"];
  1344. if (d.has("uv_scale")) {
  1345. surface.uv_scale = d["uv_scale"];
  1346. }
  1347. if (d.has("index_data")) {
  1348. ERR_FAIL_COND(!d.has("index_count"));
  1349. surface.index_data = d["index_data"];
  1350. surface.index_count = d["index_count"];
  1351. }
  1352. if (d.has("lods")) {
  1353. Array lods = d["lods"];
  1354. ERR_FAIL_COND(lods.size() & 1); //must be even
  1355. for (int j = 0; j < lods.size(); j += 2) {
  1356. RS::SurfaceData::LOD lod;
  1357. lod.edge_length = lods[j + 0];
  1358. lod.index_data = lods[j + 1];
  1359. surface.lods.push_back(lod);
  1360. }
  1361. }
  1362. if (d.has("bone_aabbs")) {
  1363. Array bone_aabbs = d["bone_aabbs"];
  1364. for (int j = 0; j < bone_aabbs.size(); j++) {
  1365. surface.bone_aabbs.push_back(bone_aabbs[j]);
  1366. }
  1367. }
  1368. if (d.has("blend_shapes")) {
  1369. surface.blend_shape_data = d["blend_shapes"];
  1370. }
  1371. Ref<Material> material;
  1372. if (d.has("material")) {
  1373. material = d["material"];
  1374. if (material.is_valid()) {
  1375. surface.material = material->get_rid();
  1376. }
  1377. }
  1378. String surf_name;
  1379. if (d.has("name")) {
  1380. surf_name = d["name"];
  1381. }
  1382. bool _2d = false;
  1383. if (d.has("2d")) {
  1384. _2d = d["2d"];
  1385. }
  1386. #ifndef DISABLE_DEPRECATED
  1387. uint64_t surface_version = surface.format & (ARRAY_FLAG_FORMAT_VERSION_MASK << ARRAY_FLAG_FORMAT_VERSION_SHIFT);
  1388. if (surface_version != ARRAY_FLAG_FORMAT_CURRENT_VERSION) {
  1389. RS::get_singleton()->fix_surface_compatibility(surface, get_path());
  1390. surface_version = surface.format & (RS::ARRAY_FLAG_FORMAT_VERSION_MASK << RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT);
  1391. ERR_FAIL_COND_MSG(surface_version != RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION,
  1392. vformat("Surface version provided (%d) does not match current version (%d).",
  1393. (surface_version >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT) & RS::ARRAY_FLAG_FORMAT_VERSION_MASK,
  1394. (RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT) & RS::ARRAY_FLAG_FORMAT_VERSION_MASK));
  1395. }
  1396. #endif
  1397. surface_data.push_back(surface);
  1398. surface_materials.push_back(material);
  1399. surface_names.push_back(surf_name);
  1400. surface_2d.push_back(_2d);
  1401. }
  1402. if (mesh.is_valid()) {
  1403. //if mesh exists, it needs to be updated
  1404. RS::get_singleton()->mesh_clear(mesh);
  1405. for (int i = 0; i < surface_data.size(); i++) {
  1406. RS::get_singleton()->mesh_add_surface(mesh, surface_data[i]);
  1407. }
  1408. } else {
  1409. // if mesh does not exist (first time this is loaded, most likely),
  1410. // we can create it with a single call, which is a lot more efficient and thread friendly
  1411. mesh = RS::get_singleton()->mesh_create_from_surfaces(surface_data, blend_shapes.size());
  1412. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode);
  1413. RS::get_singleton()->mesh_set_path(mesh, get_path());
  1414. }
  1415. surfaces.clear();
  1416. aabb = AABB();
  1417. for (int i = 0; i < surface_data.size(); i++) {
  1418. Surface s;
  1419. s.aabb = surface_data[i].aabb;
  1420. if (i == 0) {
  1421. aabb = s.aabb;
  1422. } else {
  1423. aabb.merge_with(s.aabb);
  1424. }
  1425. s.material = surface_materials[i];
  1426. s.is_2d = surface_2d[i];
  1427. s.name = surface_names[i];
  1428. s.format = surface_data[i].format;
  1429. s.primitive = PrimitiveType(surface_data[i].primitive);
  1430. s.array_length = surface_data[i].vertex_count;
  1431. s.index_array_length = surface_data[i].index_count;
  1432. surfaces.push_back(s);
  1433. }
  1434. }
  1435. bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const {
  1436. if (_is_generated()) {
  1437. return false;
  1438. }
  1439. String sname = p_name;
  1440. if (sname.begins_with("surface_")) {
  1441. int sl = sname.find_char('/');
  1442. if (sl == -1) {
  1443. return false;
  1444. }
  1445. int idx = sname.substr(8, sl - 8).to_int();
  1446. String what = sname.get_slicec('/', 1);
  1447. if (what == "material") {
  1448. r_ret = surface_get_material(idx);
  1449. } else if (what == "name") {
  1450. r_ret = surface_get_name(idx);
  1451. }
  1452. return true;
  1453. }
  1454. return false;
  1455. }
  1456. void ArrayMesh::reset_state() {
  1457. clear_surfaces();
  1458. clear_blend_shapes();
  1459. aabb = AABB();
  1460. blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
  1461. custom_aabb = AABB();
  1462. }
  1463. void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const {
  1464. if (_is_generated()) {
  1465. return;
  1466. }
  1467. for (int i = 0; i < surfaces.size(); i++) {
  1468. p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  1469. if (surfaces[i].is_2d) {
  1470. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial", PROPERTY_USAGE_EDITOR));
  1471. } else {
  1472. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_EDITOR));
  1473. }
  1474. }
  1475. }
  1476. void ArrayMesh::_recompute_aabb() {
  1477. // regenerate AABB
  1478. aabb = AABB();
  1479. for (int i = 0; i < surfaces.size(); i++) {
  1480. if (i == 0) {
  1481. aabb = surfaces[i].aabb;
  1482. } else {
  1483. aabb.merge_with(surfaces[i].aabb);
  1484. }
  1485. }
  1486. }
  1487. // TODO: Need to add binding to add_surface using future MeshSurfaceData object.
  1488. void ArrayMesh::add_surface(BitField<ArrayFormat> p_format, PrimitiveType p_primitive, const Vector<uint8_t> &p_array, const Vector<uint8_t> &p_attribute_array, const Vector<uint8_t> &p_skin_array, int p_vertex_count, const Vector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<uint8_t> &p_blend_shape_data, const Vector<AABB> &p_bone_aabbs, const Vector<RS::SurfaceData::LOD> &p_lods, const Vector4 p_uv_scale) {
  1489. ERR_FAIL_COND(surfaces.size() == RS::MAX_MESH_SURFACES);
  1490. _create_if_empty();
  1491. Surface s;
  1492. s.aabb = p_aabb;
  1493. s.is_2d = p_format & ARRAY_FLAG_USE_2D_VERTICES;
  1494. s.primitive = p_primitive;
  1495. s.array_length = p_vertex_count;
  1496. s.index_array_length = p_index_count;
  1497. s.format = p_format;
  1498. surfaces.push_back(s);
  1499. _recompute_aabb();
  1500. RS::SurfaceData sd;
  1501. sd.format = p_format;
  1502. sd.primitive = RS::PrimitiveType(p_primitive);
  1503. sd.aabb = p_aabb;
  1504. sd.vertex_count = p_vertex_count;
  1505. sd.vertex_data = p_array;
  1506. sd.attribute_data = p_attribute_array;
  1507. sd.skin_data = p_skin_array;
  1508. sd.index_count = p_index_count;
  1509. sd.index_data = p_index_array;
  1510. sd.blend_shape_data = p_blend_shape_data;
  1511. sd.bone_aabbs = p_bone_aabbs;
  1512. sd.lods = p_lods;
  1513. sd.uv_scale = p_uv_scale;
  1514. RenderingServer::get_singleton()->mesh_add_surface(mesh, sd);
  1515. clear_cache();
  1516. notify_property_list_changed();
  1517. emit_changed();
  1518. }
  1519. void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const TypedArray<Array> &p_blend_shapes, const Dictionary &p_lods, BitField<ArrayFormat> p_flags) {
  1520. ERR_FAIL_COND(p_blend_shapes.size() != blend_shapes.size());
  1521. ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX);
  1522. RS::SurfaceData surface;
  1523. Error err = RS::get_singleton()->mesh_create_surface_data_from_arrays(&surface, (RenderingServer::PrimitiveType)p_primitive, p_arrays, p_blend_shapes, p_lods, p_flags);
  1524. ERR_FAIL_COND(err != OK);
  1525. /* Debug code.
  1526. print_line("format: " + itos(surface.format));
  1527. print_line("aabb: " + surface.aabb);
  1528. print_line("array size: " + itos(surface.vertex_data.size()));
  1529. print_line("vertex count: " + itos(surface.vertex_count));
  1530. print_line("index size: " + itos(surface.index_data.size()));
  1531. print_line("index count: " + itos(surface.index_count));
  1532. print_line("primitive: " + itos(surface.primitive));
  1533. */
  1534. add_surface(surface.format, PrimitiveType(surface.primitive), surface.vertex_data, surface.attribute_data, surface.skin_data, surface.vertex_count, surface.index_data, surface.index_count, surface.aabb, surface.blend_shape_data, surface.bone_aabbs, surface.lods, surface.uv_scale);
  1535. }
  1536. Array ArrayMesh::surface_get_arrays(int p_surface) const {
  1537. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  1538. return RenderingServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
  1539. }
  1540. TypedArray<Array> ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const {
  1541. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), TypedArray<Array>());
  1542. return RenderingServer::get_singleton()->mesh_surface_get_blend_shape_arrays(mesh, p_surface);
  1543. }
  1544. Dictionary ArrayMesh::surface_get_lods(int p_surface) const {
  1545. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Dictionary());
  1546. return RenderingServer::get_singleton()->mesh_surface_get_lods(mesh, p_surface);
  1547. }
  1548. int ArrayMesh::get_surface_count() const {
  1549. return surfaces.size();
  1550. }
  1551. void ArrayMesh::add_blend_shape(const StringName &p_name) {
  1552. ERR_FAIL_COND_MSG(surfaces.size(), "Can't add a shape key count if surfaces are already created.");
  1553. StringName shape_name = p_name;
  1554. if (blend_shapes.has(shape_name)) {
  1555. int count = 2;
  1556. do {
  1557. shape_name = String(p_name) + " " + itos(count);
  1558. count++;
  1559. } while (blend_shapes.has(shape_name));
  1560. }
  1561. blend_shapes.push_back(shape_name);
  1562. if (mesh.is_valid()) {
  1563. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  1564. }
  1565. }
  1566. int ArrayMesh::get_blend_shape_count() const {
  1567. return blend_shapes.size();
  1568. }
  1569. StringName ArrayMesh::get_blend_shape_name(int p_index) const {
  1570. ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName());
  1571. return blend_shapes[p_index];
  1572. }
  1573. void ArrayMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
  1574. ERR_FAIL_INDEX(p_index, blend_shapes.size());
  1575. StringName shape_name = p_name;
  1576. int found = blend_shapes.find(shape_name);
  1577. if (found != -1 && found != p_index) {
  1578. int count = 2;
  1579. do {
  1580. shape_name = String(p_name) + " " + itos(count);
  1581. count++;
  1582. } while (blend_shapes.has(shape_name));
  1583. }
  1584. blend_shapes.write[p_index] = shape_name;
  1585. }
  1586. void ArrayMesh::clear_blend_shapes() {
  1587. ERR_FAIL_COND_MSG(surfaces.size(), "Can't set shape key count if surfaces are already created.");
  1588. blend_shapes.clear();
  1589. if (mesh.is_valid()) {
  1590. RS::get_singleton()->mesh_set_blend_shape_count(mesh, 0);
  1591. }
  1592. }
  1593. void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) {
  1594. blend_shape_mode = p_mode;
  1595. if (mesh.is_valid()) {
  1596. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)p_mode);
  1597. }
  1598. }
  1599. ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const {
  1600. return blend_shape_mode;
  1601. }
  1602. int ArrayMesh::surface_get_array_len(int p_idx) const {
  1603. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  1604. return surfaces[p_idx].array_length;
  1605. }
  1606. int ArrayMesh::surface_get_array_index_len(int p_idx) const {
  1607. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  1608. return surfaces[p_idx].index_array_length;
  1609. }
  1610. BitField<Mesh::ArrayFormat> ArrayMesh::surface_get_format(int p_idx) const {
  1611. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0);
  1612. return surfaces[p_idx].format;
  1613. }
  1614. ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const {
  1615. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES);
  1616. return surfaces[p_idx].primitive;
  1617. }
  1618. void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  1619. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1620. if (surfaces[p_idx].material == p_material) {
  1621. return;
  1622. }
  1623. surfaces.write[p_idx].material = p_material;
  1624. RenderingServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
  1625. emit_changed();
  1626. }
  1627. int ArrayMesh::surface_find_by_name(const String &p_name) const {
  1628. for (int i = 0; i < surfaces.size(); i++) {
  1629. if (surfaces[i].name == p_name) {
  1630. return i;
  1631. }
  1632. }
  1633. return -1;
  1634. }
  1635. void ArrayMesh::surface_set_name(int p_idx, const String &p_name) {
  1636. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1637. surfaces.write[p_idx].name = p_name;
  1638. emit_changed();
  1639. }
  1640. String ArrayMesh::surface_get_name(int p_idx) const {
  1641. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String());
  1642. return surfaces[p_idx].name;
  1643. }
  1644. void ArrayMesh::surface_update_vertex_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1645. ERR_FAIL_INDEX(p_surface, surfaces.size());
  1646. RS::get_singleton()->mesh_surface_update_vertex_region(mesh, p_surface, p_offset, p_data);
  1647. emit_changed();
  1648. }
  1649. void ArrayMesh::surface_update_attribute_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1650. ERR_FAIL_INDEX(p_surface, surfaces.size());
  1651. RS::get_singleton()->mesh_surface_update_attribute_region(mesh, p_surface, p_offset, p_data);
  1652. emit_changed();
  1653. }
  1654. void ArrayMesh::surface_update_skin_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1655. ERR_FAIL_INDEX(p_surface, surfaces.size());
  1656. RS::get_singleton()->mesh_surface_update_skin_region(mesh, p_surface, p_offset, p_data);
  1657. emit_changed();
  1658. }
  1659. void ArrayMesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) {
  1660. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1661. surfaces.write[p_idx].aabb = p_aabb;
  1662. // set custom aabb too?
  1663. emit_changed();
  1664. }
  1665. Ref<Material> ArrayMesh::surface_get_material(int p_idx) const {
  1666. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>());
  1667. return surfaces[p_idx].material;
  1668. }
  1669. RID ArrayMesh::get_rid() const {
  1670. _create_if_empty();
  1671. return mesh;
  1672. }
  1673. AABB ArrayMesh::get_aabb() const {
  1674. return aabb;
  1675. }
  1676. void ArrayMesh::clear_surfaces() {
  1677. if (!mesh.is_valid()) {
  1678. return;
  1679. }
  1680. RS::get_singleton()->mesh_clear(mesh);
  1681. surfaces.clear();
  1682. aabb = AABB();
  1683. }
  1684. void ArrayMesh::set_custom_aabb(const AABB &p_custom) {
  1685. _create_if_empty();
  1686. custom_aabb = p_custom;
  1687. RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  1688. emit_changed();
  1689. }
  1690. AABB ArrayMesh::get_custom_aabb() const {
  1691. return custom_aabb;
  1692. }
  1693. void ArrayMesh::regen_normal_maps() {
  1694. if (surfaces.size() == 0) {
  1695. return;
  1696. }
  1697. Vector<Ref<SurfaceTool>> surfs;
  1698. Vector<uint64_t> formats;
  1699. for (int i = 0; i < get_surface_count(); i++) {
  1700. Ref<SurfaceTool> st = memnew(SurfaceTool);
  1701. st->create_from(Ref<ArrayMesh>(this), i);
  1702. surfs.push_back(st);
  1703. formats.push_back(surface_get_format(i));
  1704. }
  1705. clear_surfaces();
  1706. for (int i = 0; i < surfs.size(); i++) {
  1707. surfs.write[i]->generate_tangents();
  1708. surfs.write[i]->commit(Ref<ArrayMesh>(this), formats[i]);
  1709. }
  1710. }
  1711. //dirty hack
  1712. bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) = nullptr;
  1713. struct ArrayMeshLightmapSurface {
  1714. Ref<Material> material;
  1715. LocalVector<SurfaceTool::Vertex> vertices;
  1716. Mesh::PrimitiveType primitive = Mesh::PrimitiveType::PRIMITIVE_MAX;
  1717. uint64_t format = 0;
  1718. };
  1719. Error ArrayMesh::lightmap_unwrap(const Transform3D &p_base_transform, float p_texel_size) {
  1720. Vector<uint8_t> null_cache;
  1721. return lightmap_unwrap_cached(p_base_transform, p_texel_size, null_cache, null_cache, false);
  1722. }
  1723. Error ArrayMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache, bool p_generate_cache) {
  1724. ERR_FAIL_NULL_V(array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
  1725. ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");
  1726. ERR_FAIL_COND_V_MSG(p_texel_size <= 0.0f, ERR_PARAMETER_RANGE_ERROR, "Texel size must be greater than 0.");
  1727. LocalVector<float> vertices;
  1728. LocalVector<float> normals;
  1729. LocalVector<int> indices;
  1730. LocalVector<float> uv;
  1731. LocalVector<Pair<int, int>> uv_indices;
  1732. Vector<ArrayMeshLightmapSurface> lightmap_surfaces;
  1733. // Keep only the scale
  1734. Basis basis = p_base_transform.get_basis();
  1735. Vector3 scale = Vector3(basis.get_column(0).length(), basis.get_column(1).length(), basis.get_column(2).length());
  1736. Transform3D transform;
  1737. transform.scale(scale);
  1738. Basis normal_basis = transform.basis.inverse().transposed();
  1739. for (int i = 0; i < get_surface_count(); i++) {
  1740. ArrayMeshLightmapSurface s;
  1741. s.primitive = surface_get_primitive_type(i);
  1742. ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap.");
  1743. s.format = surface_get_format(i);
  1744. ERR_FAIL_COND_V_MSG(!(s.format & ARRAY_FORMAT_NORMAL), ERR_UNAVAILABLE, "Normals are required for lightmap unwrap.");
  1745. Array arrays = surface_get_arrays(i);
  1746. s.material = surface_get_material(i);
  1747. SurfaceTool::create_vertex_array_from_arrays(arrays, s.vertices, &s.format);
  1748. PackedVector3Array rvertices = arrays[Mesh::ARRAY_VERTEX];
  1749. int vc = rvertices.size();
  1750. PackedVector3Array rnormals = arrays[Mesh::ARRAY_NORMAL];
  1751. int vertex_ofs = vertices.size() / 3;
  1752. vertices.resize((vertex_ofs + vc) * 3);
  1753. normals.resize((vertex_ofs + vc) * 3);
  1754. uv_indices.resize(vertex_ofs + vc);
  1755. for (int j = 0; j < vc; j++) {
  1756. Vector3 v = transform.xform(rvertices[j]);
  1757. Vector3 n = normal_basis.xform(rnormals[j]).normalized();
  1758. vertices[(j + vertex_ofs) * 3 + 0] = v.x;
  1759. vertices[(j + vertex_ofs) * 3 + 1] = v.y;
  1760. vertices[(j + vertex_ofs) * 3 + 2] = v.z;
  1761. normals[(j + vertex_ofs) * 3 + 0] = n.x;
  1762. normals[(j + vertex_ofs) * 3 + 1] = n.y;
  1763. normals[(j + vertex_ofs) * 3 + 2] = n.z;
  1764. uv_indices[j + vertex_ofs] = Pair<int, int>(i, j);
  1765. }
  1766. PackedInt32Array rindices = arrays[Mesh::ARRAY_INDEX];
  1767. int ic = rindices.size();
  1768. float eps = 1.19209290e-7F; // Taken from xatlas.h
  1769. if (ic == 0) {
  1770. for (int j = 0; j < vc / 3; j++) {
  1771. Vector3 p0 = transform.xform(rvertices[j * 3 + 0]);
  1772. Vector3 p1 = transform.xform(rvertices[j * 3 + 1]);
  1773. Vector3 p2 = transform.xform(rvertices[j * 3 + 2]);
  1774. if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {
  1775. continue;
  1776. }
  1777. indices.push_back(vertex_ofs + j * 3 + 0);
  1778. indices.push_back(vertex_ofs + j * 3 + 1);
  1779. indices.push_back(vertex_ofs + j * 3 + 2);
  1780. }
  1781. } else {
  1782. for (int j = 0; j < ic / 3; j++) {
  1783. Vector3 p0 = transform.xform(rvertices[rindices[j * 3 + 0]]);
  1784. Vector3 p1 = transform.xform(rvertices[rindices[j * 3 + 1]]);
  1785. Vector3 p2 = transform.xform(rvertices[rindices[j * 3 + 2]]);
  1786. if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {
  1787. continue;
  1788. }
  1789. indices.push_back(vertex_ofs + rindices[j * 3 + 0]);
  1790. indices.push_back(vertex_ofs + rindices[j * 3 + 1]);
  1791. indices.push_back(vertex_ofs + rindices[j * 3 + 2]);
  1792. }
  1793. }
  1794. lightmap_surfaces.push_back(s);
  1795. }
  1796. //unwrap
  1797. bool use_cache = p_generate_cache; // Used to request cache generation and to know if cache was used
  1798. uint8_t *gen_cache;
  1799. int gen_cache_size;
  1800. float *gen_uvs;
  1801. int *gen_vertices;
  1802. int *gen_indices;
  1803. int gen_vertex_count;
  1804. int gen_index_count;
  1805. int size_x;
  1806. int size_y;
  1807. bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), indices.size(), p_src_cache.ptr(), &use_cache, &gen_cache, &gen_cache_size, &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y);
  1808. if (!ok) {
  1809. return ERR_CANT_CREATE;
  1810. }
  1811. clear_surfaces();
  1812. //create surfacetools for each surface..
  1813. LocalVector<Ref<SurfaceTool>> surfaces_tools;
  1814. for (int i = 0; i < lightmap_surfaces.size(); i++) {
  1815. Ref<SurfaceTool> st;
  1816. st.instantiate();
  1817. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  1818. st->set_material(lightmap_surfaces[i].material);
  1819. surfaces_tools.push_back(st); //stay there
  1820. }
  1821. print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
  1822. //go through all indices
  1823. for (int i = 0; i < gen_index_count; i += 3) {
  1824. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], (int)uv_indices.size(), ERR_BUG);
  1825. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], (int)uv_indices.size(), ERR_BUG);
  1826. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], (int)uv_indices.size(), ERR_BUG);
  1827. ERR_FAIL_COND_V(uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 1]]].first || uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG);
  1828. int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first;
  1829. for (int j = 0; j < 3; j++) {
  1830. SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second];
  1831. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_COLOR) {
  1832. surfaces_tools[surface]->set_color(v.color);
  1833. }
  1834. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
  1835. surfaces_tools[surface]->set_uv(v.uv);
  1836. }
  1837. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
  1838. surfaces_tools[surface]->set_normal(v.normal);
  1839. }
  1840. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
  1841. Plane t;
  1842. t.normal = v.tangent;
  1843. t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
  1844. surfaces_tools[surface]->set_tangent(t);
  1845. }
  1846. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_BONES) {
  1847. surfaces_tools[surface]->set_bones(v.bones);
  1848. }
  1849. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
  1850. surfaces_tools[surface]->set_weights(v.weights);
  1851. }
  1852. Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
  1853. surfaces_tools[surface]->set_uv2(uv2);
  1854. surfaces_tools[surface]->add_vertex(v.vertex);
  1855. }
  1856. }
  1857. //generate surfaces
  1858. for (unsigned int i = 0; i < surfaces_tools.size(); i++) {
  1859. surfaces_tools[i]->index();
  1860. surfaces_tools[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), lightmap_surfaces[i].format);
  1861. }
  1862. set_lightmap_size_hint(Size2(size_x, size_y));
  1863. if (gen_cache_size > 0) {
  1864. r_dst_cache.resize(gen_cache_size);
  1865. memcpy(r_dst_cache.ptrw(), gen_cache, gen_cache_size);
  1866. memfree(gen_cache);
  1867. }
  1868. if (!use_cache) {
  1869. // Cache was not used, free the buffers
  1870. memfree(gen_vertices);
  1871. memfree(gen_indices);
  1872. memfree(gen_uvs);
  1873. }
  1874. return OK;
  1875. }
  1876. void ArrayMesh::set_shadow_mesh(const Ref<ArrayMesh> &p_mesh) {
  1877. ERR_FAIL_COND_MSG(p_mesh == this, "Cannot set a mesh as its own shadow mesh.");
  1878. shadow_mesh = p_mesh;
  1879. if (shadow_mesh.is_valid()) {
  1880. RS::get_singleton()->mesh_set_shadow_mesh(mesh, shadow_mesh->get_rid());
  1881. } else {
  1882. RS::get_singleton()->mesh_set_shadow_mesh(mesh, RID());
  1883. }
  1884. }
  1885. Ref<ArrayMesh> ArrayMesh::get_shadow_mesh() const {
  1886. return shadow_mesh;
  1887. }
  1888. void ArrayMesh::_bind_methods() {
  1889. ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape);
  1890. ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count);
  1891. ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name);
  1892. ClassDB::bind_method(D_METHOD("set_blend_shape_name", "index", "name"), &ArrayMesh::set_blend_shape_name);
  1893. ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes);
  1894. ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode);
  1895. ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode);
  1896. ClassDB::bind_method(D_METHOD("add_surface_from_arrays", "primitive", "arrays", "blend_shapes", "lods", "flags"), &ArrayMesh::add_surface_from_arrays, DEFVAL(Array()), DEFVAL(Dictionary()), DEFVAL(0));
  1897. ClassDB::bind_method(D_METHOD("clear_surfaces"), &ArrayMesh::clear_surfaces);
  1898. ClassDB::bind_method(D_METHOD("surface_update_vertex_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_vertex_region);
  1899. ClassDB::bind_method(D_METHOD("surface_update_attribute_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_attribute_region);
  1900. ClassDB::bind_method(D_METHOD("surface_update_skin_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_skin_region);
  1901. ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len);
  1902. ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len);
  1903. ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format);
  1904. ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type);
  1905. ClassDB::bind_method(D_METHOD("surface_find_by_name", "name"), &ArrayMesh::surface_find_by_name);
  1906. ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name);
  1907. ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name);
  1908. #ifndef _3D_DISABLED
  1909. ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape);
  1910. ClassDB::bind_method(D_METHOD("create_convex_shape", "clean", "simplify"), &ArrayMesh::create_convex_shape, DEFVAL(true), DEFVAL(false));
  1911. #endif // _3D_DISABLED
  1912. ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline);
  1913. ClassDB::bind_method(D_METHOD("regen_normal_maps"), &ArrayMesh::regen_normal_maps);
  1914. ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normal_maps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  1915. ClassDB::bind_method(D_METHOD("lightmap_unwrap", "transform", "texel_size"), &ArrayMesh::lightmap_unwrap);
  1916. ClassDB::set_method_flags(get_class_static(), _scs_create("lightmap_unwrap"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  1917. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh);
  1918. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb);
  1919. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb);
  1920. ClassDB::bind_method(D_METHOD("set_shadow_mesh", "mesh"), &ArrayMesh::set_shadow_mesh);
  1921. ClassDB::bind_method(D_METHOD("get_shadow_mesh"), &ArrayMesh::get_shadow_mesh);
  1922. ClassDB::bind_method(D_METHOD("_set_blend_shape_names", "blend_shape_names"), &ArrayMesh::_set_blend_shape_names);
  1923. ClassDB::bind_method(D_METHOD("_get_blend_shape_names"), &ArrayMesh::_get_blend_shape_names);
  1924. ClassDB::bind_method(D_METHOD("_set_surfaces", "surfaces"), &ArrayMesh::_set_surfaces);
  1925. ClassDB::bind_method(D_METHOD("_get_surfaces"), &ArrayMesh::_get_surfaces);
  1926. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "_blend_shape_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_blend_shape_names", "_get_blend_shape_names");
  1927. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_surfaces", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_surfaces", "_get_surfaces");
  1928. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_shape_mode", PROPERTY_HINT_ENUM, "Normalized,Relative"), "set_blend_shape_mode", "get_blend_shape_mode");
  1929. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_custom_aabb", "get_custom_aabb");
  1930. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shadow_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ArrayMesh"), "set_shadow_mesh", "get_shadow_mesh");
  1931. }
  1932. void ArrayMesh::reload_from_file() {
  1933. RenderingServer::get_singleton()->mesh_clear(mesh);
  1934. surfaces.clear();
  1935. clear_blend_shapes();
  1936. clear_cache();
  1937. Resource::reload_from_file();
  1938. notify_property_list_changed();
  1939. }
  1940. ArrayMesh::ArrayMesh() {
  1941. //mesh is now created on demand
  1942. //mesh = RenderingServer::get_singleton()->mesh_create();
  1943. }
  1944. ArrayMesh::~ArrayMesh() {
  1945. if (mesh.is_valid()) {
  1946. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1947. RenderingServer::get_singleton()->free(mesh);
  1948. }
  1949. }
  1950. ///////////////
  1951. void PlaceholderMesh::_bind_methods() {
  1952. ClassDB::bind_method(D_METHOD("set_aabb", "aabb"), &PlaceholderMesh::set_aabb);
  1953. ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_aabb", "get_aabb");
  1954. }
  1955. PlaceholderMesh::PlaceholderMesh() {
  1956. rid = RS::get_singleton()->mesh_create();
  1957. }
  1958. PlaceholderMesh::~PlaceholderMesh() {
  1959. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1960. RS::get_singleton()->free(rid);
  1961. }