mesh.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*************************************************************************/
  2. /* mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "scene/resources/concave_polygon_shape.h"
  32. #include "scene/resources/convex_polygon_shape.h"
  33. #include "surface_tool.h"
  34. void Mesh::_clear_triangle_mesh() const {
  35. triangle_mesh.unref();
  36. ;
  37. }
  38. Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
  39. if (triangle_mesh.is_valid())
  40. return triangle_mesh;
  41. int facecount = 0;
  42. for (int i = 0; i < get_surface_count(); i++) {
  43. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
  44. continue;
  45. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  46. facecount += surface_get_array_index_len(i);
  47. } else {
  48. facecount += surface_get_array_len(i);
  49. }
  50. }
  51. if (facecount == 0 || (facecount % 3) != 0)
  52. return triangle_mesh;
  53. PoolVector<Vector3> faces;
  54. faces.resize(facecount);
  55. PoolVector<Vector3>::Write facesw = faces.write();
  56. int widx = 0;
  57. for (int i = 0; i < get_surface_count(); i++) {
  58. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
  59. continue;
  60. Array a = surface_get_arrays(i);
  61. int vc = surface_get_array_len(i);
  62. PoolVector<Vector3> vertices = a[ARRAY_VERTEX];
  63. PoolVector<Vector3>::Read vr = vertices.read();
  64. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  65. int ic = surface_get_array_index_len(i);
  66. PoolVector<int> indices = a[ARRAY_INDEX];
  67. PoolVector<int>::Read ir = indices.read();
  68. for (int i = 0; i < ic; i++) {
  69. int index = ir[i];
  70. facesw[widx++] = vr[index];
  71. }
  72. } else {
  73. for (int i = 0; i < vc; i++)
  74. facesw[widx++] = vr[i];
  75. }
  76. }
  77. facesw = PoolVector<Vector3>::Write();
  78. triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  79. triangle_mesh->create(faces);
  80. return triangle_mesh;
  81. }
  82. PoolVector<Face3> Mesh::get_faces() const {
  83. Ref<TriangleMesh> tm = generate_triangle_mesh();
  84. if (tm.is_valid())
  85. return tm->get_faces();
  86. return PoolVector<Face3>();
  87. /*
  88. for (int i=0;i<surfaces.size();i++) {
  89. if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES )
  90. continue;
  91. PoolVector<int> indices;
  92. PoolVector<Vector3> vertices;
  93. vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX);
  94. int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i);
  95. bool has_indices;
  96. if (len>0) {
  97. indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX);
  98. has_indices=true;
  99. } else {
  100. len=vertices.size();
  101. has_indices=false;
  102. }
  103. if (len<=0)
  104. continue;
  105. PoolVector<int>::Read indicesr = indices.read();
  106. const int *indicesptr = indicesr.ptr();
  107. PoolVector<Vector3>::Read verticesr = vertices.read();
  108. const Vector3 *verticesptr = verticesr.ptr();
  109. int old_faces=faces.size();
  110. int new_faces=old_faces+(len/3);
  111. faces.resize(new_faces);
  112. PoolVector<Face3>::Write facesw = faces.write();
  113. Face3 *facesptr=facesw.ptr();
  114. for (int i=0;i<len/3;i++) {
  115. Face3 face;
  116. for (int j=0;j<3;j++) {
  117. int idx=i*3+j;
  118. face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx];
  119. }
  120. facesptr[i+old_faces]=face;
  121. }
  122. }
  123. */
  124. }
  125. Ref<Shape> Mesh::create_convex_shape() const {
  126. PoolVector<Vector3> vertices;
  127. for (int i = 0; i < get_surface_count(); i++) {
  128. Array a = surface_get_arrays(i);
  129. PoolVector<Vector3> v = a[ARRAY_VERTEX];
  130. vertices.append_array(v);
  131. }
  132. Ref<ConvexPolygonShape> shape = memnew(ConvexPolygonShape);
  133. shape->set_points(vertices);
  134. return shape;
  135. }
  136. Ref<Shape> Mesh::create_trimesh_shape() const {
  137. PoolVector<Face3> faces = get_faces();
  138. if (faces.size() == 0)
  139. return Ref<Shape>();
  140. PoolVector<Vector3> face_points;
  141. face_points.resize(faces.size() * 3);
  142. for (int i = 0; i < face_points.size(); i++) {
  143. Face3 f = faces.get(i / 3);
  144. face_points.set(i, f.vertex[i % 3]);
  145. }
  146. Ref<ConcavePolygonShape> shape = memnew(ConcavePolygonShape);
  147. shape->set_faces(face_points);
  148. return shape;
  149. }
  150. Ref<Mesh> Mesh::create_outline(float p_margin) const {
  151. Array arrays;
  152. int index_accum = 0;
  153. for (int i = 0; i < get_surface_count(); i++) {
  154. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
  155. continue;
  156. Array a = surface_get_arrays(i);
  157. int vcount = 0;
  158. if (i == 0) {
  159. arrays = a;
  160. PoolVector<Vector3> v = a[ARRAY_VERTEX];
  161. index_accum += v.size();
  162. } else {
  163. for (int j = 0; j < arrays.size(); j++) {
  164. if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) {
  165. //mismatch, do not use
  166. arrays[j] = Variant();
  167. continue;
  168. }
  169. switch (j) {
  170. case ARRAY_VERTEX:
  171. case ARRAY_NORMAL: {
  172. PoolVector<Vector3> dst = arrays[j];
  173. PoolVector<Vector3> src = a[j];
  174. if (j == ARRAY_VERTEX)
  175. vcount = src.size();
  176. if (dst.size() == 0 || src.size() == 0) {
  177. arrays[j] = Variant();
  178. continue;
  179. }
  180. dst.append_array(src);
  181. arrays[j] = dst;
  182. } break;
  183. case ARRAY_TANGENT:
  184. case ARRAY_BONES:
  185. case ARRAY_WEIGHTS: {
  186. PoolVector<real_t> dst = arrays[j];
  187. PoolVector<real_t> src = a[j];
  188. if (dst.size() == 0 || src.size() == 0) {
  189. arrays[j] = Variant();
  190. continue;
  191. }
  192. dst.append_array(src);
  193. arrays[j] = dst;
  194. } break;
  195. case ARRAY_COLOR: {
  196. PoolVector<Color> dst = arrays[j];
  197. PoolVector<Color> src = a[j];
  198. if (dst.size() == 0 || src.size() == 0) {
  199. arrays[j] = Variant();
  200. continue;
  201. }
  202. dst.append_array(src);
  203. arrays[j] = dst;
  204. } break;
  205. case ARRAY_TEX_UV:
  206. case ARRAY_TEX_UV2: {
  207. PoolVector<Vector2> dst = arrays[j];
  208. PoolVector<Vector2> src = a[j];
  209. if (dst.size() == 0 || src.size() == 0) {
  210. arrays[j] = Variant();
  211. continue;
  212. }
  213. dst.append_array(src);
  214. arrays[j] = dst;
  215. } break;
  216. case ARRAY_INDEX: {
  217. PoolVector<int> dst = arrays[j];
  218. PoolVector<int> src = a[j];
  219. if (dst.size() == 0 || src.size() == 0) {
  220. arrays[j] = Variant();
  221. continue;
  222. }
  223. {
  224. int ss = src.size();
  225. PoolVector<int>::Write w = src.write();
  226. for (int k = 0; k < ss; k++) {
  227. w[k] += index_accum;
  228. }
  229. }
  230. dst.append_array(src);
  231. arrays[j] = dst;
  232. index_accum += vcount;
  233. } break;
  234. }
  235. }
  236. }
  237. }
  238. {
  239. PoolVector<int>::Write ir;
  240. PoolVector<int> indices = arrays[ARRAY_INDEX];
  241. bool has_indices = false;
  242. PoolVector<Vector3> vertices = arrays[ARRAY_VERTEX];
  243. int vc = vertices.size();
  244. ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>());
  245. PoolVector<Vector3>::Write r = vertices.write();
  246. if (indices.size()) {
  247. vc = indices.size();
  248. ir = indices.write();
  249. has_indices = true;
  250. }
  251. Map<Vector3, Vector3> normal_accum;
  252. //fill normals with triangle normals
  253. for (int i = 0; i < vc; i += 3) {
  254. Vector3 t[3];
  255. if (has_indices) {
  256. t[0] = r[ir[i + 0]];
  257. t[1] = r[ir[i + 1]];
  258. t[2] = r[ir[i + 2]];
  259. } else {
  260. t[0] = r[i + 0];
  261. t[1] = r[i + 1];
  262. t[2] = r[i + 2];
  263. }
  264. Vector3 n = Plane(t[0], t[1], t[2]).normal;
  265. for (int j = 0; j < 3; j++) {
  266. Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]);
  267. if (!E) {
  268. normal_accum[t[j]] = n;
  269. } else {
  270. float d = n.dot(E->get());
  271. if (d < 1.0)
  272. E->get() += n * (1.0 - d);
  273. //E->get()+=n;
  274. }
  275. }
  276. }
  277. //normalize
  278. for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) {
  279. E->get().normalize();
  280. }
  281. //displace normals
  282. int vc2 = vertices.size();
  283. for (int i = 0; i < vc2; i++) {
  284. Vector3 t = r[i];
  285. Map<Vector3, Vector3>::Element *E = normal_accum.find(t);
  286. ERR_CONTINUE(!E);
  287. t += E->get() * p_margin;
  288. r[i] = t;
  289. }
  290. r = PoolVector<Vector3>::Write();
  291. arrays[ARRAY_VERTEX] = vertices;
  292. if (!has_indices) {
  293. PoolVector<int> new_indices;
  294. new_indices.resize(vertices.size());
  295. PoolVector<int>::Write iw = new_indices.write();
  296. for (int j = 0; j < vc2; j += 3) {
  297. iw[j] = j;
  298. iw[j + 1] = j + 2;
  299. iw[j + 2] = j + 1;
  300. }
  301. iw = PoolVector<int>::Write();
  302. arrays[ARRAY_INDEX] = new_indices;
  303. } else {
  304. for (int j = 0; j < vc; j += 3) {
  305. SWAP(ir[j + 1], ir[j + 2]);
  306. }
  307. ir = PoolVector<int>::Write();
  308. arrays[ARRAY_INDEX] = indices;
  309. }
  310. }
  311. Ref<ArrayMesh> newmesh = memnew(ArrayMesh);
  312. newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays);
  313. return newmesh;
  314. }
  315. void Mesh::_bind_methods() {
  316. BIND_ENUM_CONSTANT(PRIMITIVE_POINTS);
  317. BIND_ENUM_CONSTANT(PRIMITIVE_LINES);
  318. BIND_ENUM_CONSTANT(PRIMITIVE_LINE_STRIP);
  319. BIND_ENUM_CONSTANT(PRIMITIVE_LINE_LOOP);
  320. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLES);
  321. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
  322. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_FAN);
  323. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_NORMALIZED);
  324. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_RELATIVE);
  325. BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
  326. BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
  327. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
  328. BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
  329. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
  330. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
  331. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
  332. BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  333. BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
  334. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_BASE);
  335. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_VERTEX);
  336. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_NORMAL);
  337. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TANGENT);
  338. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_COLOR);
  339. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TEX_UV);
  340. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TEX_UV2);
  341. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_BONES);
  342. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_WEIGHTS);
  343. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_INDEX);
  344. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_2D_VERTICES);
  345. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_16_BIT_BONES);
  346. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_DEFAULT);
  347. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  348. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  349. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  350. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  351. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  352. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  353. BIND_ENUM_CONSTANT(ARRAY_BONES);
  354. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  355. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  356. BIND_ENUM_CONSTANT(ARRAY_MAX);
  357. }
  358. Mesh::Mesh() {
  359. }
  360. static const char *_array_name[] = {
  361. "vertex_array",
  362. "normal_array",
  363. "tangent_array",
  364. "color_array",
  365. "tex_uv_array",
  366. "tex_uv2_array",
  367. "bone_array",
  368. "weights_array",
  369. "index_array",
  370. NULL
  371. };
  372. static const ArrayMesh::ArrayType _array_types[] = {
  373. ArrayMesh::ARRAY_VERTEX,
  374. ArrayMesh::ARRAY_NORMAL,
  375. ArrayMesh::ARRAY_TANGENT,
  376. ArrayMesh::ARRAY_COLOR,
  377. ArrayMesh::ARRAY_TEX_UV,
  378. ArrayMesh::ARRAY_TEX_UV2,
  379. ArrayMesh::ARRAY_BONES,
  380. ArrayMesh::ARRAY_WEIGHTS,
  381. ArrayMesh::ARRAY_INDEX
  382. };
  383. /* compatibility */
  384. static const int _format_translate[] = {
  385. ArrayMesh::ARRAY_FORMAT_VERTEX,
  386. ArrayMesh::ARRAY_FORMAT_NORMAL,
  387. ArrayMesh::ARRAY_FORMAT_TANGENT,
  388. ArrayMesh::ARRAY_FORMAT_COLOR,
  389. ArrayMesh::ARRAY_FORMAT_TEX_UV,
  390. ArrayMesh::ARRAY_FORMAT_TEX_UV2,
  391. ArrayMesh::ARRAY_FORMAT_BONES,
  392. ArrayMesh::ARRAY_FORMAT_WEIGHTS,
  393. ArrayMesh::ARRAY_FORMAT_INDEX,
  394. };
  395. bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) {
  396. String sname = p_name;
  397. if (p_name == "blend_shape/names") {
  398. PoolVector<String> sk = p_value;
  399. int sz = sk.size();
  400. PoolVector<String>::Read r = sk.read();
  401. for (int i = 0; i < sz; i++)
  402. add_blend_shape(r[i]);
  403. return true;
  404. }
  405. if (p_name == "blend_shape/mode") {
  406. set_blend_shape_mode(BlendShapeMode(int(p_value)));
  407. return true;
  408. }
  409. if (sname.begins_with("surface_")) {
  410. int sl = sname.find("/");
  411. if (sl == -1)
  412. return false;
  413. int idx = sname.substr(8, sl - 8).to_int() - 1;
  414. String what = sname.get_slicec('/', 1);
  415. if (what == "material")
  416. surface_set_material(idx, p_value);
  417. else if (what == "name")
  418. surface_set_name(idx, p_value);
  419. return true;
  420. }
  421. if (sname == "custom_aabb/custom_aabb") {
  422. set_custom_aabb(p_value);
  423. return true;
  424. }
  425. if (!sname.begins_with("surfaces"))
  426. return false;
  427. int idx = sname.get_slicec('/', 1).to_int();
  428. String what = sname.get_slicec('/', 2);
  429. if (idx == surfaces.size()) {
  430. //create
  431. Dictionary d = p_value;
  432. ERR_FAIL_COND_V(!d.has("primitive"), false);
  433. if (d.has("arrays")) {
  434. //old format
  435. ERR_FAIL_COND_V(!d.has("morph_arrays"), false);
  436. add_surface_from_arrays(PrimitiveType(int(d["primitive"])), d["arrays"], d["morph_arrays"]);
  437. } else if (d.has("array_data")) {
  438. PoolVector<uint8_t> array_data = d["array_data"];
  439. PoolVector<uint8_t> array_index_data;
  440. if (d.has("array_index_data"))
  441. array_index_data = d["array_index_data"];
  442. ERR_FAIL_COND_V(!d.has("format"), false);
  443. uint32_t format = d["format"];
  444. ERR_FAIL_COND_V(!d.has("primitive"), false);
  445. uint32_t primitive = d["primitive"];
  446. ERR_FAIL_COND_V(!d.has("vertex_count"), false);
  447. int vertex_count = d["vertex_count"];
  448. int index_count = 0;
  449. if (d.has("index_count"))
  450. index_count = d["index_count"];
  451. Vector<PoolVector<uint8_t> > blend_shapes;
  452. if (d.has("blend_shape_data")) {
  453. Array blend_shape_data = d["blend_shape_data"];
  454. for (int i = 0; i < blend_shape_data.size(); i++) {
  455. PoolVector<uint8_t> shape = blend_shape_data[i];
  456. blend_shapes.push_back(shape);
  457. }
  458. }
  459. ERR_FAIL_COND_V(!d.has("aabb"), false);
  460. Rect3 aabb = d["aabb"];
  461. Vector<Rect3> bone_aabb;
  462. if (d.has("bone_aabb")) {
  463. Array baabb = d["bone_aabb"];
  464. bone_aabb.resize(baabb.size());
  465. for (int i = 0; i < baabb.size(); i++) {
  466. bone_aabb[i] = baabb[i];
  467. }
  468. }
  469. add_surface(format, PrimitiveType(primitive), array_data, vertex_count, array_index_data, index_count, aabb, blend_shapes, bone_aabb);
  470. } else {
  471. ERR_FAIL_V(false);
  472. }
  473. if (d.has("material")) {
  474. surface_set_material(idx, d["material"]);
  475. }
  476. if (d.has("name")) {
  477. surface_set_name(idx, d["name"]);
  478. }
  479. return true;
  480. }
  481. return false;
  482. }
  483. bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const {
  484. if (_is_generated())
  485. return false;
  486. String sname = p_name;
  487. if (p_name == "blend_shape/names") {
  488. PoolVector<String> sk;
  489. for (int i = 0; i < blend_shapes.size(); i++)
  490. sk.push_back(blend_shapes[i]);
  491. r_ret = sk;
  492. return true;
  493. } else if (p_name == "blend_shape/mode") {
  494. r_ret = get_blend_shape_mode();
  495. return true;
  496. } else if (sname.begins_with("surface_")) {
  497. int sl = sname.find("/");
  498. if (sl == -1)
  499. return false;
  500. int idx = sname.substr(8, sl - 8).to_int() - 1;
  501. String what = sname.get_slicec('/', 1);
  502. if (what == "material")
  503. r_ret = surface_get_material(idx);
  504. else if (what == "name")
  505. r_ret = surface_get_name(idx);
  506. return true;
  507. } else if (sname == "custom_aabb/custom_aabb") {
  508. r_ret = custom_aabb;
  509. return true;
  510. } else if (!sname.begins_with("surfaces"))
  511. return false;
  512. int idx = sname.get_slicec('/', 1).to_int();
  513. ERR_FAIL_INDEX_V(idx, surfaces.size(), false);
  514. Dictionary d;
  515. d["array_data"] = VS::get_singleton()->mesh_surface_get_array(mesh, idx);
  516. d["vertex_count"] = VS::get_singleton()->mesh_surface_get_array_len(mesh, idx);
  517. d["array_index_data"] = VS::get_singleton()->mesh_surface_get_index_array(mesh, idx);
  518. d["index_count"] = VS::get_singleton()->mesh_surface_get_array_index_len(mesh, idx);
  519. d["primitive"] = VS::get_singleton()->mesh_surface_get_primitive_type(mesh, idx);
  520. d["format"] = VS::get_singleton()->mesh_surface_get_format(mesh, idx);
  521. d["aabb"] = VS::get_singleton()->mesh_surface_get_aabb(mesh, idx);
  522. Vector<Rect3> skel_aabb = VS::get_singleton()->mesh_surface_get_skeleton_aabb(mesh, idx);
  523. Array arr;
  524. for (int i = 0; i < skel_aabb.size(); i++) {
  525. arr[i] = skel_aabb[i];
  526. }
  527. d["skeleton_aabb"] = arr;
  528. Vector<PoolVector<uint8_t> > blend_shape_data = VS::get_singleton()->mesh_surface_get_blend_shapes(mesh, idx);
  529. Array md;
  530. for (int i = 0; i < blend_shape_data.size(); i++) {
  531. md.push_back(blend_shape_data[i]);
  532. }
  533. d["blend_shape_data"] = md;
  534. Ref<Material> m = surface_get_material(idx);
  535. if (m.is_valid())
  536. d["material"] = m;
  537. String n = surface_get_name(idx);
  538. if (n != "")
  539. d["name"] = n;
  540. r_ret = d;
  541. return true;
  542. }
  543. void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const {
  544. if (_is_generated())
  545. return;
  546. if (blend_shapes.size()) {
  547. p_list->push_back(PropertyInfo(Variant::POOL_STRING_ARRAY, "blend_shape/names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  548. p_list->push_back(PropertyInfo(Variant::INT, "blend_shape/mode", PROPERTY_HINT_ENUM, "Normalized,Relative"));
  549. }
  550. for (int i = 0; i < surfaces.size(); i++) {
  551. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "surfaces/" + itos(i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  552. p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i + 1) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  553. if (surfaces[i].is_2d) {
  554. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,CanvasItemMaterial", PROPERTY_USAGE_EDITOR));
  555. } else {
  556. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial", PROPERTY_USAGE_EDITOR));
  557. }
  558. }
  559. p_list->push_back(PropertyInfo(Variant::RECT3, "custom_aabb/custom_aabb"));
  560. }
  561. void ArrayMesh::_recompute_aabb() {
  562. // regenerate AABB
  563. aabb = Rect3();
  564. for (int i = 0; i < surfaces.size(); i++) {
  565. if (i == 0)
  566. aabb = surfaces[i].aabb;
  567. else
  568. aabb.merge_with(surfaces[i].aabb);
  569. }
  570. }
  571. 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 Rect3 &p_aabb, const Vector<PoolVector<uint8_t> > &p_blend_shapes, const Vector<Rect3> &p_bone_aabbs) {
  572. Surface s;
  573. s.aabb = p_aabb;
  574. s.is_2d = p_format & ARRAY_FLAG_USE_2D_VERTICES;
  575. surfaces.push_back(s);
  576. _recompute_aabb();
  577. 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);
  578. }
  579. void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, uint32_t p_flags) {
  580. ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX);
  581. Surface s;
  582. VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh, (VisualServer::PrimitiveType)p_primitive, p_arrays, p_blend_shapes, p_flags);
  583. surfaces.push_back(s);
  584. /* make aABB? */ {
  585. Variant arr = p_arrays[ARRAY_VERTEX];
  586. PoolVector<Vector3> vertices = arr;
  587. int len = vertices.size();
  588. ERR_FAIL_COND(len == 0);
  589. PoolVector<Vector3>::Read r = vertices.read();
  590. const Vector3 *vtx = r.ptr();
  591. // check AABB
  592. Rect3 aabb;
  593. for (int i = 0; i < len; i++) {
  594. if (i == 0)
  595. aabb.position = vtx[i];
  596. else
  597. aabb.expand_to(vtx[i]);
  598. }
  599. surfaces[surfaces.size() - 1].aabb = aabb;
  600. surfaces[surfaces.size() - 1].is_2d = arr.get_type() == Variant::POOL_VECTOR2_ARRAY;
  601. _recompute_aabb();
  602. }
  603. _clear_triangle_mesh();
  604. _change_notify();
  605. emit_changed();
  606. }
  607. Array ArrayMesh::surface_get_arrays(int p_surface) const {
  608. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  609. return VisualServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
  610. }
  611. Array ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const {
  612. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  613. return VisualServer::get_singleton()->mesh_surface_get_blend_shape_arrays(mesh, p_surface);
  614. }
  615. int ArrayMesh::get_surface_count() const {
  616. return surfaces.size();
  617. }
  618. void ArrayMesh::add_blend_shape(const StringName &p_name) {
  619. if (surfaces.size()) {
  620. ERR_EXPLAIN("Can't add a shape key count if surfaces are already created.");
  621. ERR_FAIL_COND(surfaces.size());
  622. }
  623. StringName name = p_name;
  624. if (blend_shapes.find(name) != -1) {
  625. int count = 2;
  626. do {
  627. name = String(p_name) + " " + itos(count);
  628. count++;
  629. } while (blend_shapes.find(name) != -1);
  630. }
  631. blend_shapes.push_back(name);
  632. VS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  633. }
  634. int ArrayMesh::get_blend_shape_count() const {
  635. return blend_shapes.size();
  636. }
  637. StringName ArrayMesh::get_blend_shape_name(int p_index) const {
  638. ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName());
  639. return blend_shapes[p_index];
  640. }
  641. void ArrayMesh::clear_blend_shapes() {
  642. if (surfaces.size()) {
  643. ERR_EXPLAIN("Can't set shape key count if surfaces are already created.");
  644. ERR_FAIL_COND(surfaces.size());
  645. }
  646. blend_shapes.clear();
  647. }
  648. void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) {
  649. blend_shape_mode = p_mode;
  650. VS::get_singleton()->mesh_set_blend_shape_mode(mesh, (VS::BlendShapeMode)p_mode);
  651. }
  652. ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const {
  653. return blend_shape_mode;
  654. }
  655. void ArrayMesh::surface_remove(int p_idx) {
  656. ERR_FAIL_INDEX(p_idx, surfaces.size());
  657. VisualServer::get_singleton()->mesh_remove_surface(mesh, p_idx);
  658. surfaces.remove(p_idx);
  659. _clear_triangle_mesh();
  660. _recompute_aabb();
  661. _change_notify();
  662. emit_changed();
  663. }
  664. int ArrayMesh::surface_get_array_len(int p_idx) const {
  665. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  666. return VisualServer::get_singleton()->mesh_surface_get_array_len(mesh, p_idx);
  667. }
  668. int ArrayMesh::surface_get_array_index_len(int p_idx) const {
  669. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  670. return VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, p_idx);
  671. }
  672. uint32_t ArrayMesh::surface_get_format(int p_idx) const {
  673. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0);
  674. return VisualServer::get_singleton()->mesh_surface_get_format(mesh, p_idx);
  675. }
  676. ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const {
  677. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES);
  678. return (PrimitiveType)VisualServer::get_singleton()->mesh_surface_get_primitive_type(mesh, p_idx);
  679. }
  680. void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  681. ERR_FAIL_INDEX(p_idx, surfaces.size());
  682. if (surfaces[p_idx].material == p_material)
  683. return;
  684. surfaces[p_idx].material = p_material;
  685. VisualServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
  686. _change_notify("material");
  687. }
  688. void ArrayMesh::surface_set_name(int p_idx, const String &p_name) {
  689. ERR_FAIL_INDEX(p_idx, surfaces.size());
  690. surfaces[p_idx].name = p_name;
  691. }
  692. String ArrayMesh::surface_get_name(int p_idx) const {
  693. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String());
  694. return surfaces[p_idx].name;
  695. }
  696. void ArrayMesh::surface_set_custom_aabb(int p_idx, const Rect3 &p_aabb) {
  697. ERR_FAIL_INDEX(p_idx, surfaces.size());
  698. surfaces[p_idx].aabb = p_aabb;
  699. // set custom aabb too?
  700. }
  701. Ref<Material> ArrayMesh::surface_get_material(int p_idx) const {
  702. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>());
  703. return surfaces[p_idx].material;
  704. }
  705. void ArrayMesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) {
  706. VisualServer::get_singleton()->mesh_add_surface_from_mesh_data(mesh, p_mesh_data);
  707. Rect3 aabb;
  708. for (int i = 0; i < p_mesh_data.vertices.size(); i++) {
  709. if (i == 0)
  710. aabb.position = p_mesh_data.vertices[i];
  711. else
  712. aabb.expand_to(p_mesh_data.vertices[i]);
  713. }
  714. Surface s;
  715. s.aabb = aabb;
  716. if (surfaces.size() == 0)
  717. aabb = s.aabb;
  718. else
  719. aabb.merge_with(s.aabb);
  720. _clear_triangle_mesh();
  721. surfaces.push_back(s);
  722. _change_notify();
  723. emit_changed();
  724. }
  725. RID ArrayMesh::get_rid() const {
  726. return mesh;
  727. }
  728. Rect3 ArrayMesh::get_aabb() const {
  729. return aabb;
  730. }
  731. void ArrayMesh::set_custom_aabb(const Rect3 &p_custom) {
  732. custom_aabb = p_custom;
  733. VS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  734. }
  735. Rect3 ArrayMesh::get_custom_aabb() const {
  736. return custom_aabb;
  737. }
  738. void ArrayMesh::center_geometry() {
  739. /*
  740. Vector3 ofs = aabb.pos+aabb.size*0.5;
  741. for(int i=0;i<get_surface_count();i++) {
  742. PoolVector<Vector3> geom = surface_get_array(i,ARRAY_VERTEX);
  743. int gc =geom.size();
  744. PoolVector<Vector3>::Write w = geom.write();
  745. surfaces[i].aabb.pos-=ofs;
  746. for(int i=0;i<gc;i++) {
  747. w[i]-=ofs;
  748. }
  749. w = PoolVector<Vector3>::Write();
  750. surface_set_array(i,ARRAY_VERTEX,geom);
  751. }
  752. aabb.pos-=ofs;
  753. */
  754. }
  755. void ArrayMesh::regen_normalmaps() {
  756. Vector<Ref<SurfaceTool> > surfs;
  757. for (int i = 0; i < get_surface_count(); i++) {
  758. Ref<SurfaceTool> st = memnew(SurfaceTool);
  759. st->create_from(Ref<ArrayMesh>(this), i);
  760. surfs.push_back(st);
  761. }
  762. while (get_surface_count()) {
  763. surface_remove(0);
  764. }
  765. for (int i = 0; i < surfs.size(); i++) {
  766. surfs[i]->generate_tangents();
  767. surfs[i]->commit(Ref<ArrayMesh>(this));
  768. }
  769. }
  770. void ArrayMesh::_bind_methods() {
  771. ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape);
  772. ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count);
  773. ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name);
  774. ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes);
  775. ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode);
  776. ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode);
  777. 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));
  778. ClassDB::bind_method(D_METHOD("get_surface_count"), &ArrayMesh::get_surface_count);
  779. ClassDB::bind_method(D_METHOD("surface_remove", "surf_idx"), &ArrayMesh::surface_remove);
  780. ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len);
  781. ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len);
  782. ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format);
  783. ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type);
  784. ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &ArrayMesh::surface_set_material);
  785. ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &ArrayMesh::surface_get_material);
  786. ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name);
  787. ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name);
  788. ClassDB::bind_method(D_METHOD("surface_get_arrays", "surf_idx"), &ArrayMesh::surface_get_arrays);
  789. ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &ArrayMesh::surface_get_blend_shape_arrays);
  790. ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape);
  791. ClassDB::bind_method(D_METHOD("create_convex_shape"), &ArrayMesh::create_convex_shape);
  792. ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline);
  793. ClassDB::bind_method(D_METHOD("center_geometry"), &ArrayMesh::center_geometry);
  794. ClassDB::set_method_flags(get_class_static(), _scs_create("center_geometry"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  795. ClassDB::bind_method(D_METHOD("regen_normalmaps"), &ArrayMesh::regen_normalmaps);
  796. ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normalmaps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  797. ClassDB::bind_method(D_METHOD("get_faces"), &ArrayMesh::get_faces);
  798. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh);
  799. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb);
  800. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb);
  801. BIND_CONSTANT(NO_INDEX_ARRAY);
  802. BIND_CONSTANT(ARRAY_WEIGHTS_SIZE);
  803. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  804. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  805. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  806. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  807. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  808. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  809. BIND_ENUM_CONSTANT(ARRAY_BONES);
  810. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  811. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  812. BIND_ENUM_CONSTANT(ARRAY_MAX);
  813. BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
  814. BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
  815. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
  816. BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
  817. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
  818. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
  819. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
  820. BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  821. BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
  822. }
  823. ArrayMesh::ArrayMesh() {
  824. mesh = VisualServer::get_singleton()->mesh_create();
  825. blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
  826. }
  827. ArrayMesh::~ArrayMesh() {
  828. VisualServer::get_singleton()->free(mesh);
  829. }