mesh.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*************************************************************************/
  2. /* mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/templates/pair.h"
  32. #include "scene/resources/concave_polygon_shape_3d.h"
  33. #include "scene/resources/convex_polygon_shape_3d.h"
  34. #include "surface_tool.h"
  35. #include <stdlib.h>
  36. Mesh::ConvexDecompositionFunc Mesh::convex_composition_function = nullptr;
  37. Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
  38. if (triangle_mesh.is_valid()) {
  39. return triangle_mesh;
  40. }
  41. int facecount = 0;
  42. for (int i = 0; i < get_surface_count(); i++) {
  43. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
  44. continue;
  45. }
  46. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  47. facecount += surface_get_array_index_len(i);
  48. } else {
  49. facecount += surface_get_array_len(i);
  50. }
  51. }
  52. if (facecount == 0 || (facecount % 3) != 0) {
  53. return triangle_mesh;
  54. }
  55. Vector<Vector3> faces;
  56. faces.resize(facecount);
  57. Vector3 *facesw = faces.ptrw();
  58. int widx = 0;
  59. for (int i = 0; i < get_surface_count(); i++) {
  60. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
  61. continue;
  62. }
  63. Array a = surface_get_arrays(i);
  64. ERR_FAIL_COND_V(a.is_empty(), Ref<TriangleMesh>());
  65. int vc = surface_get_array_len(i);
  66. Vector<Vector3> vertices = a[ARRAY_VERTEX];
  67. const Vector3 *vr = vertices.ptr();
  68. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  69. int ic = surface_get_array_index_len(i);
  70. Vector<int> indices = a[ARRAY_INDEX];
  71. const int *ir = indices.ptr();
  72. for (int j = 0; j < ic; j++) {
  73. int index = ir[j];
  74. facesw[widx++] = vr[index];
  75. }
  76. } else {
  77. for (int j = 0; j < vc; j++) {
  78. facesw[widx++] = vr[j];
  79. }
  80. }
  81. }
  82. triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  83. triangle_mesh->create(faces);
  84. return triangle_mesh;
  85. }
  86. void Mesh::generate_debug_mesh_lines(Vector<Vector3> &r_lines) {
  87. if (debug_lines.size() > 0) {
  88. r_lines = debug_lines;
  89. return;
  90. }
  91. Ref<TriangleMesh> tm = generate_triangle_mesh();
  92. if (tm.is_null()) {
  93. return;
  94. }
  95. Vector<int> triangle_indices;
  96. tm->get_indices(&triangle_indices);
  97. const int triangles_num = tm->get_triangles().size();
  98. Vector<Vector3> vertices = tm->get_vertices();
  99. debug_lines.resize(tm->get_triangles().size() * 6); // 3 lines x 2 points each line
  100. const int *ind_r = triangle_indices.ptr();
  101. const Vector3 *ver_r = vertices.ptr();
  102. for (int j = 0, x = 0, i = 0; i < triangles_num; j += 6, x += 3, ++i) {
  103. // Triangle line 1
  104. debug_lines.write[j + 0] = ver_r[ind_r[x + 0]];
  105. debug_lines.write[j + 1] = ver_r[ind_r[x + 1]];
  106. // Triangle line 2
  107. debug_lines.write[j + 2] = ver_r[ind_r[x + 1]];
  108. debug_lines.write[j + 3] = ver_r[ind_r[x + 2]];
  109. // Triangle line 3
  110. debug_lines.write[j + 4] = ver_r[ind_r[x + 2]];
  111. debug_lines.write[j + 5] = ver_r[ind_r[x + 0]];
  112. }
  113. r_lines = debug_lines;
  114. }
  115. void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) {
  116. Ref<TriangleMesh> tm = generate_triangle_mesh();
  117. if (tm.is_null()) {
  118. return;
  119. }
  120. Vector<Vector3> vertices = tm->get_vertices();
  121. int vertices_size = vertices.size();
  122. r_points.resize(vertices_size);
  123. for (int i = 0; i < vertices_size; ++i) {
  124. r_points.write[i] = vertices[i];
  125. }
  126. }
  127. bool Mesh::surface_is_softbody_friendly(int p_idx) const {
  128. const uint32_t surface_format = surface_get_format(p_idx);
  129. return (surface_format & Mesh::ARRAY_FLAG_USE_DYNAMIC_UPDATE);
  130. }
  131. Vector<Face3> Mesh::get_faces() const {
  132. Ref<TriangleMesh> tm = generate_triangle_mesh();
  133. if (tm.is_valid()) {
  134. return tm->get_faces();
  135. }
  136. return Vector<Face3>();
  137. /*
  138. for (int i=0;i<surfaces.size();i++) {
  139. if (RenderingServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != RenderingServer::PRIMITIVE_TRIANGLES )
  140. continue;
  141. Vector<int> indices;
  142. Vector<Vector3> vertices;
  143. vertices=RenderingServer::get_singleton()->mesh_surface_get_array(mesh, i,RenderingServer::ARRAY_VERTEX);
  144. int len=RenderingServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i);
  145. bool has_indices;
  146. if (len>0) {
  147. indices=RenderingServer::get_singleton()->mesh_surface_get_array(mesh, i,RenderingServer::ARRAY_INDEX);
  148. has_indices=true;
  149. } else {
  150. len=vertices.size();
  151. has_indices=false;
  152. }
  153. if (len<=0)
  154. continue;
  155. const int* indicesr = indices.ptr();
  156. const int *indicesptr = indicesr.ptr();
  157. const Vector3* verticesr = vertices.ptr();
  158. const Vector3 *verticesptr = verticesr.ptr();
  159. int old_faces=faces.size();
  160. int new_faces=old_faces+(len/3);
  161. faces.resize(new_faces);
  162. Face3* facesw = faces.ptrw();
  163. Face3 *facesptr=facesw.ptr();
  164. for (int i=0;i<len/3;i++) {
  165. Face3 face;
  166. for (int j=0;j<3;j++) {
  167. int idx=i*3+j;
  168. face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx];
  169. }
  170. facesptr[i+old_faces]=face;
  171. }
  172. }
  173. */
  174. }
  175. Ref<Shape3D> Mesh::create_convex_shape() const {
  176. Vector<Vector3> vertices;
  177. for (int i = 0; i < get_surface_count(); i++) {
  178. Array a = surface_get_arrays(i);
  179. ERR_FAIL_COND_V(a.is_empty(), Ref<ConvexPolygonShape3D>());
  180. Vector<Vector3> v = a[ARRAY_VERTEX];
  181. vertices.append_array(v);
  182. }
  183. Ref<ConvexPolygonShape3D> shape = memnew(ConvexPolygonShape3D);
  184. shape->set_points(vertices);
  185. return shape;
  186. }
  187. Ref<Shape3D> Mesh::create_trimesh_shape() const {
  188. Vector<Face3> faces = get_faces();
  189. if (faces.size() == 0) {
  190. return Ref<Shape3D>();
  191. }
  192. Vector<Vector3> face_points;
  193. face_points.resize(faces.size() * 3);
  194. for (int i = 0; i < face_points.size(); i += 3) {
  195. Face3 f = faces.get(i / 3);
  196. face_points.set(i, f.vertex[0]);
  197. face_points.set(i + 1, f.vertex[1]);
  198. face_points.set(i + 2, f.vertex[2]);
  199. }
  200. Ref<ConcavePolygonShape3D> shape = memnew(ConcavePolygonShape3D);
  201. shape->set_faces(face_points);
  202. return shape;
  203. }
  204. Ref<Mesh> Mesh::create_outline(float p_margin) const {
  205. Array arrays;
  206. int index_accum = 0;
  207. for (int i = 0; i < get_surface_count(); i++) {
  208. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
  209. continue;
  210. }
  211. Array a = surface_get_arrays(i);
  212. ERR_FAIL_COND_V(a.is_empty(), Ref<ArrayMesh>());
  213. if (i == 0) {
  214. arrays = a;
  215. Vector<Vector3> v = a[ARRAY_VERTEX];
  216. index_accum += v.size();
  217. } else {
  218. int vcount = 0;
  219. for (int j = 0; j < arrays.size(); j++) {
  220. if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) {
  221. //mismatch, do not use
  222. arrays[j] = Variant();
  223. continue;
  224. }
  225. switch (j) {
  226. case ARRAY_VERTEX:
  227. case ARRAY_NORMAL: {
  228. Vector<Vector3> dst = arrays[j];
  229. Vector<Vector3> src = a[j];
  230. if (j == ARRAY_VERTEX) {
  231. vcount = src.size();
  232. }
  233. if (dst.size() == 0 || src.size() == 0) {
  234. arrays[j] = Variant();
  235. continue;
  236. }
  237. dst.append_array(src);
  238. arrays[j] = dst;
  239. } break;
  240. case ARRAY_TANGENT:
  241. case ARRAY_BONES:
  242. case ARRAY_WEIGHTS: {
  243. Vector<real_t> dst = arrays[j];
  244. Vector<real_t> src = a[j];
  245. if (dst.size() == 0 || src.size() == 0) {
  246. arrays[j] = Variant();
  247. continue;
  248. }
  249. dst.append_array(src);
  250. arrays[j] = dst;
  251. } break;
  252. case ARRAY_COLOR: {
  253. Vector<Color> dst = arrays[j];
  254. Vector<Color> src = a[j];
  255. if (dst.size() == 0 || src.size() == 0) {
  256. arrays[j] = Variant();
  257. continue;
  258. }
  259. dst.append_array(src);
  260. arrays[j] = dst;
  261. } break;
  262. case ARRAY_TEX_UV:
  263. case ARRAY_TEX_UV2: {
  264. Vector<Vector2> dst = arrays[j];
  265. Vector<Vector2> src = a[j];
  266. if (dst.size() == 0 || src.size() == 0) {
  267. arrays[j] = Variant();
  268. continue;
  269. }
  270. dst.append_array(src);
  271. arrays[j] = dst;
  272. } break;
  273. case ARRAY_INDEX: {
  274. Vector<int> dst = arrays[j];
  275. Vector<int> src = a[j];
  276. if (dst.size() == 0 || src.size() == 0) {
  277. arrays[j] = Variant();
  278. continue;
  279. }
  280. {
  281. int ss = src.size();
  282. int *w = src.ptrw();
  283. for (int k = 0; k < ss; k++) {
  284. w[k] += index_accum;
  285. }
  286. }
  287. dst.append_array(src);
  288. arrays[j] = dst;
  289. index_accum += vcount;
  290. } break;
  291. }
  292. }
  293. }
  294. }
  295. ERR_FAIL_COND_V(arrays.size() != ARRAY_MAX, Ref<ArrayMesh>());
  296. {
  297. int *ir = nullptr;
  298. Vector<int> indices = arrays[ARRAY_INDEX];
  299. bool has_indices = false;
  300. Vector<Vector3> vertices = arrays[ARRAY_VERTEX];
  301. int vc = vertices.size();
  302. ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>());
  303. Vector3 *r = vertices.ptrw();
  304. if (indices.size()) {
  305. ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ArrayMesh>());
  306. vc = indices.size();
  307. ir = indices.ptrw();
  308. has_indices = true;
  309. }
  310. Map<Vector3, Vector3> normal_accum;
  311. //fill normals with triangle normals
  312. for (int i = 0; i < vc; i += 3) {
  313. Vector3 t[3];
  314. if (has_indices) {
  315. t[0] = r[ir[i + 0]];
  316. t[1] = r[ir[i + 1]];
  317. t[2] = r[ir[i + 2]];
  318. } else {
  319. t[0] = r[i + 0];
  320. t[1] = r[i + 1];
  321. t[2] = r[i + 2];
  322. }
  323. Vector3 n = Plane(t[0], t[1], t[2]).normal;
  324. for (int j = 0; j < 3; j++) {
  325. Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]);
  326. if (!E) {
  327. normal_accum[t[j]] = n;
  328. } else {
  329. float d = n.dot(E->get());
  330. if (d < 1.0) {
  331. E->get() += n * (1.0 - d);
  332. }
  333. //E->get()+=n;
  334. }
  335. }
  336. }
  337. //normalize
  338. for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) {
  339. E->get().normalize();
  340. }
  341. //displace normals
  342. int vc2 = vertices.size();
  343. for (int i = 0; i < vc2; i++) {
  344. Vector3 t = r[i];
  345. Map<Vector3, Vector3>::Element *E = normal_accum.find(t);
  346. ERR_CONTINUE(!E);
  347. t += E->get() * p_margin;
  348. r[i] = t;
  349. }
  350. arrays[ARRAY_VERTEX] = vertices;
  351. if (!has_indices) {
  352. Vector<int> new_indices;
  353. new_indices.resize(vertices.size());
  354. int *iw = new_indices.ptrw();
  355. for (int j = 0; j < vc2; j += 3) {
  356. iw[j] = j;
  357. iw[j + 1] = j + 2;
  358. iw[j + 2] = j + 1;
  359. }
  360. arrays[ARRAY_INDEX] = new_indices;
  361. } else {
  362. for (int j = 0; j < vc; j += 3) {
  363. SWAP(ir[j + 1], ir[j + 2]);
  364. }
  365. arrays[ARRAY_INDEX] = indices;
  366. }
  367. }
  368. Ref<ArrayMesh> newmesh = memnew(ArrayMesh);
  369. newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays);
  370. return newmesh;
  371. }
  372. void Mesh::set_lightmap_size_hint(const Size2i &p_size) {
  373. lightmap_size_hint = p_size;
  374. }
  375. Size2i Mesh::get_lightmap_size_hint() const {
  376. return lightmap_size_hint;
  377. }
  378. void Mesh::_bind_methods() {
  379. ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &Mesh::set_lightmap_size_hint);
  380. ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &Mesh::get_lightmap_size_hint);
  381. ClassDB::bind_method(D_METHOD("get_aabb"), &Mesh::get_aabb);
  382. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "lightmap_size_hint"), "set_lightmap_size_hint", "get_lightmap_size_hint");
  383. ClassDB::bind_method(D_METHOD("get_surface_count"), &Mesh::get_surface_count);
  384. ClassDB::bind_method(D_METHOD("surface_get_arrays", "surf_idx"), &Mesh::surface_get_arrays);
  385. ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &Mesh::surface_get_blend_shape_arrays);
  386. ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material);
  387. ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material);
  388. BIND_ENUM_CONSTANT(PRIMITIVE_POINTS);
  389. BIND_ENUM_CONSTANT(PRIMITIVE_LINES);
  390. BIND_ENUM_CONSTANT(PRIMITIVE_LINE_STRIP);
  391. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLES);
  392. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
  393. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  394. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  395. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  396. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  397. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  398. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  399. BIND_ENUM_CONSTANT(ARRAY_CUSTOM0);
  400. BIND_ENUM_CONSTANT(ARRAY_CUSTOM1);
  401. BIND_ENUM_CONSTANT(ARRAY_CUSTOM2);
  402. BIND_ENUM_CONSTANT(ARRAY_CUSTOM3);
  403. BIND_ENUM_CONSTANT(ARRAY_BONES);
  404. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  405. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  406. BIND_ENUM_CONSTANT(ARRAY_MAX);
  407. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_UNORM);
  408. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_SNORM);
  409. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_HALF);
  410. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_HALF);
  411. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_R_FLOAT);
  412. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_FLOAT);
  413. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGB_FLOAT);
  414. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_FLOAT);
  415. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_MAX);
  416. BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
  417. BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
  418. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
  419. BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
  420. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
  421. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
  422. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM0);
  423. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM1);
  424. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM2);
  425. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM3);
  426. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
  427. BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  428. BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
  429. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BLEND_SHAPE_MASK);
  430. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM_BASE);
  431. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM0_SHIFT);
  432. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM1_SHIFT);
  433. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM2_SHIFT);
  434. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM3_SHIFT);
  435. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM_MASK);
  436. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_FLAGS_BASE);
  437. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_2D_VERTICES);
  438. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_DYNAMIC_UPDATE);
  439. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_8_BONE_WEIGHTS);
  440. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_NORMALIZED);
  441. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_RELATIVE);
  442. }
  443. void Mesh::clear_cache() const {
  444. triangle_mesh.unref();
  445. debug_lines.clear();
  446. }
  447. Vector<Ref<Shape3D>> Mesh::convex_decompose() const {
  448. ERR_FAIL_COND_V(!convex_composition_function, Vector<Ref<Shape3D>>());
  449. const Vector<Face3> faces = get_faces();
  450. Vector<Vector<Face3>> decomposed = convex_composition_function(faces);
  451. Vector<Ref<Shape3D>> ret;
  452. for (int i = 0; i < decomposed.size(); i++) {
  453. Set<Vector3> points;
  454. for (int j = 0; j < decomposed[i].size(); j++) {
  455. points.insert(decomposed[i][j].vertex[0]);
  456. points.insert(decomposed[i][j].vertex[1]);
  457. points.insert(decomposed[i][j].vertex[2]);
  458. }
  459. Vector<Vector3> convex_points;
  460. convex_points.resize(points.size());
  461. {
  462. Vector3 *w = convex_points.ptrw();
  463. int idx = 0;
  464. for (Set<Vector3>::Element *E = points.front(); E; E = E->next()) {
  465. w[idx++] = E->get();
  466. }
  467. }
  468. Ref<ConvexPolygonShape3D> shape;
  469. shape.instance();
  470. shape->set_points(convex_points);
  471. ret.push_back(shape);
  472. }
  473. return ret;
  474. }
  475. Mesh::Mesh() {
  476. }
  477. #if 0
  478. static Vector<uint8_t> _fix_array_compatibility(const Vector<uint8_t> &p_src, uint32_t p_format, uint32_t p_elements) {
  479. enum ArrayType {
  480. OLD_ARRAY_VERTEX = 0,
  481. OLD_ARRAY_NORMAL = 1,
  482. OLD_ARRAY_TANGENT = 2,
  483. OLD_ARRAY_COLOR = 3,
  484. OLD_ARRAY_TEX_UV = 4,
  485. OLD_ARRAY_TEX_UV2 = 5,
  486. OLD_ARRAY_BONES = 6,
  487. OLD_ARRAY_WEIGHTS = 7,
  488. OLD_ARRAY_INDEX = 8,
  489. OLD_ARRAY_MAX = 9
  490. };
  491. enum ArrayFormat {
  492. /* OLD_ARRAY FORMAT FLAGS */
  493. OLD_ARRAY_FORMAT_VERTEX = 1 << OLD_ARRAY_VERTEX, // mandatory
  494. OLD_ARRAY_FORMAT_NORMAL = 1 << OLD_ARRAY_NORMAL,
  495. OLD_ARRAY_FORMAT_TANGENT = 1 << OLD_ARRAY_TANGENT,
  496. OLD_ARRAY_FORMAT_COLOR = 1 << OLD_ARRAY_COLOR,
  497. OLD_ARRAY_FORMAT_TEX_UV = 1 << OLD_ARRAY_TEX_UV,
  498. OLD_ARRAY_FORMAT_TEX_UV2 = 1 << OLD_ARRAY_TEX_UV2,
  499. OLD_ARRAY_FORMAT_BONES = 1 << OLD_ARRAY_BONES,
  500. OLD_ARRAY_FORMAT_WEIGHTS = 1 << OLD_ARRAY_WEIGHTS,
  501. OLD_ARRAY_FORMAT_INDEX = 1 << OLD_ARRAY_INDEX,
  502. OLD_ARRAY_COMPRESS_BASE = (OLD_ARRAY_INDEX + 1),
  503. OLD_ARRAY_COMPRESS_NORMAL = 1 << (OLD_ARRAY_NORMAL + OLD_ARRAY_COMPRESS_BASE),
  504. OLD_ARRAY_COMPRESS_TANGENT = 1 << (OLD_ARRAY_TANGENT + OLD_ARRAY_COMPRESS_BASE),
  505. OLD_ARRAY_COMPRESS_COLOR = 1 << (OLD_ARRAY_COLOR + OLD_ARRAY_COMPRESS_BASE),
  506. OLD_ARRAY_COMPRESS_TEX_UV = 1 << (OLD_ARRAY_TEX_UV + OLD_ARRAY_COMPRESS_BASE),
  507. OLD_ARRAY_COMPRESS_TEX_UV2 = 1 << (OLD_ARRAY_TEX_UV2 + OLD_ARRAY_COMPRESS_BASE),
  508. OLD_ARRAY_COMPRESS_INDEX = 1 << (OLD_ARRAY_INDEX + OLD_ARRAY_COMPRESS_BASE),
  509. OLD_ARRAY_COMPRESS_DEFAULT = OLD_ARRAY_COMPRESS_NORMAL | OLD_ARRAY_COMPRESS_TANGENT | OLD_ARRAY_COMPRESS_COLOR | OLD_ARRAY_COMPRESS_TEX_UV | OLD_ARRAY_COMPRESS_TEX_UV2,
  510. OLD_ARRAY_FLAG_USE_2D_VERTICES = OLD_ARRAY_COMPRESS_INDEX << 1,
  511. OLD_ARRAY_FLAG_USE_DYNAMIC_UPDATE = OLD_ARRAY_COMPRESS_INDEX << 3,
  512. };
  513. bool vertex_16bit = p_format & ((1 << (OLD_ARRAY_VERTEX + OLD_ARRAY_COMPRESS_BASE)));
  514. bool has_bones = (p_format & OLD_ARRAY_FORMAT_BONES);
  515. bool bone_8 = has_bones && !(p_format & (OLD_ARRAY_COMPRESS_INDEX << 2));
  516. bool weight_32 = has_bones && !(p_format & (OLD_ARRAY_COMPRESS_TEX_UV2 << 2));
  517. print_line("convert vertex16: " + itos(vertex_16bit) + " convert bone 8 " + itos(bone_8) + " convert weight 32 " + itos(weight_32));
  518. if (!vertex_16bit && !bone_8 && !weight_32) {
  519. return p_src;
  520. }
  521. bool vertex_2d = (p_format & (OLD_ARRAY_COMPRESS_INDEX << 1));
  522. uint32_t src_stride = p_src.size() / p_elements;
  523. uint32_t dst_stride = src_stride + (vertex_16bit ? 4 : 0) + (bone_8 ? 4 : 0) - (weight_32 ? 8 : 0);
  524. Vector<uint8_t> ret = p_src;
  525. ret.resize(dst_stride * p_elements);
  526. {
  527. uint8_t *w = ret.ptrw();
  528. const uint8_t *r = p_src.ptr();
  529. for (uint32_t i = 0; i < p_elements; i++) {
  530. uint32_t remaining = src_stride;
  531. const uint8_t *src = (const uint8_t *)(r + src_stride * i);
  532. uint8_t *dst = (uint8_t *)(w + dst_stride * i);
  533. if (!vertex_2d) { //3D
  534. if (vertex_16bit) {
  535. float *dstw = (float *)dst;
  536. const uint16_t *srcr = (const uint16_t *)src;
  537. dstw[0] = Math::half_to_float(srcr[0]);
  538. dstw[1] = Math::half_to_float(srcr[1]);
  539. dstw[2] = Math::half_to_float(srcr[2]);
  540. remaining -= 8;
  541. src += 8;
  542. } else {
  543. src += 12;
  544. remaining -= 12;
  545. }
  546. dst += 12;
  547. } else {
  548. if (vertex_16bit) {
  549. float *dstw = (float *)dst;
  550. const uint16_t *srcr = (const uint16_t *)src;
  551. dstw[0] = Math::half_to_float(srcr[0]);
  552. dstw[1] = Math::half_to_float(srcr[1]);
  553. remaining -= 4;
  554. src += 4;
  555. } else {
  556. src += 8;
  557. remaining -= 8;
  558. }
  559. dst += 8;
  560. }
  561. if (has_bones) {
  562. remaining -= bone_8 ? 4 : 8;
  563. remaining -= weight_32 ? 16 : 8;
  564. }
  565. for (uint32_t j = 0; j < remaining; j++) {
  566. dst[j] = src[j];
  567. }
  568. if (has_bones) {
  569. dst += remaining;
  570. src += remaining;
  571. if (bone_8) {
  572. const uint8_t *src_bones = (const uint8_t *)src;
  573. uint16_t *dst_bones = (uint16_t *)dst;
  574. dst_bones[0] = src_bones[0];
  575. dst_bones[1] = src_bones[1];
  576. dst_bones[2] = src_bones[2];
  577. dst_bones[3] = src_bones[3];
  578. src += 4;
  579. } else {
  580. for (uint32_t j = 0; j < 8; j++) {
  581. dst[j] = src[j];
  582. }
  583. src += 8;
  584. }
  585. dst += 8;
  586. if (weight_32) {
  587. const float *src_weights = (const float *)src;
  588. uint16_t *dst_weights = (uint16_t *)dst;
  589. dst_weights[0] = CLAMP(src_weights[0] * 65535, 0, 65535); //16bits unorm
  590. dst_weights[1] = CLAMP(src_weights[1] * 65535, 0, 65535);
  591. dst_weights[2] = CLAMP(src_weights[2] * 65535, 0, 65535);
  592. dst_weights[3] = CLAMP(src_weights[3] * 65535, 0, 65535);
  593. } else {
  594. for (uint32_t j = 0; j < 8; j++) {
  595. dst[j] = src[j];
  596. }
  597. }
  598. }
  599. }
  600. }
  601. return ret;
  602. }
  603. #endif
  604. bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) {
  605. String sname = p_name;
  606. if (sname.begins_with("surface_")) {
  607. int sl = sname.find("/");
  608. if (sl == -1) {
  609. return false;
  610. }
  611. int idx = sname.substr(8, sl - 8).to_int() - 1;
  612. String what = sname.get_slicec('/', 1);
  613. if (what == "material") {
  614. surface_set_material(idx, p_value);
  615. } else if (what == "name") {
  616. surface_set_name(idx, p_value);
  617. }
  618. return true;
  619. }
  620. #ifndef DISABLE_DEPRECATED
  621. // Kept for compatibility from 3.x to 4.0.
  622. if (!sname.begins_with("surfaces")) {
  623. return false;
  624. }
  625. WARN_DEPRECATED_MSG("Mesh uses old surface format, which is deprecated (and loads slower). Consider re-importing or re-saving the scene.");
  626. int idx = sname.get_slicec('/', 1).to_int();
  627. String what = sname.get_slicec('/', 2);
  628. if (idx == surfaces.size()) {
  629. //create
  630. Dictionary d = p_value;
  631. ERR_FAIL_COND_V(!d.has("primitive"), false);
  632. if (d.has("arrays")) {
  633. //oldest format (2.x)
  634. ERR_FAIL_COND_V(!d.has("morph_arrays"), false);
  635. add_surface_from_arrays(PrimitiveType(int(d["primitive"])), d["arrays"], d["morph_arrays"]);
  636. } else if (d.has("array_data")) {
  637. #if 0
  638. //print_line("array data (old style");
  639. //older format (3.x)
  640. Vector<uint8_t> array_data = d["array_data"];
  641. Vector<uint8_t> array_index_data;
  642. if (d.has("array_index_data")) {
  643. array_index_data = d["array_index_data"];
  644. }
  645. ERR_FAIL_COND_V(!d.has("format"), false);
  646. uint32_t format = d["format"];
  647. uint32_t primitive = d["primitive"];
  648. uint32_t primitive_remap[7] = {
  649. PRIMITIVE_POINTS,
  650. PRIMITIVE_LINES,
  651. PRIMITIVE_LINE_STRIP,
  652. PRIMITIVE_LINES,
  653. PRIMITIVE_TRIANGLES,
  654. PRIMITIVE_TRIANGLE_STRIP,
  655. PRIMITIVE_TRIANGLE_STRIP
  656. };
  657. primitive = primitive_remap[primitive]; //compatibility
  658. ERR_FAIL_COND_V(!d.has("vertex_count"), false);
  659. int vertex_count = d["vertex_count"];
  660. array_data = _fix_array_compatibility(array_data, format, vertex_count);
  661. int index_count = 0;
  662. if (d.has("index_count")) {
  663. index_count = d["index_count"];
  664. }
  665. Vector<Vector<uint8_t>> blend_shapes;
  666. if (d.has("blend_shape_data")) {
  667. Array blend_shape_data = d["blend_shape_data"];
  668. for (int i = 0; i < blend_shape_data.size(); i++) {
  669. Vector<uint8_t> shape = blend_shape_data[i];
  670. shape = _fix_array_compatibility(shape, format, vertex_count);
  671. blend_shapes.push_back(shape);
  672. }
  673. }
  674. //clear unused flags
  675. print_line("format pre: " + itos(format));
  676. format &= ~uint32_t((1 << (ARRAY_VERTEX + ARRAY_COMPRESS_BASE)) | (ARRAY_COMPRESS_INDEX << 2) | (ARRAY_COMPRESS_TEX_UV2 << 2));
  677. print_line("format post: " + itos(format));
  678. ERR_FAIL_COND_V(!d.has("aabb"), false);
  679. AABB aabb = d["aabb"];
  680. Vector<AABB> bone_aabb;
  681. if (d.has("skeleton_aabb")) {
  682. Array baabb = d["skeleton_aabb"];
  683. bone_aabb.resize(baabb.size());
  684. for (int i = 0; i < baabb.size(); i++) {
  685. bone_aabb.write[i] = baabb[i];
  686. }
  687. }
  688. add_surface(format, PrimitiveType(primitive), array_data, vertex_count, array_index_data, index_count, aabb, blend_shapes, bone_aabb);
  689. #endif
  690. } else {
  691. ERR_FAIL_V(false);
  692. }
  693. if (d.has("material")) {
  694. surface_set_material(idx, d["material"]);
  695. }
  696. if (d.has("name")) {
  697. surface_set_name(idx, d["name"]);
  698. }
  699. return true;
  700. }
  701. #endif // DISABLE_DEPRECATED
  702. return false;
  703. }
  704. void ArrayMesh::_set_blend_shape_names(const PackedStringArray &p_names) {
  705. ERR_FAIL_COND(surfaces.size() > 0);
  706. blend_shapes.resize(p_names.size());
  707. for (int i = 0; i < p_names.size(); i++) {
  708. blend_shapes.write[i] = p_names[i];
  709. }
  710. if (mesh.is_valid()) {
  711. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  712. }
  713. }
  714. PackedStringArray ArrayMesh::_get_blend_shape_names() const {
  715. PackedStringArray sarr;
  716. sarr.resize(blend_shapes.size());
  717. for (int i = 0; i < blend_shapes.size(); i++) {
  718. sarr.write[i] = blend_shapes[i];
  719. }
  720. return sarr;
  721. }
  722. Array ArrayMesh::_get_surfaces() const {
  723. if (mesh.is_null()) {
  724. return Array();
  725. }
  726. Array ret;
  727. for (int i = 0; i < surfaces.size(); i++) {
  728. RenderingServer::SurfaceData surface = RS::get_singleton()->mesh_get_surface(mesh, i);
  729. Dictionary data;
  730. data["format"] = surface.format;
  731. data["primitive"] = surface.primitive;
  732. data["vertex_data"] = surface.vertex_data;
  733. data["vertex_count"] = surface.vertex_count;
  734. if (surface.skin_data.size()) {
  735. data["skin_data"] = surface.skin_data;
  736. }
  737. if (surface.attribute_data.size()) {
  738. data["attribute_data"] = surface.attribute_data;
  739. }
  740. data["aabb"] = surface.aabb;
  741. if (surface.index_count) {
  742. data["index_data"] = surface.index_data;
  743. data["index_count"] = surface.index_count;
  744. };
  745. Array lods;
  746. for (int j = 0; j < surface.lods.size(); j++) {
  747. lods.push_back(surface.lods[j].edge_length);
  748. lods.push_back(surface.lods[j].index_data);
  749. }
  750. if (lods.size()) {
  751. data["lods"] = lods;
  752. }
  753. Array bone_aabbs;
  754. for (int j = 0; j < surface.bone_aabbs.size(); j++) {
  755. bone_aabbs.push_back(surface.bone_aabbs[j]);
  756. }
  757. if (bone_aabbs.size()) {
  758. data["bone_aabbs"] = bone_aabbs;
  759. }
  760. if (surface.blend_shape_data.size()) {
  761. data["blend_shapes"] = surface.blend_shape_data;
  762. }
  763. if (surfaces[i].material.is_valid()) {
  764. data["material"] = surfaces[i].material;
  765. }
  766. if (surfaces[i].name != String()) {
  767. data["name"] = surfaces[i].name;
  768. }
  769. if (surfaces[i].is_2d) {
  770. data["2d"] = true;
  771. }
  772. ret.push_back(data);
  773. }
  774. return ret;
  775. }
  776. void ArrayMesh::_create_if_empty() const {
  777. if (!mesh.is_valid()) {
  778. mesh = RS::get_singleton()->mesh_create();
  779. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode);
  780. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  781. }
  782. }
  783. void ArrayMesh::_set_surfaces(const Array &p_surfaces) {
  784. Vector<RS::SurfaceData> surface_data;
  785. Vector<Ref<Material>> surface_materials;
  786. Vector<String> surface_names;
  787. Vector<bool> surface_2d;
  788. for (int i = 0; i < p_surfaces.size(); i++) {
  789. RS::SurfaceData surface;
  790. Dictionary d = p_surfaces[i];
  791. ERR_FAIL_COND(!d.has("format"));
  792. ERR_FAIL_COND(!d.has("primitive"));
  793. ERR_FAIL_COND(!d.has("vertex_data"));
  794. ERR_FAIL_COND(!d.has("vertex_count"));
  795. ERR_FAIL_COND(!d.has("aabb"));
  796. surface.format = d["format"];
  797. surface.primitive = RS::PrimitiveType(int(d["primitive"]));
  798. surface.vertex_data = d["vertex_data"];
  799. surface.vertex_count = d["vertex_count"];
  800. if (d.has("attribute_data")) {
  801. surface.attribute_data = d["attribute_data"];
  802. }
  803. if (d.has("skin_data")) {
  804. surface.skin_data = d["skin_data"];
  805. }
  806. surface.aabb = d["aabb"];
  807. if (d.has("index_data")) {
  808. ERR_FAIL_COND(!d.has("index_count"));
  809. surface.index_data = d["index_data"];
  810. surface.index_count = d["index_count"];
  811. }
  812. if (d.has("lods")) {
  813. Array lods = d["lods"];
  814. ERR_FAIL_COND(lods.size() & 1); //must be even
  815. for (int j = 0; j < lods.size(); j += 2) {
  816. RS::SurfaceData::LOD lod;
  817. lod.edge_length = lods[j + 0];
  818. lod.index_data = lods[j + 1];
  819. surface.lods.push_back(lod);
  820. }
  821. }
  822. if (d.has("bone_aabbs")) {
  823. Array bone_aabbs = d["bone_aabbs"];
  824. for (int j = 0; j < bone_aabbs.size(); j++) {
  825. surface.bone_aabbs.push_back(bone_aabbs[j]);
  826. }
  827. }
  828. if (d.has("blend_shapes")) {
  829. surface.blend_shape_data = d["blend_shapes"];
  830. }
  831. Ref<Material> material;
  832. if (d.has("material")) {
  833. material = d["material"];
  834. if (material.is_valid()) {
  835. surface.material = material->get_rid();
  836. }
  837. }
  838. String name;
  839. if (d.has("name")) {
  840. name = d["name"];
  841. }
  842. bool _2d = false;
  843. if (d.has("2d")) {
  844. _2d = d["2d"];
  845. }
  846. surface_data.push_back(surface);
  847. surface_materials.push_back(material);
  848. surface_names.push_back(name);
  849. surface_2d.push_back(_2d);
  850. }
  851. if (mesh.is_valid()) {
  852. //if mesh exists, it needs to be updated
  853. RS::get_singleton()->mesh_clear(mesh);
  854. for (int i = 0; i < surface_data.size(); i++) {
  855. RS::get_singleton()->mesh_add_surface(mesh, surface_data[i]);
  856. }
  857. } else {
  858. // if mesh does not exist (first time this is loaded, most likely),
  859. // we can create it with a single call, which is a lot more efficient and thread friendly
  860. mesh = RS::get_singleton()->mesh_create_from_surfaces(surface_data, blend_shapes.size());
  861. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode);
  862. }
  863. surfaces.clear();
  864. aabb = AABB();
  865. for (int i = 0; i < surface_data.size(); i++) {
  866. Surface s;
  867. s.aabb = surface_data[i].aabb;
  868. if (i == 0) {
  869. aabb = s.aabb;
  870. } else {
  871. aabb.merge_with(s.aabb);
  872. }
  873. s.material = surface_materials[i];
  874. s.is_2d = surface_2d[i];
  875. s.name = surface_names[i];
  876. s.format = surface_data[i].format;
  877. s.primitive = PrimitiveType(surface_data[i].primitive);
  878. s.array_length = surface_data[i].vertex_count;
  879. s.index_array_length = surface_data[i].index_count;
  880. surfaces.push_back(s);
  881. }
  882. }
  883. bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const {
  884. if (_is_generated()) {
  885. return false;
  886. }
  887. String sname = p_name;
  888. if (sname.begins_with("surface_")) {
  889. int sl = sname.find("/");
  890. if (sl == -1) {
  891. return false;
  892. }
  893. int idx = sname.substr(8, sl - 8).to_int() - 1;
  894. String what = sname.get_slicec('/', 1);
  895. if (what == "material") {
  896. r_ret = surface_get_material(idx);
  897. } else if (what == "name") {
  898. r_ret = surface_get_name(idx);
  899. }
  900. return true;
  901. }
  902. return true;
  903. }
  904. void ArrayMesh::reset_state() {
  905. clear_surfaces();
  906. clear_blend_shapes();
  907. aabb = AABB();
  908. blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
  909. custom_aabb = AABB();
  910. }
  911. void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const {
  912. if (_is_generated()) {
  913. return;
  914. }
  915. for (int i = 0; i < surfaces.size(); i++) {
  916. p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i + 1) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  917. if (surfaces[i].is_2d) {
  918. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,CanvasItemMaterial", PROPERTY_USAGE_EDITOR));
  919. } else {
  920. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,StandardMaterial3D", PROPERTY_USAGE_EDITOR));
  921. }
  922. }
  923. }
  924. void ArrayMesh::_recompute_aabb() {
  925. // regenerate AABB
  926. aabb = AABB();
  927. for (int i = 0; i < surfaces.size(); i++) {
  928. if (i == 0) {
  929. aabb = surfaces[i].aabb;
  930. } else {
  931. aabb.merge_with(surfaces[i].aabb);
  932. }
  933. }
  934. }
  935. #ifndef _MSC_VER
  936. #warning need to add binding to add_surface using future MeshSurfaceData object
  937. #endif
  938. 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) {
  939. _create_if_empty();
  940. Surface s;
  941. s.aabb = p_aabb;
  942. s.is_2d = p_format & ARRAY_FLAG_USE_2D_VERTICES;
  943. s.primitive = p_primitive;
  944. s.array_length = p_vertex_count;
  945. s.index_array_length = p_index_count;
  946. s.format = p_format;
  947. surfaces.push_back(s);
  948. _recompute_aabb();
  949. RS::SurfaceData sd;
  950. sd.format = p_format;
  951. sd.primitive = RS::PrimitiveType(p_primitive);
  952. sd.aabb = p_aabb;
  953. sd.vertex_count = p_vertex_count;
  954. sd.vertex_data = p_array;
  955. sd.attribute_data = p_attribute_array;
  956. sd.skin_data = p_skin_array;
  957. sd.index_count = p_index_count;
  958. sd.index_data = p_index_array;
  959. sd.blend_shape_data = p_blend_shape_data;
  960. sd.bone_aabbs = p_bone_aabbs;
  961. sd.lods = p_lods;
  962. RenderingServer::get_singleton()->mesh_add_surface(mesh, sd);
  963. clear_cache();
  964. notify_property_list_changed();
  965. emit_changed();
  966. }
  967. 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) {
  968. ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX);
  969. RS::SurfaceData surface;
  970. 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);
  971. ERR_FAIL_COND(err != OK);
  972. /* Debug code.
  973. print_line("format: " + itos(surface.format));
  974. print_line("aabb: " + surface.aabb);
  975. print_line("array size: " + itos(surface.vertex_data.size()));
  976. print_line("vertex count: " + itos(surface.vertex_count));
  977. print_line("index size: " + itos(surface.index_data.size()));
  978. print_line("index count: " + itos(surface.index_count));
  979. print_line("primitive: " + itos(surface.primitive));
  980. */
  981. 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);
  982. }
  983. Array ArrayMesh::surface_get_arrays(int p_surface) const {
  984. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  985. return RenderingServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
  986. }
  987. Array ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const {
  988. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  989. return RenderingServer::get_singleton()->mesh_surface_get_blend_shape_arrays(mesh, p_surface);
  990. }
  991. Dictionary ArrayMesh::surface_get_lods(int p_surface) const {
  992. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Dictionary());
  993. return RenderingServer::get_singleton()->mesh_surface_get_lods(mesh, p_surface);
  994. }
  995. int ArrayMesh::get_surface_count() const {
  996. return surfaces.size();
  997. }
  998. void ArrayMesh::add_blend_shape(const StringName &p_name) {
  999. ERR_FAIL_COND_MSG(surfaces.size(), "Can't add a shape key count if surfaces are already created.");
  1000. StringName name = p_name;
  1001. if (blend_shapes.find(name) != -1) {
  1002. int count = 2;
  1003. do {
  1004. name = String(p_name) + " " + itos(count);
  1005. count++;
  1006. } while (blend_shapes.find(name) != -1);
  1007. }
  1008. blend_shapes.push_back(name);
  1009. if (mesh.is_valid()) {
  1010. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  1011. }
  1012. }
  1013. int ArrayMesh::get_blend_shape_count() const {
  1014. return blend_shapes.size();
  1015. }
  1016. StringName ArrayMesh::get_blend_shape_name(int p_index) const {
  1017. ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName());
  1018. return blend_shapes[p_index];
  1019. }
  1020. void ArrayMesh::clear_blend_shapes() {
  1021. ERR_FAIL_COND_MSG(surfaces.size(), "Can't set shape key count if surfaces are already created.");
  1022. blend_shapes.clear();
  1023. if (mesh.is_valid()) {
  1024. RS::get_singleton()->mesh_set_blend_shape_count(mesh, 0);
  1025. }
  1026. }
  1027. void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) {
  1028. blend_shape_mode = p_mode;
  1029. if (mesh.is_valid()) {
  1030. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)p_mode);
  1031. }
  1032. }
  1033. ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const {
  1034. return blend_shape_mode;
  1035. }
  1036. int ArrayMesh::surface_get_array_len(int p_idx) const {
  1037. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  1038. return surfaces[p_idx].array_length;
  1039. }
  1040. int ArrayMesh::surface_get_array_index_len(int p_idx) const {
  1041. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  1042. return surfaces[p_idx].index_array_length;
  1043. }
  1044. uint32_t ArrayMesh::surface_get_format(int p_idx) const {
  1045. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0);
  1046. return surfaces[p_idx].format;
  1047. }
  1048. ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const {
  1049. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES);
  1050. return surfaces[p_idx].primitive;
  1051. }
  1052. void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  1053. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1054. if (surfaces[p_idx].material == p_material) {
  1055. return;
  1056. }
  1057. surfaces.write[p_idx].material = p_material;
  1058. RenderingServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
  1059. emit_changed();
  1060. }
  1061. int ArrayMesh::surface_find_by_name(const String &p_name) const {
  1062. for (int i = 0; i < surfaces.size(); i++) {
  1063. if (surfaces[i].name == p_name) {
  1064. return i;
  1065. }
  1066. }
  1067. return -1;
  1068. }
  1069. void ArrayMesh::surface_set_name(int p_idx, const String &p_name) {
  1070. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1071. surfaces.write[p_idx].name = p_name;
  1072. emit_changed();
  1073. }
  1074. String ArrayMesh::surface_get_name(int p_idx) const {
  1075. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String());
  1076. return surfaces[p_idx].name;
  1077. }
  1078. void ArrayMesh::surface_update_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1079. ERR_FAIL_INDEX(p_surface, surfaces.size());
  1080. RS::get_singleton()->mesh_surface_update_region(mesh, p_surface, p_offset, p_data);
  1081. emit_changed();
  1082. }
  1083. void ArrayMesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) {
  1084. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1085. surfaces.write[p_idx].aabb = p_aabb;
  1086. // set custom aabb too?
  1087. emit_changed();
  1088. }
  1089. Ref<Material> ArrayMesh::surface_get_material(int p_idx) const {
  1090. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>());
  1091. return surfaces[p_idx].material;
  1092. }
  1093. RID ArrayMesh::get_rid() const {
  1094. _create_if_empty();
  1095. return mesh;
  1096. }
  1097. AABB ArrayMesh::get_aabb() const {
  1098. return aabb;
  1099. }
  1100. void ArrayMesh::clear_surfaces() {
  1101. if (!mesh.is_valid()) {
  1102. return;
  1103. }
  1104. RS::get_singleton()->mesh_clear(mesh);
  1105. surfaces.clear();
  1106. aabb = AABB();
  1107. }
  1108. void ArrayMesh::set_custom_aabb(const AABB &p_custom) {
  1109. _create_if_empty();
  1110. custom_aabb = p_custom;
  1111. RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  1112. emit_changed();
  1113. }
  1114. AABB ArrayMesh::get_custom_aabb() const {
  1115. return custom_aabb;
  1116. }
  1117. void ArrayMesh::regen_normal_maps() {
  1118. if (surfaces.size() == 0) {
  1119. return;
  1120. }
  1121. Vector<Ref<SurfaceTool>> surfs;
  1122. for (int i = 0; i < get_surface_count(); i++) {
  1123. Ref<SurfaceTool> st = memnew(SurfaceTool);
  1124. st->create_from(Ref<ArrayMesh>(this), i);
  1125. surfs.push_back(st);
  1126. }
  1127. clear_surfaces();
  1128. for (int i = 0; i < surfs.size(); i++) {
  1129. surfs.write[i]->generate_tangents();
  1130. surfs.write[i]->commit(Ref<ArrayMesh>(this));
  1131. }
  1132. }
  1133. //dirty hack
  1134. 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, 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, int *&r_cache_data, unsigned int &r_cache_size, bool &r_used_cache);
  1135. struct ArrayMeshLightmapSurface {
  1136. Ref<Material> material;
  1137. LocalVector<SurfaceTool::Vertex> vertices;
  1138. Mesh::PrimitiveType primitive = Mesh::PrimitiveType::PRIMITIVE_MAX;
  1139. uint32_t format = 0;
  1140. };
  1141. Error ArrayMesh::lightmap_unwrap(const Transform &p_base_transform, float p_texel_size) {
  1142. int *cache_data = nullptr;
  1143. unsigned int cache_size = 0;
  1144. bool use_cache = false; // Don't use cache
  1145. return lightmap_unwrap_cached(cache_data, cache_size, use_cache, p_base_transform, p_texel_size);
  1146. }
  1147. Error ArrayMesh::lightmap_unwrap_cached(int *&r_cache_data, unsigned int &r_cache_size, bool &r_used_cache, const Transform &p_base_transform, float p_texel_size) {
  1148. ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
  1149. ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");
  1150. Vector<float> vertices;
  1151. Vector<float> normals;
  1152. Vector<int> indices;
  1153. Vector<float> uv;
  1154. Vector<Pair<int, int>> uv_indices;
  1155. Vector<ArrayMeshLightmapSurface> lightmap_surfaces;
  1156. // Keep only the scale
  1157. Transform transform = p_base_transform;
  1158. transform.origin = Vector3();
  1159. transform.looking_at(Vector3(1, 0, 0), Vector3(0, 1, 0));
  1160. Basis normal_basis = transform.basis.inverse().transposed();
  1161. for (int i = 0; i < get_surface_count(); i++) {
  1162. ArrayMeshLightmapSurface s;
  1163. s.primitive = surface_get_primitive_type(i);
  1164. ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap.");
  1165. s.format = surface_get_format(i);
  1166. ERR_FAIL_COND_V_MSG(!(s.format & ARRAY_FORMAT_NORMAL), ERR_UNAVAILABLE, "Normals are required for lightmap unwrap.");
  1167. Array arrays = surface_get_arrays(i);
  1168. s.material = surface_get_material(i);
  1169. SurfaceTool::create_vertex_array_from_triangle_arrays(arrays, s.vertices);
  1170. Vector<Vector3> rvertices = arrays[Mesh::ARRAY_VERTEX];
  1171. int vc = rvertices.size();
  1172. const Vector3 *r = rvertices.ptr();
  1173. Vector<Vector3> rnormals = arrays[Mesh::ARRAY_NORMAL];
  1174. const Vector3 *rn = rnormals.ptr();
  1175. int vertex_ofs = vertices.size() / 3;
  1176. vertices.resize((vertex_ofs + vc) * 3);
  1177. normals.resize((vertex_ofs + vc) * 3);
  1178. uv_indices.resize(vertex_ofs + vc);
  1179. for (int j = 0; j < vc; j++) {
  1180. Vector3 v = transform.xform(r[j]);
  1181. Vector3 n = normal_basis.xform(rn[j]).normalized();
  1182. vertices.write[(j + vertex_ofs) * 3 + 0] = v.x;
  1183. vertices.write[(j + vertex_ofs) * 3 + 1] = v.y;
  1184. vertices.write[(j + vertex_ofs) * 3 + 2] = v.z;
  1185. normals.write[(j + vertex_ofs) * 3 + 0] = n.x;
  1186. normals.write[(j + vertex_ofs) * 3 + 1] = n.y;
  1187. normals.write[(j + vertex_ofs) * 3 + 2] = n.z;
  1188. uv_indices.write[j + vertex_ofs] = Pair<int, int>(i, j);
  1189. }
  1190. Vector<int> rindices = arrays[Mesh::ARRAY_INDEX];
  1191. int ic = rindices.size();
  1192. if (ic == 0) {
  1193. for (int j = 0; j < vc / 3; j++) {
  1194. if (Face3(r[j * 3 + 0], r[j * 3 + 1], r[j * 3 + 2]).is_degenerate()) {
  1195. continue;
  1196. }
  1197. indices.push_back(vertex_ofs + j * 3 + 0);
  1198. indices.push_back(vertex_ofs + j * 3 + 1);
  1199. indices.push_back(vertex_ofs + j * 3 + 2);
  1200. }
  1201. } else {
  1202. const int *ri = rindices.ptr();
  1203. for (int j = 0; j < ic / 3; j++) {
  1204. if (Face3(r[ri[j * 3 + 0]], r[ri[j * 3 + 1]], r[ri[j * 3 + 2]]).is_degenerate()) {
  1205. continue;
  1206. }
  1207. indices.push_back(vertex_ofs + ri[j * 3 + 0]);
  1208. indices.push_back(vertex_ofs + ri[j * 3 + 1]);
  1209. indices.push_back(vertex_ofs + ri[j * 3 + 2]);
  1210. }
  1211. }
  1212. lightmap_surfaces.push_back(s);
  1213. }
  1214. //unwrap
  1215. float *gen_uvs;
  1216. int *gen_vertices;
  1217. int *gen_indices;
  1218. int gen_vertex_count;
  1219. int gen_index_count;
  1220. int size_x;
  1221. int size_y;
  1222. bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), indices.size(), &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y, r_cache_data, r_cache_size, r_used_cache);
  1223. if (!ok) {
  1224. return ERR_CANT_CREATE;
  1225. }
  1226. //remove surfaces
  1227. clear_surfaces();
  1228. //create surfacetools for each surface..
  1229. Vector<Ref<SurfaceTool>> surfaces_tools;
  1230. for (int i = 0; i < lightmap_surfaces.size(); i++) {
  1231. Ref<SurfaceTool> st;
  1232. st.instance();
  1233. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  1234. st->set_material(lightmap_surfaces[i].material);
  1235. surfaces_tools.push_back(st); //stay there
  1236. }
  1237. print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
  1238. //go through all indices
  1239. for (int i = 0; i < gen_index_count; i += 3) {
  1240. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], uv_indices.size(), ERR_BUG);
  1241. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], uv_indices.size(), ERR_BUG);
  1242. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], uv_indices.size(), ERR_BUG);
  1243. 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);
  1244. int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first;
  1245. for (int j = 0; j < 3; j++) {
  1246. SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second];
  1247. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_COLOR) {
  1248. surfaces_tools.write[surface]->set_color(v.color);
  1249. }
  1250. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
  1251. surfaces_tools.write[surface]->set_uv(v.uv);
  1252. }
  1253. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
  1254. surfaces_tools.write[surface]->set_normal(v.normal);
  1255. }
  1256. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
  1257. Plane t;
  1258. t.normal = v.tangent;
  1259. t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
  1260. surfaces_tools.write[surface]->set_tangent(t);
  1261. }
  1262. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_BONES) {
  1263. surfaces_tools.write[surface]->set_bones(v.bones);
  1264. }
  1265. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
  1266. surfaces_tools.write[surface]->set_weights(v.weights);
  1267. }
  1268. Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
  1269. surfaces_tools.write[surface]->set_uv2(uv2);
  1270. surfaces_tools.write[surface]->add_vertex(v.vertex);
  1271. }
  1272. }
  1273. //generate surfaces
  1274. for (int i = 0; i < surfaces_tools.size(); i++) {
  1275. surfaces_tools.write[i]->index();
  1276. surfaces_tools.write[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), lightmap_surfaces[i].format);
  1277. }
  1278. set_lightmap_size_hint(Size2(size_x, size_y));
  1279. if (!r_used_cache) {
  1280. //free stuff
  1281. ::free(gen_vertices);
  1282. ::free(gen_indices);
  1283. ::free(gen_uvs);
  1284. }
  1285. return OK;
  1286. }
  1287. void ArrayMesh::set_shadow_mesh(const Ref<ArrayMesh> &p_mesh) {
  1288. shadow_mesh = p_mesh;
  1289. if (shadow_mesh.is_valid()) {
  1290. RS::get_singleton()->mesh_set_shadow_mesh(mesh, shadow_mesh->get_rid());
  1291. } else {
  1292. RS::get_singleton()->mesh_set_shadow_mesh(mesh, RID());
  1293. }
  1294. }
  1295. Ref<ArrayMesh> ArrayMesh::get_shadow_mesh() const {
  1296. return shadow_mesh;
  1297. }
  1298. void ArrayMesh::_bind_methods() {
  1299. ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape);
  1300. ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count);
  1301. ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name);
  1302. ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes);
  1303. ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode);
  1304. ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode);
  1305. 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));
  1306. ClassDB::bind_method(D_METHOD("clear_surfaces"), &ArrayMesh::clear_surfaces);
  1307. ClassDB::bind_method(D_METHOD("surface_update_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_region);
  1308. ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len);
  1309. ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len);
  1310. ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format);
  1311. ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type);
  1312. ClassDB::bind_method(D_METHOD("surface_find_by_name", "name"), &ArrayMesh::surface_find_by_name);
  1313. ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name);
  1314. ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name);
  1315. ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape);
  1316. ClassDB::bind_method(D_METHOD("create_convex_shape"), &ArrayMesh::create_convex_shape);
  1317. ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline);
  1318. ClassDB::bind_method(D_METHOD("regen_normal_maps"), &ArrayMesh::regen_normal_maps);
  1319. ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normal_maps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  1320. ClassDB::bind_method(D_METHOD("lightmap_unwrap", "transform", "texel_size"), &ArrayMesh::lightmap_unwrap);
  1321. ClassDB::set_method_flags(get_class_static(), _scs_create("lightmap_unwrap"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  1322. ClassDB::bind_method(D_METHOD("get_faces"), &ArrayMesh::get_faces);
  1323. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh);
  1324. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb);
  1325. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb);
  1326. ClassDB::bind_method(D_METHOD("set_shadow_mesh", "mesh"), &ArrayMesh::set_shadow_mesh);
  1327. ClassDB::bind_method(D_METHOD("get_shadow_mesh"), &ArrayMesh::get_shadow_mesh);
  1328. ClassDB::bind_method(D_METHOD("_set_blend_shape_names", "blend_shape_names"), &ArrayMesh::_set_blend_shape_names);
  1329. ClassDB::bind_method(D_METHOD("_get_blend_shape_names"), &ArrayMesh::_get_blend_shape_names);
  1330. ClassDB::bind_method(D_METHOD("_set_surfaces", "surfaces"), &ArrayMesh::_set_surfaces);
  1331. ClassDB::bind_method(D_METHOD("_get_surfaces"), &ArrayMesh::_get_surfaces);
  1332. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "_blend_shape_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_blend_shape_names", "_get_blend_shape_names");
  1333. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_surfaces", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_surfaces", "_get_surfaces");
  1334. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_shape_mode", PROPERTY_HINT_ENUM, "Normalized,Relative"), "set_blend_shape_mode", "get_blend_shape_mode");
  1335. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
  1336. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shadow_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ArrayMesh"), "set_shadow_mesh", "get_shadow_mesh");
  1337. }
  1338. void ArrayMesh::reload_from_file() {
  1339. RenderingServer::get_singleton()->mesh_clear(mesh);
  1340. surfaces.clear();
  1341. clear_blend_shapes();
  1342. clear_cache();
  1343. Resource::reload_from_file();
  1344. notify_property_list_changed();
  1345. }
  1346. ArrayMesh::ArrayMesh() {
  1347. //mesh is now created on demand
  1348. //mesh = RenderingServer::get_singleton()->mesh_create();
  1349. }
  1350. ArrayMesh::~ArrayMesh() {
  1351. if (mesh.is_valid()) {
  1352. RenderingServer::get_singleton()->free(mesh);
  1353. }
  1354. }