mesh.cpp 61 KB

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