mesh.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  1. /*************************************************************************/
  2. /* mesh.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 "mesh.h"
  31. #include "core/math/convex_hull.h"
  32. #include "core/templates/pair.h"
  33. #include "scene/resources/concave_polygon_shape_3d.h"
  34. #include "scene/resources/convex_polygon_shape_3d.h"
  35. #include "surface_tool.h"
  36. #include <stdlib.h>
  37. Mesh::ConvexDecompositionFunc Mesh::convex_decomposition_function = nullptr;
  38. Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
  39. if (triangle_mesh.is_valid()) {
  40. return triangle_mesh;
  41. }
  42. int facecount = 0;
  43. for (int i = 0; i < get_surface_count(); i++) {
  44. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
  45. continue;
  46. }
  47. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  48. facecount += surface_get_array_index_len(i);
  49. } else {
  50. facecount += surface_get_array_len(i);
  51. }
  52. }
  53. if (facecount == 0 || (facecount % 3) != 0) {
  54. return triangle_mesh;
  55. }
  56. Vector<Vector3> faces;
  57. faces.resize(facecount);
  58. Vector3 *facesw = faces.ptrw();
  59. int widx = 0;
  60. for (int i = 0; i < get_surface_count(); i++) {
  61. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
  62. continue;
  63. }
  64. Array a = surface_get_arrays(i);
  65. ERR_FAIL_COND_V(a.is_empty(), Ref<TriangleMesh>());
  66. int vc = surface_get_array_len(i);
  67. Vector<Vector3> vertices = a[ARRAY_VERTEX];
  68. const Vector3 *vr = vertices.ptr();
  69. if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
  70. int ic = surface_get_array_index_len(i);
  71. Vector<int> indices = a[ARRAY_INDEX];
  72. const int *ir = indices.ptr();
  73. for (int j = 0; j < ic; j++) {
  74. int index = ir[j];
  75. facesw[widx++] = vr[index];
  76. }
  77. } else {
  78. for (int j = 0; j < vc; j++) {
  79. facesw[widx++] = vr[j];
  80. }
  81. }
  82. }
  83. triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  84. triangle_mesh->create(faces);
  85. return triangle_mesh;
  86. }
  87. void Mesh::generate_debug_mesh_lines(Vector<Vector3> &r_lines) {
  88. if (debug_lines.size() > 0) {
  89. r_lines = debug_lines;
  90. return;
  91. }
  92. Ref<TriangleMesh> tm = generate_triangle_mesh();
  93. if (tm.is_null()) {
  94. return;
  95. }
  96. Vector<int> triangle_indices;
  97. tm->get_indices(&triangle_indices);
  98. const int triangles_num = tm->get_triangles().size();
  99. Vector<Vector3> vertices = tm->get_vertices();
  100. debug_lines.resize(tm->get_triangles().size() * 6); // 3 lines x 2 points each line
  101. const int *ind_r = triangle_indices.ptr();
  102. const Vector3 *ver_r = vertices.ptr();
  103. for (int j = 0, x = 0, i = 0; i < triangles_num; j += 6, x += 3, ++i) {
  104. // Triangle line 1
  105. debug_lines.write[j + 0] = ver_r[ind_r[x + 0]];
  106. debug_lines.write[j + 1] = ver_r[ind_r[x + 1]];
  107. // Triangle line 2
  108. debug_lines.write[j + 2] = ver_r[ind_r[x + 1]];
  109. debug_lines.write[j + 3] = ver_r[ind_r[x + 2]];
  110. // Triangle line 3
  111. debug_lines.write[j + 4] = ver_r[ind_r[x + 2]];
  112. debug_lines.write[j + 5] = ver_r[ind_r[x + 0]];
  113. }
  114. r_lines = debug_lines;
  115. }
  116. void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) {
  117. Ref<TriangleMesh> tm = generate_triangle_mesh();
  118. if (tm.is_null()) {
  119. return;
  120. }
  121. Vector<Vector3> vertices = tm->get_vertices();
  122. int vertices_size = vertices.size();
  123. r_points.resize(vertices_size);
  124. for (int i = 0; i < vertices_size; ++i) {
  125. r_points.write[i] = vertices[i];
  126. }
  127. }
  128. bool Mesh::surface_is_softbody_friendly(int p_idx) const {
  129. const uint32_t surface_format = surface_get_format(p_idx);
  130. return (surface_format & Mesh::ARRAY_FLAG_USE_DYNAMIC_UPDATE);
  131. }
  132. Vector<Face3> Mesh::get_faces() const {
  133. Ref<TriangleMesh> tm = generate_triangle_mesh();
  134. if (tm.is_valid()) {
  135. return tm->get_faces();
  136. }
  137. return Vector<Face3>();
  138. }
  139. Ref<Shape3D> Mesh::create_convex_shape(bool p_clean, bool p_simplify) const {
  140. if (p_simplify) {
  141. ConvexDecompositionSettings settings;
  142. settings.max_convex_hulls = 1;
  143. Vector<Ref<Shape3D>> decomposed = convex_decompose(settings);
  144. if (decomposed.size() == 1) {
  145. return decomposed[0];
  146. } else {
  147. ERR_PRINT("Convex shape simplification failed, falling back to simpler process.");
  148. }
  149. }
  150. Vector<Vector3> vertices;
  151. for (int i = 0; i < get_surface_count(); i++) {
  152. Array a = surface_get_arrays(i);
  153. ERR_FAIL_COND_V(a.is_empty(), Ref<ConvexPolygonShape3D>());
  154. Vector<Vector3> v = a[ARRAY_VERTEX];
  155. vertices.append_array(v);
  156. }
  157. Ref<ConvexPolygonShape3D> shape = memnew(ConvexPolygonShape3D);
  158. if (p_clean) {
  159. Geometry3D::MeshData md;
  160. Error err = ConvexHullComputer::convex_hull(vertices, md);
  161. if (err == OK) {
  162. shape->set_points(md.vertices);
  163. return shape;
  164. } else {
  165. ERR_PRINT("Convex shape cleaning failed, falling back to simpler process.");
  166. }
  167. }
  168. shape->set_points(vertices);
  169. return shape;
  170. }
  171. Ref<Shape3D> Mesh::create_trimesh_shape() const {
  172. Vector<Face3> faces = get_faces();
  173. if (faces.size() == 0) {
  174. return Ref<Shape3D>();
  175. }
  176. Vector<Vector3> face_points;
  177. face_points.resize(faces.size() * 3);
  178. for (int i = 0; i < face_points.size(); i += 3) {
  179. Face3 f = faces.get(i / 3);
  180. face_points.set(i, f.vertex[0]);
  181. face_points.set(i + 1, f.vertex[1]);
  182. face_points.set(i + 2, f.vertex[2]);
  183. }
  184. Ref<ConcavePolygonShape3D> shape = memnew(ConcavePolygonShape3D);
  185. shape->set_faces(face_points);
  186. return shape;
  187. }
  188. Ref<Mesh> Mesh::create_outline(float p_margin) const {
  189. Array arrays;
  190. int index_accum = 0;
  191. for (int i = 0; i < get_surface_count(); i++) {
  192. if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
  193. continue;
  194. }
  195. Array a = surface_get_arrays(i);
  196. ERR_FAIL_COND_V(a.is_empty(), Ref<ArrayMesh>());
  197. if (i == 0) {
  198. arrays = a;
  199. Vector<Vector3> v = a[ARRAY_VERTEX];
  200. index_accum += v.size();
  201. } else {
  202. int vcount = 0;
  203. for (int j = 0; j < arrays.size(); j++) {
  204. if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) {
  205. //mismatch, do not use
  206. arrays[j] = Variant();
  207. continue;
  208. }
  209. switch (j) {
  210. case ARRAY_VERTEX:
  211. case ARRAY_NORMAL: {
  212. Vector<Vector3> dst = arrays[j];
  213. Vector<Vector3> src = a[j];
  214. if (j == ARRAY_VERTEX) {
  215. vcount = src.size();
  216. }
  217. if (dst.size() == 0 || src.size() == 0) {
  218. arrays[j] = Variant();
  219. continue;
  220. }
  221. dst.append_array(src);
  222. arrays[j] = dst;
  223. } break;
  224. case ARRAY_TANGENT:
  225. case ARRAY_BONES:
  226. case ARRAY_WEIGHTS: {
  227. Vector<real_t> dst = arrays[j];
  228. Vector<real_t> src = a[j];
  229. if (dst.size() == 0 || src.size() == 0) {
  230. arrays[j] = Variant();
  231. continue;
  232. }
  233. dst.append_array(src);
  234. arrays[j] = dst;
  235. } break;
  236. case ARRAY_COLOR: {
  237. Vector<Color> dst = arrays[j];
  238. Vector<Color> src = a[j];
  239. if (dst.size() == 0 || src.size() == 0) {
  240. arrays[j] = Variant();
  241. continue;
  242. }
  243. dst.append_array(src);
  244. arrays[j] = dst;
  245. } break;
  246. case ARRAY_TEX_UV:
  247. case ARRAY_TEX_UV2: {
  248. Vector<Vector2> dst = arrays[j];
  249. Vector<Vector2> src = a[j];
  250. if (dst.size() == 0 || src.size() == 0) {
  251. arrays[j] = Variant();
  252. continue;
  253. }
  254. dst.append_array(src);
  255. arrays[j] = dst;
  256. } break;
  257. case ARRAY_INDEX: {
  258. Vector<int> dst = arrays[j];
  259. Vector<int> src = a[j];
  260. if (dst.size() == 0 || src.size() == 0) {
  261. arrays[j] = Variant();
  262. continue;
  263. }
  264. {
  265. int ss = src.size();
  266. int *w = src.ptrw();
  267. for (int k = 0; k < ss; k++) {
  268. w[k] += index_accum;
  269. }
  270. }
  271. dst.append_array(src);
  272. arrays[j] = dst;
  273. index_accum += vcount;
  274. } break;
  275. }
  276. }
  277. }
  278. }
  279. ERR_FAIL_COND_V(arrays.size() != ARRAY_MAX, Ref<ArrayMesh>());
  280. {
  281. int *ir = nullptr;
  282. Vector<int> indices = arrays[ARRAY_INDEX];
  283. bool has_indices = false;
  284. Vector<Vector3> vertices = arrays[ARRAY_VERTEX];
  285. int vc = vertices.size();
  286. ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>());
  287. Vector3 *r = vertices.ptrw();
  288. if (indices.size()) {
  289. ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ArrayMesh>());
  290. vc = indices.size();
  291. ir = indices.ptrw();
  292. has_indices = true;
  293. }
  294. Map<Vector3, Vector3> normal_accum;
  295. //fill normals with triangle normals
  296. for (int i = 0; i < vc; i += 3) {
  297. Vector3 t[3];
  298. if (has_indices) {
  299. t[0] = r[ir[i + 0]];
  300. t[1] = r[ir[i + 1]];
  301. t[2] = r[ir[i + 2]];
  302. } else {
  303. t[0] = r[i + 0];
  304. t[1] = r[i + 1];
  305. t[2] = r[i + 2];
  306. }
  307. Vector3 n = Plane(t[0], t[1], t[2]).normal;
  308. for (int j = 0; j < 3; j++) {
  309. Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]);
  310. if (!E) {
  311. normal_accum[t[j]] = n;
  312. } else {
  313. float d = n.dot(E->get());
  314. if (d < 1.0) {
  315. E->get() += n * (1.0 - d);
  316. }
  317. //E->get()+=n;
  318. }
  319. }
  320. }
  321. //normalize
  322. for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) {
  323. E->get().normalize();
  324. }
  325. //displace normals
  326. int vc2 = vertices.size();
  327. for (int i = 0; i < vc2; i++) {
  328. Vector3 t = r[i];
  329. Map<Vector3, Vector3>::Element *E = normal_accum.find(t);
  330. ERR_CONTINUE(!E);
  331. t += E->get() * p_margin;
  332. r[i] = t;
  333. }
  334. arrays[ARRAY_VERTEX] = vertices;
  335. if (!has_indices) {
  336. Vector<int> new_indices;
  337. new_indices.resize(vertices.size());
  338. int *iw = new_indices.ptrw();
  339. for (int j = 0; j < vc2; j += 3) {
  340. iw[j] = j;
  341. iw[j + 1] = j + 2;
  342. iw[j + 2] = j + 1;
  343. }
  344. arrays[ARRAY_INDEX] = new_indices;
  345. } else {
  346. for (int j = 0; j < vc; j += 3) {
  347. SWAP(ir[j + 1], ir[j + 2]);
  348. }
  349. arrays[ARRAY_INDEX] = indices;
  350. }
  351. }
  352. Ref<ArrayMesh> newmesh = memnew(ArrayMesh);
  353. newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays);
  354. return newmesh;
  355. }
  356. void Mesh::set_lightmap_size_hint(const Size2i &p_size) {
  357. lightmap_size_hint = p_size;
  358. }
  359. Size2i Mesh::get_lightmap_size_hint() const {
  360. return lightmap_size_hint;
  361. }
  362. void Mesh::_bind_methods() {
  363. ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &Mesh::set_lightmap_size_hint);
  364. ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &Mesh::get_lightmap_size_hint);
  365. ClassDB::bind_method(D_METHOD("get_aabb"), &Mesh::get_aabb);
  366. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "lightmap_size_hint"), "set_lightmap_size_hint", "get_lightmap_size_hint");
  367. ClassDB::bind_method(D_METHOD("get_surface_count"), &Mesh::get_surface_count);
  368. ClassDB::bind_method(D_METHOD("surface_get_arrays", "surf_idx"), &Mesh::surface_get_arrays);
  369. ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &Mesh::surface_get_blend_shape_arrays);
  370. ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material);
  371. ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material);
  372. BIND_ENUM_CONSTANT(PRIMITIVE_POINTS);
  373. BIND_ENUM_CONSTANT(PRIMITIVE_LINES);
  374. BIND_ENUM_CONSTANT(PRIMITIVE_LINE_STRIP);
  375. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLES);
  376. BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
  377. BIND_ENUM_CONSTANT(ARRAY_VERTEX);
  378. BIND_ENUM_CONSTANT(ARRAY_NORMAL);
  379. BIND_ENUM_CONSTANT(ARRAY_TANGENT);
  380. BIND_ENUM_CONSTANT(ARRAY_COLOR);
  381. BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
  382. BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
  383. BIND_ENUM_CONSTANT(ARRAY_CUSTOM0);
  384. BIND_ENUM_CONSTANT(ARRAY_CUSTOM1);
  385. BIND_ENUM_CONSTANT(ARRAY_CUSTOM2);
  386. BIND_ENUM_CONSTANT(ARRAY_CUSTOM3);
  387. BIND_ENUM_CONSTANT(ARRAY_BONES);
  388. BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
  389. BIND_ENUM_CONSTANT(ARRAY_INDEX);
  390. BIND_ENUM_CONSTANT(ARRAY_MAX);
  391. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_UNORM);
  392. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA8_SNORM);
  393. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_HALF);
  394. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_HALF);
  395. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_R_FLOAT);
  396. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RG_FLOAT);
  397. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGB_FLOAT);
  398. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_RGBA_FLOAT);
  399. BIND_ENUM_CONSTANT(ARRAY_CUSTOM_MAX);
  400. BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
  401. BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
  402. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
  403. BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
  404. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
  405. BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
  406. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM0);
  407. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM1);
  408. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM2);
  409. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM3);
  410. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
  411. BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  412. BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
  413. BIND_ENUM_CONSTANT(ARRAY_FORMAT_BLEND_SHAPE_MASK);
  414. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM_BASE);
  415. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM_BITS);
  416. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM0_SHIFT);
  417. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM1_SHIFT);
  418. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM2_SHIFT);
  419. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM3_SHIFT);
  420. BIND_ENUM_CONSTANT(ARRAY_FORMAT_CUSTOM_MASK);
  421. BIND_ENUM_CONSTANT(ARRAY_COMPRESS_FLAGS_BASE);
  422. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_2D_VERTICES);
  423. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_DYNAMIC_UPDATE);
  424. BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_8_BONE_WEIGHTS);
  425. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_NORMALIZED);
  426. BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_RELATIVE);
  427. }
  428. void Mesh::clear_cache() const {
  429. triangle_mesh.unref();
  430. debug_lines.clear();
  431. }
  432. Vector<Ref<Shape3D>> Mesh::convex_decompose(const ConvexDecompositionSettings &p_settings) const {
  433. ERR_FAIL_COND_V(!convex_decomposition_function, Vector<Ref<Shape3D>>());
  434. Ref<TriangleMesh> tm = generate_triangle_mesh();
  435. ERR_FAIL_COND_V(!tm.is_valid(), Vector<Ref<Shape3D>>());
  436. const Vector<TriangleMesh::Triangle> &triangles = tm->get_triangles();
  437. int triangle_count = triangles.size();
  438. Vector<uint32_t> indices;
  439. {
  440. indices.resize(triangle_count * 3);
  441. uint32_t *w = indices.ptrw();
  442. for (int i = 0; i < triangle_count; i++) {
  443. for (int j = 0; j < 3; j++) {
  444. w[i * 3 + j] = triangles[i].indices[j];
  445. }
  446. }
  447. }
  448. const Vector<Vector3> &vertices = tm->get_vertices();
  449. int vertex_count = vertices.size();
  450. Vector<Vector<Vector3>> decomposed = convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), triangle_count, p_settings, nullptr);
  451. Vector<Ref<Shape3D>> ret;
  452. for (int i = 0; i < decomposed.size(); i++) {
  453. Ref<ConvexPolygonShape3D> shape;
  454. shape.instantiate();
  455. shape->set_points(decomposed[i]);
  456. ret.push_back(shape);
  457. }
  458. return ret;
  459. }
  460. int Mesh::get_builtin_bind_pose_count() const {
  461. return 0;
  462. }
  463. Transform3D Mesh::get_builtin_bind_pose(int p_index) const {
  464. return Transform3D();
  465. }
  466. Mesh::Mesh() {
  467. }
  468. enum OldArrayType {
  469. OLD_ARRAY_VERTEX,
  470. OLD_ARRAY_NORMAL,
  471. OLD_ARRAY_TANGENT,
  472. OLD_ARRAY_COLOR,
  473. OLD_ARRAY_TEX_UV,
  474. OLD_ARRAY_TEX_UV2,
  475. OLD_ARRAY_BONES,
  476. OLD_ARRAY_WEIGHTS,
  477. OLD_ARRAY_INDEX,
  478. OLD_ARRAY_MAX,
  479. };
  480. enum OldArrayFormat {
  481. /* OLD_ARRAY FORMAT FLAGS */
  482. OLD_ARRAY_FORMAT_VERTEX = 1 << OLD_ARRAY_VERTEX, // mandatory
  483. OLD_ARRAY_FORMAT_NORMAL = 1 << OLD_ARRAY_NORMAL,
  484. OLD_ARRAY_FORMAT_TANGENT = 1 << OLD_ARRAY_TANGENT,
  485. OLD_ARRAY_FORMAT_COLOR = 1 << OLD_ARRAY_COLOR,
  486. OLD_ARRAY_FORMAT_TEX_UV = 1 << OLD_ARRAY_TEX_UV,
  487. OLD_ARRAY_FORMAT_TEX_UV2 = 1 << OLD_ARRAY_TEX_UV2,
  488. OLD_ARRAY_FORMAT_BONES = 1 << OLD_ARRAY_BONES,
  489. OLD_ARRAY_FORMAT_WEIGHTS = 1 << OLD_ARRAY_WEIGHTS,
  490. OLD_ARRAY_FORMAT_INDEX = 1 << OLD_ARRAY_INDEX,
  491. OLD_ARRAY_COMPRESS_BASE = (OLD_ARRAY_INDEX + 1),
  492. OLD_ARRAY_COMPRESS_VERTEX = 1 << (OLD_ARRAY_VERTEX + OLD_ARRAY_COMPRESS_BASE), // mandatory
  493. OLD_ARRAY_COMPRESS_NORMAL = 1 << (OLD_ARRAY_NORMAL + OLD_ARRAY_COMPRESS_BASE),
  494. OLD_ARRAY_COMPRESS_TANGENT = 1 << (OLD_ARRAY_TANGENT + OLD_ARRAY_COMPRESS_BASE),
  495. OLD_ARRAY_COMPRESS_COLOR = 1 << (OLD_ARRAY_COLOR + OLD_ARRAY_COMPRESS_BASE),
  496. OLD_ARRAY_COMPRESS_TEX_UV = 1 << (OLD_ARRAY_TEX_UV + OLD_ARRAY_COMPRESS_BASE),
  497. OLD_ARRAY_COMPRESS_TEX_UV2 = 1 << (OLD_ARRAY_TEX_UV2 + OLD_ARRAY_COMPRESS_BASE),
  498. OLD_ARRAY_COMPRESS_BONES = 1 << (OLD_ARRAY_BONES + OLD_ARRAY_COMPRESS_BASE),
  499. OLD_ARRAY_COMPRESS_WEIGHTS = 1 << (OLD_ARRAY_WEIGHTS + OLD_ARRAY_COMPRESS_BASE),
  500. OLD_ARRAY_COMPRESS_INDEX = 1 << (OLD_ARRAY_INDEX + OLD_ARRAY_COMPRESS_BASE),
  501. OLD_ARRAY_FLAG_USE_2D_VERTICES = OLD_ARRAY_COMPRESS_INDEX << 1,
  502. OLD_ARRAY_FLAG_USE_16_BIT_BONES = OLD_ARRAY_COMPRESS_INDEX << 2,
  503. OLD_ARRAY_FLAG_USE_DYNAMIC_UPDATE = OLD_ARRAY_COMPRESS_INDEX << 3,
  504. };
  505. #ifndef DISABLE_DEPRECATED
  506. static Array _convert_old_array(const Array &p_old) {
  507. Array new_array;
  508. new_array.resize(Mesh::ARRAY_MAX);
  509. new_array[Mesh::ARRAY_VERTEX] = p_old[OLD_ARRAY_VERTEX];
  510. new_array[Mesh::ARRAY_NORMAL] = p_old[OLD_ARRAY_NORMAL];
  511. new_array[Mesh::ARRAY_TANGENT] = p_old[OLD_ARRAY_TANGENT];
  512. new_array[Mesh::ARRAY_COLOR] = p_old[OLD_ARRAY_COLOR];
  513. new_array[Mesh::ARRAY_TEX_UV] = p_old[OLD_ARRAY_TEX_UV];
  514. new_array[Mesh::ARRAY_TEX_UV2] = p_old[OLD_ARRAY_TEX_UV2];
  515. new_array[Mesh::ARRAY_BONES] = p_old[OLD_ARRAY_BONES];
  516. new_array[Mesh::ARRAY_WEIGHTS] = p_old[OLD_ARRAY_WEIGHTS];
  517. new_array[Mesh::ARRAY_INDEX] = p_old[OLD_ARRAY_INDEX];
  518. return new_array;
  519. }
  520. static Mesh::PrimitiveType _old_primitives[7] = {
  521. Mesh::PRIMITIVE_POINTS,
  522. Mesh::PRIMITIVE_LINES,
  523. Mesh::PRIMITIVE_LINE_STRIP,
  524. Mesh::PRIMITIVE_LINES,
  525. Mesh::PRIMITIVE_TRIANGLES,
  526. Mesh::PRIMITIVE_TRIANGLE_STRIP,
  527. Mesh::PRIMITIVE_TRIANGLE_STRIP
  528. };
  529. #endif // DISABLE_DEPRECATED
  530. void _fix_array_compatibility(const Vector<uint8_t> &p_src, uint32_t p_old_format, uint32_t p_new_format, uint32_t p_elements, Vector<uint8_t> &vertex_data, Vector<uint8_t> &attribute_data, Vector<uint8_t> &skin_data) {
  531. uint32_t dst_vertex_stride;
  532. uint32_t dst_attribute_stride;
  533. uint32_t dst_skin_stride;
  534. uint32_t dst_offsets[Mesh::ARRAY_MAX];
  535. RenderingServer::get_singleton()->mesh_surface_make_offsets_from_format(p_new_format & (~RS::ARRAY_FORMAT_INDEX), p_elements, 0, dst_offsets, dst_vertex_stride, dst_attribute_stride, dst_skin_stride);
  536. vertex_data.resize(dst_vertex_stride * p_elements);
  537. attribute_data.resize(dst_attribute_stride * p_elements);
  538. skin_data.resize(dst_skin_stride * p_elements);
  539. uint8_t *dst_vertex_ptr = vertex_data.ptrw();
  540. uint8_t *dst_attribute_ptr = attribute_data.ptrw();
  541. uint8_t *dst_skin_ptr = skin_data.ptrw();
  542. const uint8_t *src_vertex_ptr = p_src.ptr();
  543. uint32_t src_vertex_stride = p_src.size() / p_elements;
  544. uint32_t src_offset = 0;
  545. for (uint32_t j = 0; j < OLD_ARRAY_INDEX; j++) {
  546. if (!(p_old_format & (1 << j))) {
  547. continue;
  548. }
  549. switch (j) {
  550. case OLD_ARRAY_VERTEX: {
  551. if (p_old_format & OLD_ARRAY_FLAG_USE_2D_VERTICES) {
  552. if (p_old_format & OLD_ARRAY_COMPRESS_VERTEX) {
  553. for (uint32_t i = 0; i < p_elements; i++) {
  554. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride];
  555. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  556. dst[0] = Math::half_to_float(src[0]);
  557. dst[1] = Math::half_to_float(src[1]);
  558. }
  559. src_offset += sizeof(uint16_t) * 2;
  560. } else {
  561. for (uint32_t i = 0; i < p_elements; i++) {
  562. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride];
  563. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  564. dst[0] = src[0];
  565. dst[1] = src[1];
  566. }
  567. src_offset += sizeof(float) * 2;
  568. }
  569. } else {
  570. if (p_old_format & OLD_ARRAY_COMPRESS_VERTEX) {
  571. for (uint32_t i = 0; i < p_elements; i++) {
  572. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride];
  573. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  574. dst[0] = Math::half_to_float(src[0]);
  575. dst[1] = Math::half_to_float(src[1]);
  576. dst[2] = Math::half_to_float(src[2]);
  577. }
  578. src_offset += sizeof(uint16_t) * 4; //+pad
  579. } else {
  580. for (uint32_t i = 0; i < p_elements; i++) {
  581. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride];
  582. float *dst = (float *)&dst_vertex_ptr[i * dst_vertex_stride];
  583. dst[0] = src[0];
  584. dst[1] = src[1];
  585. dst[2] = src[2];
  586. }
  587. src_offset += sizeof(float) * 3;
  588. }
  589. }
  590. } break;
  591. case OLD_ARRAY_NORMAL: {
  592. if (p_old_format & OLD_ARRAY_COMPRESS_NORMAL) {
  593. const float multiplier = 1.f / 127.f * 1023.0f;
  594. for (uint32_t i = 0; i < p_elements; i++) {
  595. const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  596. uint32_t *dst = (uint32_t *)&dst_vertex_ptr[i * dst_vertex_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  597. *dst = 0;
  598. *dst |= CLAMP(int(src[0] * multiplier), 0, 1023);
  599. *dst |= CLAMP(int(src[1] * multiplier), 0, 1023) << 10;
  600. *dst |= CLAMP(int(src[2] * multiplier), 0, 1023) << 20;
  601. }
  602. src_offset += sizeof(uint32_t);
  603. } else {
  604. for (uint32_t i = 0; i < p_elements; i++) {
  605. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  606. uint32_t *dst = (uint32_t *)&dst_vertex_ptr[i * dst_vertex_stride + dst_offsets[Mesh::ARRAY_NORMAL]];
  607. *dst = 0;
  608. *dst |= CLAMP(int(src[0] * 1023.0), 0, 1023);
  609. *dst |= CLAMP(int(src[1] * 1023.0), 0, 1023) << 10;
  610. *dst |= CLAMP(int(src[2] * 1023.0), 0, 1023) << 20;
  611. }
  612. src_offset += sizeof(float) * 3;
  613. }
  614. } break;
  615. case OLD_ARRAY_TANGENT: {
  616. if (p_old_format & OLD_ARRAY_COMPRESS_TANGENT) {
  617. const float multiplier = 1.f / 127.f * 1023.0f;
  618. for (uint32_t i = 0; i < p_elements; i++) {
  619. const int8_t *src = (const int8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  620. uint32_t *dst = (uint32_t *)&dst_vertex_ptr[i * dst_vertex_stride + dst_offsets[Mesh::ARRAY_TANGENT]];
  621. *dst = 0;
  622. *dst |= CLAMP(int(src[0] * multiplier), 0, 1023);
  623. *dst |= CLAMP(int(src[1] * multiplier), 0, 1023) << 10;
  624. *dst |= CLAMP(int(src[2] * multiplier), 0, 1023) << 20;
  625. if (src[3] > 0) {
  626. *dst |= 3 << 30;
  627. }
  628. }
  629. src_offset += sizeof(uint32_t);
  630. } else {
  631. for (uint32_t i = 0; i < p_elements; i++) {
  632. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  633. uint32_t *dst = (uint32_t *)&dst_vertex_ptr[i * dst_vertex_stride + dst_offsets[Mesh::ARRAY_TANGENT]];
  634. *dst = 0;
  635. *dst |= CLAMP(int(src[0] * 1023.0), 0, 1023);
  636. *dst |= CLAMP(int(src[1] * 1023.0), 0, 1023) << 10;
  637. *dst |= CLAMP(int(src[2] * 1023.0), 0, 1023) << 20;
  638. if (src[3] > 0) {
  639. *dst |= 3 << 30;
  640. }
  641. }
  642. src_offset += sizeof(float) * 4;
  643. }
  644. } break;
  645. case OLD_ARRAY_COLOR: {
  646. if (p_old_format & OLD_ARRAY_COMPRESS_COLOR) {
  647. for (uint32_t i = 0; i < p_elements; i++) {
  648. const uint32_t *src = (const uint32_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  649. uint32_t *dst = (uint32_t *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_COLOR]];
  650. *dst = *src;
  651. }
  652. src_offset += sizeof(uint32_t);
  653. } else {
  654. for (uint32_t i = 0; i < p_elements; i++) {
  655. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  656. uint8_t *dst = (uint8_t *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_COLOR]];
  657. dst[0] = uint8_t(CLAMP(src[0] * 255.0, 0.0, 255.0));
  658. dst[1] = uint8_t(CLAMP(src[1] * 255.0, 0.0, 255.0));
  659. dst[2] = uint8_t(CLAMP(src[2] * 255.0, 0.0, 255.0));
  660. dst[3] = uint8_t(CLAMP(src[3] * 255.0, 0.0, 255.0));
  661. }
  662. src_offset += sizeof(float) * 4;
  663. }
  664. } break;
  665. case OLD_ARRAY_TEX_UV: {
  666. if (p_old_format & OLD_ARRAY_COMPRESS_TEX_UV) {
  667. for (uint32_t i = 0; i < p_elements; i++) {
  668. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  669. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV]];
  670. dst[0] = Math::half_to_float(src[0]);
  671. dst[1] = Math::half_to_float(src[1]);
  672. }
  673. src_offset += sizeof(uint16_t) * 2;
  674. } else {
  675. for (uint32_t i = 0; i < p_elements; i++) {
  676. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  677. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV]];
  678. dst[0] = src[0];
  679. dst[1] = src[1];
  680. }
  681. src_offset += sizeof(float) * 2;
  682. }
  683. } break;
  684. case OLD_ARRAY_TEX_UV2: {
  685. if (p_old_format & OLD_ARRAY_COMPRESS_TEX_UV2) {
  686. for (uint32_t i = 0; i < p_elements; i++) {
  687. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  688. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV2]];
  689. dst[0] = Math::half_to_float(src[0]);
  690. dst[1] = Math::half_to_float(src[1]);
  691. }
  692. src_offset += sizeof(uint16_t) * 2;
  693. } else {
  694. for (uint32_t i = 0; i < p_elements; i++) {
  695. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  696. float *dst = (float *)&dst_attribute_ptr[i * dst_attribute_stride + dst_offsets[Mesh::ARRAY_TEX_UV2]];
  697. dst[0] = src[0];
  698. dst[1] = src[1];
  699. }
  700. src_offset += sizeof(float) * 2;
  701. }
  702. } break;
  703. case OLD_ARRAY_BONES: {
  704. if (p_old_format & OLD_ARRAY_FLAG_USE_16_BIT_BONES) {
  705. for (uint32_t i = 0; i < p_elements; i++) {
  706. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  707. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_BONES]];
  708. dst[0] = src[0];
  709. dst[1] = src[1];
  710. dst[2] = src[2];
  711. dst[3] = src[3];
  712. }
  713. src_offset += sizeof(uint16_t) * 4;
  714. } else {
  715. for (uint32_t i = 0; i < p_elements; i++) {
  716. const uint8_t *src = (const uint8_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  717. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_BONES]];
  718. dst[0] = src[0];
  719. dst[1] = src[1];
  720. dst[2] = src[2];
  721. dst[3] = src[3];
  722. }
  723. src_offset += sizeof(uint8_t) * 4;
  724. }
  725. } break;
  726. case OLD_ARRAY_WEIGHTS: {
  727. if (p_old_format & OLD_ARRAY_COMPRESS_WEIGHTS) {
  728. for (uint32_t i = 0; i < p_elements; i++) {
  729. const uint16_t *src = (const uint16_t *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  730. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_WEIGHTS]];
  731. dst[0] = src[0];
  732. dst[1] = src[1];
  733. dst[2] = src[2];
  734. dst[3] = src[3];
  735. }
  736. src_offset += sizeof(uint16_t) * 4;
  737. } else {
  738. for (uint32_t i = 0; i < p_elements; i++) {
  739. const float *src = (const float *)&src_vertex_ptr[i * src_vertex_stride + src_offset];
  740. uint16_t *dst = (uint16_t *)&dst_skin_ptr[i * dst_skin_stride + dst_offsets[Mesh::ARRAY_WEIGHTS]];
  741. dst[0] = uint16_t(CLAMP(src[0] * 65535.0, 0, 65535.0));
  742. dst[1] = uint16_t(CLAMP(src[1] * 65535.0, 0, 65535.0));
  743. dst[2] = uint16_t(CLAMP(src[2] * 65535.0, 0, 65535.0));
  744. dst[3] = uint16_t(CLAMP(src[3] * 65535.0, 0, 65535.0));
  745. }
  746. src_offset += sizeof(float) * 4;
  747. }
  748. } break;
  749. default: {
  750. }
  751. }
  752. }
  753. }
  754. bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) {
  755. String sname = p_name;
  756. if (sname.begins_with("surface_")) {
  757. int sl = sname.find("/");
  758. if (sl == -1) {
  759. return false;
  760. }
  761. int idx = sname.substr(8, sl - 8).to_int() - 1;
  762. String what = sname.get_slicec('/', 1);
  763. if (what == "material") {
  764. surface_set_material(idx, p_value);
  765. } else if (what == "name") {
  766. surface_set_name(idx, p_value);
  767. }
  768. return true;
  769. }
  770. #ifndef DISABLE_DEPRECATED
  771. // Kept for compatibility from 3.x to 4.0.
  772. if (!sname.begins_with("surfaces")) {
  773. return false;
  774. }
  775. WARN_DEPRECATED_MSG("Mesh uses old surface format, which is deprecated (and loads slower). Consider re-importing or re-saving the scene.");
  776. int idx = sname.get_slicec('/', 1).to_int();
  777. String what = sname.get_slicec('/', 2);
  778. if (idx == surfaces.size()) {
  779. //create
  780. Dictionary d = p_value;
  781. ERR_FAIL_COND_V(!d.has("primitive"), false);
  782. if (d.has("arrays")) {
  783. //oldest format (2.x)
  784. ERR_FAIL_COND_V(!d.has("morph_arrays"), false);
  785. Array morph_arrays = d["morph_arrays"];
  786. for (int i = 0; i < morph_arrays.size(); i++) {
  787. morph_arrays[i] = _convert_old_array(morph_arrays[i]);
  788. }
  789. add_surface_from_arrays(_old_primitives[int(d["primitive"])], _convert_old_array(d["arrays"]), morph_arrays);
  790. } else if (d.has("array_data")) {
  791. //print_line("array data (old style");
  792. //older format (3.x)
  793. Vector<uint8_t> array_data = d["array_data"];
  794. Vector<uint8_t> array_index_data;
  795. if (d.has("array_index_data")) {
  796. array_index_data = d["array_index_data"];
  797. }
  798. ERR_FAIL_COND_V(!d.has("format"), false);
  799. uint32_t old_format = d["format"];
  800. uint32_t primitive = d["primitive"];
  801. primitive = _old_primitives[primitive]; //compatibility
  802. ERR_FAIL_COND_V(!d.has("vertex_count"), false);
  803. int vertex_count = d["vertex_count"];
  804. uint32_t new_format = ARRAY_FORMAT_VERTEX;
  805. if (old_format & OLD_ARRAY_FORMAT_NORMAL) {
  806. new_format |= ARRAY_FORMAT_NORMAL;
  807. }
  808. if (old_format & OLD_ARRAY_FORMAT_TANGENT) {
  809. new_format |= ARRAY_FORMAT_TANGENT;
  810. }
  811. if (old_format & OLD_ARRAY_FORMAT_COLOR) {
  812. new_format |= ARRAY_FORMAT_COLOR;
  813. }
  814. if (old_format & OLD_ARRAY_FORMAT_TEX_UV) {
  815. new_format |= ARRAY_FORMAT_TEX_UV;
  816. }
  817. if (old_format & OLD_ARRAY_FORMAT_TEX_UV2) {
  818. new_format |= ARRAY_FORMAT_TEX_UV2;
  819. }
  820. if (old_format & OLD_ARRAY_FORMAT_BONES) {
  821. new_format |= ARRAY_FORMAT_BONES;
  822. }
  823. if (old_format & OLD_ARRAY_FORMAT_WEIGHTS) {
  824. new_format |= ARRAY_FORMAT_WEIGHTS;
  825. }
  826. if (old_format & OLD_ARRAY_FORMAT_INDEX) {
  827. new_format |= ARRAY_FORMAT_INDEX;
  828. }
  829. if (old_format & OLD_ARRAY_FLAG_USE_2D_VERTICES) {
  830. new_format |= OLD_ARRAY_FLAG_USE_2D_VERTICES;
  831. }
  832. Vector<uint8_t> vertex_array;
  833. Vector<uint8_t> attribute_array;
  834. Vector<uint8_t> skin_array;
  835. _fix_array_compatibility(array_data, old_format, new_format, vertex_count, vertex_array, attribute_array, skin_array);
  836. int index_count = 0;
  837. if (d.has("index_count")) {
  838. index_count = d["index_count"];
  839. }
  840. Vector<uint8_t> blend_shapes;
  841. if (d.has("blend_shape_data")) {
  842. Array blend_shape_data = d["blend_shape_data"];
  843. for (int i = 0; i < blend_shape_data.size(); i++) {
  844. Vector<uint8_t> blend_vertex_array;
  845. Vector<uint8_t> blend_attribute_array;
  846. Vector<uint8_t> blend_skin_array;
  847. Vector<uint8_t> shape = blend_shape_data[i];
  848. _fix_array_compatibility(shape, old_format, new_format, vertex_count, blend_vertex_array, blend_attribute_array, blend_skin_array);
  849. blend_shapes.append_array(blend_vertex_array);
  850. }
  851. }
  852. //clear unused flags
  853. print_line("format pre: " + itos(old_format));
  854. print_line("format post: " + itos(new_format));
  855. ERR_FAIL_COND_V(!d.has("aabb"), false);
  856. AABB aabb = d["aabb"];
  857. Vector<AABB> bone_aabb;
  858. if (d.has("skeleton_aabb")) {
  859. Array baabb = d["skeleton_aabb"];
  860. bone_aabb.resize(baabb.size());
  861. for (int i = 0; i < baabb.size(); i++) {
  862. bone_aabb.write[i] = baabb[i];
  863. }
  864. }
  865. add_surface(new_format, PrimitiveType(primitive), vertex_array, attribute_array, skin_array, vertex_count, array_index_data, index_count, aabb, blend_shapes, bone_aabb);
  866. } else {
  867. ERR_FAIL_V(false);
  868. }
  869. if (d.has("material")) {
  870. surface_set_material(idx, d["material"]);
  871. }
  872. if (d.has("name")) {
  873. surface_set_name(idx, d["name"]);
  874. }
  875. return true;
  876. }
  877. #endif // DISABLE_DEPRECATED
  878. return false;
  879. }
  880. void ArrayMesh::_set_blend_shape_names(const PackedStringArray &p_names) {
  881. ERR_FAIL_COND(surfaces.size() > 0);
  882. blend_shapes.resize(p_names.size());
  883. for (int i = 0; i < p_names.size(); i++) {
  884. blend_shapes.write[i] = p_names[i];
  885. }
  886. if (mesh.is_valid()) {
  887. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  888. }
  889. }
  890. PackedStringArray ArrayMesh::_get_blend_shape_names() const {
  891. PackedStringArray sarr;
  892. sarr.resize(blend_shapes.size());
  893. for (int i = 0; i < blend_shapes.size(); i++) {
  894. sarr.write[i] = blend_shapes[i];
  895. }
  896. return sarr;
  897. }
  898. Array ArrayMesh::_get_surfaces() const {
  899. if (mesh.is_null()) {
  900. return Array();
  901. }
  902. Array ret;
  903. for (int i = 0; i < surfaces.size(); i++) {
  904. RenderingServer::SurfaceData surface = RS::get_singleton()->mesh_get_surface(mesh, i);
  905. Dictionary data;
  906. data["format"] = surface.format;
  907. data["primitive"] = surface.primitive;
  908. data["vertex_data"] = surface.vertex_data;
  909. data["vertex_count"] = surface.vertex_count;
  910. if (surface.skin_data.size()) {
  911. data["skin_data"] = surface.skin_data;
  912. }
  913. if (surface.attribute_data.size()) {
  914. data["attribute_data"] = surface.attribute_data;
  915. }
  916. data["aabb"] = surface.aabb;
  917. if (surface.index_count) {
  918. data["index_data"] = surface.index_data;
  919. data["index_count"] = surface.index_count;
  920. };
  921. Array lods;
  922. for (int j = 0; j < surface.lods.size(); j++) {
  923. lods.push_back(surface.lods[j].edge_length);
  924. lods.push_back(surface.lods[j].index_data);
  925. }
  926. if (lods.size()) {
  927. data["lods"] = lods;
  928. }
  929. Array bone_aabbs;
  930. for (int j = 0; j < surface.bone_aabbs.size(); j++) {
  931. bone_aabbs.push_back(surface.bone_aabbs[j]);
  932. }
  933. if (bone_aabbs.size()) {
  934. data["bone_aabbs"] = bone_aabbs;
  935. }
  936. if (surface.blend_shape_data.size()) {
  937. data["blend_shapes"] = surface.blend_shape_data;
  938. }
  939. if (surfaces[i].material.is_valid()) {
  940. data["material"] = surfaces[i].material;
  941. }
  942. if (surfaces[i].name != String()) {
  943. data["name"] = surfaces[i].name;
  944. }
  945. if (surfaces[i].is_2d) {
  946. data["2d"] = true;
  947. }
  948. ret.push_back(data);
  949. }
  950. return ret;
  951. }
  952. void ArrayMesh::_create_if_empty() const {
  953. if (!mesh.is_valid()) {
  954. mesh = RS::get_singleton()->mesh_create();
  955. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode);
  956. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  957. }
  958. }
  959. void ArrayMesh::_set_surfaces(const Array &p_surfaces) {
  960. Vector<RS::SurfaceData> surface_data;
  961. Vector<Ref<Material>> surface_materials;
  962. Vector<String> surface_names;
  963. Vector<bool> surface_2d;
  964. for (int i = 0; i < p_surfaces.size(); i++) {
  965. RS::SurfaceData surface;
  966. Dictionary d = p_surfaces[i];
  967. ERR_FAIL_COND(!d.has("format"));
  968. ERR_FAIL_COND(!d.has("primitive"));
  969. ERR_FAIL_COND(!d.has("vertex_data"));
  970. ERR_FAIL_COND(!d.has("vertex_count"));
  971. ERR_FAIL_COND(!d.has("aabb"));
  972. surface.format = d["format"];
  973. surface.primitive = RS::PrimitiveType(int(d["primitive"]));
  974. surface.vertex_data = d["vertex_data"];
  975. surface.vertex_count = d["vertex_count"];
  976. if (d.has("attribute_data")) {
  977. surface.attribute_data = d["attribute_data"];
  978. }
  979. if (d.has("skin_data")) {
  980. surface.skin_data = d["skin_data"];
  981. }
  982. surface.aabb = d["aabb"];
  983. if (d.has("index_data")) {
  984. ERR_FAIL_COND(!d.has("index_count"));
  985. surface.index_data = d["index_data"];
  986. surface.index_count = d["index_count"];
  987. }
  988. if (d.has("lods")) {
  989. Array lods = d["lods"];
  990. ERR_FAIL_COND(lods.size() & 1); //must be even
  991. for (int j = 0; j < lods.size(); j += 2) {
  992. RS::SurfaceData::LOD lod;
  993. lod.edge_length = lods[j + 0];
  994. lod.index_data = lods[j + 1];
  995. surface.lods.push_back(lod);
  996. }
  997. }
  998. if (d.has("bone_aabbs")) {
  999. Array bone_aabbs = d["bone_aabbs"];
  1000. for (int j = 0; j < bone_aabbs.size(); j++) {
  1001. surface.bone_aabbs.push_back(bone_aabbs[j]);
  1002. }
  1003. }
  1004. if (d.has("blend_shapes")) {
  1005. surface.blend_shape_data = d["blend_shapes"];
  1006. }
  1007. Ref<Material> material;
  1008. if (d.has("material")) {
  1009. material = d["material"];
  1010. if (material.is_valid()) {
  1011. surface.material = material->get_rid();
  1012. }
  1013. }
  1014. String name;
  1015. if (d.has("name")) {
  1016. name = d["name"];
  1017. }
  1018. bool _2d = false;
  1019. if (d.has("2d")) {
  1020. _2d = d["2d"];
  1021. }
  1022. surface_data.push_back(surface);
  1023. surface_materials.push_back(material);
  1024. surface_names.push_back(name);
  1025. surface_2d.push_back(_2d);
  1026. }
  1027. if (mesh.is_valid()) {
  1028. //if mesh exists, it needs to be updated
  1029. RS::get_singleton()->mesh_clear(mesh);
  1030. for (int i = 0; i < surface_data.size(); i++) {
  1031. RS::get_singleton()->mesh_add_surface(mesh, surface_data[i]);
  1032. }
  1033. } else {
  1034. // if mesh does not exist (first time this is loaded, most likely),
  1035. // we can create it with a single call, which is a lot more efficient and thread friendly
  1036. mesh = RS::get_singleton()->mesh_create_from_surfaces(surface_data, blend_shapes.size());
  1037. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)blend_shape_mode);
  1038. }
  1039. surfaces.clear();
  1040. aabb = AABB();
  1041. for (int i = 0; i < surface_data.size(); i++) {
  1042. Surface s;
  1043. s.aabb = surface_data[i].aabb;
  1044. if (i == 0) {
  1045. aabb = s.aabb;
  1046. } else {
  1047. aabb.merge_with(s.aabb);
  1048. }
  1049. s.material = surface_materials[i];
  1050. s.is_2d = surface_2d[i];
  1051. s.name = surface_names[i];
  1052. s.format = surface_data[i].format;
  1053. s.primitive = PrimitiveType(surface_data[i].primitive);
  1054. s.array_length = surface_data[i].vertex_count;
  1055. s.index_array_length = surface_data[i].index_count;
  1056. surfaces.push_back(s);
  1057. }
  1058. }
  1059. bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const {
  1060. if (_is_generated()) {
  1061. return false;
  1062. }
  1063. String sname = p_name;
  1064. if (sname.begins_with("surface_")) {
  1065. int sl = sname.find("/");
  1066. if (sl == -1) {
  1067. return false;
  1068. }
  1069. int idx = sname.substr(8, sl - 8).to_int() - 1;
  1070. String what = sname.get_slicec('/', 1);
  1071. if (what == "material") {
  1072. r_ret = surface_get_material(idx);
  1073. } else if (what == "name") {
  1074. r_ret = surface_get_name(idx);
  1075. }
  1076. return true;
  1077. }
  1078. return true;
  1079. }
  1080. void ArrayMesh::reset_state() {
  1081. clear_surfaces();
  1082. clear_blend_shapes();
  1083. aabb = AABB();
  1084. blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
  1085. custom_aabb = AABB();
  1086. }
  1087. void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const {
  1088. if (_is_generated()) {
  1089. return;
  1090. }
  1091. for (int i = 0; i < surfaces.size(); i++) {
  1092. p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i + 1) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  1093. if (surfaces[i].is_2d) {
  1094. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial", PROPERTY_USAGE_EDITOR));
  1095. } else {
  1096. p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_EDITOR));
  1097. }
  1098. }
  1099. }
  1100. void ArrayMesh::_recompute_aabb() {
  1101. // regenerate AABB
  1102. aabb = AABB();
  1103. for (int i = 0; i < surfaces.size(); i++) {
  1104. if (i == 0) {
  1105. aabb = surfaces[i].aabb;
  1106. } else {
  1107. aabb.merge_with(surfaces[i].aabb);
  1108. }
  1109. }
  1110. }
  1111. #ifndef _MSC_VER
  1112. #warning need to add binding to add_surface using future MeshSurfaceData object
  1113. #endif
  1114. void ArrayMesh::add_surface(uint32_t p_format, PrimitiveType p_primitive, const Vector<uint8_t> &p_array, const Vector<uint8_t> &p_attribute_array, const Vector<uint8_t> &p_skin_array, int p_vertex_count, const Vector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<uint8_t> &p_blend_shape_data, const Vector<AABB> &p_bone_aabbs, const Vector<RS::SurfaceData::LOD> &p_lods) {
  1115. _create_if_empty();
  1116. Surface s;
  1117. s.aabb = p_aabb;
  1118. s.is_2d = p_format & ARRAY_FLAG_USE_2D_VERTICES;
  1119. s.primitive = p_primitive;
  1120. s.array_length = p_vertex_count;
  1121. s.index_array_length = p_index_count;
  1122. s.format = p_format;
  1123. surfaces.push_back(s);
  1124. _recompute_aabb();
  1125. RS::SurfaceData sd;
  1126. sd.format = p_format;
  1127. sd.primitive = RS::PrimitiveType(p_primitive);
  1128. sd.aabb = p_aabb;
  1129. sd.vertex_count = p_vertex_count;
  1130. sd.vertex_data = p_array;
  1131. sd.attribute_data = p_attribute_array;
  1132. sd.skin_data = p_skin_array;
  1133. sd.index_count = p_index_count;
  1134. sd.index_data = p_index_array;
  1135. sd.blend_shape_data = p_blend_shape_data;
  1136. sd.bone_aabbs = p_bone_aabbs;
  1137. sd.lods = p_lods;
  1138. RenderingServer::get_singleton()->mesh_add_surface(mesh, sd);
  1139. clear_cache();
  1140. notify_property_list_changed();
  1141. emit_changed();
  1142. }
  1143. void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, const Dictionary &p_lods, uint32_t p_flags) {
  1144. ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX);
  1145. RS::SurfaceData surface;
  1146. Error err = RS::get_singleton()->mesh_create_surface_data_from_arrays(&surface, (RenderingServer::PrimitiveType)p_primitive, p_arrays, p_blend_shapes, p_lods, p_flags);
  1147. ERR_FAIL_COND(err != OK);
  1148. /* Debug code.
  1149. print_line("format: " + itos(surface.format));
  1150. print_line("aabb: " + surface.aabb);
  1151. print_line("array size: " + itos(surface.vertex_data.size()));
  1152. print_line("vertex count: " + itos(surface.vertex_count));
  1153. print_line("index size: " + itos(surface.index_data.size()));
  1154. print_line("index count: " + itos(surface.index_count));
  1155. print_line("primitive: " + itos(surface.primitive));
  1156. */
  1157. add_surface(surface.format, PrimitiveType(surface.primitive), surface.vertex_data, surface.attribute_data, surface.skin_data, surface.vertex_count, surface.index_data, surface.index_count, surface.aabb, surface.blend_shape_data, surface.bone_aabbs, surface.lods);
  1158. }
  1159. Array ArrayMesh::surface_get_arrays(int p_surface) const {
  1160. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  1161. return RenderingServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
  1162. }
  1163. Array ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const {
  1164. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
  1165. return RenderingServer::get_singleton()->mesh_surface_get_blend_shape_arrays(mesh, p_surface);
  1166. }
  1167. Dictionary ArrayMesh::surface_get_lods(int p_surface) const {
  1168. ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Dictionary());
  1169. return RenderingServer::get_singleton()->mesh_surface_get_lods(mesh, p_surface);
  1170. }
  1171. int ArrayMesh::get_surface_count() const {
  1172. return surfaces.size();
  1173. }
  1174. void ArrayMesh::add_blend_shape(const StringName &p_name) {
  1175. ERR_FAIL_COND_MSG(surfaces.size(), "Can't add a shape key count if surfaces are already created.");
  1176. StringName name = p_name;
  1177. if (blend_shapes.find(name) != -1) {
  1178. int count = 2;
  1179. do {
  1180. name = String(p_name) + " " + itos(count);
  1181. count++;
  1182. } while (blend_shapes.find(name) != -1);
  1183. }
  1184. blend_shapes.push_back(name);
  1185. if (mesh.is_valid()) {
  1186. RS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
  1187. }
  1188. }
  1189. int ArrayMesh::get_blend_shape_count() const {
  1190. return blend_shapes.size();
  1191. }
  1192. StringName ArrayMesh::get_blend_shape_name(int p_index) const {
  1193. ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName());
  1194. return blend_shapes[p_index];
  1195. }
  1196. void ArrayMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
  1197. ERR_FAIL_INDEX(p_index, blend_shapes.size());
  1198. StringName name = p_name;
  1199. int found = blend_shapes.find(name);
  1200. if (found != -1 && found != p_index) {
  1201. int count = 2;
  1202. do {
  1203. name = String(p_name) + " " + itos(count);
  1204. count++;
  1205. } while (blend_shapes.find(name) != -1);
  1206. }
  1207. blend_shapes.write[p_index] = name;
  1208. }
  1209. void ArrayMesh::clear_blend_shapes() {
  1210. ERR_FAIL_COND_MSG(surfaces.size(), "Can't set shape key count if surfaces are already created.");
  1211. blend_shapes.clear();
  1212. if (mesh.is_valid()) {
  1213. RS::get_singleton()->mesh_set_blend_shape_count(mesh, 0);
  1214. }
  1215. }
  1216. void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) {
  1217. blend_shape_mode = p_mode;
  1218. if (mesh.is_valid()) {
  1219. RS::get_singleton()->mesh_set_blend_shape_mode(mesh, (RS::BlendShapeMode)p_mode);
  1220. }
  1221. }
  1222. ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const {
  1223. return blend_shape_mode;
  1224. }
  1225. int ArrayMesh::surface_get_array_len(int p_idx) const {
  1226. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  1227. return surfaces[p_idx].array_length;
  1228. }
  1229. int ArrayMesh::surface_get_array_index_len(int p_idx) const {
  1230. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
  1231. return surfaces[p_idx].index_array_length;
  1232. }
  1233. uint32_t ArrayMesh::surface_get_format(int p_idx) const {
  1234. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0);
  1235. return surfaces[p_idx].format;
  1236. }
  1237. ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const {
  1238. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES);
  1239. return surfaces[p_idx].primitive;
  1240. }
  1241. void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  1242. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1243. if (surfaces[p_idx].material == p_material) {
  1244. return;
  1245. }
  1246. surfaces.write[p_idx].material = p_material;
  1247. RenderingServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
  1248. emit_changed();
  1249. }
  1250. int ArrayMesh::surface_find_by_name(const String &p_name) const {
  1251. for (int i = 0; i < surfaces.size(); i++) {
  1252. if (surfaces[i].name == p_name) {
  1253. return i;
  1254. }
  1255. }
  1256. return -1;
  1257. }
  1258. void ArrayMesh::surface_set_name(int p_idx, const String &p_name) {
  1259. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1260. surfaces.write[p_idx].name = p_name;
  1261. emit_changed();
  1262. }
  1263. String ArrayMesh::surface_get_name(int p_idx) const {
  1264. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String());
  1265. return surfaces[p_idx].name;
  1266. }
  1267. void ArrayMesh::surface_update_vertex_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1268. ERR_FAIL_INDEX(p_surface, surfaces.size());
  1269. RS::get_singleton()->mesh_surface_update_vertex_region(mesh, p_surface, p_offset, p_data);
  1270. emit_changed();
  1271. }
  1272. void ArrayMesh::surface_update_attribute_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1273. ERR_FAIL_INDEX(p_surface, surfaces.size());
  1274. RS::get_singleton()->mesh_surface_update_attribute_region(mesh, p_surface, p_offset, p_data);
  1275. emit_changed();
  1276. }
  1277. void ArrayMesh::surface_update_skin_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1278. ERR_FAIL_INDEX(p_surface, surfaces.size());
  1279. RS::get_singleton()->mesh_surface_update_skin_region(mesh, p_surface, p_offset, p_data);
  1280. emit_changed();
  1281. }
  1282. void ArrayMesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) {
  1283. ERR_FAIL_INDEX(p_idx, surfaces.size());
  1284. surfaces.write[p_idx].aabb = p_aabb;
  1285. // set custom aabb too?
  1286. emit_changed();
  1287. }
  1288. Ref<Material> ArrayMesh::surface_get_material(int p_idx) const {
  1289. ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>());
  1290. return surfaces[p_idx].material;
  1291. }
  1292. RID ArrayMesh::get_rid() const {
  1293. _create_if_empty();
  1294. return mesh;
  1295. }
  1296. AABB ArrayMesh::get_aabb() const {
  1297. return aabb;
  1298. }
  1299. void ArrayMesh::clear_surfaces() {
  1300. if (!mesh.is_valid()) {
  1301. return;
  1302. }
  1303. RS::get_singleton()->mesh_clear(mesh);
  1304. surfaces.clear();
  1305. aabb = AABB();
  1306. }
  1307. void ArrayMesh::set_custom_aabb(const AABB &p_custom) {
  1308. _create_if_empty();
  1309. custom_aabb = p_custom;
  1310. RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  1311. emit_changed();
  1312. }
  1313. AABB ArrayMesh::get_custom_aabb() const {
  1314. return custom_aabb;
  1315. }
  1316. void ArrayMesh::regen_normal_maps() {
  1317. if (surfaces.size() == 0) {
  1318. return;
  1319. }
  1320. Vector<Ref<SurfaceTool>> surfs;
  1321. for (int i = 0; i < get_surface_count(); i++) {
  1322. Ref<SurfaceTool> st = memnew(SurfaceTool);
  1323. st->create_from(Ref<ArrayMesh>(this), i);
  1324. surfs.push_back(st);
  1325. }
  1326. clear_surfaces();
  1327. for (int i = 0; i < surfs.size(); i++) {
  1328. surfs.write[i]->generate_tangents();
  1329. surfs.write[i]->commit(Ref<ArrayMesh>(this));
  1330. }
  1331. }
  1332. //dirty hack
  1333. bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) = NULL;
  1334. struct ArrayMeshLightmapSurface {
  1335. Ref<Material> material;
  1336. LocalVector<SurfaceTool::Vertex> vertices;
  1337. Mesh::PrimitiveType primitive = Mesh::PrimitiveType::PRIMITIVE_MAX;
  1338. uint32_t format = 0;
  1339. };
  1340. Error ArrayMesh::lightmap_unwrap(const Transform3D &p_base_transform, float p_texel_size) {
  1341. Vector<uint8_t> null_cache;
  1342. return lightmap_unwrap_cached(p_base_transform, p_texel_size, null_cache, null_cache, false);
  1343. }
  1344. Error ArrayMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache, bool p_generate_cache) {
  1345. ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
  1346. ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");
  1347. LocalVector<float> vertices;
  1348. LocalVector<float> normals;
  1349. LocalVector<int> indices;
  1350. LocalVector<float> uv;
  1351. LocalVector<Pair<int, int>> uv_indices;
  1352. Vector<ArrayMeshLightmapSurface> lightmap_surfaces;
  1353. // Keep only the scale
  1354. Basis basis = p_base_transform.get_basis();
  1355. Vector3 scale = Vector3(basis.get_axis(0).length(), basis.get_axis(1).length(), basis.get_axis(2).length());
  1356. Transform3D transform;
  1357. transform.scale(scale);
  1358. Basis normal_basis = transform.basis.inverse().transposed();
  1359. for (int i = 0; i < get_surface_count(); i++) {
  1360. ArrayMeshLightmapSurface s;
  1361. s.primitive = surface_get_primitive_type(i);
  1362. ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap.");
  1363. s.format = surface_get_format(i);
  1364. ERR_FAIL_COND_V_MSG(!(s.format & ARRAY_FORMAT_NORMAL), ERR_UNAVAILABLE, "Normals are required for lightmap unwrap.");
  1365. Array arrays = surface_get_arrays(i);
  1366. s.material = surface_get_material(i);
  1367. SurfaceTool::create_vertex_array_from_triangle_arrays(arrays, s.vertices, &s.format);
  1368. PackedVector3Array rvertices = arrays[Mesh::ARRAY_VERTEX];
  1369. int vc = rvertices.size();
  1370. PackedVector3Array rnormals = arrays[Mesh::ARRAY_NORMAL];
  1371. int vertex_ofs = vertices.size() / 3;
  1372. vertices.resize((vertex_ofs + vc) * 3);
  1373. normals.resize((vertex_ofs + vc) * 3);
  1374. uv_indices.resize(vertex_ofs + vc);
  1375. for (int j = 0; j < vc; j++) {
  1376. Vector3 v = transform.xform(rvertices[j]);
  1377. Vector3 n = normal_basis.xform(rnormals[j]).normalized();
  1378. vertices[(j + vertex_ofs) * 3 + 0] = v.x;
  1379. vertices[(j + vertex_ofs) * 3 + 1] = v.y;
  1380. vertices[(j + vertex_ofs) * 3 + 2] = v.z;
  1381. normals[(j + vertex_ofs) * 3 + 0] = n.x;
  1382. normals[(j + vertex_ofs) * 3 + 1] = n.y;
  1383. normals[(j + vertex_ofs) * 3 + 2] = n.z;
  1384. uv_indices[j + vertex_ofs] = Pair<int, int>(i, j);
  1385. }
  1386. PackedInt32Array rindices = arrays[Mesh::ARRAY_INDEX];
  1387. int ic = rindices.size();
  1388. float eps = 1.19209290e-7F; // Taken from xatlas.h
  1389. if (ic == 0) {
  1390. for (int j = 0; j < vc / 3; j++) {
  1391. Vector3 p0 = transform.xform(rvertices[j * 3 + 0]);
  1392. Vector3 p1 = transform.xform(rvertices[j * 3 + 1]);
  1393. Vector3 p2 = transform.xform(rvertices[j * 3 + 2]);
  1394. if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {
  1395. continue;
  1396. }
  1397. indices.push_back(vertex_ofs + j * 3 + 0);
  1398. indices.push_back(vertex_ofs + j * 3 + 1);
  1399. indices.push_back(vertex_ofs + j * 3 + 2);
  1400. }
  1401. } else {
  1402. for (int j = 0; j < ic / 3; j++) {
  1403. Vector3 p0 = transform.xform(rvertices[rindices[j * 3 + 0]]);
  1404. Vector3 p1 = transform.xform(rvertices[rindices[j * 3 + 1]]);
  1405. Vector3 p2 = transform.xform(rvertices[rindices[j * 3 + 2]]);
  1406. if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {
  1407. continue;
  1408. }
  1409. indices.push_back(vertex_ofs + rindices[j * 3 + 0]);
  1410. indices.push_back(vertex_ofs + rindices[j * 3 + 1]);
  1411. indices.push_back(vertex_ofs + rindices[j * 3 + 2]);
  1412. }
  1413. }
  1414. lightmap_surfaces.push_back(s);
  1415. }
  1416. //unwrap
  1417. bool use_cache = p_generate_cache; // Used to request cache generation and to know if cache was used
  1418. uint8_t *gen_cache;
  1419. int gen_cache_size;
  1420. float *gen_uvs;
  1421. int *gen_vertices;
  1422. int *gen_indices;
  1423. int gen_vertex_count;
  1424. int gen_index_count;
  1425. int size_x;
  1426. int size_y;
  1427. bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), indices.size(), p_src_cache.ptr(), &use_cache, &gen_cache, &gen_cache_size, &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y);
  1428. if (!ok) {
  1429. return ERR_CANT_CREATE;
  1430. }
  1431. clear_surfaces();
  1432. //create surfacetools for each surface..
  1433. LocalVector<Ref<SurfaceTool>> surfaces_tools;
  1434. for (int i = 0; i < lightmap_surfaces.size(); i++) {
  1435. Ref<SurfaceTool> st;
  1436. st.instantiate();
  1437. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  1438. st->set_material(lightmap_surfaces[i].material);
  1439. surfaces_tools.push_back(st); //stay there
  1440. }
  1441. print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
  1442. //go through all indices
  1443. for (int i = 0; i < gen_index_count; i += 3) {
  1444. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], (int)uv_indices.size(), ERR_BUG);
  1445. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], (int)uv_indices.size(), ERR_BUG);
  1446. ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], (int)uv_indices.size(), ERR_BUG);
  1447. ERR_FAIL_COND_V(uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 1]]].first || uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG);
  1448. int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first;
  1449. for (int j = 0; j < 3; j++) {
  1450. SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second];
  1451. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_COLOR) {
  1452. surfaces_tools[surface]->set_color(v.color);
  1453. }
  1454. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
  1455. surfaces_tools[surface]->set_uv(v.uv);
  1456. }
  1457. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
  1458. surfaces_tools[surface]->set_normal(v.normal);
  1459. }
  1460. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
  1461. Plane t;
  1462. t.normal = v.tangent;
  1463. t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
  1464. surfaces_tools[surface]->set_tangent(t);
  1465. }
  1466. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_BONES) {
  1467. surfaces_tools[surface]->set_bones(v.bones);
  1468. }
  1469. if (lightmap_surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
  1470. surfaces_tools[surface]->set_weights(v.weights);
  1471. }
  1472. Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
  1473. surfaces_tools[surface]->set_uv2(uv2);
  1474. surfaces_tools[surface]->add_vertex(v.vertex);
  1475. }
  1476. }
  1477. //generate surfaces
  1478. for (unsigned int i = 0; i < surfaces_tools.size(); i++) {
  1479. surfaces_tools[i]->index();
  1480. surfaces_tools[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), lightmap_surfaces[i].format);
  1481. }
  1482. set_lightmap_size_hint(Size2(size_x, size_y));
  1483. if (gen_cache_size > 0) {
  1484. r_dst_cache.resize(gen_cache_size);
  1485. memcpy(r_dst_cache.ptrw(), gen_cache, gen_cache_size);
  1486. memfree(gen_cache);
  1487. }
  1488. if (!use_cache) {
  1489. // Cache was not used, free the buffers
  1490. memfree(gen_vertices);
  1491. memfree(gen_indices);
  1492. memfree(gen_uvs);
  1493. }
  1494. return OK;
  1495. }
  1496. void ArrayMesh::set_shadow_mesh(const Ref<ArrayMesh> &p_mesh) {
  1497. shadow_mesh = p_mesh;
  1498. if (shadow_mesh.is_valid()) {
  1499. RS::get_singleton()->mesh_set_shadow_mesh(mesh, shadow_mesh->get_rid());
  1500. } else {
  1501. RS::get_singleton()->mesh_set_shadow_mesh(mesh, RID());
  1502. }
  1503. }
  1504. Ref<ArrayMesh> ArrayMesh::get_shadow_mesh() const {
  1505. return shadow_mesh;
  1506. }
  1507. void ArrayMesh::_bind_methods() {
  1508. ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape);
  1509. ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count);
  1510. ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name);
  1511. ClassDB::bind_method(D_METHOD("set_blend_shape_name", "index", "name"), &ArrayMesh::set_blend_shape_name);
  1512. ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes);
  1513. ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode);
  1514. ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode);
  1515. ClassDB::bind_method(D_METHOD("add_surface_from_arrays", "primitive", "arrays", "blend_shapes", "lods", "compress_flags"), &ArrayMesh::add_surface_from_arrays, DEFVAL(Array()), DEFVAL(Dictionary()), DEFVAL(0));
  1516. ClassDB::bind_method(D_METHOD("clear_surfaces"), &ArrayMesh::clear_surfaces);
  1517. ClassDB::bind_method(D_METHOD("surface_update_vertex_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_vertex_region);
  1518. ClassDB::bind_method(D_METHOD("surface_update_attribute_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_attribute_region);
  1519. ClassDB::bind_method(D_METHOD("surface_update_skin_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_skin_region);
  1520. ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len);
  1521. ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len);
  1522. ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format);
  1523. ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type);
  1524. ClassDB::bind_method(D_METHOD("surface_find_by_name", "name"), &ArrayMesh::surface_find_by_name);
  1525. ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name);
  1526. ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name);
  1527. ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape);
  1528. ClassDB::bind_method(D_METHOD("create_convex_shape", "clean", "simplify"), &ArrayMesh::create_convex_shape, DEFVAL(true), DEFVAL(false));
  1529. ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline);
  1530. ClassDB::bind_method(D_METHOD("regen_normal_maps"), &ArrayMesh::regen_normal_maps);
  1531. ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normal_maps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  1532. ClassDB::bind_method(D_METHOD("lightmap_unwrap", "transform", "texel_size"), &ArrayMesh::lightmap_unwrap);
  1533. ClassDB::set_method_flags(get_class_static(), _scs_create("lightmap_unwrap"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  1534. ClassDB::bind_method(D_METHOD("get_faces"), &ArrayMesh::get_faces);
  1535. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh);
  1536. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb);
  1537. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb);
  1538. ClassDB::bind_method(D_METHOD("set_shadow_mesh", "mesh"), &ArrayMesh::set_shadow_mesh);
  1539. ClassDB::bind_method(D_METHOD("get_shadow_mesh"), &ArrayMesh::get_shadow_mesh);
  1540. ClassDB::bind_method(D_METHOD("_set_blend_shape_names", "blend_shape_names"), &ArrayMesh::_set_blend_shape_names);
  1541. ClassDB::bind_method(D_METHOD("_get_blend_shape_names"), &ArrayMesh::_get_blend_shape_names);
  1542. ClassDB::bind_method(D_METHOD("_set_surfaces", "surfaces"), &ArrayMesh::_set_surfaces);
  1543. ClassDB::bind_method(D_METHOD("_get_surfaces"), &ArrayMesh::_get_surfaces);
  1544. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "_blend_shape_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_blend_shape_names", "_get_blend_shape_names");
  1545. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_surfaces", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_surfaces", "_get_surfaces");
  1546. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_shape_mode", PROPERTY_HINT_ENUM, "Normalized,Relative"), "set_blend_shape_mode", "get_blend_shape_mode");
  1547. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
  1548. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shadow_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ArrayMesh"), "set_shadow_mesh", "get_shadow_mesh");
  1549. }
  1550. void ArrayMesh::reload_from_file() {
  1551. RenderingServer::get_singleton()->mesh_clear(mesh);
  1552. surfaces.clear();
  1553. clear_blend_shapes();
  1554. clear_cache();
  1555. Resource::reload_from_file();
  1556. notify_property_list_changed();
  1557. }
  1558. ArrayMesh::ArrayMesh() {
  1559. //mesh is now created on demand
  1560. //mesh = RenderingServer::get_singleton()->mesh_create();
  1561. }
  1562. ArrayMesh::~ArrayMesh() {
  1563. if (mesh.is_valid()) {
  1564. RenderingServer::get_singleton()->free(mesh);
  1565. }
  1566. }