surface_tool.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*************************************************************************/
  2. /* surface_tool.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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 "surface_tool.h"
  31. #include "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_b) const {
  35. if (vertex != p_b.vertex)
  36. return false;
  37. if (uv != p_b.uv)
  38. return false;
  39. if (uv2 != p_b.uv2)
  40. return false;
  41. if (normal != p_b.normal)
  42. return false;
  43. if (binormal != p_b.binormal)
  44. return false;
  45. if (color != p_b.color)
  46. return false;
  47. if (bones.size() != p_b.bones.size())
  48. return false;
  49. for (int i = 0; i < bones.size(); i++) {
  50. if (bones[i] != p_b.bones[i])
  51. return false;
  52. }
  53. for (int i = 0; i < weights.size(); i++) {
  54. if (weights[i] != p_b.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.weights = last_weights;
  85. vtx.bones = last_bones;
  86. vtx.tangent = last_tangent.normal;
  87. vtx.binormal = last_normal.cross(last_tangent.normal).normalized() * last_tangent.d;
  88. vertex_array.push_back(vtx);
  89. first = false;
  90. format |= Mesh::ARRAY_FORMAT_VERTEX;
  91. }
  92. void SurfaceTool::add_color(Color p_color) {
  93. ERR_FAIL_COND(!begun);
  94. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_COLOR));
  95. format |= Mesh::ARRAY_FORMAT_COLOR;
  96. last_color = p_color;
  97. }
  98. void SurfaceTool::add_normal(const Vector3 &p_normal) {
  99. ERR_FAIL_COND(!begun);
  100. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_NORMAL));
  101. format |= Mesh::ARRAY_FORMAT_NORMAL;
  102. last_normal = p_normal;
  103. }
  104. void SurfaceTool::add_tangent(const Plane &p_tangent) {
  105. ERR_FAIL_COND(!begun);
  106. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TANGENT));
  107. format |= Mesh::ARRAY_FORMAT_TANGENT;
  108. last_tangent = p_tangent;
  109. }
  110. void SurfaceTool::add_uv(const Vector2 &p_uv) {
  111. ERR_FAIL_COND(!begun);
  112. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV));
  113. format |= Mesh::ARRAY_FORMAT_TEX_UV;
  114. last_uv = p_uv;
  115. }
  116. void SurfaceTool::add_uv2(const Vector2 &p_uv2) {
  117. ERR_FAIL_COND(!begun);
  118. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV2));
  119. format |= Mesh::ARRAY_FORMAT_TEX_UV2;
  120. last_uv2 = p_uv2;
  121. }
  122. void SurfaceTool::add_bones(const Vector<int> &p_bones) {
  123. ERR_FAIL_COND(!begun);
  124. ERR_FAIL_COND(p_bones.size() != 4);
  125. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_BONES));
  126. format |= Mesh::ARRAY_FORMAT_BONES;
  127. last_bones = p_bones;
  128. }
  129. void SurfaceTool::add_weights(const Vector<float> &p_weights) {
  130. ERR_FAIL_COND(!begun);
  131. ERR_FAIL_COND(p_weights.size() != 4);
  132. ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_WEIGHTS));
  133. format |= Mesh::ARRAY_FORMAT_WEIGHTS;
  134. last_weights = p_weights;
  135. }
  136. void SurfaceTool::add_smooth_group(bool p_smooth) {
  137. ERR_FAIL_COND(!begun);
  138. if (index_array.size()) {
  139. smooth_groups[index_array.size()] = p_smooth;
  140. } else {
  141. smooth_groups[vertex_array.size()] = p_smooth;
  142. }
  143. }
  144. 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) {
  145. ERR_FAIL_COND(!begun);
  146. ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
  147. ERR_FAIL_COND(p_vertexes.size() < 3);
  148. #define ADD_POINT(n) \
  149. { \
  150. if (p_colors.size() > n) \
  151. add_color(p_colors[n]); \
  152. if (p_uvs.size() > n) \
  153. add_uv(p_uvs[n]); \
  154. if (p_uv2s.size() > n) \
  155. add_uv2(p_uv2s[n]); \
  156. if (p_normals.size() > n) \
  157. add_normal(p_normals[n]); \
  158. if (p_tangents.size() > n) \
  159. add_tangent(p_tangents[n]); \
  160. add_vertex(p_vertexes[n]); \
  161. }
  162. for (int i = 0; i < p_vertexes.size() - 2; i++) {
  163. ADD_POINT(0);
  164. ADD_POINT(i + 1);
  165. ADD_POINT(i + 2);
  166. }
  167. #undef ADD_POINT
  168. }
  169. void SurfaceTool::add_index(int p_index) {
  170. ERR_FAIL_COND(!begun);
  171. format |= Mesh::ARRAY_FORMAT_INDEX;
  172. index_array.push_back(p_index);
  173. }
  174. Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
  175. Ref<ArrayMesh> mesh;
  176. if (p_existing.is_valid())
  177. mesh = p_existing;
  178. else
  179. mesh.instance();
  180. int varr_len = vertex_array.size();
  181. if (varr_len == 0)
  182. return mesh;
  183. int surface = mesh->get_surface_count();
  184. Array a;
  185. a.resize(Mesh::ARRAY_MAX);
  186. for (int i = 0; i < Mesh::ARRAY_MAX; i++) {
  187. switch (format & (1 << i)) {
  188. case Mesh::ARRAY_FORMAT_VERTEX:
  189. case Mesh::ARRAY_FORMAT_NORMAL: {
  190. PoolVector<Vector3> array;
  191. array.resize(varr_len);
  192. PoolVector<Vector3>::Write w = array.write();
  193. int idx = 0;
  194. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  195. const Vertex &v = E->get();
  196. switch (i) {
  197. case Mesh::ARRAY_VERTEX: {
  198. w[idx] = v.vertex;
  199. } break;
  200. case Mesh::ARRAY_NORMAL: {
  201. w[idx] = v.normal;
  202. } break;
  203. }
  204. }
  205. w = PoolVector<Vector3>::Write();
  206. a[i] = array;
  207. } break;
  208. case Mesh::ARRAY_FORMAT_TEX_UV:
  209. case Mesh::ARRAY_FORMAT_TEX_UV2: {
  210. PoolVector<Vector2> array;
  211. array.resize(varr_len);
  212. PoolVector<Vector2>::Write w = array.write();
  213. int idx = 0;
  214. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  215. const Vertex &v = E->get();
  216. switch (i) {
  217. case Mesh::ARRAY_TEX_UV: {
  218. w[idx] = v.uv;
  219. } break;
  220. case Mesh::ARRAY_TEX_UV2: {
  221. w[idx] = v.uv2;
  222. } break;
  223. }
  224. }
  225. w = PoolVector<Vector2>::Write();
  226. a[i] = array;
  227. } break;
  228. case Mesh::ARRAY_FORMAT_TANGENT: {
  229. PoolVector<float> array;
  230. array.resize(varr_len * 4);
  231. PoolVector<float>::Write w = array.write();
  232. int idx = 0;
  233. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  234. const Vertex &v = E->get();
  235. w[idx + 0] = v.tangent.x;
  236. w[idx + 1] = v.tangent.y;
  237. w[idx + 2] = v.tangent.z;
  238. //float d = v.tangent.dot(v.binormal,v.normal);
  239. float d = v.binormal.dot(v.normal.cross(v.tangent));
  240. w[idx + 3] = d < 0 ? -1 : 1;
  241. }
  242. w = PoolVector<float>::Write();
  243. a[i] = array;
  244. } break;
  245. case Mesh::ARRAY_FORMAT_COLOR: {
  246. PoolVector<Color> array;
  247. array.resize(varr_len);
  248. PoolVector<Color>::Write w = array.write();
  249. int idx = 0;
  250. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  251. const Vertex &v = E->get();
  252. w[idx] = v.color;
  253. }
  254. w = PoolVector<Color>::Write();
  255. a[i] = array;
  256. } break;
  257. case Mesh::ARRAY_FORMAT_BONES: {
  258. PoolVector<int> array;
  259. array.resize(varr_len * 4);
  260. PoolVector<int>::Write w = array.write();
  261. int idx = 0;
  262. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  263. const Vertex &v = E->get();
  264. ERR_CONTINUE(v.bones.size() != 4);
  265. for (int j = 0; j < 4; j++) {
  266. w[idx + j] = v.bones[j];
  267. }
  268. }
  269. w = PoolVector<int>::Write();
  270. a[i] = array;
  271. } break;
  272. case Mesh::ARRAY_FORMAT_WEIGHTS: {
  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. ERR_CONTINUE(v.weights.size() != 4);
  280. for (int j = 0; j < 4; j++) {
  281. w[idx + j] = v.weights[j];
  282. }
  283. }
  284. w = PoolVector<float>::Write();
  285. a[i] = array;
  286. } break;
  287. case Mesh::ARRAY_FORMAT_INDEX: {
  288. ERR_CONTINUE(index_array.size() == 0);
  289. PoolVector<int> array;
  290. array.resize(index_array.size());
  291. PoolVector<int>::Write w = array.write();
  292. int idx = 0;
  293. for (List<int>::Element *E = index_array.front(); E; E = E->next(), idx++) {
  294. w[idx] = E->get();
  295. }
  296. w = PoolVector<int>::Write();
  297. a[i] = array;
  298. } break;
  299. default: {}
  300. }
  301. }
  302. mesh->add_surface_from_arrays(primitive, a);
  303. if (material.is_valid())
  304. mesh->surface_set_material(surface, material);
  305. return mesh;
  306. }
  307. void SurfaceTool::index() {
  308. if (index_array.size())
  309. return; //already indexed
  310. HashMap<Vertex, int, VertexHasher> indices;
  311. List<Vertex> new_vertices;
  312. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  313. int *idxptr = indices.getptr(E->get());
  314. int idx;
  315. if (!idxptr) {
  316. idx = indices.size();
  317. new_vertices.push_back(E->get());
  318. indices[E->get()] = idx;
  319. } else {
  320. idx = *idxptr;
  321. }
  322. index_array.push_back(idx);
  323. }
  324. vertex_array.clear();
  325. vertex_array = new_vertices;
  326. format |= Mesh::ARRAY_FORMAT_INDEX;
  327. }
  328. void SurfaceTool::deindex() {
  329. if (index_array.size() == 0)
  330. return; //nothing to deindex
  331. Vector<Vertex> varr;
  332. varr.resize(vertex_array.size());
  333. int idx = 0;
  334. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  335. varr[idx++] = E->get();
  336. }
  337. vertex_array.clear();
  338. for (List<int>::Element *E = index_array.front(); E; E = E->next()) {
  339. ERR_FAIL_INDEX(E->get(), varr.size());
  340. vertex_array.push_back(varr[E->get()]);
  341. }
  342. format &= ~Mesh::ARRAY_FORMAT_INDEX;
  343. }
  344. void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
  345. Array arr = p_existing->surface_get_arrays(p_surface);
  346. ERR_FAIL_COND(arr.size() != VS::ARRAY_MAX);
  347. PoolVector<Vector3> varr = arr[VS::ARRAY_VERTEX];
  348. PoolVector<Vector3> narr = arr[VS::ARRAY_NORMAL];
  349. PoolVector<float> tarr = arr[VS::ARRAY_TANGENT];
  350. PoolVector<Color> carr = arr[VS::ARRAY_COLOR];
  351. PoolVector<Vector2> uvarr = arr[VS::ARRAY_TEX_UV];
  352. PoolVector<Vector2> uv2arr = arr[VS::ARRAY_TEX_UV2];
  353. PoolVector<int> barr = arr[VS::ARRAY_BONES];
  354. PoolVector<float> warr = arr[VS::ARRAY_WEIGHTS];
  355. int vc = varr.size();
  356. if (vc == 0)
  357. return;
  358. lformat = 0;
  359. PoolVector<Vector3>::Read rv;
  360. if (varr.size()) {
  361. lformat |= VS::ARRAY_FORMAT_VERTEX;
  362. rv = varr.read();
  363. }
  364. PoolVector<Vector3>::Read rn;
  365. if (narr.size()) {
  366. lformat |= VS::ARRAY_FORMAT_NORMAL;
  367. rn = narr.read();
  368. }
  369. PoolVector<float>::Read rt;
  370. if (tarr.size()) {
  371. lformat |= VS::ARRAY_FORMAT_TANGENT;
  372. rt = tarr.read();
  373. }
  374. PoolVector<Color>::Read rc;
  375. if (carr.size()) {
  376. lformat |= VS::ARRAY_FORMAT_COLOR;
  377. rc = carr.read();
  378. }
  379. PoolVector<Vector2>::Read ruv;
  380. if (uvarr.size()) {
  381. lformat |= VS::ARRAY_FORMAT_TEX_UV;
  382. ruv = uvarr.read();
  383. }
  384. PoolVector<Vector2>::Read ruv2;
  385. if (uv2arr.size()) {
  386. lformat |= VS::ARRAY_FORMAT_TEX_UV2;
  387. ruv2 = uv2arr.read();
  388. }
  389. PoolVector<int>::Read rb;
  390. if (barr.size()) {
  391. lformat |= VS::ARRAY_FORMAT_BONES;
  392. rb = barr.read();
  393. }
  394. PoolVector<float>::Read rw;
  395. if (warr.size()) {
  396. lformat |= VS::ARRAY_FORMAT_WEIGHTS;
  397. rw = warr.read();
  398. }
  399. for (int i = 0; i < vc; i++) {
  400. Vertex v;
  401. if (lformat & VS::ARRAY_FORMAT_VERTEX)
  402. v.vertex = varr[i];
  403. if (lformat & VS::ARRAY_FORMAT_NORMAL)
  404. v.normal = narr[i];
  405. if (lformat & VS::ARRAY_FORMAT_TANGENT) {
  406. Plane p(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2], tarr[i * 4 + 3]);
  407. v.tangent = p.normal;
  408. v.binormal = p.normal.cross(last_normal).normalized() * p.d;
  409. }
  410. if (lformat & VS::ARRAY_FORMAT_COLOR)
  411. v.color = carr[i];
  412. if (lformat & VS::ARRAY_FORMAT_TEX_UV)
  413. v.uv = uvarr[i];
  414. if (lformat & VS::ARRAY_FORMAT_TEX_UV2)
  415. v.uv2 = uv2arr[i];
  416. if (lformat & VS::ARRAY_FORMAT_BONES) {
  417. Vector<int> b;
  418. b.resize(4);
  419. b[0] = barr[i * 4 + 0];
  420. b[1] = barr[i * 4 + 1];
  421. b[2] = barr[i * 4 + 2];
  422. b[3] = barr[i * 4 + 3];
  423. v.bones = b;
  424. }
  425. if (lformat & VS::ARRAY_FORMAT_WEIGHTS) {
  426. Vector<float> w;
  427. w.resize(4);
  428. w[0] = warr[i * 4 + 0];
  429. w[1] = warr[i * 4 + 1];
  430. w[2] = warr[i * 4 + 2];
  431. w[3] = warr[i * 4 + 3];
  432. v.weights = w;
  433. }
  434. r_vertex->push_back(v);
  435. }
  436. //indices
  437. PoolVector<int> idx = arr[VS::ARRAY_INDEX];
  438. int is = idx.size();
  439. if (is) {
  440. lformat |= VS::ARRAY_FORMAT_INDEX;
  441. PoolVector<int>::Read iarr = idx.read();
  442. for (int i = 0; i < is; i++) {
  443. r_index->push_back(iarr[i]);
  444. }
  445. }
  446. }
  447. void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) {
  448. clear();
  449. primitive = p_existing->surface_get_primitive_type(p_surface);
  450. _create_list(p_existing, p_surface, &vertex_array, &index_array, format);
  451. material = p_existing->surface_get_material(p_surface);
  452. }
  453. void SurfaceTool::append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform) {
  454. if (vertex_array.size() == 0) {
  455. primitive = p_existing->surface_get_primitive_type(p_surface);
  456. format = 0;
  457. }
  458. int nformat;
  459. List<Vertex> nvertices;
  460. List<int> nindices;
  461. _create_list(p_existing, p_surface, &nvertices, &nindices, nformat);
  462. format |= nformat;
  463. int vfrom = vertex_array.size();
  464. for (List<Vertex>::Element *E = nvertices.front(); E; E = E->next()) {
  465. Vertex v = E->get();
  466. v.vertex = p_xform.xform(v.vertex);
  467. if (nformat & VS::ARRAY_FORMAT_NORMAL) {
  468. v.normal = p_xform.basis.xform(v.normal);
  469. }
  470. if (nformat & VS::ARRAY_FORMAT_TANGENT) {
  471. v.tangent = p_xform.basis.xform(v.tangent);
  472. v.binormal = p_xform.basis.xform(v.binormal);
  473. }
  474. vertex_array.push_back(v);
  475. }
  476. for (List<int>::Element *E = nindices.front(); E; E = E->next()) {
  477. int dst_index = E->get() + vfrom;
  478. /*
  479. if (dst_index <0 || dst_index>=vertex_array.size()) {
  480. print_line("invalid index!");
  481. }
  482. */
  483. index_array.push_back(dst_index);
  484. }
  485. if (index_array.size() % 3)
  486. print_line("IA not div of 3?");
  487. }
  488. //mikktspace callbacks
  489. int SurfaceTool::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
  490. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  491. return varr.size() / 3;
  492. }
  493. int SurfaceTool::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
  494. return 3; //always 3
  495. }
  496. void SurfaceTool::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
  497. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  498. Vector3 v = varr[iFace * 3 + iVert]->get().vertex;
  499. fvPosOut[0] = v.x;
  500. fvPosOut[1] = v.y;
  501. fvPosOut[2] = v.z;
  502. }
  503. void SurfaceTool::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert) {
  504. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  505. Vector3 v = varr[iFace * 3 + iVert]->get().normal;
  506. fvNormOut[0] = v.x;
  507. fvNormOut[1] = v.y;
  508. fvNormOut[2] = v.z;
  509. }
  510. void SurfaceTool::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
  511. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  512. Vector2 v = varr[iFace * 3 + iVert]->get().uv;
  513. fvTexcOut[0] = v.x;
  514. fvTexcOut[1] = v.y;
  515. //fvTexcOut[1]=1.0-v.y;
  516. }
  517. void SurfaceTool::mikktSetTSpaceBasic(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert) {
  518. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  519. Vertex &vtx = varr[iFace * 3 + iVert]->get();
  520. vtx.tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
  521. vtx.binormal = vtx.normal.cross(vtx.tangent) * fSign;
  522. }
  523. void SurfaceTool::generate_tangents() {
  524. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_TEX_UV));
  525. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_NORMAL));
  526. bool indexed = index_array.size() > 0;
  527. if (indexed)
  528. deindex();
  529. SMikkTSpaceInterface mkif;
  530. mkif.m_getNormal = mikktGetNormal;
  531. mkif.m_getNumFaces = mikktGetNumFaces;
  532. mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
  533. mkif.m_getPosition = mikktGetPosition;
  534. mkif.m_getTexCoord = mikktGetTexCoord;
  535. mkif.m_setTSpaceBasic = mikktSetTSpaceBasic;
  536. mkif.m_setTSpace = NULL;
  537. SMikkTSpaceContext msc;
  538. msc.m_pInterface = &mkif;
  539. Vector<List<Vertex>::Element *> vtx;
  540. vtx.resize(vertex_array.size());
  541. int idx = 0;
  542. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  543. vtx[idx++] = E;
  544. E->get().binormal = Vector3();
  545. E->get().tangent = Vector3();
  546. }
  547. msc.m_pUserData = &vtx;
  548. bool res = genTangSpaceDefault(&msc);
  549. ERR_FAIL_COND(!res);
  550. format |= Mesh::ARRAY_FORMAT_TANGENT;
  551. if (indexed)
  552. index();
  553. }
  554. void SurfaceTool::generate_normals() {
  555. ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
  556. bool was_indexed = index_array.size();
  557. deindex();
  558. HashMap<Vertex, Vector3, VertexHasher> vertex_hash;
  559. int count = 0;
  560. bool smooth = false;
  561. if (smooth_groups.has(0))
  562. smooth = smooth_groups[0];
  563. List<Vertex>::Element *B = vertex_array.front();
  564. for (List<Vertex>::Element *E = B; E;) {
  565. List<Vertex>::Element *v[3];
  566. v[0] = E;
  567. v[1] = v[0]->next();
  568. ERR_FAIL_COND(!v[1]);
  569. v[2] = v[1]->next();
  570. ERR_FAIL_COND(!v[2]);
  571. E = v[2]->next();
  572. Vector3 normal = Plane(v[0]->get().vertex, v[1]->get().vertex, v[2]->get().vertex).normal;
  573. if (smooth) {
  574. for (int i = 0; i < 3; i++) {
  575. Vector3 *lv = vertex_hash.getptr(v[i]->get());
  576. if (!lv) {
  577. vertex_hash.set(v[i]->get(), normal);
  578. } else {
  579. (*lv) += normal;
  580. }
  581. }
  582. } else {
  583. for (int i = 0; i < 3; i++) {
  584. v[i]->get().normal = normal;
  585. }
  586. }
  587. count += 3;
  588. if (smooth_groups.has(count) || !E) {
  589. if (vertex_hash.size()) {
  590. while (B != E) {
  591. Vector3 *lv = vertex_hash.getptr(B->get());
  592. if (lv) {
  593. B->get().normal = lv->normalized();
  594. }
  595. B = B->next();
  596. }
  597. } else {
  598. B = E;
  599. }
  600. vertex_hash.clear();
  601. if (E) {
  602. smooth = smooth_groups[count];
  603. print_line("SMOOTH AT " + itos(count) + ": " + itos(smooth));
  604. }
  605. }
  606. }
  607. format |= Mesh::ARRAY_FORMAT_NORMAL;
  608. if (was_indexed) {
  609. index();
  610. smooth_groups.clear();
  611. }
  612. }
  613. void SurfaceTool::set_material(const Ref<Material> &p_material) {
  614. material = p_material;
  615. }
  616. void SurfaceTool::clear() {
  617. begun = false;
  618. primitive = Mesh::PRIMITIVE_LINES;
  619. format = 0;
  620. last_bones.clear();
  621. last_weights.clear();
  622. index_array.clear();
  623. vertex_array.clear();
  624. smooth_groups.clear();
  625. material.unref();
  626. }
  627. void SurfaceTool::_bind_methods() {
  628. ClassDB::bind_method(D_METHOD("begin", "primitive"), &SurfaceTool::begin);
  629. ClassDB::bind_method(D_METHOD("add_vertex", "vertex"), &SurfaceTool::add_vertex);
  630. ClassDB::bind_method(D_METHOD("add_color", "color"), &SurfaceTool::add_color);
  631. ClassDB::bind_method(D_METHOD("add_normal", "normal"), &SurfaceTool::add_normal);
  632. ClassDB::bind_method(D_METHOD("add_tangent", "tangent"), &SurfaceTool::add_tangent);
  633. ClassDB::bind_method(D_METHOD("add_uv", "uv"), &SurfaceTool::add_uv);
  634. ClassDB::bind_method(D_METHOD("add_uv2", "uv2"), &SurfaceTool::add_uv2);
  635. ClassDB::bind_method(D_METHOD("add_bones", "bones"), &SurfaceTool::add_bones);
  636. ClassDB::bind_method(D_METHOD("add_weights", "weights"), &SurfaceTool::add_weights);
  637. ClassDB::bind_method(D_METHOD("add_smooth_group", "smooth"), &SurfaceTool::add_smooth_group);
  638. 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>()));
  639. ClassDB::bind_method(D_METHOD("add_index", "index"), &SurfaceTool::add_index);
  640. ClassDB::bind_method(D_METHOD("index"), &SurfaceTool::index);
  641. ClassDB::bind_method(D_METHOD("deindex"), &SurfaceTool::deindex);
  642. ClassDB::bind_method(D_METHOD("generate_normals"), &SurfaceTool::generate_normals);
  643. ClassDB::bind_method(D_METHOD("generate_tangents"), &SurfaceTool::generate_tangents);
  644. ClassDB::bind_method(D_METHOD("add_to_format", "flags"), &SurfaceTool::add_to_format);
  645. ClassDB::bind_method(D_METHOD("set_material", "material:Material"), &SurfaceTool::set_material);
  646. ClassDB::bind_method(D_METHOD("clear"), &SurfaceTool::clear);
  647. ClassDB::bind_method(D_METHOD("create_from", "existing:Mesh", "surface"), &SurfaceTool::create_from);
  648. ClassDB::bind_method(D_METHOD("append_from", "existing:Mesh", "surface", "transform"), &SurfaceTool::append_from);
  649. ClassDB::bind_method(D_METHOD("commit:Mesh", "existing:Mesh"), &SurfaceTool::commit, DEFVAL(Variant()));
  650. }
  651. SurfaceTool::SurfaceTool() {
  652. first = false;
  653. begun = false;
  654. primitive = Mesh::PRIMITIVE_LINES;
  655. format = 0;
  656. }