mesh.cpp 39 KB

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