surface_tool.cpp 30 KB

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