surface_tool.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*************************************************************************/
  2. /* surface_tool.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 "surface_tool.h"
  31. #include "core/method_bind_ext.gen.inc"
  32. #define _VERTEX_SNAP 0.0001
  33. #define EQ_VERTEX_DIST 0.00001
  34. bool SurfaceTool::Vertex::operator==(const Vertex &p_vertex) const {
  35. if (vertex != p_vertex.vertex)
  36. return false;
  37. if (uv != p_vertex.uv)
  38. return false;
  39. if (uv2 != p_vertex.uv2)
  40. return false;
  41. if (normal != p_vertex.normal)
  42. return false;
  43. if (binormal != p_vertex.binormal)
  44. return false;
  45. if (color != p_vertex.color)
  46. return false;
  47. if (bones.size() != p_vertex.bones.size())
  48. return false;
  49. for (int i = 0; i < bones.size(); i++) {
  50. if (bones[i] != p_vertex.bones[i])
  51. return false;
  52. }
  53. for (int i = 0; i < weights.size(); i++) {
  54. if (weights[i] != p_vertex.weights[i])
  55. return false;
  56. }
  57. return true;
  58. }
  59. uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) {
  60. uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3);
  61. h = hash_djb2_buffer((const uint8_t *)&p_vtx.normal, sizeof(real_t) * 3, h);
  62. h = hash_djb2_buffer((const uint8_t *)&p_vtx.binormal, sizeof(real_t) * 3, h);
  63. h = hash_djb2_buffer((const uint8_t *)&p_vtx.tangent, sizeof(real_t) * 3, h);
  64. h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv, sizeof(real_t) * 2, h);
  65. h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv2, sizeof(real_t) * 2, h);
  66. h = hash_djb2_buffer((const uint8_t *)&p_vtx.color, sizeof(real_t) * 4, h);
  67. h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int), h);
  68. h = hash_djb2_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h);
  69. return h;
  70. }
  71. void SurfaceTool::begin(Mesh::PrimitiveType p_primitive) {
  72. clear();
  73. primitive = p_primitive;
  74. begun = true;
  75. first = true;
  76. }
  77. void SurfaceTool::add_vertex(const Vector3 &p_vertex) {
  78. ERR_FAIL_COND(!begun);
  79. Vertex vtx;
  80. vtx.vertex = p_vertex;
  81. vtx.color = last_color;
  82. vtx.normal = last_normal;
  83. vtx.uv = last_uv;
  84. vtx.uv2 = last_uv2;
  85. vtx.weights = last_weights;
  86. vtx.bones = last_bones;
  87. vtx.tangent = last_tangent.normal;
  88. vtx.binormal = last_normal.cross(last_tangent.normal).normalized() * last_tangent.d;
  89. vertex_array.push_back(vtx);
  90. first = false;
  91. format |= Mesh::ARRAY_FORMAT_VERTEX;
  92. }
  93. void SurfaceTool::add_color(Color p_color) {
  94. ERR_FAIL_COND(!begun);
  95. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_COLOR));
  96. format |= Mesh::ARRAY_FORMAT_COLOR;
  97. last_color = p_color;
  98. }
  99. void SurfaceTool::add_normal(const Vector3 &p_normal) {
  100. ERR_FAIL_COND(!begun);
  101. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_NORMAL));
  102. format |= Mesh::ARRAY_FORMAT_NORMAL;
  103. last_normal = p_normal;
  104. }
  105. void SurfaceTool::add_tangent(const Plane &p_tangent) {
  106. ERR_FAIL_COND(!begun);
  107. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TANGENT));
  108. format |= Mesh::ARRAY_FORMAT_TANGENT;
  109. last_tangent = p_tangent;
  110. }
  111. void SurfaceTool::add_uv(const Vector2 &p_uv) {
  112. ERR_FAIL_COND(!begun);
  113. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV));
  114. format |= Mesh::ARRAY_FORMAT_TEX_UV;
  115. last_uv = p_uv;
  116. }
  117. void SurfaceTool::add_uv2(const Vector2 &p_uv2) {
  118. ERR_FAIL_COND(!begun);
  119. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV2));
  120. format |= Mesh::ARRAY_FORMAT_TEX_UV2;
  121. last_uv2 = p_uv2;
  122. }
  123. void SurfaceTool::add_bones(const Vector<int> &p_bones) {
  124. ERR_FAIL_COND(!begun);
  125. ERR_FAIL_COND(p_bones.size() != 4);
  126. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_BONES));
  127. format |= Mesh::ARRAY_FORMAT_BONES;
  128. last_bones = p_bones;
  129. }
  130. void SurfaceTool::add_weights(const Vector<float> &p_weights) {
  131. ERR_FAIL_COND(!begun);
  132. ERR_FAIL_COND(p_weights.size() != 4);
  133. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_WEIGHTS));
  134. format |= Mesh::ARRAY_FORMAT_WEIGHTS;
  135. last_weights = p_weights;
  136. }
  137. void SurfaceTool::add_smooth_group(bool p_smooth) {
  138. ERR_FAIL_COND(!begun);
  139. if (index_array.size()) {
  140. smooth_groups[index_array.size()] = p_smooth;
  141. } else {
  142. smooth_groups[vertex_array.size()] = p_smooth;
  143. }
  144. }
  145. void SurfaceTool::add_triangle_fan(const Vector<Vector3> &p_vertexes, const Vector<Vector2> &p_uvs, const Vector<Color> &p_colors, const Vector<Vector2> &p_uv2s, const Vector<Vector3> &p_normals, const Vector<Plane> &p_tangents) {
  146. ERR_FAIL_COND(!begun);
  147. ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
  148. ERR_FAIL_COND(p_vertexes.size() < 3);
  149. #define ADD_POINT(n) \
  150. { \
  151. if (p_colors.size() > n) \
  152. add_color(p_colors[n]); \
  153. if (p_uvs.size() > n) \
  154. add_uv(p_uvs[n]); \
  155. if (p_uv2s.size() > n) \
  156. add_uv2(p_uv2s[n]); \
  157. if (p_normals.size() > n) \
  158. add_normal(p_normals[n]); \
  159. if (p_tangents.size() > n) \
  160. add_tangent(p_tangents[n]); \
  161. add_vertex(p_vertexes[n]); \
  162. }
  163. for (int i = 0; i < p_vertexes.size() - 2; i++) {
  164. ADD_POINT(0);
  165. ADD_POINT(i + 1);
  166. ADD_POINT(i + 2);
  167. }
  168. #undef ADD_POINT
  169. }
  170. void SurfaceTool::add_index(int p_index) {
  171. ERR_FAIL_COND(!begun);
  172. format |= Mesh::ARRAY_FORMAT_INDEX;
  173. index_array.push_back(p_index);
  174. }
  175. Array SurfaceTool::commit_to_arrays() {
  176. int varr_len = vertex_array.size();
  177. Array a;
  178. a.resize(Mesh::ARRAY_MAX);
  179. for (int i = 0; i < Mesh::ARRAY_MAX; i++) {
  180. if (!(format & (1 << i)))
  181. continue; //not in format
  182. switch (i) {
  183. case Mesh::ARRAY_VERTEX:
  184. case Mesh::ARRAY_NORMAL: {
  185. PoolVector<Vector3> array;
  186. array.resize(varr_len);
  187. PoolVector<Vector3>::Write w = array.write();
  188. int idx = 0;
  189. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  190. const Vertex &v = E->get();
  191. switch (i) {
  192. case Mesh::ARRAY_VERTEX: {
  193. w[idx] = v.vertex;
  194. } break;
  195. case Mesh::ARRAY_NORMAL: {
  196. w[idx] = v.normal;
  197. } break;
  198. }
  199. }
  200. w = PoolVector<Vector3>::Write();
  201. a[i] = array;
  202. } break;
  203. case Mesh::ARRAY_TEX_UV:
  204. case Mesh::ARRAY_TEX_UV2: {
  205. PoolVector<Vector2> array;
  206. array.resize(varr_len);
  207. PoolVector<Vector2>::Write w = array.write();
  208. int idx = 0;
  209. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  210. const Vertex &v = E->get();
  211. switch (i) {
  212. case Mesh::ARRAY_TEX_UV: {
  213. w[idx] = v.uv;
  214. } break;
  215. case Mesh::ARRAY_TEX_UV2: {
  216. w[idx] = v.uv2;
  217. } break;
  218. }
  219. }
  220. w = PoolVector<Vector2>::Write();
  221. a[i] = array;
  222. } break;
  223. case Mesh::ARRAY_TANGENT: {
  224. PoolVector<float> array;
  225. array.resize(varr_len * 4);
  226. PoolVector<float>::Write w = array.write();
  227. int idx = 0;
  228. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  229. const Vertex &v = E->get();
  230. w[idx + 0] = v.tangent.x;
  231. w[idx + 1] = v.tangent.y;
  232. w[idx + 2] = v.tangent.z;
  233. //float d = v.tangent.dot(v.binormal,v.normal);
  234. float d = v.binormal.dot(v.normal.cross(v.tangent));
  235. w[idx + 3] = d < 0 ? -1 : 1;
  236. }
  237. w = PoolVector<float>::Write();
  238. a[i] = array;
  239. } break;
  240. case Mesh::ARRAY_COLOR: {
  241. PoolVector<Color> array;
  242. array.resize(varr_len);
  243. PoolVector<Color>::Write w = array.write();
  244. int idx = 0;
  245. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  246. const Vertex &v = E->get();
  247. w[idx] = v.color;
  248. }
  249. w = PoolVector<Color>::Write();
  250. a[i] = array;
  251. } break;
  252. case Mesh::ARRAY_BONES: {
  253. PoolVector<int> array;
  254. array.resize(varr_len * 4);
  255. PoolVector<int>::Write w = array.write();
  256. int idx = 0;
  257. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  258. const Vertex &v = E->get();
  259. ERR_CONTINUE(v.bones.size() != 4);
  260. for (int j = 0; j < 4; j++) {
  261. w[idx + j] = v.bones[j];
  262. }
  263. }
  264. w = PoolVector<int>::Write();
  265. a[i] = array;
  266. } break;
  267. case Mesh::ARRAY_WEIGHTS: {
  268. PoolVector<float> array;
  269. array.resize(varr_len * 4);
  270. PoolVector<float>::Write w = array.write();
  271. int idx = 0;
  272. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  273. const Vertex &v = E->get();
  274. ERR_CONTINUE(v.weights.size() != 4);
  275. for (int j = 0; j < 4; j++) {
  276. w[idx + j] = v.weights[j];
  277. }
  278. }
  279. w = PoolVector<float>::Write();
  280. a[i] = array;
  281. } break;
  282. case Mesh::ARRAY_INDEX: {
  283. ERR_CONTINUE(index_array.size() == 0);
  284. PoolVector<int> array;
  285. array.resize(index_array.size());
  286. PoolVector<int>::Write w = array.write();
  287. int idx = 0;
  288. for (List<int>::Element *E = index_array.front(); E; E = E->next(), idx++) {
  289. w[idx] = E->get();
  290. }
  291. w = PoolVector<int>::Write();
  292. a[i] = array;
  293. } break;
  294. default: {}
  295. }
  296. }
  297. return a;
  298. }
  299. Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing, uint32_t p_flags) {
  300. Ref<ArrayMesh> mesh;
  301. if (p_existing.is_valid())
  302. mesh = p_existing;
  303. else
  304. mesh.instance();
  305. int varr_len = vertex_array.size();
  306. if (varr_len == 0)
  307. return mesh;
  308. int surface = mesh->get_surface_count();
  309. Array a = commit_to_arrays();
  310. mesh->add_surface_from_arrays(primitive, a, Array(), p_flags);
  311. if (material.is_valid())
  312. mesh->surface_set_material(surface, material);
  313. return mesh;
  314. }
  315. void SurfaceTool::index() {
  316. if (index_array.size())
  317. return; //already indexed
  318. HashMap<Vertex, int, VertexHasher> indices;
  319. List<Vertex> new_vertices;
  320. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  321. int *idxptr = indices.getptr(E->get());
  322. int idx;
  323. if (!idxptr) {
  324. idx = indices.size();
  325. new_vertices.push_back(E->get());
  326. indices[E->get()] = idx;
  327. } else {
  328. idx = *idxptr;
  329. }
  330. index_array.push_back(idx);
  331. }
  332. vertex_array.clear();
  333. vertex_array = new_vertices;
  334. format |= Mesh::ARRAY_FORMAT_INDEX;
  335. }
  336. void SurfaceTool::deindex() {
  337. if (index_array.size() == 0)
  338. return; //nothing to deindex
  339. Vector<Vertex> varr;
  340. varr.resize(vertex_array.size());
  341. int idx = 0;
  342. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  343. varr.write[idx++] = E->get();
  344. }
  345. vertex_array.clear();
  346. for (List<int>::Element *E = index_array.front(); E; E = E->next()) {
  347. ERR_FAIL_INDEX(E->get(), varr.size());
  348. vertex_array.push_back(varr[E->get()]);
  349. }
  350. format &= ~Mesh::ARRAY_FORMAT_INDEX;
  351. index_array.clear();
  352. }
  353. void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
  354. Array arr = p_existing->surface_get_arrays(p_surface);
  355. ERR_FAIL_COND(arr.size() != VS::ARRAY_MAX);
  356. _create_list_from_arrays(arr, r_vertex, r_index, lformat);
  357. }
  358. Vector<SurfaceTool::Vertex> SurfaceTool::create_vertex_array_from_triangle_arrays(const Array &p_arrays) {
  359. Vector<SurfaceTool::Vertex> ret;
  360. PoolVector<Vector3> varr = p_arrays[VS::ARRAY_VERTEX];
  361. PoolVector<Vector3> narr = p_arrays[VS::ARRAY_NORMAL];
  362. PoolVector<float> tarr = p_arrays[VS::ARRAY_TANGENT];
  363. PoolVector<Color> carr = p_arrays[VS::ARRAY_COLOR];
  364. PoolVector<Vector2> uvarr = p_arrays[VS::ARRAY_TEX_UV];
  365. PoolVector<Vector2> uv2arr = p_arrays[VS::ARRAY_TEX_UV2];
  366. PoolVector<int> barr = p_arrays[VS::ARRAY_BONES];
  367. PoolVector<float> warr = p_arrays[VS::ARRAY_WEIGHTS];
  368. int vc = varr.size();
  369. if (vc == 0)
  370. return ret;
  371. int lformat = 0;
  372. PoolVector<Vector3>::Read rv;
  373. if (varr.size()) {
  374. lformat |= VS::ARRAY_FORMAT_VERTEX;
  375. rv = varr.read();
  376. }
  377. PoolVector<Vector3>::Read rn;
  378. if (narr.size()) {
  379. lformat |= VS::ARRAY_FORMAT_NORMAL;
  380. rn = narr.read();
  381. }
  382. PoolVector<float>::Read rt;
  383. if (tarr.size()) {
  384. lformat |= VS::ARRAY_FORMAT_TANGENT;
  385. rt = tarr.read();
  386. }
  387. PoolVector<Color>::Read rc;
  388. if (carr.size()) {
  389. lformat |= VS::ARRAY_FORMAT_COLOR;
  390. rc = carr.read();
  391. }
  392. PoolVector<Vector2>::Read ruv;
  393. if (uvarr.size()) {
  394. lformat |= VS::ARRAY_FORMAT_TEX_UV;
  395. ruv = uvarr.read();
  396. }
  397. PoolVector<Vector2>::Read ruv2;
  398. if (uv2arr.size()) {
  399. lformat |= VS::ARRAY_FORMAT_TEX_UV2;
  400. ruv2 = uv2arr.read();
  401. }
  402. PoolVector<int>::Read rb;
  403. if (barr.size()) {
  404. lformat |= VS::ARRAY_FORMAT_BONES;
  405. rb = barr.read();
  406. }
  407. PoolVector<float>::Read rw;
  408. if (warr.size()) {
  409. lformat |= VS::ARRAY_FORMAT_WEIGHTS;
  410. rw = warr.read();
  411. }
  412. for (int i = 0; i < vc; i++) {
  413. Vertex v;
  414. if (lformat & VS::ARRAY_FORMAT_VERTEX)
  415. v.vertex = varr[i];
  416. if (lformat & VS::ARRAY_FORMAT_NORMAL)
  417. v.normal = narr[i];
  418. if (lformat & VS::ARRAY_FORMAT_TANGENT) {
  419. Plane p(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2], tarr[i * 4 + 3]);
  420. v.tangent = p.normal;
  421. v.binormal = p.normal.cross(v.tangent).normalized() * p.d;
  422. }
  423. if (lformat & VS::ARRAY_FORMAT_COLOR)
  424. v.color = carr[i];
  425. if (lformat & VS::ARRAY_FORMAT_TEX_UV)
  426. v.uv = uvarr[i];
  427. if (lformat & VS::ARRAY_FORMAT_TEX_UV2)
  428. v.uv2 = uv2arr[i];
  429. if (lformat & VS::ARRAY_FORMAT_BONES) {
  430. Vector<int> b;
  431. b.resize(4);
  432. b.write[0] = barr[i * 4 + 0];
  433. b.write[1] = barr[i * 4 + 1];
  434. b.write[2] = barr[i * 4 + 2];
  435. b.write[3] = barr[i * 4 + 3];
  436. v.bones = b;
  437. }
  438. if (lformat & VS::ARRAY_FORMAT_WEIGHTS) {
  439. Vector<float> w;
  440. w.resize(4);
  441. w.write[0] = warr[i * 4 + 0];
  442. w.write[1] = warr[i * 4 + 1];
  443. w.write[2] = warr[i * 4 + 2];
  444. w.write[3] = warr[i * 4 + 3];
  445. v.weights = w;
  446. }
  447. ret.push_back(v);
  448. }
  449. return ret;
  450. }
  451. void SurfaceTool::_create_list_from_arrays(Array arr, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
  452. PoolVector<Vector3> varr = arr[VS::ARRAY_VERTEX];
  453. PoolVector<Vector3> narr = arr[VS::ARRAY_NORMAL];
  454. PoolVector<float> tarr = arr[VS::ARRAY_TANGENT];
  455. PoolVector<Color> carr = arr[VS::ARRAY_COLOR];
  456. PoolVector<Vector2> uvarr = arr[VS::ARRAY_TEX_UV];
  457. PoolVector<Vector2> uv2arr = arr[VS::ARRAY_TEX_UV2];
  458. PoolVector<int> barr = arr[VS::ARRAY_BONES];
  459. PoolVector<float> warr = arr[VS::ARRAY_WEIGHTS];
  460. int vc = varr.size();
  461. if (vc == 0)
  462. return;
  463. lformat = 0;
  464. PoolVector<Vector3>::Read rv;
  465. if (varr.size()) {
  466. lformat |= VS::ARRAY_FORMAT_VERTEX;
  467. rv = varr.read();
  468. }
  469. PoolVector<Vector3>::Read rn;
  470. if (narr.size()) {
  471. lformat |= VS::ARRAY_FORMAT_NORMAL;
  472. rn = narr.read();
  473. }
  474. PoolVector<float>::Read rt;
  475. if (tarr.size()) {
  476. lformat |= VS::ARRAY_FORMAT_TANGENT;
  477. rt = tarr.read();
  478. }
  479. PoolVector<Color>::Read rc;
  480. if (carr.size()) {
  481. lformat |= VS::ARRAY_FORMAT_COLOR;
  482. rc = carr.read();
  483. }
  484. PoolVector<Vector2>::Read ruv;
  485. if (uvarr.size()) {
  486. lformat |= VS::ARRAY_FORMAT_TEX_UV;
  487. ruv = uvarr.read();
  488. }
  489. PoolVector<Vector2>::Read ruv2;
  490. if (uv2arr.size()) {
  491. lformat |= VS::ARRAY_FORMAT_TEX_UV2;
  492. ruv2 = uv2arr.read();
  493. }
  494. PoolVector<int>::Read rb;
  495. if (barr.size()) {
  496. lformat |= VS::ARRAY_FORMAT_BONES;
  497. rb = barr.read();
  498. }
  499. PoolVector<float>::Read rw;
  500. if (warr.size()) {
  501. lformat |= VS::ARRAY_FORMAT_WEIGHTS;
  502. rw = warr.read();
  503. }
  504. for (int i = 0; i < vc; i++) {
  505. Vertex v;
  506. if (lformat & VS::ARRAY_FORMAT_VERTEX)
  507. v.vertex = varr[i];
  508. if (lformat & VS::ARRAY_FORMAT_NORMAL)
  509. v.normal = narr[i];
  510. if (lformat & VS::ARRAY_FORMAT_TANGENT) {
  511. Plane p(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2], tarr[i * 4 + 3]);
  512. v.tangent = p.normal;
  513. v.binormal = p.normal.cross(v.tangent).normalized() * p.d;
  514. }
  515. if (lformat & VS::ARRAY_FORMAT_COLOR)
  516. v.color = carr[i];
  517. if (lformat & VS::ARRAY_FORMAT_TEX_UV)
  518. v.uv = uvarr[i];
  519. if (lformat & VS::ARRAY_FORMAT_TEX_UV2)
  520. v.uv2 = uv2arr[i];
  521. if (lformat & VS::ARRAY_FORMAT_BONES) {
  522. Vector<int> b;
  523. b.resize(4);
  524. b.write[0] = barr[i * 4 + 0];
  525. b.write[1] = barr[i * 4 + 1];
  526. b.write[2] = barr[i * 4 + 2];
  527. b.write[3] = barr[i * 4 + 3];
  528. v.bones = b;
  529. }
  530. if (lformat & VS::ARRAY_FORMAT_WEIGHTS) {
  531. Vector<float> w;
  532. w.resize(4);
  533. w.write[0] = warr[i * 4 + 0];
  534. w.write[1] = warr[i * 4 + 1];
  535. w.write[2] = warr[i * 4 + 2];
  536. w.write[3] = warr[i * 4 + 3];
  537. v.weights = w;
  538. }
  539. r_vertex->push_back(v);
  540. }
  541. //indices
  542. PoolVector<int> idx = arr[VS::ARRAY_INDEX];
  543. int is = idx.size();
  544. if (is) {
  545. lformat |= VS::ARRAY_FORMAT_INDEX;
  546. PoolVector<int>::Read iarr = idx.read();
  547. for (int i = 0; i < is; i++) {
  548. r_index->push_back(iarr[i]);
  549. }
  550. }
  551. }
  552. void SurfaceTool::create_from_triangle_arrays(const Array &p_arrays) {
  553. clear();
  554. primitive = Mesh::PRIMITIVE_TRIANGLES;
  555. _create_list_from_arrays(p_arrays, &vertex_array, &index_array, format);
  556. }
  557. void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) {
  558. clear();
  559. primitive = p_existing->surface_get_primitive_type(p_surface);
  560. _create_list(p_existing, p_surface, &vertex_array, &index_array, format);
  561. material = p_existing->surface_get_material(p_surface);
  562. }
  563. void SurfaceTool::append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform) {
  564. if (vertex_array.size() == 0) {
  565. primitive = p_existing->surface_get_primitive_type(p_surface);
  566. format = 0;
  567. }
  568. int nformat;
  569. List<Vertex> nvertices;
  570. List<int> nindices;
  571. _create_list(p_existing, p_surface, &nvertices, &nindices, nformat);
  572. format |= nformat;
  573. int vfrom = vertex_array.size();
  574. for (List<Vertex>::Element *E = nvertices.front(); E; E = E->next()) {
  575. Vertex v = E->get();
  576. v.vertex = p_xform.xform(v.vertex);
  577. if (nformat & VS::ARRAY_FORMAT_NORMAL) {
  578. v.normal = p_xform.basis.xform(v.normal);
  579. }
  580. if (nformat & VS::ARRAY_FORMAT_TANGENT) {
  581. v.tangent = p_xform.basis.xform(v.tangent);
  582. v.binormal = p_xform.basis.xform(v.binormal);
  583. }
  584. vertex_array.push_back(v);
  585. }
  586. for (List<int>::Element *E = nindices.front(); E; E = E->next()) {
  587. int dst_index = E->get() + vfrom;
  588. index_array.push_back(dst_index);
  589. }
  590. if (index_array.size() % 3) {
  591. WARN_PRINT("SurfaceTool: Index array not a multiple of 3.");
  592. }
  593. }
  594. //mikktspace callbacks
  595. namespace {
  596. struct TangentGenerationContextUserData {
  597. Vector<List<SurfaceTool::Vertex>::Element *> vertices;
  598. Vector<List<int>::Element *> indices;
  599. };
  600. } // namespace
  601. int SurfaceTool::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
  602. TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
  603. if (triangle_data.indices.size() > 0) {
  604. return triangle_data.indices.size() / 3;
  605. } else {
  606. return triangle_data.vertices.size() / 3;
  607. }
  608. }
  609. int SurfaceTool::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
  610. return 3; //always 3
  611. }
  612. void SurfaceTool::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
  613. TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
  614. Vector3 v;
  615. if (triangle_data.indices.size() > 0) {
  616. int index = triangle_data.indices[iFace * 3 + iVert]->get();
  617. if (index < triangle_data.vertices.size()) {
  618. v = triangle_data.vertices[index]->get().vertex;
  619. }
  620. } else {
  621. v = triangle_data.vertices[iFace * 3 + iVert]->get().vertex;
  622. }
  623. fvPosOut[0] = v.x;
  624. fvPosOut[1] = v.y;
  625. fvPosOut[2] = v.z;
  626. }
  627. void SurfaceTool::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert) {
  628. TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
  629. Vector3 v;
  630. if (triangle_data.indices.size() > 0) {
  631. int index = triangle_data.indices[iFace * 3 + iVert]->get();
  632. if (index < triangle_data.vertices.size()) {
  633. v = triangle_data.vertices[index]->get().normal;
  634. }
  635. } else {
  636. v = triangle_data.vertices[iFace * 3 + iVert]->get().normal;
  637. }
  638. fvNormOut[0] = v.x;
  639. fvNormOut[1] = v.y;
  640. fvNormOut[2] = v.z;
  641. }
  642. void SurfaceTool::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
  643. TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
  644. Vector2 v;
  645. if (triangle_data.indices.size() > 0) {
  646. int index = triangle_data.indices[iFace * 3 + iVert]->get();
  647. if (index < triangle_data.vertices.size()) {
  648. v = triangle_data.vertices[index]->get().uv;
  649. }
  650. } else {
  651. v = triangle_data.vertices[iFace * 3 + iVert]->get().uv;
  652. }
  653. fvTexcOut[0] = v.x;
  654. fvTexcOut[1] = v.y;
  655. }
  656. void SurfaceTool::mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  657. const tbool bIsOrientationPreserving, const int iFace, const int iVert) {
  658. TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
  659. Vertex *vtx = NULL;
  660. if (triangle_data.indices.size() > 0) {
  661. int index = triangle_data.indices[iFace * 3 + iVert]->get();
  662. if (index < triangle_data.vertices.size()) {
  663. vtx = &triangle_data.vertices[index]->get();
  664. }
  665. } else {
  666. vtx = &triangle_data.vertices[iFace * 3 + iVert]->get();
  667. }
  668. if (vtx != NULL) {
  669. vtx->tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
  670. vtx->binormal = Vector3(-fvBiTangent[0], -fvBiTangent[1], -fvBiTangent[2]); // for some reason these are reversed, something with the coordinate system in Godot
  671. }
  672. }
  673. void SurfaceTool::generate_tangents() {
  674. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_TEX_UV));
  675. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_NORMAL));
  676. SMikkTSpaceInterface mkif;
  677. mkif.m_getNormal = mikktGetNormal;
  678. mkif.m_getNumFaces = mikktGetNumFaces;
  679. mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
  680. mkif.m_getPosition = mikktGetPosition;
  681. mkif.m_getTexCoord = mikktGetTexCoord;
  682. mkif.m_setTSpace = mikktSetTSpaceDefault;
  683. mkif.m_setTSpaceBasic = NULL;
  684. SMikkTSpaceContext msc;
  685. msc.m_pInterface = &mkif;
  686. TangentGenerationContextUserData triangle_data;
  687. triangle_data.vertices.resize(vertex_array.size());
  688. int idx = 0;
  689. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  690. triangle_data.vertices.write[idx++] = E;
  691. E->get().binormal = Vector3();
  692. E->get().tangent = Vector3();
  693. }
  694. triangle_data.indices.resize(index_array.size());
  695. idx = 0;
  696. for (List<int>::Element *E = index_array.front(); E; E = E->next()) {
  697. triangle_data.indices.write[idx++] = E;
  698. }
  699. msc.m_pUserData = &triangle_data;
  700. bool res = genTangSpaceDefault(&msc);
  701. ERR_FAIL_COND(!res);
  702. format |= Mesh::ARRAY_FORMAT_TANGENT;
  703. }
  704. void SurfaceTool::generate_normals(bool p_flip) {
  705. ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
  706. bool was_indexed = index_array.size();
  707. deindex();
  708. HashMap<Vertex, Vector3, VertexHasher> vertex_hash;
  709. int count = 0;
  710. bool smooth = false;
  711. if (smooth_groups.has(0))
  712. smooth = smooth_groups[0];
  713. List<Vertex>::Element *B = vertex_array.front();
  714. for (List<Vertex>::Element *E = B; E;) {
  715. List<Vertex>::Element *v[3];
  716. v[0] = E;
  717. v[1] = v[0]->next();
  718. ERR_FAIL_COND(!v[1]);
  719. v[2] = v[1]->next();
  720. ERR_FAIL_COND(!v[2]);
  721. E = v[2]->next();
  722. Vector3 normal;
  723. if (!p_flip)
  724. normal = Plane(v[0]->get().vertex, v[1]->get().vertex, v[2]->get().vertex).normal;
  725. else
  726. normal = Plane(v[2]->get().vertex, v[1]->get().vertex, v[0]->get().vertex).normal;
  727. if (smooth) {
  728. for (int i = 0; i < 3; i++) {
  729. Vector3 *lv = vertex_hash.getptr(v[i]->get());
  730. if (!lv) {
  731. vertex_hash.set(v[i]->get(), normal);
  732. } else {
  733. (*lv) += normal;
  734. }
  735. }
  736. } else {
  737. for (int i = 0; i < 3; i++) {
  738. v[i]->get().normal = normal;
  739. }
  740. }
  741. count += 3;
  742. if (smooth_groups.has(count) || !E) {
  743. if (vertex_hash.size()) {
  744. while (B != E) {
  745. Vector3 *lv = vertex_hash.getptr(B->get());
  746. if (lv) {
  747. B->get().normal = lv->normalized();
  748. }
  749. B = B->next();
  750. }
  751. } else {
  752. B = E;
  753. }
  754. vertex_hash.clear();
  755. if (E) {
  756. smooth = smooth_groups[count];
  757. }
  758. }
  759. }
  760. format |= Mesh::ARRAY_FORMAT_NORMAL;
  761. if (was_indexed) {
  762. index();
  763. smooth_groups.clear();
  764. }
  765. }
  766. void SurfaceTool::set_material(const Ref<Material> &p_material) {
  767. material = p_material;
  768. }
  769. void SurfaceTool::clear() {
  770. begun = false;
  771. primitive = Mesh::PRIMITIVE_LINES;
  772. format = 0;
  773. last_bones.clear();
  774. last_weights.clear();
  775. index_array.clear();
  776. vertex_array.clear();
  777. smooth_groups.clear();
  778. material.unref();
  779. }
  780. void SurfaceTool::_bind_methods() {
  781. ClassDB::bind_method(D_METHOD("begin", "primitive"), &SurfaceTool::begin);
  782. ClassDB::bind_method(D_METHOD("add_vertex", "vertex"), &SurfaceTool::add_vertex);
  783. ClassDB::bind_method(D_METHOD("add_color", "color"), &SurfaceTool::add_color);
  784. ClassDB::bind_method(D_METHOD("add_normal", "normal"), &SurfaceTool::add_normal);
  785. ClassDB::bind_method(D_METHOD("add_tangent", "tangent"), &SurfaceTool::add_tangent);
  786. ClassDB::bind_method(D_METHOD("add_uv", "uv"), &SurfaceTool::add_uv);
  787. ClassDB::bind_method(D_METHOD("add_uv2", "uv2"), &SurfaceTool::add_uv2);
  788. ClassDB::bind_method(D_METHOD("add_bones", "bones"), &SurfaceTool::add_bones);
  789. ClassDB::bind_method(D_METHOD("add_weights", "weights"), &SurfaceTool::add_weights);
  790. ClassDB::bind_method(D_METHOD("add_smooth_group", "smooth"), &SurfaceTool::add_smooth_group);
  791. ClassDB::bind_method(D_METHOD("add_triangle_fan", "vertexes", "uvs", "colors", "uv2s", "normals", "tangents"), &SurfaceTool::add_triangle_fan, DEFVAL(Vector<Vector2>()), DEFVAL(Vector<Color>()), DEFVAL(Vector<Vector2>()), DEFVAL(Vector<Vector3>()), DEFVAL(Vector<Plane>()));
  792. ClassDB::bind_method(D_METHOD("add_index", "index"), &SurfaceTool::add_index);
  793. ClassDB::bind_method(D_METHOD("index"), &SurfaceTool::index);
  794. ClassDB::bind_method(D_METHOD("deindex"), &SurfaceTool::deindex);
  795. ClassDB::bind_method(D_METHOD("generate_normals", "flip"), &SurfaceTool::generate_normals, DEFVAL(false));
  796. ClassDB::bind_method(D_METHOD("generate_tangents"), &SurfaceTool::generate_tangents);
  797. ClassDB::bind_method(D_METHOD("add_to_format", "flags"), &SurfaceTool::add_to_format);
  798. ClassDB::bind_method(D_METHOD("set_material", "material"), &SurfaceTool::set_material);
  799. ClassDB::bind_method(D_METHOD("clear"), &SurfaceTool::clear);
  800. ClassDB::bind_method(D_METHOD("create_from", "existing", "surface"), &SurfaceTool::create_from);
  801. ClassDB::bind_method(D_METHOD("append_from", "existing", "surface", "transform"), &SurfaceTool::append_from);
  802. ClassDB::bind_method(D_METHOD("commit", "existing", "flags"), &SurfaceTool::commit, DEFVAL(Variant()), DEFVAL(Mesh::ARRAY_COMPRESS_DEFAULT));
  803. }
  804. SurfaceTool::SurfaceTool() {
  805. first = false;
  806. begun = false;
  807. primitive = Mesh::PRIMITIVE_LINES;
  808. format = 0;
  809. }