surface_tool.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*************************************************************************/
  2. /* surface_tool.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "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_vertex) const {
  35. if (vertex != p_vertex.vertex)
  36. return false;
  37. if (uv != p_vertex.uv)
  38. return false;
  39. if (uv2 != p_vertex.uv2)
  40. return false;
  41. if (normal != p_vertex.normal)
  42. return false;
  43. if (binormal != p_vertex.binormal)
  44. return false;
  45. if (color != p_vertex.color)
  46. return false;
  47. if (bones.size() != p_vertex.bones.size())
  48. return false;
  49. for (int i = 0; i < bones.size(); i++) {
  50. if (bones[i] != p_vertex.bones[i])
  51. return false;
  52. }
  53. for (int i = 0; i < weights.size(); i++) {
  54. if (weights[i] != p_vertex.weights[i])
  55. return false;
  56. }
  57. return true;
  58. }
  59. uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) {
  60. uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3);
  61. h = hash_djb2_buffer((const uint8_t *)&p_vtx.normal, sizeof(real_t) * 3, h);
  62. h = hash_djb2_buffer((const uint8_t *)&p_vtx.binormal, sizeof(real_t) * 3, h);
  63. h = hash_djb2_buffer((const uint8_t *)&p_vtx.tangent, sizeof(real_t) * 3, h);
  64. h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv, sizeof(real_t) * 2, h);
  65. h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv2, sizeof(real_t) * 2, h);
  66. h = hash_djb2_buffer((const uint8_t *)&p_vtx.color, sizeof(real_t) * 4, h);
  67. h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int), h);
  68. h = hash_djb2_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h);
  69. return h;
  70. }
  71. void SurfaceTool::begin(Mesh::PrimitiveType p_primitive) {
  72. clear();
  73. primitive = p_primitive;
  74. begun = true;
  75. first = true;
  76. }
  77. void SurfaceTool::add_vertex(const Vector3 &p_vertex) {
  78. ERR_FAIL_COND(!begun);
  79. Vertex vtx;
  80. vtx.vertex = p_vertex;
  81. vtx.color = last_color;
  82. vtx.normal = last_normal;
  83. vtx.uv = last_uv;
  84. vtx.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. Array SurfaceTool::commit_to_arrays() {
  175. int varr_len = vertex_array.size();
  176. Array a;
  177. a.resize(Mesh::ARRAY_MAX);
  178. for (int i = 0; i < Mesh::ARRAY_MAX; i++) {
  179. if (!(format & (1 << i)))
  180. continue; //not in format
  181. switch (i) {
  182. case Mesh::ARRAY_VERTEX:
  183. case Mesh::ARRAY_NORMAL: {
  184. PoolVector<Vector3> array;
  185. array.resize(varr_len);
  186. PoolVector<Vector3>::Write w = array.write();
  187. int idx = 0;
  188. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  189. const Vertex &v = E->get();
  190. switch (i) {
  191. case Mesh::ARRAY_VERTEX: {
  192. w[idx] = v.vertex;
  193. } break;
  194. case Mesh::ARRAY_NORMAL: {
  195. w[idx] = v.normal;
  196. } break;
  197. }
  198. }
  199. w = PoolVector<Vector3>::Write();
  200. a[i] = array;
  201. } break;
  202. case Mesh::ARRAY_TEX_UV:
  203. case Mesh::ARRAY_TEX_UV2: {
  204. PoolVector<Vector2> array;
  205. array.resize(varr_len);
  206. PoolVector<Vector2>::Write w = array.write();
  207. int idx = 0;
  208. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  209. const Vertex &v = E->get();
  210. switch (i) {
  211. case Mesh::ARRAY_TEX_UV: {
  212. w[idx] = v.uv;
  213. } break;
  214. case Mesh::ARRAY_TEX_UV2: {
  215. w[idx] = v.uv2;
  216. } break;
  217. }
  218. }
  219. w = PoolVector<Vector2>::Write();
  220. a[i] = array;
  221. } break;
  222. case Mesh::ARRAY_TANGENT: {
  223. PoolVector<float> array;
  224. array.resize(varr_len * 4);
  225. PoolVector<float>::Write w = array.write();
  226. int idx = 0;
  227. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  228. const Vertex &v = E->get();
  229. w[idx + 0] = v.tangent.x;
  230. w[idx + 1] = v.tangent.y;
  231. w[idx + 2] = v.tangent.z;
  232. //float d = v.tangent.dot(v.binormal,v.normal);
  233. float d = v.binormal.dot(v.normal.cross(v.tangent));
  234. w[idx + 3] = d < 0 ? -1 : 1;
  235. }
  236. w = PoolVector<float>::Write();
  237. a[i] = array;
  238. } break;
  239. case Mesh::ARRAY_COLOR: {
  240. PoolVector<Color> array;
  241. array.resize(varr_len);
  242. PoolVector<Color>::Write w = array.write();
  243. int idx = 0;
  244. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
  245. const Vertex &v = E->get();
  246. w[idx] = v.color;
  247. }
  248. w = PoolVector<Color>::Write();
  249. a[i] = array;
  250. } break;
  251. case Mesh::ARRAY_BONES: {
  252. PoolVector<int> array;
  253. array.resize(varr_len * 4);
  254. PoolVector<int>::Write w = array.write();
  255. int idx = 0;
  256. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  257. const Vertex &v = E->get();
  258. ERR_CONTINUE(v.bones.size() != 4);
  259. for (int j = 0; j < 4; j++) {
  260. w[idx + j] = v.bones[j];
  261. }
  262. }
  263. w = PoolVector<int>::Write();
  264. a[i] = array;
  265. } break;
  266. case Mesh::ARRAY_WEIGHTS: {
  267. PoolVector<float> array;
  268. array.resize(varr_len * 4);
  269. PoolVector<float>::Write w = array.write();
  270. int idx = 0;
  271. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
  272. const Vertex &v = E->get();
  273. ERR_CONTINUE(v.weights.size() != 4);
  274. for (int j = 0; j < 4; j++) {
  275. w[idx + j] = v.weights[j];
  276. }
  277. }
  278. w = PoolVector<float>::Write();
  279. a[i] = array;
  280. } break;
  281. case Mesh::ARRAY_INDEX: {
  282. ERR_CONTINUE(index_array.size() == 0);
  283. PoolVector<int> array;
  284. array.resize(index_array.size());
  285. PoolVector<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 = PoolVector<int>::Write();
  291. a[i] = array;
  292. } break;
  293. default: {}
  294. }
  295. }
  296. return a;
  297. }
  298. Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
  299. Ref<ArrayMesh> mesh;
  300. if (p_existing.is_valid())
  301. mesh = p_existing;
  302. else
  303. mesh.instance();
  304. int varr_len = vertex_array.size();
  305. if (varr_len == 0)
  306. return mesh;
  307. int surface = mesh->get_surface_count();
  308. Array a = commit_to_arrays();
  309. mesh->add_surface_from_arrays(primitive, a);
  310. if (material.is_valid())
  311. mesh->surface_set_material(surface, material);
  312. return mesh;
  313. }
  314. void SurfaceTool::index() {
  315. if (index_array.size())
  316. return; //already indexed
  317. HashMap<Vertex, int, VertexHasher> indices;
  318. List<Vertex> new_vertices;
  319. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  320. int *idxptr = indices.getptr(E->get());
  321. int idx;
  322. if (!idxptr) {
  323. idx = indices.size();
  324. new_vertices.push_back(E->get());
  325. indices[E->get()] = idx;
  326. } else {
  327. idx = *idxptr;
  328. }
  329. index_array.push_back(idx);
  330. }
  331. vertex_array.clear();
  332. vertex_array = new_vertices;
  333. format |= Mesh::ARRAY_FORMAT_INDEX;
  334. }
  335. void SurfaceTool::deindex() {
  336. if (index_array.size() == 0)
  337. return; //nothing to deindex
  338. Vector<Vertex> varr;
  339. varr.resize(vertex_array.size());
  340. int idx = 0;
  341. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  342. varr[idx++] = E->get();
  343. }
  344. vertex_array.clear();
  345. for (List<int>::Element *E = index_array.front(); E; E = E->next()) {
  346. ERR_FAIL_INDEX(E->get(), varr.size());
  347. vertex_array.push_back(varr[E->get()]);
  348. }
  349. format &= ~Mesh::ARRAY_FORMAT_INDEX;
  350. index_array.clear();
  351. }
  352. void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
  353. Array arr = p_existing->surface_get_arrays(p_surface);
  354. ERR_FAIL_COND(arr.size() != VS::ARRAY_MAX);
  355. _create_list_from_arrays(arr, r_vertex, r_index, lformat);
  356. }
  357. void SurfaceTool::_create_list_from_arrays(Array arr, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
  358. PoolVector<Vector3> varr = arr[VS::ARRAY_VERTEX];
  359. PoolVector<Vector3> narr = arr[VS::ARRAY_NORMAL];
  360. PoolVector<float> tarr = arr[VS::ARRAY_TANGENT];
  361. PoolVector<Color> carr = arr[VS::ARRAY_COLOR];
  362. PoolVector<Vector2> uvarr = arr[VS::ARRAY_TEX_UV];
  363. PoolVector<Vector2> uv2arr = arr[VS::ARRAY_TEX_UV2];
  364. PoolVector<int> barr = arr[VS::ARRAY_BONES];
  365. PoolVector<float> warr = arr[VS::ARRAY_WEIGHTS];
  366. int vc = varr.size();
  367. if (vc == 0)
  368. return;
  369. lformat = 0;
  370. PoolVector<Vector3>::Read rv;
  371. if (varr.size()) {
  372. lformat |= VS::ARRAY_FORMAT_VERTEX;
  373. rv = varr.read();
  374. }
  375. PoolVector<Vector3>::Read rn;
  376. if (narr.size()) {
  377. lformat |= VS::ARRAY_FORMAT_NORMAL;
  378. rn = narr.read();
  379. }
  380. PoolVector<float>::Read rt;
  381. if (tarr.size()) {
  382. lformat |= VS::ARRAY_FORMAT_TANGENT;
  383. rt = tarr.read();
  384. }
  385. PoolVector<Color>::Read rc;
  386. if (carr.size()) {
  387. lformat |= VS::ARRAY_FORMAT_COLOR;
  388. rc = carr.read();
  389. }
  390. PoolVector<Vector2>::Read ruv;
  391. if (uvarr.size()) {
  392. lformat |= VS::ARRAY_FORMAT_TEX_UV;
  393. ruv = uvarr.read();
  394. }
  395. PoolVector<Vector2>::Read ruv2;
  396. if (uv2arr.size()) {
  397. lformat |= VS::ARRAY_FORMAT_TEX_UV2;
  398. ruv2 = uv2arr.read();
  399. }
  400. PoolVector<int>::Read rb;
  401. if (barr.size()) {
  402. lformat |= VS::ARRAY_FORMAT_BONES;
  403. rb = barr.read();
  404. }
  405. PoolVector<float>::Read rw;
  406. if (warr.size()) {
  407. lformat |= VS::ARRAY_FORMAT_WEIGHTS;
  408. rw = warr.read();
  409. }
  410. for (int i = 0; i < vc; i++) {
  411. Vertex v;
  412. if (lformat & VS::ARRAY_FORMAT_VERTEX)
  413. v.vertex = varr[i];
  414. if (lformat & VS::ARRAY_FORMAT_NORMAL)
  415. v.normal = narr[i];
  416. if (lformat & VS::ARRAY_FORMAT_TANGENT) {
  417. Plane p(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2], tarr[i * 4 + 3]);
  418. v.tangent = p.normal;
  419. v.binormal = p.normal.cross(v.tangent).normalized() * p.d;
  420. }
  421. if (lformat & VS::ARRAY_FORMAT_COLOR)
  422. v.color = carr[i];
  423. if (lformat & VS::ARRAY_FORMAT_TEX_UV)
  424. v.uv = uvarr[i];
  425. if (lformat & VS::ARRAY_FORMAT_TEX_UV2)
  426. v.uv2 = uv2arr[i];
  427. if (lformat & VS::ARRAY_FORMAT_BONES) {
  428. Vector<int> b;
  429. b.resize(4);
  430. b[0] = barr[i * 4 + 0];
  431. b[1] = barr[i * 4 + 1];
  432. b[2] = barr[i * 4 + 2];
  433. b[3] = barr[i * 4 + 3];
  434. v.bones = b;
  435. }
  436. if (lformat & VS::ARRAY_FORMAT_WEIGHTS) {
  437. Vector<float> w;
  438. w.resize(4);
  439. w[0] = warr[i * 4 + 0];
  440. w[1] = warr[i * 4 + 1];
  441. w[2] = warr[i * 4 + 2];
  442. w[3] = warr[i * 4 + 3];
  443. v.weights = w;
  444. }
  445. r_vertex->push_back(v);
  446. }
  447. //indices
  448. PoolVector<int> idx = arr[VS::ARRAY_INDEX];
  449. int is = idx.size();
  450. if (is) {
  451. lformat |= VS::ARRAY_FORMAT_INDEX;
  452. PoolVector<int>::Read iarr = idx.read();
  453. for (int i = 0; i < is; i++) {
  454. r_index->push_back(iarr[i]);
  455. }
  456. }
  457. }
  458. void SurfaceTool::create_from_triangle_arrays(const Array &p_arrays) {
  459. clear();
  460. primitive = Mesh::PRIMITIVE_TRIANGLES;
  461. _create_list_from_arrays(p_arrays, &vertex_array, &index_array, format);
  462. }
  463. void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) {
  464. clear();
  465. primitive = p_existing->surface_get_primitive_type(p_surface);
  466. _create_list(p_existing, p_surface, &vertex_array, &index_array, format);
  467. material = p_existing->surface_get_material(p_surface);
  468. }
  469. void SurfaceTool::append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform) {
  470. if (vertex_array.size() == 0) {
  471. primitive = p_existing->surface_get_primitive_type(p_surface);
  472. format = 0;
  473. }
  474. int nformat;
  475. List<Vertex> nvertices;
  476. List<int> nindices;
  477. _create_list(p_existing, p_surface, &nvertices, &nindices, nformat);
  478. format |= nformat;
  479. int vfrom = vertex_array.size();
  480. for (List<Vertex>::Element *E = nvertices.front(); E; E = E->next()) {
  481. Vertex v = E->get();
  482. v.vertex = p_xform.xform(v.vertex);
  483. if (nformat & VS::ARRAY_FORMAT_NORMAL) {
  484. v.normal = p_xform.basis.xform(v.normal);
  485. }
  486. if (nformat & VS::ARRAY_FORMAT_TANGENT) {
  487. v.tangent = p_xform.basis.xform(v.tangent);
  488. v.binormal = p_xform.basis.xform(v.binormal);
  489. }
  490. vertex_array.push_back(v);
  491. }
  492. for (List<int>::Element *E = nindices.front(); E; E = E->next()) {
  493. int dst_index = E->get() + vfrom;
  494. /*
  495. if (dst_index <0 || dst_index>=vertex_array.size()) {
  496. print_line("invalid index!");
  497. }
  498. */
  499. index_array.push_back(dst_index);
  500. }
  501. if (index_array.size() % 3)
  502. print_line("IA not div of 3?");
  503. }
  504. //mikktspace callbacks
  505. int SurfaceTool::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
  506. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  507. return varr.size() / 3;
  508. }
  509. int SurfaceTool::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
  510. return 3; //always 3
  511. }
  512. void SurfaceTool::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
  513. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  514. Vector3 v = varr[iFace * 3 + iVert]->get().vertex;
  515. fvPosOut[0] = v.x;
  516. fvPosOut[1] = v.y;
  517. fvPosOut[2] = v.z;
  518. }
  519. void SurfaceTool::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert) {
  520. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  521. Vector3 v = varr[iFace * 3 + iVert]->get().normal;
  522. fvNormOut[0] = v.x;
  523. fvNormOut[1] = v.y;
  524. fvNormOut[2] = v.z;
  525. }
  526. void SurfaceTool::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
  527. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  528. Vector2 v = varr[iFace * 3 + iVert]->get().uv;
  529. fvTexcOut[0] = v.x;
  530. fvTexcOut[1] = v.y;
  531. //fvTexcOut[1]=1.0-v.y;
  532. }
  533. void SurfaceTool::mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  534. const tbool bIsOrientationPreserving, const int iFace, const int iVert) {
  535. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  536. Vertex *vtx = &varr[iFace * 3 + iVert]->get();
  537. vtx->tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
  538. vtx->binormal = Vector3(fvBiTangent[0], fvBiTangent[1], fvBiTangent[2]);
  539. }
  540. void SurfaceTool::mikktSetTSpaceBasic(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert) {
  541. Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
  542. Vertex &vtx = varr[iFace * 3 + iVert]->get();
  543. vtx.tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
  544. vtx.binormal = vtx.normal.cross(vtx.tangent) * fSign;
  545. }
  546. void SurfaceTool::generate_tangents() {
  547. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_TEX_UV));
  548. ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_NORMAL));
  549. bool indexed = index_array.size() > 0;
  550. if (indexed)
  551. deindex();
  552. SMikkTSpaceInterface mkif;
  553. mkif.m_getNormal = mikktGetNormal;
  554. mkif.m_getNumFaces = mikktGetNumFaces;
  555. mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
  556. mkif.m_getPosition = mikktGetPosition;
  557. mkif.m_getTexCoord = mikktGetTexCoord;
  558. mkif.m_setTSpace = mikktSetTSpaceDefault;
  559. mkif.m_setTSpaceBasic = NULL;
  560. SMikkTSpaceContext msc;
  561. msc.m_pInterface = &mkif;
  562. Vector<List<Vertex>::Element *> vtx;
  563. vtx.resize(vertex_array.size());
  564. int idx = 0;
  565. for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
  566. vtx[idx++] = E;
  567. E->get().binormal = Vector3();
  568. E->get().tangent = Vector3();
  569. }
  570. msc.m_pUserData = &vtx;
  571. bool res = genTangSpaceDefault(&msc);
  572. ERR_FAIL_COND(!res);
  573. format |= Mesh::ARRAY_FORMAT_TANGENT;
  574. if (indexed) {
  575. index();
  576. }
  577. }
  578. void SurfaceTool::generate_normals() {
  579. ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
  580. bool was_indexed = index_array.size();
  581. deindex();
  582. HashMap<Vertex, Vector3, VertexHasher> vertex_hash;
  583. int count = 0;
  584. bool smooth = false;
  585. if (smooth_groups.has(0))
  586. smooth = smooth_groups[0];
  587. List<Vertex>::Element *B = vertex_array.front();
  588. for (List<Vertex>::Element *E = B; E;) {
  589. List<Vertex>::Element *v[3];
  590. v[0] = E;
  591. v[1] = v[0]->next();
  592. ERR_FAIL_COND(!v[1]);
  593. v[2] = v[1]->next();
  594. ERR_FAIL_COND(!v[2]);
  595. E = v[2]->next();
  596. Vector3 normal = Plane(v[0]->get().vertex, v[1]->get().vertex, v[2]->get().vertex).normal;
  597. if (smooth) {
  598. for (int i = 0; i < 3; i++) {
  599. Vector3 *lv = vertex_hash.getptr(v[i]->get());
  600. if (!lv) {
  601. vertex_hash.set(v[i]->get(), normal);
  602. } else {
  603. (*lv) += normal;
  604. }
  605. }
  606. } else {
  607. for (int i = 0; i < 3; i++) {
  608. v[i]->get().normal = normal;
  609. }
  610. }
  611. count += 3;
  612. if (smooth_groups.has(count) || !E) {
  613. if (vertex_hash.size()) {
  614. while (B != E) {
  615. Vector3 *lv = vertex_hash.getptr(B->get());
  616. if (lv) {
  617. B->get().normal = lv->normalized();
  618. }
  619. B = B->next();
  620. }
  621. } else {
  622. B = E;
  623. }
  624. vertex_hash.clear();
  625. if (E) {
  626. smooth = smooth_groups[count];
  627. }
  628. }
  629. }
  630. format |= Mesh::ARRAY_FORMAT_NORMAL;
  631. if (was_indexed) {
  632. index();
  633. smooth_groups.clear();
  634. }
  635. }
  636. void SurfaceTool::set_material(const Ref<Material> &p_material) {
  637. material = p_material;
  638. }
  639. void SurfaceTool::clear() {
  640. begun = false;
  641. primitive = Mesh::PRIMITIVE_LINES;
  642. format = 0;
  643. last_bones.clear();
  644. last_weights.clear();
  645. index_array.clear();
  646. vertex_array.clear();
  647. smooth_groups.clear();
  648. material.unref();
  649. }
  650. void SurfaceTool::_bind_methods() {
  651. ClassDB::bind_method(D_METHOD("begin", "primitive"), &SurfaceTool::begin);
  652. ClassDB::bind_method(D_METHOD("add_vertex", "vertex"), &SurfaceTool::add_vertex);
  653. ClassDB::bind_method(D_METHOD("add_color", "color"), &SurfaceTool::add_color);
  654. ClassDB::bind_method(D_METHOD("add_normal", "normal"), &SurfaceTool::add_normal);
  655. ClassDB::bind_method(D_METHOD("add_tangent", "tangent"), &SurfaceTool::add_tangent);
  656. ClassDB::bind_method(D_METHOD("add_uv", "uv"), &SurfaceTool::add_uv);
  657. ClassDB::bind_method(D_METHOD("add_uv2", "uv2"), &SurfaceTool::add_uv2);
  658. ClassDB::bind_method(D_METHOD("add_bones", "bones"), &SurfaceTool::add_bones);
  659. ClassDB::bind_method(D_METHOD("add_weights", "weights"), &SurfaceTool::add_weights);
  660. ClassDB::bind_method(D_METHOD("add_smooth_group", "smooth"), &SurfaceTool::add_smooth_group);
  661. 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>()));
  662. ClassDB::bind_method(D_METHOD("add_index", "index"), &SurfaceTool::add_index);
  663. ClassDB::bind_method(D_METHOD("index"), &SurfaceTool::index);
  664. ClassDB::bind_method(D_METHOD("deindex"), &SurfaceTool::deindex);
  665. ClassDB::bind_method(D_METHOD("generate_normals"), &SurfaceTool::generate_normals);
  666. ClassDB::bind_method(D_METHOD("generate_tangents"), &SurfaceTool::generate_tangents);
  667. ClassDB::bind_method(D_METHOD("add_to_format", "flags"), &SurfaceTool::add_to_format);
  668. ClassDB::bind_method(D_METHOD("set_material", "material"), &SurfaceTool::set_material);
  669. ClassDB::bind_method(D_METHOD("clear"), &SurfaceTool::clear);
  670. ClassDB::bind_method(D_METHOD("create_from", "existing", "surface"), &SurfaceTool::create_from);
  671. ClassDB::bind_method(D_METHOD("append_from", "existing", "surface", "transform"), &SurfaceTool::append_from);
  672. ClassDB::bind_method(D_METHOD("commit", "existing"), &SurfaceTool::commit, DEFVAL(Variant()));
  673. }
  674. SurfaceTool::SurfaceTool() {
  675. first = false;
  676. begun = false;
  677. primitive = Mesh::PRIMITIVE_LINES;
  678. format = 0;
  679. }