surface_tool.cpp 37 KB

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