mesh.cpp 82 KB

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