surface_tool.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*************************************************************************/
  2. /* surface_tool.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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<Mesh> SurfaceTool::commit(const Ref<Mesh> &p_existing) {
  175. Ref<Mesh> mesh;
  176. if (p_existing.is_valid())
  177. mesh = p_existing;
  178. else
  179. mesh = Ref<Mesh>(memnew(Mesh));
  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. DVector<Vector3> array;
  191. array.resize(varr_len);
  192. DVector<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 = DVector<Vector3>::Write();
  206. a[i] = array;
  207. } break;
  208. case Mesh::ARRAY_FORMAT_TEX_UV:
  209. case Mesh::ARRAY_FORMAT_TEX_UV2: {
  210. DVector<Vector2> array;
  211. array.resize(varr_len);
  212. DVector<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 = DVector<Vector2>::Write();
  226. a[i] = array;
  227. } break;
  228. case Mesh::ARRAY_FORMAT_TANGENT: {
  229. DVector<float> array;
  230. array.resize(varr_len * 4);
  231. DVector<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 = DVector<float>::Write();
  243. a[i] = array;
  244. } break;
  245. case Mesh::ARRAY_FORMAT_COLOR: {
  246. DVector<Color> array;
  247. array.resize(varr_len);
  248. DVector<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 = DVector<Color>::Write();
  255. a[i] = array;
  256. } break;
  257. case Mesh::ARRAY_FORMAT_BONES:
  258. case Mesh::ARRAY_FORMAT_WEIGHTS: {
  259. DVector<float> array;
  260. array.resize(varr_len * 4);
  261. DVector<float>::Write w = array.write();
  262. int idx = 0;
  263. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  264. const Vertex &v = E->get();
  265. for (int j = 0; j < 4; j++) {
  266. switch (i) {
  267. case Mesh::ARRAY_WEIGHTS: {
  268. ERR_CONTINUE(v.weights.size() != 4);
  269. w[idx + j] = v.weights[j];
  270. } break;
  271. case Mesh::ARRAY_BONES: {
  272. ERR_CONTINUE(v.bones.size() != 4);
  273. w[idx + j] = v.bones[j];
  274. } break;
  275. }
  276. }
  277. }
  278. w = DVector<float>::Write();
  279. a[i] = array;
  280. } break;
  281. case Mesh::ARRAY_FORMAT_INDEX: {
  282. ERR_CONTINUE(index_array.size() == 0);
  283. DVector<int> array;
  284. array.resize(index_array.size());
  285. DVector<int>::Write w = array.write();
  286. int idx = 0;
  287. for (List<int>::Element *E = index_array.front(); E; E = E->next(), idx++) {
  288. w[idx] = E->get();
  289. }
  290. w = DVector<int>::Write();
  291. a[i] = array;
  292. } break;
  293. default: {
  294. }
  295. }
  296. }
  297. mesh->add_surface(primitive, a);
  298. if (material.is_valid())
  299. mesh->surface_set_material(surface, material);
  300. return mesh;
  301. }
  302. void SurfaceTool::index() {
  303. if (index_array.size())
  304. return; //already indexed
  305. HashMap<Vertex, int, VertexHasher> indices;
  306. List<Vertex> new_vertices;
  307. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  308. int *idxptr = indices.getptr(E->get());
  309. int idx;
  310. if (!idxptr) {
  311. idx = indices.size();
  312. new_vertices.push_back(E->get());
  313. indices[E->get()] = idx;
  314. } else {
  315. idx = *idxptr;
  316. }
  317. index_array.push_back(idx);
  318. }
  319. vertex_array.clear();
  320. vertex_array = new_vertices;
  321. format |= Mesh::ARRAY_FORMAT_INDEX;
  322. }
  323. void SurfaceTool::deindex() {
  324. if (index_array.size() == 0)
  325. return; //nothing to deindex
  326. Vector<Vertex> varr;
  327. varr.resize(vertex_array.size());
  328. int idx = 0;
  329. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  330. varr[idx++] = E->get();
  331. }
  332. vertex_array.clear();
  333. for (List<int>::Element *E = index_array.front(); E; E = E->next()) {
  334. ERR_FAIL_INDEX(E->get(), varr.size());
  335. vertex_array.push_back(varr[E->get()]);
  336. }
  337. format &= ~Mesh::ARRAY_FORMAT_INDEX;
  338. }
  339. void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
  340. Array arr = p_existing->surface_get_arrays(p_surface);
  341. ERR_FAIL_COND(arr.size() != VS::ARRAY_MAX);
  342. DVector<Vector3> varr = arr[VS::ARRAY_VERTEX];
  343. DVector<Vector3> narr = arr[VS::ARRAY_NORMAL];
  344. DVector<float> tarr = arr[VS::ARRAY_TANGENT];
  345. DVector<Color> carr = arr[VS::ARRAY_COLOR];
  346. DVector<Vector2> uvarr = arr[VS::ARRAY_TEX_UV];
  347. DVector<Vector2> uv2arr = arr[VS::ARRAY_TEX_UV2];
  348. DVector<int> barr = arr[VS::ARRAY_BONES];
  349. DVector<float> warr = arr[VS::ARRAY_WEIGHTS];
  350. int vc = varr.size();
  351. if (vc == 0)
  352. return;
  353. lformat = 0;
  354. DVector<Vector3>::Read rv;
  355. if (varr.size()) {
  356. lformat |= VS::ARRAY_FORMAT_VERTEX;
  357. rv = varr.read();
  358. }
  359. DVector<Vector3>::Read rn;
  360. if (narr.size()) {
  361. lformat |= VS::ARRAY_FORMAT_NORMAL;
  362. rn = narr.read();
  363. }
  364. DVector<float>::Read rt;
  365. if (tarr.size()) {
  366. lformat |= VS::ARRAY_FORMAT_TANGENT;
  367. rt = tarr.read();
  368. }
  369. DVector<Color>::Read rc;
  370. if (carr.size()) {
  371. lformat |= VS::ARRAY_FORMAT_COLOR;
  372. rc = carr.read();
  373. }
  374. DVector<Vector2>::Read ruv;
  375. if (uvarr.size()) {
  376. lformat |= VS::ARRAY_FORMAT_TEX_UV;
  377. ruv = uvarr.read();
  378. }
  379. DVector<Vector2>::Read ruv2;
  380. if (uv2arr.size()) {
  381. lformat |= VS::ARRAY_FORMAT_TEX_UV2;
  382. ruv2 = uv2arr.read();
  383. }
  384. DVector<int>::Read rb;
  385. if (barr.size()) {
  386. lformat |= VS::ARRAY_FORMAT_BONES;
  387. rb = barr.read();
  388. }
  389. DVector<float>::Read rw;
  390. if (warr.size()) {
  391. lformat |= VS::ARRAY_FORMAT_WEIGHTS;
  392. rw = warr.read();
  393. }
  394. for (int i = 0; i < vc; i++) {
  395. Vertex v;
  396. if (lformat & VS::ARRAY_FORMAT_VERTEX)
  397. v.vertex = varr[i];
  398. if (lformat & VS::ARRAY_FORMAT_NORMAL)
  399. v.normal = narr[i];
  400. if (lformat & VS::ARRAY_FORMAT_TANGENT) {
  401. Plane p(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2], tarr[i * 4 + 3]);
  402. v.tangent = p.normal;
  403. v.binormal = p.normal.cross(last_normal).normalized() * p.d;
  404. }
  405. if (lformat & VS::ARRAY_FORMAT_COLOR)
  406. v.color = carr[i];
  407. if (lformat & VS::ARRAY_FORMAT_TEX_UV)
  408. v.uv = uvarr[i];
  409. if (lformat & VS::ARRAY_FORMAT_TEX_UV2)
  410. v.uv2 = uv2arr[i];
  411. if (lformat & VS::ARRAY_FORMAT_BONES) {
  412. Vector<int> b;
  413. b.resize(4);
  414. b[0] = barr[i * 4 + 0];
  415. b[1] = barr[i * 4 + 1];
  416. b[2] = barr[i * 4 + 2];
  417. b[3] = barr[i * 4 + 3];
  418. v.bones = b;
  419. }
  420. if (lformat & VS::ARRAY_FORMAT_WEIGHTS) {
  421. Vector<float> w;
  422. w.resize(4);
  423. w[0] = warr[i * 4 + 0];
  424. w[1] = warr[i * 4 + 1];
  425. w[2] = warr[i * 4 + 2];
  426. w[3] = warr[i * 4 + 3];
  427. v.weights = w;
  428. }
  429. r_vertex->push_back(v);
  430. }
  431. //indices
  432. DVector<int> idx = arr[VS::ARRAY_INDEX];
  433. int is = idx.size();
  434. if (is) {
  435. lformat |= VS::ARRAY_FORMAT_INDEX;
  436. DVector<int>::Read iarr = idx.read();
  437. for (int i = 0; i < is; i++) {
  438. r_index->push_back(iarr[i]);
  439. }
  440. }
  441. }
  442. void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) {
  443. clear();
  444. primitive = p_existing->surface_get_primitive_type(p_surface);
  445. _create_list(p_existing, p_surface, &vertex_array, &index_array, format);
  446. material = p_existing->surface_get_material(p_surface);
  447. }
  448. void SurfaceTool::append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform) {
  449. if (vertex_array.size() == 0) {
  450. primitive = p_existing->surface_get_primitive_type(p_surface);
  451. format = 0;
  452. }
  453. int nformat;
  454. List<Vertex> nvertices;
  455. List<int> nindices;
  456. _create_list(p_existing, p_surface, &nvertices, &nindices, nformat);
  457. format |= nformat;
  458. int vfrom = vertex_array.size();
  459. for (List<Vertex>::Element *E = nvertices.front(); E; E = E->next()) {
  460. Vertex v = E->get();
  461. v.vertex = p_xform.xform(v.vertex);
  462. if (nformat & VS::ARRAY_FORMAT_NORMAL) {
  463. v.normal = p_xform.basis.xform(v.normal);
  464. }
  465. if (nformat & VS::ARRAY_FORMAT_TANGENT) {
  466. v.tangent = p_xform.basis.xform(v.tangent);
  467. v.binormal = p_xform.basis.xform(v.binormal);
  468. }
  469. vertex_array.push_back(v);
  470. }
  471. for (List<int>::Element *E = nindices.front(); E; E = E->next()) {
  472. int dst_index = E->get() + vfrom;
  473. //if (dst_index <0 || dst_index>=vertex_array.size()) {
  474. // print_line("invalid index!");
  475. //}
  476. index_array.push_back(dst_index);
  477. }
  478. if (index_array.size() % 3)
  479. print_line("IA not div of 3?");
  480. }
  481. //mikktspace callbacks
  482. int SurfaceTool::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
  483. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  484. return varr.size() / 3;
  485. }
  486. int SurfaceTool::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
  487. return 3; //always 3
  488. }
  489. void SurfaceTool::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
  490. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  491. Vector3 v = varr[iFace * 3 + iVert]->get().vertex;
  492. fvPosOut[0] = v.x;
  493. fvPosOut[1] = v.y;
  494. fvPosOut[2] = v.z;
  495. }
  496. void SurfaceTool::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], 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().normal;
  499. fvNormOut[0] = v.x;
  500. fvNormOut[1] = v.y;
  501. fvNormOut[2] = v.z;
  502. }
  503. void SurfaceTool::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
  504. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  505. Vector2 v = varr[iFace * 3 + iVert]->get().uv;
  506. fvTexcOut[0] = v.x;
  507. fvTexcOut[1] = v.y;
  508. //fvTexcOut[1]=1.0-v.y;
  509. }
  510. void SurfaceTool::mikktSetTSpaceBasic(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert) {
  511. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  512. Vertex &vtx = varr[iFace * 3 + iVert]->get();
  513. vtx.tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
  514. vtx.binormal = vtx.normal.cross(vtx.tangent) * fSign;
  515. }
  516. void SurfaceTool::generate_tangents() {
  517. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_TEX_UV));
  518. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_NORMAL));
  519. bool indexed = index_array.size() > 0;
  520. if (indexed)
  521. deindex();
  522. SMikkTSpaceInterface mkif;
  523. mkif.m_getNormal = mikktGetNormal;
  524. mkif.m_getNumFaces = mikktGetNumFaces;
  525. mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
  526. mkif.m_getPosition = mikktGetPosition;
  527. mkif.m_getTexCoord = mikktGetTexCoord;
  528. mkif.m_setTSpaceBasic = mikktSetTSpaceBasic;
  529. mkif.m_setTSpace = NULL;
  530. SMikkTSpaceContext msc;
  531. msc.m_pInterface = &mkif;
  532. Vector<List<Vertex>::Element *> vtx;
  533. vtx.resize(vertex_array.size());
  534. int idx = 0;
  535. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  536. vtx[idx++] = E;
  537. E->get().binormal = Vector3();
  538. E->get().tangent = Vector3();
  539. }
  540. msc.m_pUserData = &vtx;
  541. bool res = genTangSpaceDefault(&msc);
  542. ERR_FAIL_COND(!res);
  543. format |= Mesh::ARRAY_FORMAT_TANGENT;
  544. if (indexed)
  545. index();
  546. }
  547. void SurfaceTool::generate_normals() {
  548. ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
  549. bool was_indexed = index_array.size();
  550. deindex();
  551. HashMap<Vertex, Vector3, VertexHasher> vertex_hash;
  552. int count = 0;
  553. bool smooth = false;
  554. if (smooth_groups.has(0))
  555. smooth = smooth_groups[0];
  556. List<Vertex>::Element *B = vertex_array.front();
  557. for (List<Vertex>::Element *E = B; E;) {
  558. List<Vertex>::Element *v[3];
  559. v[0] = E;
  560. v[1] = v[0]->next();
  561. ERR_FAIL_COND(!v[1]);
  562. v[2] = v[1]->next();
  563. ERR_FAIL_COND(!v[2]);
  564. E = v[2]->next();
  565. Vector3 normal = Plane(v[0]->get().vertex, v[1]->get().vertex, v[2]->get().vertex).normal;
  566. if (smooth) {
  567. for (int i = 0; i < 3; i++) {
  568. Vector3 *lv = vertex_hash.getptr(v[i]->get());
  569. if (!lv) {
  570. vertex_hash.set(v[i]->get(), normal);
  571. } else {
  572. (*lv) += normal;
  573. }
  574. }
  575. } else {
  576. for (int i = 0; i < 3; i++) {
  577. v[i]->get().normal = normal;
  578. }
  579. }
  580. count += 3;
  581. if (smooth_groups.has(count) || !E) {
  582. if (vertex_hash.size()) {
  583. while (B != E) {
  584. Vector3 *lv = vertex_hash.getptr(B->get());
  585. if (lv) {
  586. B->get().normal = lv->normalized();
  587. }
  588. B = B->next();
  589. }
  590. } else {
  591. B = E;
  592. }
  593. vertex_hash.clear();
  594. if (E) {
  595. smooth = smooth_groups[count];
  596. print_line("Smooth at " + itos(count) + ": " + itos(smooth));
  597. }
  598. }
  599. }
  600. format |= Mesh::ARRAY_FORMAT_NORMAL;
  601. if (was_indexed) {
  602. index();
  603. smooth_groups.clear();
  604. }
  605. }
  606. void SurfaceTool::set_material(const Ref<Material> &p_material) {
  607. material = p_material;
  608. }
  609. void SurfaceTool::clear() {
  610. begun = false;
  611. primitive = Mesh::PRIMITIVE_LINES;
  612. format = 0;
  613. last_bones.clear();
  614. last_weights.clear();
  615. index_array.clear();
  616. vertex_array.clear();
  617. smooth_groups.clear();
  618. }
  619. void SurfaceTool::_bind_methods() {
  620. ObjectTypeDB::bind_method(_MD("begin", "primitive"), &SurfaceTool::begin);
  621. ObjectTypeDB::bind_method(_MD("add_vertex", "vertex"), &SurfaceTool::add_vertex);
  622. ObjectTypeDB::bind_method(_MD("add_color", "color"), &SurfaceTool::add_color);
  623. ObjectTypeDB::bind_method(_MD("add_normal", "normal"), &SurfaceTool::add_normal);
  624. ObjectTypeDB::bind_method(_MD("add_tangent", "tangent"), &SurfaceTool::add_tangent);
  625. ObjectTypeDB::bind_method(_MD("add_uv", "uv"), &SurfaceTool::add_uv);
  626. ObjectTypeDB::bind_method(_MD("add_uv2", "uv2"), &SurfaceTool::add_uv2);
  627. ObjectTypeDB::bind_method(_MD("add_bones", "bones"), &SurfaceTool::add_bones);
  628. ObjectTypeDB::bind_method(_MD("add_weights", "weights"), &SurfaceTool::add_weights);
  629. ObjectTypeDB::bind_method(_MD("add_smooth_group", "smooth"), &SurfaceTool::add_smooth_group);
  630. ObjectTypeDB::bind_method(_MD("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>()));
  631. ObjectTypeDB::bind_method(_MD("add_index", "index"), &SurfaceTool::add_index);
  632. ObjectTypeDB::bind_method(_MD("index"), &SurfaceTool::index);
  633. ObjectTypeDB::bind_method(_MD("deindex"), &SurfaceTool::deindex);
  634. ObjectTypeDB::bind_method(_MD("generate_normals"), &SurfaceTool::generate_normals);
  635. ObjectTypeDB::bind_method(_MD("generate_tangents"), &SurfaceTool::generate_tangents);
  636. ObjectTypeDB::bind_method(_MD("add_to_format", "flags"), &SurfaceTool::add_to_format);
  637. ObjectTypeDB::bind_method(_MD("set_material", "material:Material"), &SurfaceTool::set_material);
  638. ObjectTypeDB::bind_method(_MD("clear"), &SurfaceTool::clear);
  639. ObjectTypeDB::bind_method(_MD("create_from", "existing:Mesh", "surface"), &SurfaceTool::create_from);
  640. ObjectTypeDB::bind_method(_MD("append_from", "existing:Mesh", "surface", "transform"), &SurfaceTool::append_from);
  641. ObjectTypeDB::bind_method(_MD("commit:Mesh", "existing:Mesh"), &SurfaceTool::commit, DEFVAL(Variant()));
  642. }
  643. SurfaceTool::SurfaceTool() {
  644. first = false;
  645. begun = false;
  646. primitive = Mesh::PRIMITIVE_LINES;
  647. format = 0;
  648. }