mesh.cpp 65 KB

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