2
0

mesh.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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 j = 0; j < ic; j++) {
  67. int index = ir[j];
  68. facesw[widx++] = vr[index];
  69. }
  70. } else {
  71. for (int j = 0; j < vc; j++)
  72. facesw[widx++] = vr[j];
  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. /* make aABB? */ {
  600. Variant arr = p_arrays[ARRAY_VERTEX];
  601. PoolVector<Vector3> vertices = arr;
  602. int len = vertices.size();
  603. ERR_FAIL_COND(len == 0);
  604. PoolVector<Vector3>::Read r = vertices.read();
  605. const Vector3 *vtx = r.ptr();
  606. // check AABB
  607. AABB aabb;
  608. for (int i = 0; i < len; i++) {
  609. if (i == 0)
  610. aabb.position = vtx[i];
  611. else
  612. aabb.expand_to(vtx[i]);
  613. }
  614. s.aabb = aabb;
  615. s.is_2d = arr.get_type() == Variant::POOL_VECTOR2_ARRAY;
  616. surfaces.push_back(s);
  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::regen_normalmaps() {
  772. Vector<Ref<SurfaceTool> > surfs;
  773. for (int i = 0; i < get_surface_count(); i++) {
  774. Ref<SurfaceTool> st = memnew(SurfaceTool);
  775. st->create_from(Ref<ArrayMesh>(this), i);
  776. surfs.push_back(st);
  777. }
  778. while (get_surface_count()) {
  779. surface_remove(0);
  780. }
  781. for (int i = 0; i < surfs.size(); i++) {
  782. surfs.write[i]->generate_tangents();
  783. surfs.write[i]->commit(Ref<ArrayMesh>(this));
  784. }
  785. }
  786. //dirty hack
  787. 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;
  788. struct ArrayMeshLightmapSurface {
  789. Ref<Material> material;
  790. Vector<SurfaceTool::Vertex> vertices;
  791. Mesh::PrimitiveType primitive;
  792. uint32_t format;
  793. };
  794. Error ArrayMesh::lightmap_unwrap(const Transform &p_base_transform, float p_texel_size) {
  795. ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
  796. ERR_EXPLAIN("Can't unwrap mesh with blend shapes");
  797. ERR_FAIL_COND_V(blend_shapes.size() != 0, ERR_UNAVAILABLE);
  798. Vector<float> vertices;
  799. Vector<float> normals;
  800. Vector<int> indices;
  801. Vector<int> face_materials;
  802. Vector<float> uv;
  803. Vector<Pair<int, int> > uv_index;
  804. Vector<ArrayMeshLightmapSurface> surfaces;
  805. for (int i = 0; i < get_surface_count(); i++) {
  806. ArrayMeshLightmapSurface s;
  807. s.primitive = surface_get_primitive_type(i);
  808. if (s.primitive != Mesh::PRIMITIVE_TRIANGLES) {
  809. ERR_EXPLAIN("Only triangles are supported for lightmap unwrap");
  810. ERR_FAIL_V(ERR_UNAVAILABLE);
  811. }
  812. s.format = surface_get_format(i);
  813. if (!(s.format & ARRAY_FORMAT_NORMAL)) {
  814. ERR_EXPLAIN("Normals are required for lightmap unwrap");
  815. ERR_FAIL_V(ERR_UNAVAILABLE);
  816. }
  817. Array arrays = surface_get_arrays(i);
  818. s.material = surface_get_material(i);
  819. s.vertices = SurfaceTool::create_vertex_array_from_triangle_arrays(arrays);
  820. PoolVector<Vector3> rvertices = arrays[Mesh::ARRAY_VERTEX];
  821. int vc = rvertices.size();
  822. PoolVector<Vector3>::Read r = rvertices.read();
  823. PoolVector<Vector3> rnormals = arrays[Mesh::ARRAY_NORMAL];
  824. PoolVector<Vector3>::Read rn = rnormals.read();
  825. int vertex_ofs = vertices.size() / 3;
  826. vertices.resize((vertex_ofs + vc) * 3);
  827. normals.resize((vertex_ofs + vc) * 3);
  828. uv_index.resize(vertex_ofs + vc);
  829. for (int j = 0; j < vc; j++) {
  830. Vector3 v = p_base_transform.xform(r[j]);
  831. Vector3 n = p_base_transform.basis.xform(rn[j]).normalized();
  832. vertices.write[(j + vertex_ofs) * 3 + 0] = v.x;
  833. vertices.write[(j + vertex_ofs) * 3 + 1] = v.y;
  834. vertices.write[(j + vertex_ofs) * 3 + 2] = v.z;
  835. normals.write[(j + vertex_ofs) * 3 + 0] = n.x;
  836. normals.write[(j + vertex_ofs) * 3 + 1] = n.y;
  837. normals.write[(j + vertex_ofs) * 3 + 2] = n.z;
  838. uv_index.write[j + vertex_ofs] = Pair<int, int>(i, j);
  839. }
  840. PoolVector<int> rindices = arrays[Mesh::ARRAY_INDEX];
  841. int ic = rindices.size();
  842. if (ic == 0) {
  843. for (int j = 0; j < vc / 3; j++) {
  844. if (Face3(r[j * 3 + 0], r[j * 3 + 1], r[j * 3 + 2]).is_degenerate())
  845. continue;
  846. indices.push_back(vertex_ofs + j * 3 + 0);
  847. indices.push_back(vertex_ofs + j * 3 + 1);
  848. indices.push_back(vertex_ofs + j * 3 + 2);
  849. face_materials.push_back(i);
  850. }
  851. } else {
  852. PoolVector<int>::Read ri = rindices.read();
  853. for (int j = 0; j < ic / 3; j++) {
  854. if (Face3(r[ri[j * 3 + 0]], r[ri[j * 3 + 1]], r[ri[j * 3 + 2]]).is_degenerate())
  855. continue;
  856. indices.push_back(vertex_ofs + ri[j * 3 + 0]);
  857. indices.push_back(vertex_ofs + ri[j * 3 + 1]);
  858. indices.push_back(vertex_ofs + ri[j * 3 + 2]);
  859. face_materials.push_back(i);
  860. }
  861. }
  862. surfaces.push_back(s);
  863. }
  864. //unwrap
  865. float *gen_uvs;
  866. int *gen_vertices;
  867. int *gen_indices;
  868. int gen_vertex_count;
  869. int gen_index_count;
  870. int size_x;
  871. int size_y;
  872. 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);
  873. if (!ok) {
  874. return ERR_CANT_CREATE;
  875. }
  876. //remove surfaces
  877. while (get_surface_count()) {
  878. surface_remove(0);
  879. }
  880. //create surfacetools for each surface..
  881. Vector<Ref<SurfaceTool> > surfaces_tools;
  882. for (int i = 0; i < surfaces.size(); i++) {
  883. Ref<SurfaceTool> st;
  884. st.instance();
  885. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  886. st->set_material(surfaces[i].material);
  887. surfaces_tools.push_back(st); //stay there
  888. }
  889. print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
  890. //go through all indices
  891. for (int i = 0; i < gen_index_count; i += 3) {
  892. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], uv_index.size(), ERR_BUG);
  893. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], uv_index.size(), ERR_BUG);
  894. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], uv_index.size(), ERR_BUG);
  895. 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);
  896. int surface = uv_index[gen_vertices[gen_indices[i + 0]]].first;
  897. for (int j = 0; j < 3; j++) {
  898. SurfaceTool::Vertex v = surfaces[surface].vertices[uv_index[gen_vertices[gen_indices[i + j]]].second];
  899. if (surfaces[surface].format & ARRAY_FORMAT_COLOR) {
  900. surfaces_tools.write[surface]->add_color(v.color);
  901. }
  902. if (surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
  903. surfaces_tools.write[surface]->add_uv(v.uv);
  904. }
  905. if (surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
  906. surfaces_tools.write[surface]->add_normal(v.normal);
  907. }
  908. if (surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
  909. Plane t;
  910. t.normal = v.tangent;
  911. t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
  912. surfaces_tools.write[surface]->add_tangent(t);
  913. }
  914. if (surfaces[surface].format & ARRAY_FORMAT_BONES) {
  915. surfaces_tools.write[surface]->add_bones(v.bones);
  916. }
  917. if (surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
  918. surfaces_tools.write[surface]->add_weights(v.weights);
  919. }
  920. Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
  921. surfaces_tools.write[surface]->add_uv2(uv2);
  922. surfaces_tools.write[surface]->add_vertex(v.vertex);
  923. }
  924. }
  925. //free stuff
  926. ::free(gen_vertices);
  927. ::free(gen_indices);
  928. ::free(gen_uvs);
  929. //generate surfaces
  930. for (int i = 0; i < surfaces_tools.size(); i++) {
  931. surfaces_tools.write[i]->index();
  932. surfaces_tools.write[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), surfaces[i].format);
  933. }
  934. set_lightmap_size_hint(Size2(size_x, size_y));
  935. return OK;
  936. }
  937. void ArrayMesh::_bind_methods() {
  938. ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape);
  939. ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count);
  940. ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name);
  941. ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes);
  942. ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode);
  943. ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode);
  944. 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));
  945. ClassDB::bind_method(D_METHOD("surface_remove", "surf_idx"), &ArrayMesh::surface_remove);
  946. ClassDB::bind_method(D_METHOD("surface_update_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_region);
  947. ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len);
  948. ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len);
  949. ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format);
  950. ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type);
  951. ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &ArrayMesh::surface_set_material);
  952. ClassDB::bind_method(D_METHOD("surface_find_by_name", "name"), &ArrayMesh::surface_find_by_name);
  953. ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name);
  954. ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name);
  955. ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape);
  956. ClassDB::bind_method(D_METHOD("create_convex_shape"), &ArrayMesh::create_convex_shape);
  957. ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline);
  958. ClassDB::bind_method(D_METHOD("regen_normalmaps"), &ArrayMesh::regen_normalmaps);
  959. ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normalmaps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  960. ClassDB::bind_method(D_METHOD("lightmap_unwrap", "transform", "texel_size"), &ArrayMesh::lightmap_unwrap);
  961. ClassDB::set_method_flags(get_class_static(), _scs_create("lightmap_unwrap"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  962. ClassDB::bind_method(D_METHOD("get_faces"), &ArrayMesh::get_faces);
  963. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh);
  964. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb);
  965. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb);
  966. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_shape_mode", PROPERTY_HINT_ENUM, "Normalized,Relative", PROPERTY_USAGE_NOEDITOR), "set_blend_shape_mode", "get_blend_shape_mode");
  967. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
  968. BIND_CONSTANT(NO_INDEX_ARRAY);
  969. BIND_CONSTANT(ARRAY_WEIGHTS_SIZE);
  970. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  971. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  972. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  973. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  974. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  975. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  976. BIND_ENUM_CONSTANT(ARRAY_BONES);
  977. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  978. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  979. BIND_ENUM_CONSTANT(ARRAY_MAX);
  980. BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
  981. BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
  982. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
  983. BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
  984. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
  985. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
  986. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
  987. BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  988. BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
  989. }
  990. void ArrayMesh::reload_from_file() {
  991. VisualServer::get_singleton()->mesh_clear(mesh);
  992. surfaces.clear();
  993. clear_blend_shapes();
  994. clear_cache();
  995. Resource::reload_from_file();
  996. _change_notify();
  997. }
  998. ArrayMesh::ArrayMesh() {
  999. mesh = VisualServer::get_singleton()->mesh_create();
  1000. blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
  1001. }
  1002. ArrayMesh::~ArrayMesh() {
  1003. VisualServer::get_singleton()->free(mesh);
  1004. }