mesh.cpp 70 KB

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