mesh.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*************************************************************************/
  2. /* mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/pair.h"
  32. #include "scene/resources/concave_polygon_shape.h"
  33. #include "scene/resources/convex_polygon_shape.h"
  34. #include "surface_tool.h"
  35. #include <stdlib.h>
  36. Mesh::ConvexDecompositionFunc Mesh::convex_composition_function = NULL;
  37. Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
  38. if (triangle_mesh.is_valid())
  39. return triangle_mesh;
  40. int facecount = 0;
  41. for (int i = 0; i < get_surface_count(); i++) {
  42. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
  43. continue;
  44. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  45. facecount += surface_get_array_index_len(i);
  46. } else {
  47. facecount += surface_get_array_len(i);
  48. }
  49. }
  50. if (facecount == 0 || (facecount % 3) != 0)
  51. return triangle_mesh;
  52. PoolVector<Vector3> faces;
  53. faces.resize(facecount);
  54. PoolVector<Vector3>::Write facesw = faces.write();
  55. int widx = 0;
  56. for (int i = 0; i < get_surface_count(); i++) {
  57. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
  58. continue;
  59. Array a = surface_get_arrays(i);
  60. ERR_FAIL_COND_V(a.empty(), Ref<TriangleMesh>());
  61. int vc = surface_get_array_len(i);
  62. PoolVector<Vector3> vertices = a[ARRAY_VERTEX];
  63. PoolVector<Vector3>::Read vr = vertices.read();
  64. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  65. int ic = surface_get_array_index_len(i);
  66. PoolVector<int> indices = a[ARRAY_INDEX];
  67. PoolVector<int>::Read ir = indices.read();
  68. for (int j = 0; j < ic; j++) {
  69. int index = ir[j];
  70. facesw[widx++] = vr[index];
  71. }
  72. } else {
  73. for (int j = 0; j < vc; j++)
  74. facesw[widx++] = vr[j];
  75. }
  76. }
  77. facesw.release();
  78. triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  79. triangle_mesh->create(faces);
  80. return triangle_mesh;
  81. }
  82. void Mesh::generate_debug_mesh_lines(Vector<Vector3> &r_lines) {
  83. if (debug_lines.size() > 0) {
  84. r_lines = debug_lines;
  85. return;
  86. }
  87. Ref<TriangleMesh> tm = generate_triangle_mesh();
  88. if (tm.is_null())
  89. return;
  90. PoolVector<int> triangle_indices;
  91. tm->get_indices(&triangle_indices);
  92. const int triangles_num = tm->get_triangles().size();
  93. PoolVector<Vector3> vertices = tm->get_vertices();
  94. debug_lines.resize(tm->get_triangles().size() * 6); // 3 lines x 2 points each line
  95. PoolVector<int>::Read ind_r = triangle_indices.read();
  96. PoolVector<Vector3>::Read ver_r = vertices.read();
  97. for (int j = 0, x = 0, i = 0; i < triangles_num; j += 6, x += 3, ++i) {
  98. // Triangle line 1
  99. debug_lines.write[j + 0] = ver_r[ind_r[x + 0]];
  100. debug_lines.write[j + 1] = ver_r[ind_r[x + 1]];
  101. // Triangle line 2
  102. debug_lines.write[j + 2] = ver_r[ind_r[x + 1]];
  103. debug_lines.write[j + 3] = ver_r[ind_r[x + 2]];
  104. // Triangle line 3
  105. debug_lines.write[j + 4] = ver_r[ind_r[x + 2]];
  106. debug_lines.write[j + 5] = ver_r[ind_r[x + 0]];
  107. }
  108. r_lines = debug_lines;
  109. }
  110. void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) {
  111. Ref<TriangleMesh> tm = generate_triangle_mesh();
  112. if (tm.is_null())
  113. return;
  114. PoolVector<Vector3> vertices = tm->get_vertices();
  115. int vertices_size = vertices.size();
  116. r_points.resize(vertices_size);
  117. for (int i = 0; i < vertices_size; ++i) {
  118. r_points.write[i] = vertices[i];
  119. }
  120. }
  121. bool Mesh::surface_is_softbody_friendly(int p_idx) const {
  122. const uint32_t surface_format = surface_get_format(p_idx);
  123. return (surface_format & Mesh::ARRAY_FLAG_USE_DYNAMIC_UPDATE && (!(surface_format & Mesh::ARRAY_COMPRESS_VERTEX)) && (!(surface_format & Mesh::ARRAY_COMPRESS_NORMAL)));
  124. }
  125. PoolVector<Face3> Mesh::get_faces() const {
  126. Ref<TriangleMesh> tm = generate_triangle_mesh();
  127. if (tm.is_valid())
  128. return tm->get_faces();
  129. return PoolVector<Face3>();
  130. /*
  131. for (int i=0;i<surfaces.size();i++) {
  132. if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES )
  133. continue;
  134. PoolVector<int> indices;
  135. PoolVector<Vector3> vertices;
  136. vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX);
  137. int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i);
  138. bool has_indices;
  139. if (len>0) {
  140. indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX);
  141. has_indices=true;
  142. } else {
  143. len=vertices.size();
  144. has_indices=false;
  145. }
  146. if (len<=0)
  147. continue;
  148. PoolVector<int>::Read indicesr = indices.read();
  149. const int *indicesptr = indicesr.ptr();
  150. PoolVector<Vector3>::Read verticesr = vertices.read();
  151. const Vector3 *verticesptr = verticesr.ptr();
  152. int old_faces=faces.size();
  153. int new_faces=old_faces+(len/3);
  154. faces.resize(new_faces);
  155. PoolVector<Face3>::Write facesw = faces.write();
  156. Face3 *facesptr=facesw.ptr();
  157. for (int i=0;i<len/3;i++) {
  158. Face3 face;
  159. for (int j=0;j<3;j++) {
  160. int idx=i*3+j;
  161. face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx];
  162. }
  163. facesptr[i+old_faces]=face;
  164. }
  165. }
  166. */
  167. }
  168. Ref<Shape> Mesh::create_convex_shape() const {
  169. PoolVector<Vector3> vertices;
  170. for (int i = 0; i < get_surface_count(); i++) {
  171. Array a = surface_get_arrays(i);
  172. ERR_FAIL_COND_V(a.empty(), Ref<ConvexPolygonShape>());
  173. PoolVector<Vector3> v = a[ARRAY_VERTEX];
  174. vertices.append_array(v);
  175. }
  176. Ref<ConvexPolygonShape> shape = memnew(ConvexPolygonShape);
  177. shape->set_points(vertices);
  178. return shape;
  179. }
  180. Ref<Shape> Mesh::create_trimesh_shape() const {
  181. PoolVector<Face3> faces = get_faces();
  182. if (faces.size() == 0)
  183. return Ref<Shape>();
  184. PoolVector<Vector3> face_points;
  185. face_points.resize(faces.size() * 3);
  186. for (int i = 0; i < face_points.size(); i += 3) {
  187. Face3 f = faces.get(i / 3);
  188. face_points.set(i, f.vertex[0]);
  189. face_points.set(i + 1, f.vertex[1]);
  190. face_points.set(i + 2, f.vertex[2]);
  191. }
  192. Ref<ConcavePolygonShape> shape = memnew(ConcavePolygonShape);
  193. shape->set_faces(face_points);
  194. return shape;
  195. }
  196. Ref<Mesh> Mesh::create_outline(float p_margin) const {
  197. Array arrays;
  198. int index_accum = 0;
  199. for (int i = 0; i < get_surface_count(); i++) {
  200. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
  201. continue;
  202. Array a = surface_get_arrays(i);
  203. ERR_FAIL_COND_V(a.empty(), Ref<ArrayMesh>());
  204. if (i == 0) {
  205. arrays = a;
  206. PoolVector<Vector3> v = a[ARRAY_VERTEX];
  207. index_accum += v.size();
  208. } else {
  209. int vcount = 0;
  210. for (int j = 0; j < arrays.size(); j++) {
  211. if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) {
  212. //mismatch, do not use
  213. arrays[j] = Variant();
  214. continue;
  215. }
  216. switch (j) {
  217. case ARRAY_VERTEX:
  218. case ARRAY_NORMAL: {
  219. PoolVector<Vector3> dst = arrays[j];
  220. PoolVector<Vector3> src = a[j];
  221. if (j == ARRAY_VERTEX)
  222. vcount = src.size();
  223. if (dst.size() == 0 || src.size() == 0) {
  224. arrays[j] = Variant();
  225. continue;
  226. }
  227. dst.append_array(src);
  228. arrays[j] = dst;
  229. } break;
  230. case ARRAY_TANGENT:
  231. case ARRAY_BONES:
  232. case ARRAY_WEIGHTS: {
  233. PoolVector<real_t> dst = arrays[j];
  234. PoolVector<real_t> src = a[j];
  235. if (dst.size() == 0 || src.size() == 0) {
  236. arrays[j] = Variant();
  237. continue;
  238. }
  239. dst.append_array(src);
  240. arrays[j] = dst;
  241. } break;
  242. case ARRAY_COLOR: {
  243. PoolVector<Color> dst = arrays[j];
  244. PoolVector<Color> 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_TEX_UV:
  253. case ARRAY_TEX_UV2: {
  254. PoolVector<Vector2> dst = arrays[j];
  255. PoolVector<Vector2> src = a[j];
  256. if (dst.size() == 0 || src.size() == 0) {
  257. arrays[j] = Variant();
  258. continue;
  259. }
  260. dst.append_array(src);
  261. arrays[j] = dst;
  262. } break;
  263. case ARRAY_INDEX: {
  264. PoolVector<int> dst = arrays[j];
  265. PoolVector<int> src = a[j];
  266. if (dst.size() == 0 || src.size() == 0) {
  267. arrays[j] = Variant();
  268. continue;
  269. }
  270. {
  271. int ss = src.size();
  272. PoolVector<int>::Write w = src.write();
  273. for (int k = 0; k < ss; k++) {
  274. w[k] += index_accum;
  275. }
  276. }
  277. dst.append_array(src);
  278. arrays[j] = dst;
  279. index_accum += vcount;
  280. } break;
  281. }
  282. }
  283. }
  284. }
  285. ERR_FAIL_COND_V(arrays.size() != ARRAY_MAX, Ref<ArrayMesh>());
  286. {
  287. PoolVector<int>::Write ir;
  288. PoolVector<int> indices = arrays[ARRAY_INDEX];
  289. bool has_indices = false;
  290. PoolVector<Vector3> vertices = arrays[ARRAY_VERTEX];
  291. int vc = vertices.size();
  292. ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>());
  293. PoolVector<Vector3>::Write r = vertices.write();
  294. if (indices.size()) {
  295. ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ArrayMesh>());
  296. vc = indices.size();
  297. ir = indices.write();
  298. has_indices = true;
  299. }
  300. Map<Vector3, Vector3> normal_accum;
  301. //fill normals with triangle normals
  302. for (int i = 0; i < vc; i += 3) {
  303. Vector3 t[3];
  304. if (has_indices) {
  305. t[0] = r[ir[i + 0]];
  306. t[1] = r[ir[i + 1]];
  307. t[2] = r[ir[i + 2]];
  308. } else {
  309. t[0] = r[i + 0];
  310. t[1] = r[i + 1];
  311. t[2] = r[i + 2];
  312. }
  313. Vector3 n = Plane(t[0], t[1], t[2]).normal;
  314. for (int j = 0; j < 3; j++) {
  315. Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]);
  316. if (!E) {
  317. normal_accum[t[j]] = n;
  318. } else {
  319. float d = n.dot(E->get());
  320. if (d < 1.0)
  321. E->get() += n * (1.0 - d);
  322. //E->get()+=n;
  323. }
  324. }
  325. }
  326. //normalize
  327. for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) {
  328. E->get().normalize();
  329. }
  330. //displace normals
  331. int vc2 = vertices.size();
  332. for (int i = 0; i < vc2; i++) {
  333. Vector3 t = r[i];
  334. Map<Vector3, Vector3>::Element *E = normal_accum.find(t);
  335. ERR_CONTINUE(!E);
  336. t += E->get() * p_margin;
  337. r[i] = t;
  338. }
  339. r.release();
  340. arrays[ARRAY_VERTEX] = vertices;
  341. if (!has_indices) {
  342. PoolVector<int> new_indices;
  343. new_indices.resize(vertices.size());
  344. PoolVector<int>::Write iw = new_indices.write();
  345. for (int j = 0; j < vc2; j += 3) {
  346. iw[j] = j;
  347. iw[j + 1] = j + 2;
  348. iw[j + 2] = j + 1;
  349. }
  350. iw.release();
  351. arrays[ARRAY_INDEX] = new_indices;
  352. } else {
  353. for (int j = 0; j < vc; j += 3) {
  354. SWAP(ir[j + 1], ir[j + 2]);
  355. }
  356. ir.release();
  357. arrays[ARRAY_INDEX] = indices;
  358. }
  359. }
  360. Ref<ArrayMesh> newmesh = memnew(ArrayMesh);
  361. newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays);
  362. return newmesh;
  363. }
  364. void Mesh::set_lightmap_size_hint(const Vector2 &p_size) {
  365. lightmap_size_hint = p_size;
  366. }
  367. Size2 Mesh::get_lightmap_size_hint() const {
  368. return lightmap_size_hint;
  369. }
  370. void Mesh::_bind_methods() {
  371. ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &Mesh::set_lightmap_size_hint);
  372. ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &Mesh::get_lightmap_size_hint);
  373. ClassDB::bind_method(D_METHOD("get_aabb"), &Mesh::get_aabb);
  374. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "lightmap_size_hint"), "set_lightmap_size_hint", "get_lightmap_size_hint");
  375. ClassDB::bind_method(D_METHOD("get_surface_count"), &Mesh::get_surface_count);
  376. ClassDB::bind_method(D_METHOD("surface_get_arrays", "surf_idx"), &Mesh::surface_get_arrays);
  377. ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &Mesh::surface_get_blend_shape_arrays);
  378. ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material);
  379. ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material);
  380. BIND_ENUM_CONSTANT(PRIMITIVE_POINTS);
  381. BIND_ENUM_CONSTANT(PRIMITIVE_LINES);
  382. BIND_ENUM_CONSTANT(PRIMITIVE_LINE_STRIP);
  383. BIND_ENUM_CONSTANT(PRIMITIVE_LINE_LOOP);
  384. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLES);
  385. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
  386. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_FAN);
  387. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_NORMALIZED);
  388. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_RELATIVE);
  389. BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
  390. BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
  391. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
  392. BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
  393. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
  394. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
  395. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
  396. BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  397. BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
  398. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_BASE);
  399. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_VERTEX);
  400. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_NORMAL);
  401. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TANGENT);
  402. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_COLOR);
  403. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TEX_UV);
  404. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TEX_UV2);
  405. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_BONES);
  406. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_WEIGHTS);
  407. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_INDEX);
  408. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_2D_VERTICES);
  409. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_16_BIT_BONES);
  410. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_DEFAULT);
  411. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  412. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  413. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  414. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  415. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  416. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  417. BIND_ENUM_CONSTANT(ARRAY_BONES);
  418. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  419. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  420. BIND_ENUM_CONSTANT(ARRAY_MAX);
  421. }
  422. void Mesh::clear_cache() const {
  423. triangle_mesh.unref();
  424. debug_lines.clear();
  425. }
  426. Vector<Ref<Shape> > Mesh::convex_decompose() const {
  427. ERR_FAIL_COND_V(!convex_composition_function, Vector<Ref<Shape> >());
  428. PoolVector<Face3> faces = get_faces();
  429. Vector<Face3> f3;
  430. f3.resize(faces.size());
  431. PoolVector<Face3>::Read f = faces.read();
  432. for (int i = 0; i < f3.size(); i++) {
  433. f3.write[i] = f[i];
  434. }
  435. Vector<Vector<Face3> > decomposed = convex_composition_function(f3);
  436. Vector<Ref<Shape> > ret;
  437. for (int i = 0; i < decomposed.size(); i++) {
  438. Set<Vector3> points;
  439. for (int j = 0; j < decomposed[i].size(); j++) {
  440. points.insert(decomposed[i][j].vertex[0]);
  441. points.insert(decomposed[i][j].vertex[1]);
  442. points.insert(decomposed[i][j].vertex[2]);
  443. }
  444. PoolVector<Vector3> convex_points;
  445. convex_points.resize(points.size());
  446. {
  447. PoolVector<Vector3>::Write w = convex_points.write();
  448. int idx = 0;
  449. for (Set<Vector3>::Element *E = points.front(); E; E = E->next()) {
  450. w[idx++] = E->get();
  451. }
  452. }
  453. Ref<ConvexPolygonShape> shape;
  454. shape.instance();
  455. shape->set_points(convex_points);
  456. ret.push_back(shape);
  457. }
  458. return ret;
  459. }
  460. Mesh::Mesh() {
  461. }
  462. bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) {
  463. String sname = p_name;
  464. if (p_name == "blend_shape/names") {
  465. PoolVector<String> sk = p_value;
  466. int sz = sk.size();
  467. PoolVector<String>::Read r = sk.read();
  468. for (int i = 0; i < sz; i++)
  469. add_blend_shape(r[i]);
  470. return true;
  471. }
  472. if (p_name == "blend_shape/mode") {
  473. set_blend_shape_mode(BlendShapeMode(int(p_value)));
  474. return true;
  475. }
  476. if (sname.begins_with("surface_")) {
  477. int sl = sname.find("/");
  478. if (sl == -1)
  479. return false;
  480. int idx = sname.substr(8, sl - 8).to_int() - 1;
  481. String what = sname.get_slicec('/', 1);
  482. if (what == "material")
  483. surface_set_material(idx, p_value);
  484. else if (what == "name")
  485. surface_set_name(idx, p_value);
  486. return true;
  487. }
  488. if (!sname.begins_with("surfaces"))
  489. return false;
  490. int idx = sname.get_slicec('/', 1).to_int();
  491. String what = sname.get_slicec('/', 2);
  492. if (idx == surfaces.size()) {
  493. //create
  494. Dictionary d = p_value;
  495. ERR_FAIL_COND_V(!d.has("primitive"), false);
  496. if (d.has("arrays")) {
  497. //old format
  498. ERR_FAIL_COND_V(!d.has("morph_arrays"), false);
  499. add_surface_from_arrays(PrimitiveType(int(d["primitive"])), d["arrays"], d["morph_arrays"]);
  500. } else if (d.has("array_data")) {
  501. PoolVector<uint8_t> array_data = d["array_data"];
  502. PoolVector<uint8_t> array_index_data;
  503. if (d.has("array_index_data"))
  504. array_index_data = d["array_index_data"];
  505. ERR_FAIL_COND_V(!d.has("format"), false);
  506. uint32_t format = d["format"];
  507. uint32_t primitive = d["primitive"];
  508. ERR_FAIL_COND_V(!d.has("vertex_count"), false);
  509. int vertex_count = d["vertex_count"];
  510. int index_count = 0;
  511. if (d.has("index_count"))
  512. index_count = d["index_count"];
  513. Vector<PoolVector<uint8_t> > blend_shapes;
  514. if (d.has("blend_shape_data")) {
  515. Array blend_shape_data = d["blend_shape_data"];
  516. for (int i = 0; i < blend_shape_data.size(); i++) {
  517. PoolVector<uint8_t> shape = blend_shape_data[i];
  518. blend_shapes.push_back(shape);
  519. }
  520. }
  521. ERR_FAIL_COND_V(!d.has("aabb"), false);
  522. AABB aabb = d["aabb"];
  523. Vector<AABB> bone_aabb;
  524. if (d.has("skeleton_aabb")) {
  525. Array baabb = d["skeleton_aabb"];
  526. bone_aabb.resize(baabb.size());
  527. for (int i = 0; i < baabb.size(); i++) {
  528. bone_aabb.write[i] = baabb[i];
  529. }
  530. }
  531. add_surface(format, PrimitiveType(primitive), array_data, vertex_count, array_index_data, index_count, aabb, blend_shapes, bone_aabb);
  532. } else {
  533. ERR_FAIL_V(false);
  534. }
  535. if (d.has("material")) {
  536. surface_set_material(idx, d["material"]);
  537. }
  538. if (d.has("name")) {
  539. surface_set_name(idx, d["name"]);
  540. }
  541. return true;
  542. }
  543. return false;
  544. }
  545. bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const {
  546. if (_is_generated())
  547. return false;
  548. String sname = p_name;
  549. if (p_name == "blend_shape/names") {
  550. PoolVector<String> sk;
  551. for (int i = 0; i < blend_shapes.size(); i++)
  552. sk.push_back(blend_shapes[i]);
  553. r_ret = sk;
  554. return true;
  555. } else if (p_name == "blend_shape/mode") {
  556. r_ret = get_blend_shape_mode();
  557. return true;
  558. } else if (sname.begins_with("surface_")) {
  559. int sl = sname.find("/");
  560. if (sl == -1)
  561. return false;
  562. int idx = sname.substr(8, sl - 8).to_int() - 1;
  563. String what = sname.get_slicec('/', 1);
  564. if (what == "material")
  565. r_ret = surface_get_material(idx);
  566. else if (what == "name")
  567. r_ret = surface_get_name(idx);
  568. return true;
  569. } else if (!sname.begins_with("surfaces"))
  570. return false;
  571. int idx = sname.get_slicec('/', 1).to_int();
  572. ERR_FAIL_INDEX_V(idx, surfaces.size(), false);
  573. Dictionary d;
  574. d["array_data"] = VS::get_singleton()->mesh_surface_get_array(mesh, idx);
  575. d["vertex_count"] = VS::get_singleton()->mesh_surface_get_array_len(mesh, idx);
  576. d["array_index_data"] = VS::get_singleton()->mesh_surface_get_index_array(mesh, idx);
  577. d["index_count"] = VS::get_singleton()->mesh_surface_get_array_index_len(mesh, idx);
  578. d["primitive"] = VS::get_singleton()->mesh_surface_get_primitive_type(mesh, idx);
  579. d["format"] = VS::get_singleton()->mesh_surface_get_format(mesh, idx);
  580. d["aabb"] = VS::get_singleton()->mesh_surface_get_aabb(mesh, idx);
  581. Vector<AABB> skel_aabb = VS::get_singleton()->mesh_surface_get_skeleton_aabb(mesh, idx);
  582. Array arr;
  583. arr.resize(skel_aabb.size());
  584. for (int i = 0; i < skel_aabb.size(); i++) {
  585. arr[i] = skel_aabb[i];
  586. }
  587. d["skeleton_aabb"] = arr;
  588. Vector<PoolVector<uint8_t> > blend_shape_data = VS::get_singleton()->mesh_surface_get_blend_shapes(mesh, idx);
  589. Array md;
  590. for (int i = 0; i < blend_shape_data.size(); i++) {
  591. md.push_back(blend_shape_data[i]);
  592. }
  593. d["blend_shape_data"] = md;
  594. Ref<Material> m = surface_get_material(idx);
  595. if (m.is_valid())
  596. d["material"] = m;
  597. String n = surface_get_name(idx);
  598. if (n != "")
  599. d["name"] = n;
  600. r_ret = d;
  601. return true;
  602. }
  603. void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const {
  604. if (_is_generated())
  605. return;
  606. if (blend_shapes.size()) {
  607. p_list->push_back(PropertyInfo(Variant::POOL_STRING_ARRAY, "blend_shape/names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  608. p_list->push_back(PropertyInfo(Variant::INT, "blend_shape/mode", PROPERTY_HINT_ENUM, "Normalized,Relative"));
  609. }
  610. for (int i = 0; i < surfaces.size(); i++) {
  611. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "surfaces/" + itos(i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  612. p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i + 1) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  613. if (surfaces[i].is_2d) {
  614. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,CanvasItemMaterial", PROPERTY_USAGE_EDITOR));
  615. } else {
  616. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial", PROPERTY_USAGE_EDITOR));
  617. }
  618. }
  619. }
  620. void ArrayMesh::_recompute_aabb() {
  621. // regenerate AABB
  622. aabb = AABB();
  623. for (int i = 0; i < surfaces.size(); i++) {
  624. if (i == 0)
  625. aabb = surfaces[i].aabb;
  626. else
  627. aabb.merge_with(surfaces[i].aabb);
  628. }
  629. }
  630. void ArrayMesh::add_surface(uint32_t p_format, PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t> > &p_blend_shapes, const Vector<AABB> &p_bone_aabbs) {
  631. Surface s;
  632. s.aabb = p_aabb;
  633. s.is_2d = p_format & ARRAY_FLAG_USE_2D_VERTICES;
  634. surfaces.push_back(s);
  635. _recompute_aabb();
  636. VisualServer::get_singleton()->mesh_add_surface(mesh, p_format, (VS::PrimitiveType)p_primitive, p_array, p_vertex_count, p_index_array, p_index_count, p_aabb, p_blend_shapes, p_bone_aabbs);
  637. }
  638. void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, uint32_t p_flags) {
  639. ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX);
  640. Surface s;
  641. VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh, (VisualServer::PrimitiveType)p_primitive, p_arrays, p_blend_shapes, p_flags);
  642. /* make aABB? */ {
  643. Variant arr = p_arrays[ARRAY_VERTEX];
  644. PoolVector<Vector3> vertices = arr;
  645. int len = vertices.size();
  646. ERR_FAIL_COND(len == 0);
  647. PoolVector<Vector3>::Read r = vertices.read();
  648. const Vector3 *vtx = r.ptr();
  649. // check AABB
  650. AABB aabb;
  651. for (int i = 0; i < len; i++) {
  652. if (i == 0)
  653. aabb.position = vtx[i];
  654. else
  655. aabb.expand_to(vtx[i]);
  656. }
  657. s.aabb = aabb;
  658. s.is_2d = arr.get_type() == Variant::POOL_VECTOR2_ARRAY;
  659. surfaces.push_back(s);
  660. _recompute_aabb();
  661. }
  662. clear_cache();
  663. _change_notify();
  664. emit_changed();
  665. }
  666. Array ArrayMesh::surface_get_arrays(int p_surface) const {
  667. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  668. return VisualServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
  669. }
  670. Array ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const {
  671. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  672. return VisualServer::get_singleton()->mesh_surface_get_blend_shape_arrays(mesh, p_surface);
  673. }
  674. int ArrayMesh::get_surface_count() const {
  675. return surfaces.size();
  676. }
  677. void ArrayMesh::add_blend_shape(const StringName &p_name) {
  678. ERR_FAIL_COND_MSG(surfaces.size(), "Can't add a shape key count if surfaces are already created.");
  679. StringName name = p_name;
  680. if (blend_shapes.find(name) != -1) {
  681. int count = 2;
  682. do {
  683. name = String(p_name) + " " + itos(count);
  684. count++;
  685. } while (blend_shapes.find(name) != -1);
  686. }
  687. blend_shapes.push_back(name);
  688. VS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  689. }
  690. int ArrayMesh::get_blend_shape_count() const {
  691. return blend_shapes.size();
  692. }
  693. StringName ArrayMesh::get_blend_shape_name(int p_index) const {
  694. ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName());
  695. return blend_shapes[p_index];
  696. }
  697. void ArrayMesh::clear_blend_shapes() {
  698. ERR_FAIL_COND_MSG(surfaces.size(), "Can't set shape key count if surfaces are already created.");
  699. blend_shapes.clear();
  700. }
  701. void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) {
  702. blend_shape_mode = p_mode;
  703. VS::get_singleton()->mesh_set_blend_shape_mode(mesh, (VS::BlendShapeMode)p_mode);
  704. }
  705. ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const {
  706. return blend_shape_mode;
  707. }
  708. void ArrayMesh::surface_remove(int p_idx) {
  709. ERR_FAIL_INDEX(p_idx, surfaces.size());
  710. VisualServer::get_singleton()->mesh_remove_surface(mesh, p_idx);
  711. surfaces.remove(p_idx);
  712. clear_cache();
  713. _recompute_aabb();
  714. _change_notify();
  715. emit_changed();
  716. }
  717. int ArrayMesh::surface_get_array_len(int p_idx) const {
  718. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  719. return VisualServer::get_singleton()->mesh_surface_get_array_len(mesh, p_idx);
  720. }
  721. int ArrayMesh::surface_get_array_index_len(int p_idx) const {
  722. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  723. return VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, p_idx);
  724. }
  725. uint32_t ArrayMesh::surface_get_format(int p_idx) const {
  726. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0);
  727. return VisualServer::get_singleton()->mesh_surface_get_format(mesh, p_idx);
  728. }
  729. ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const {
  730. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES);
  731. return (PrimitiveType)VisualServer::get_singleton()->mesh_surface_get_primitive_type(mesh, p_idx);
  732. }
  733. void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  734. ERR_FAIL_INDEX(p_idx, surfaces.size());
  735. if (surfaces[p_idx].material == p_material)
  736. return;
  737. surfaces.write[p_idx].material = p_material;
  738. VisualServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
  739. _change_notify("material");
  740. emit_changed();
  741. }
  742. int ArrayMesh::surface_find_by_name(const String &p_name) const {
  743. for (int i = 0; i < surfaces.size(); i++) {
  744. if (surfaces[i].name == p_name) {
  745. return i;
  746. }
  747. }
  748. return -1;
  749. }
  750. void ArrayMesh::surface_set_name(int p_idx, const String &p_name) {
  751. ERR_FAIL_INDEX(p_idx, surfaces.size());
  752. surfaces.write[p_idx].name = p_name;
  753. emit_changed();
  754. }
  755. String ArrayMesh::surface_get_name(int p_idx) const {
  756. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String());
  757. return surfaces[p_idx].name;
  758. }
  759. void ArrayMesh::surface_update_region(int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) {
  760. ERR_FAIL_INDEX(p_surface, surfaces.size());
  761. VS::get_singleton()->mesh_surface_update_region(mesh, p_surface, p_offset, p_data);
  762. emit_changed();
  763. }
  764. void ArrayMesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) {
  765. ERR_FAIL_INDEX(p_idx, surfaces.size());
  766. surfaces.write[p_idx].aabb = p_aabb;
  767. // set custom aabb too?
  768. emit_changed();
  769. }
  770. Ref<Material> ArrayMesh::surface_get_material(int p_idx) const {
  771. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>());
  772. return surfaces[p_idx].material;
  773. }
  774. void ArrayMesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) {
  775. VisualServer::get_singleton()->mesh_add_surface_from_mesh_data(mesh, p_mesh_data);
  776. AABB aabb;
  777. for (int i = 0; i < p_mesh_data.vertices.size(); i++) {
  778. if (i == 0)
  779. aabb.position = p_mesh_data.vertices[i];
  780. else
  781. aabb.expand_to(p_mesh_data.vertices[i]);
  782. }
  783. Surface s;
  784. s.aabb = aabb;
  785. if (surfaces.size() == 0)
  786. aabb = s.aabb;
  787. else
  788. aabb.merge_with(s.aabb);
  789. clear_cache();
  790. surfaces.push_back(s);
  791. _change_notify();
  792. emit_changed();
  793. }
  794. RID ArrayMesh::get_rid() const {
  795. return mesh;
  796. }
  797. AABB ArrayMesh::get_aabb() const {
  798. return aabb;
  799. }
  800. void ArrayMesh::set_custom_aabb(const AABB &p_custom) {
  801. custom_aabb = p_custom;
  802. VS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  803. emit_changed();
  804. }
  805. AABB ArrayMesh::get_custom_aabb() const {
  806. return custom_aabb;
  807. }
  808. void ArrayMesh::regen_normalmaps() {
  809. Vector<Ref<SurfaceTool> > surfs;
  810. for (int i = 0; i < get_surface_count(); i++) {
  811. Ref<SurfaceTool> st = memnew(SurfaceTool);
  812. st->create_from(Ref<ArrayMesh>(this), i);
  813. surfs.push_back(st);
  814. }
  815. while (get_surface_count()) {
  816. surface_remove(0);
  817. }
  818. for (int i = 0; i < surfs.size(); i++) {
  819. surfs.write[i]->generate_tangents();
  820. surfs.write[i]->commit(Ref<ArrayMesh>(this));
  821. }
  822. }
  823. //dirty hack
  824. 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, const int *p_face_materials, 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) = NULL;
  825. struct ArrayMeshLightmapSurface {
  826. Ref<Material> material;
  827. Vector<SurfaceTool::Vertex> vertices;
  828. Mesh::PrimitiveType primitive;
  829. uint32_t format;
  830. };
  831. Error ArrayMesh::lightmap_unwrap(const Transform &p_base_transform, float p_texel_size) {
  832. ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
  833. ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");
  834. Vector<float> vertices;
  835. Vector<float> normals;
  836. Vector<int> indices;
  837. Vector<int> face_materials;
  838. Vector<float> uv;
  839. Vector<Pair<int, int> > uv_index;
  840. Vector<ArrayMeshLightmapSurface> surfaces;
  841. for (int i = 0; i < get_surface_count(); i++) {
  842. ArrayMeshLightmapSurface s;
  843. s.primitive = surface_get_primitive_type(i);
  844. ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap.");
  845. s.format = surface_get_format(i);
  846. ERR_FAIL_COND_V_MSG(!(s.format & ARRAY_FORMAT_NORMAL), ERR_UNAVAILABLE, "Normals are required for lightmap unwrap.");
  847. Array arrays = surface_get_arrays(i);
  848. s.material = surface_get_material(i);
  849. s.vertices = SurfaceTool::create_vertex_array_from_triangle_arrays(arrays);
  850. PoolVector<Vector3> rvertices = arrays[Mesh::ARRAY_VERTEX];
  851. int vc = rvertices.size();
  852. PoolVector<Vector3>::Read r = rvertices.read();
  853. PoolVector<Vector3> rnormals = arrays[Mesh::ARRAY_NORMAL];
  854. PoolVector<Vector3>::Read rn = rnormals.read();
  855. int vertex_ofs = vertices.size() / 3;
  856. vertices.resize((vertex_ofs + vc) * 3);
  857. normals.resize((vertex_ofs + vc) * 3);
  858. uv_index.resize(vertex_ofs + vc);
  859. for (int j = 0; j < vc; j++) {
  860. Vector3 v = p_base_transform.xform(r[j]);
  861. Vector3 n = p_base_transform.basis.xform(rn[j]).normalized();
  862. vertices.write[(j + vertex_ofs) * 3 + 0] = v.x;
  863. vertices.write[(j + vertex_ofs) * 3 + 1] = v.y;
  864. vertices.write[(j + vertex_ofs) * 3 + 2] = v.z;
  865. normals.write[(j + vertex_ofs) * 3 + 0] = n.x;
  866. normals.write[(j + vertex_ofs) * 3 + 1] = n.y;
  867. normals.write[(j + vertex_ofs) * 3 + 2] = n.z;
  868. uv_index.write[j + vertex_ofs] = Pair<int, int>(i, j);
  869. }
  870. PoolVector<int> rindices = arrays[Mesh::ARRAY_INDEX];
  871. int ic = rindices.size();
  872. if (ic == 0) {
  873. for (int j = 0; j < vc / 3; j++) {
  874. if (Face3(r[j * 3 + 0], r[j * 3 + 1], r[j * 3 + 2]).is_degenerate())
  875. continue;
  876. indices.push_back(vertex_ofs + j * 3 + 0);
  877. indices.push_back(vertex_ofs + j * 3 + 1);
  878. indices.push_back(vertex_ofs + j * 3 + 2);
  879. face_materials.push_back(i);
  880. }
  881. } else {
  882. PoolVector<int>::Read ri = rindices.read();
  883. for (int j = 0; j < ic / 3; j++) {
  884. if (Face3(r[ri[j * 3 + 0]], r[ri[j * 3 + 1]], r[ri[j * 3 + 2]]).is_degenerate())
  885. continue;
  886. indices.push_back(vertex_ofs + ri[j * 3 + 0]);
  887. indices.push_back(vertex_ofs + ri[j * 3 + 1]);
  888. indices.push_back(vertex_ofs + ri[j * 3 + 2]);
  889. face_materials.push_back(i);
  890. }
  891. }
  892. surfaces.push_back(s);
  893. }
  894. //unwrap
  895. float *gen_uvs;
  896. int *gen_vertices;
  897. int *gen_indices;
  898. int gen_vertex_count;
  899. int gen_index_count;
  900. int size_x;
  901. int size_y;
  902. bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), face_materials.ptr(), indices.size(), &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y);
  903. if (!ok) {
  904. return ERR_CANT_CREATE;
  905. }
  906. //remove surfaces
  907. while (get_surface_count()) {
  908. surface_remove(0);
  909. }
  910. //create surfacetools for each surface..
  911. Vector<Ref<SurfaceTool> > surfaces_tools;
  912. for (int i = 0; i < surfaces.size(); i++) {
  913. Ref<SurfaceTool> st;
  914. st.instance();
  915. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  916. st->set_material(surfaces[i].material);
  917. surfaces_tools.push_back(st); //stay there
  918. }
  919. print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
  920. //go through all indices
  921. for (int i = 0; i < gen_index_count; i += 3) {
  922. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], uv_index.size(), ERR_BUG);
  923. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], uv_index.size(), ERR_BUG);
  924. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], uv_index.size(), ERR_BUG);
  925. ERR_FAIL_COND_V(uv_index[gen_vertices[gen_indices[i + 0]]].first != uv_index[gen_vertices[gen_indices[i + 1]]].first || uv_index[gen_vertices[gen_indices[i + 0]]].first != uv_index[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG);
  926. int surface = uv_index[gen_vertices[gen_indices[i + 0]]].first;
  927. for (int j = 0; j < 3; j++) {
  928. SurfaceTool::Vertex v = surfaces[surface].vertices[uv_index[gen_vertices[gen_indices[i + j]]].second];
  929. if (surfaces[surface].format & ARRAY_FORMAT_COLOR) {
  930. surfaces_tools.write[surface]->add_color(v.color);
  931. }
  932. if (surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
  933. surfaces_tools.write[surface]->add_uv(v.uv);
  934. }
  935. if (surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
  936. surfaces_tools.write[surface]->add_normal(v.normal);
  937. }
  938. if (surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
  939. Plane t;
  940. t.normal = v.tangent;
  941. t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
  942. surfaces_tools.write[surface]->add_tangent(t);
  943. }
  944. if (surfaces[surface].format & ARRAY_FORMAT_BONES) {
  945. surfaces_tools.write[surface]->add_bones(v.bones);
  946. }
  947. if (surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
  948. surfaces_tools.write[surface]->add_weights(v.weights);
  949. }
  950. Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
  951. surfaces_tools.write[surface]->add_uv2(uv2);
  952. surfaces_tools.write[surface]->add_vertex(v.vertex);
  953. }
  954. }
  955. //free stuff
  956. ::free(gen_vertices);
  957. ::free(gen_indices);
  958. ::free(gen_uvs);
  959. //generate surfaces
  960. for (int i = 0; i < surfaces_tools.size(); i++) {
  961. surfaces_tools.write[i]->index();
  962. surfaces_tools.write[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), surfaces[i].format);
  963. }
  964. set_lightmap_size_hint(Size2(size_x, size_y));
  965. return OK;
  966. }
  967. void ArrayMesh::_bind_methods() {
  968. ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape);
  969. ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count);
  970. ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name);
  971. ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes);
  972. ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode);
  973. ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode);
  974. ClassDB::bind_method(D_METHOD("add_surface_from_arrays", "primitive", "arrays", "blend_shapes", "compress_flags"), &ArrayMesh::add_surface_from_arrays, DEFVAL(Array()), DEFVAL(ARRAY_COMPRESS_DEFAULT));
  975. ClassDB::bind_method(D_METHOD("surface_remove", "surf_idx"), &ArrayMesh::surface_remove);
  976. ClassDB::bind_method(D_METHOD("surface_update_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_region);
  977. ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len);
  978. ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len);
  979. ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format);
  980. ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type);
  981. ClassDB::bind_method(D_METHOD("surface_find_by_name", "name"), &ArrayMesh::surface_find_by_name);
  982. ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name);
  983. ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name);
  984. ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape);
  985. ClassDB::bind_method(D_METHOD("create_convex_shape"), &ArrayMesh::create_convex_shape);
  986. ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline);
  987. ClassDB::bind_method(D_METHOD("regen_normalmaps"), &ArrayMesh::regen_normalmaps);
  988. ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normalmaps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  989. ClassDB::bind_method(D_METHOD("lightmap_unwrap", "transform", "texel_size"), &ArrayMesh::lightmap_unwrap);
  990. ClassDB::set_method_flags(get_class_static(), _scs_create("lightmap_unwrap"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  991. ClassDB::bind_method(D_METHOD("get_faces"), &ArrayMesh::get_faces);
  992. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh);
  993. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb);
  994. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb);
  995. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_shape_mode", PROPERTY_HINT_ENUM, "Normalized,Relative", PROPERTY_USAGE_NOEDITOR), "set_blend_shape_mode", "get_blend_shape_mode");
  996. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
  997. BIND_CONSTANT(NO_INDEX_ARRAY);
  998. BIND_CONSTANT(ARRAY_WEIGHTS_SIZE);
  999. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  1000. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  1001. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  1002. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  1003. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  1004. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  1005. BIND_ENUM_CONSTANT(ARRAY_BONES);
  1006. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  1007. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  1008. BIND_ENUM_CONSTANT(ARRAY_MAX);
  1009. BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
  1010. BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
  1011. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
  1012. BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
  1013. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
  1014. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
  1015. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
  1016. BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  1017. BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
  1018. }
  1019. void ArrayMesh::reload_from_file() {
  1020. VisualServer::get_singleton()->mesh_clear(mesh);
  1021. surfaces.clear();
  1022. clear_blend_shapes();
  1023. clear_cache();
  1024. Resource::reload_from_file();
  1025. _change_notify();
  1026. }
  1027. ArrayMesh::ArrayMesh() {
  1028. mesh = VisualServer::get_singleton()->mesh_create();
  1029. blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
  1030. }
  1031. ArrayMesh::~ArrayMesh() {
  1032. VisualServer::get_singleton()->free(mesh);
  1033. }