test_arraymesh.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**************************************************************************/
  2. /* test_arraymesh.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #include "scene/resources/3d/primitive_meshes.h"
  32. #include "scene/resources/mesh.h"
  33. #include "tests/test_macros.h"
  34. namespace TestArrayMesh {
  35. TEST_CASE("[SceneTree][ArrayMesh] Adding and modifying blendshapes.") {
  36. Ref<ArrayMesh> mesh;
  37. mesh.instantiate();
  38. StringName name_a{ "ShapeA" };
  39. StringName name_b{ "ShapeB" };
  40. SUBCASE("Adding a blend shape to the mesh before a surface is added.") {
  41. mesh->add_blend_shape(name_a);
  42. mesh->add_blend_shape(name_b);
  43. CHECK(mesh->get_blend_shape_name(0) == name_a);
  44. CHECK(mesh->get_blend_shape_name(1) == name_b);
  45. }
  46. SUBCASE("Add same blend shape multiple times appends name with number.") {
  47. mesh->add_blend_shape(name_a);
  48. mesh->add_blend_shape(name_a);
  49. mesh->add_blend_shape(name_a);
  50. CHECK(mesh->get_blend_shape_name(0) == "ShapeA");
  51. bool all_different = (static_cast<String>(mesh->get_blend_shape_name(0)) != static_cast<String>(mesh->get_blend_shape_name(1))) &&
  52. (static_cast<String>(mesh->get_blend_shape_name(1)) != static_cast<String>(mesh->get_blend_shape_name(2))) &&
  53. (static_cast<String>(mesh->get_blend_shape_name(0)) != static_cast<String>(mesh->get_blend_shape_name(2)));
  54. bool all_have_name = static_cast<String>(mesh->get_blend_shape_name(1)).contains("ShapeA") &&
  55. static_cast<String>(mesh->get_blend_shape_name(2)).contains("ShapeA");
  56. CHECK((all_different && all_have_name));
  57. }
  58. SUBCASE("ArrayMesh keeps correct count of number of blend shapes") {
  59. mesh->add_blend_shape(name_a);
  60. mesh->add_blend_shape(name_a);
  61. mesh->add_blend_shape(name_b);
  62. mesh->add_blend_shape(name_b);
  63. mesh->add_blend_shape(name_b);
  64. REQUIRE(mesh->get_blend_shape_count() == 5);
  65. }
  66. SUBCASE("Adding blend shape after surface is added causes error") {
  67. Ref<CylinderMesh> cylinder;
  68. cylinder.instantiate();
  69. Array cylinder_array;
  70. cylinder_array.resize(Mesh::ARRAY_MAX);
  71. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  72. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  73. ERR_PRINT_OFF
  74. mesh->add_blend_shape(name_a);
  75. ERR_PRINT_ON
  76. CHECK(mesh->get_blend_shape_count() == 0);
  77. }
  78. SUBCASE("Adding blend shapes once all surfaces have been removed is allowed") {
  79. Ref<CylinderMesh> cylinder;
  80. cylinder.instantiate();
  81. Array cylinder_array;
  82. cylinder_array.resize(Mesh::ARRAY_MAX);
  83. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  84. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  85. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  86. mesh->surface_remove(0);
  87. ERR_PRINT_OFF
  88. mesh->add_blend_shape(name_a);
  89. ERR_PRINT_ON
  90. CHECK(mesh->get_blend_shape_count() == 0);
  91. mesh->surface_remove(0);
  92. mesh->add_blend_shape(name_a);
  93. CHECK(mesh->get_blend_shape_count() == 1);
  94. }
  95. SUBCASE("Change blend shape name after adding.") {
  96. mesh->add_blend_shape(name_a);
  97. mesh->set_blend_shape_name(0, name_b);
  98. CHECK(mesh->get_blend_shape_name(0) == name_b);
  99. }
  100. SUBCASE("Change blend shape name to the name of one already there, should append number to end") {
  101. mesh->add_blend_shape(name_a);
  102. mesh->add_blend_shape(name_b);
  103. mesh->set_blend_shape_name(0, name_b);
  104. String name_string = mesh->get_blend_shape_name(0);
  105. CHECK(name_string.contains("ShapeB"));
  106. CHECK(name_string.length() > static_cast<String>(name_b).size());
  107. }
  108. SUBCASE("Clear all blend shapes before surface has been added.") {
  109. mesh->add_blend_shape(name_a);
  110. mesh->add_blend_shape(name_b);
  111. CHECK(mesh->get_blend_shape_count() == 2);
  112. mesh->clear_blend_shapes();
  113. CHECK(mesh->get_blend_shape_count() == 0);
  114. }
  115. SUBCASE("Clearing all blend shapes once all surfaces have been removed is allowed") {
  116. mesh->add_blend_shape(name_a);
  117. mesh->add_blend_shape(name_b);
  118. Ref<CylinderMesh> cylinder;
  119. cylinder.instantiate();
  120. Array cylinder_array;
  121. cylinder_array.resize(Mesh::ARRAY_MAX);
  122. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  123. Array blend_shape;
  124. blend_shape.resize(Mesh::ARRAY_MAX);
  125. blend_shape[Mesh::ARRAY_VERTEX] = cylinder_array[Mesh::ARRAY_VERTEX];
  126. blend_shape[Mesh::ARRAY_NORMAL] = cylinder_array[Mesh::ARRAY_NORMAL];
  127. blend_shape[Mesh::ARRAY_TANGENT] = cylinder_array[Mesh::ARRAY_TANGENT];
  128. Array blend_shapes = { blend_shape, blend_shape };
  129. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, blend_shapes);
  130. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, blend_shapes);
  131. mesh->surface_remove(0);
  132. ERR_PRINT_OFF
  133. mesh->clear_blend_shapes();
  134. ERR_PRINT_ON
  135. CHECK(mesh->get_blend_shape_count() == 2);
  136. mesh->surface_remove(0);
  137. mesh->clear_blend_shapes();
  138. CHECK(mesh->get_blend_shape_count() == 0);
  139. }
  140. SUBCASE("Can't add surface with incorrect number of blend shapes.") {
  141. mesh->add_blend_shape(name_a);
  142. mesh->add_blend_shape(name_b);
  143. Array cylinder_array;
  144. ERR_PRINT_OFF
  145. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  146. ERR_PRINT_ON
  147. CHECK(mesh->get_surface_count() == 0);
  148. }
  149. SUBCASE("Can't clear blend shapes after surface had been added.") {
  150. mesh->add_blend_shape(name_a);
  151. mesh->add_blend_shape(name_b);
  152. Ref<CylinderMesh> cylinder;
  153. cylinder.instantiate();
  154. Array cylinder_array;
  155. cylinder_array.resize(Mesh::ARRAY_MAX);
  156. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  157. Array blend_shape;
  158. blend_shape.resize(Mesh::ARRAY_MAX);
  159. blend_shape[Mesh::ARRAY_VERTEX] = cylinder_array[Mesh::ARRAY_VERTEX];
  160. blend_shape[Mesh::ARRAY_NORMAL] = cylinder_array[Mesh::ARRAY_NORMAL];
  161. blend_shape[Mesh::ARRAY_TANGENT] = cylinder_array[Mesh::ARRAY_TANGENT];
  162. Array blend_shapes = { blend_shape, blend_shape };
  163. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, blend_shapes);
  164. ERR_PRINT_OFF
  165. mesh->clear_blend_shapes();
  166. ERR_PRINT_ON
  167. CHECK(mesh->get_blend_shape_count() == 2);
  168. }
  169. SUBCASE("Set the blend shape mode of ArrayMesh and underlying mesh RID.") {
  170. mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_RELATIVE);
  171. CHECK(mesh->get_blend_shape_mode() == Mesh::BLEND_SHAPE_MODE_RELATIVE);
  172. }
  173. }
  174. TEST_CASE("[SceneTree][ArrayMesh] Surface metadata tests.") {
  175. Ref<ArrayMesh> mesh;
  176. mesh.instantiate();
  177. Ref<CylinderMesh> cylinder;
  178. cylinder.instantiate();
  179. Array cylinder_array;
  180. cylinder_array.resize(Mesh::ARRAY_MAX);
  181. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  182. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  183. Ref<BoxMesh> box;
  184. box.instantiate();
  185. Array box_array;
  186. box_array.resize(Mesh::ARRAY_MAX);
  187. box->create_mesh_array(box_array, Vector3(2.f, 1.2f, 1.6f));
  188. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, box_array);
  189. SUBCASE("Add 2 surfaces and count the number of surfaces in the mesh.") {
  190. REQUIRE(mesh->get_surface_count() == 2);
  191. }
  192. SUBCASE("Get the surface array from mesh.") {
  193. REQUIRE(mesh->surface_get_arrays(0)[0] == cylinder_array[0]);
  194. REQUIRE(mesh->surface_get_arrays(1)[0] == box_array[0]);
  195. }
  196. SUBCASE("Get the array length of a particular surface.") {
  197. CHECK(mesh->surface_get_array_len(0) == static_cast<Vector<Vector3>>(cylinder_array[RenderingServer::ARRAY_VERTEX]).size());
  198. CHECK(mesh->surface_get_array_len(1) == static_cast<Vector<Vector3>>(box_array[RenderingServer::ARRAY_VERTEX]).size());
  199. }
  200. SUBCASE("Get the index array length of a particular surface.") {
  201. CHECK(mesh->surface_get_array_index_len(0) == static_cast<Vector<Vector3>>(cylinder_array[RenderingServer::ARRAY_INDEX]).size());
  202. CHECK(mesh->surface_get_array_index_len(1) == static_cast<Vector<Vector3>>(box_array[RenderingServer::ARRAY_INDEX]).size());
  203. }
  204. SUBCASE("Get correct primitive type") {
  205. CHECK(mesh->surface_get_primitive_type(0) == Mesh::PRIMITIVE_TRIANGLES);
  206. CHECK(mesh->surface_get_primitive_type(1) == Mesh::PRIMITIVE_TRIANGLES);
  207. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, box_array);
  208. CHECK(mesh->surface_get_primitive_type(2) == Mesh::PRIMITIVE_TRIANGLE_STRIP);
  209. }
  210. SUBCASE("Returns correct format for the mesh") {
  211. int format = RS::ARRAY_FORMAT_BLEND_SHAPE_MASK | RS::ARRAY_FORMAT_TEX_UV | RS::ARRAY_FORMAT_INDEX;
  212. CHECK((mesh->surface_get_format(0) & format) != 0);
  213. CHECK((mesh->surface_get_format(1) & format) != 0);
  214. }
  215. SUBCASE("Set a surface name and retrieve it by name.") {
  216. mesh->surface_set_name(0, "surf1");
  217. CHECK(mesh->surface_find_by_name("surf1") == 0);
  218. CHECK(mesh->surface_get_name(0) == "surf1");
  219. }
  220. SUBCASE("Set material to two different surfaces.") {
  221. Ref<Material> mat;
  222. mat.instantiate();
  223. mesh->surface_set_material(0, mat);
  224. CHECK(mesh->surface_get_material(0) == mat);
  225. mesh->surface_set_material(1, mat);
  226. CHECK(mesh->surface_get_material(1) == mat);
  227. }
  228. SUBCASE("Set same material multiple times doesn't change material of surface.") {
  229. Ref<Material> mat;
  230. mat.instantiate();
  231. mesh->surface_set_material(0, mat);
  232. mesh->surface_set_material(0, mat);
  233. mesh->surface_set_material(0, mat);
  234. CHECK(mesh->surface_get_material(0) == mat);
  235. }
  236. SUBCASE("Set material of surface then change to different material.") {
  237. Ref<Material> mat1;
  238. mat1.instantiate();
  239. Ref<Material> mat2;
  240. mat2.instantiate();
  241. mesh->surface_set_material(1, mat1);
  242. CHECK(mesh->surface_get_material(1) == mat1);
  243. mesh->surface_set_material(1, mat2);
  244. CHECK(mesh->surface_get_material(1) == mat2);
  245. }
  246. SUBCASE("Get the LOD of the mesh.") {
  247. Dictionary lod;
  248. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, TypedArray<Array>(), lod);
  249. CHECK(mesh->surface_get_lods(2) == lod);
  250. }
  251. SUBCASE("Get the blend shape arrays from the mesh.") {
  252. TypedArray<Array> blend;
  253. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, blend);
  254. CHECK(mesh->surface_get_blend_shape_arrays(2) == blend);
  255. }
  256. }
  257. TEST_CASE("[SceneTree][ArrayMesh] Get/Set mesh metadata and actions") {
  258. Ref<ArrayMesh> mesh;
  259. mesh.instantiate();
  260. Ref<CylinderMesh> cylinder;
  261. cylinder.instantiate();
  262. Array cylinder_array;
  263. cylinder_array.resize(Mesh::ARRAY_MAX);
  264. constexpr float cylinder_radius = 3.f;
  265. constexpr float cylinder_height = 5.f;
  266. cylinder->create_mesh_array(cylinder_array, cylinder_radius, cylinder_radius, cylinder_height);
  267. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  268. Ref<BoxMesh> box;
  269. box.instantiate();
  270. Array box_array;
  271. box_array.resize(Mesh::ARRAY_MAX);
  272. const Vector3 box_size = Vector3(2.f, 1.2f, 1.6f);
  273. box->create_mesh_array(box_array, box_size);
  274. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, box_array);
  275. SUBCASE("Set the shadow mesh.") {
  276. Ref<ArrayMesh> shadow;
  277. shadow.instantiate();
  278. mesh->set_shadow_mesh(shadow);
  279. CHECK(mesh->get_shadow_mesh() == shadow);
  280. }
  281. SUBCASE("Set the shadow mesh multiple times.") {
  282. Ref<ArrayMesh> shadow;
  283. shadow.instantiate();
  284. mesh->set_shadow_mesh(shadow);
  285. mesh->set_shadow_mesh(shadow);
  286. mesh->set_shadow_mesh(shadow);
  287. mesh->set_shadow_mesh(shadow);
  288. CHECK(mesh->get_shadow_mesh() == shadow);
  289. }
  290. SUBCASE("Set the same shadow mesh on multiple meshes.") {
  291. Ref<ArrayMesh> shadow;
  292. shadow.instantiate();
  293. Ref<ArrayMesh> mesh2;
  294. mesh2.instantiate();
  295. mesh->set_shadow_mesh(shadow);
  296. mesh2->set_shadow_mesh(shadow);
  297. CHECK(mesh->get_shadow_mesh() == shadow);
  298. CHECK(mesh2->get_shadow_mesh() == shadow);
  299. }
  300. SUBCASE("Set the shadow mesh and then change it.") {
  301. Ref<ArrayMesh> shadow;
  302. shadow.instantiate();
  303. mesh->set_shadow_mesh(shadow);
  304. CHECK(mesh->get_shadow_mesh() == shadow);
  305. Ref<ArrayMesh> shadow2;
  306. shadow2.instantiate();
  307. mesh->set_shadow_mesh(shadow2);
  308. CHECK(mesh->get_shadow_mesh() == shadow2);
  309. }
  310. SUBCASE("Set custom AABB.") {
  311. AABB bound;
  312. mesh->set_custom_aabb(bound);
  313. CHECK(mesh->get_custom_aabb() == bound);
  314. }
  315. SUBCASE("Set custom AABB multiple times.") {
  316. AABB bound;
  317. mesh->set_custom_aabb(bound);
  318. mesh->set_custom_aabb(bound);
  319. mesh->set_custom_aabb(bound);
  320. mesh->set_custom_aabb(bound);
  321. CHECK(mesh->get_custom_aabb() == bound);
  322. }
  323. SUBCASE("Set custom AABB then change to another AABB.") {
  324. AABB bound;
  325. AABB bound2;
  326. mesh->set_custom_aabb(bound);
  327. CHECK(mesh->get_custom_aabb() == bound);
  328. mesh->set_custom_aabb(bound2);
  329. CHECK(mesh->get_custom_aabb() == bound2);
  330. }
  331. SUBCASE("Clear all surfaces should leave zero count.") {
  332. mesh->clear_surfaces();
  333. CHECK(mesh->get_surface_count() == 0);
  334. }
  335. SUBCASE("Able to get correct mesh RID.") {
  336. RID rid = mesh->get_rid();
  337. CHECK(RS::get_singleton()->mesh_get_surface_count(rid) == 2);
  338. }
  339. SUBCASE("Create surface from raw SurfaceData data.") {
  340. RID mesh_rid = mesh->get_rid();
  341. RS::SurfaceData surface_data = RS::get_singleton()->mesh_get_surface(mesh_rid, 0);
  342. Ref<ArrayMesh> mesh2;
  343. mesh2.instantiate();
  344. mesh2->add_surface(surface_data.format, Mesh::PRIMITIVE_TRIANGLES, surface_data.vertex_data, surface_data.attribute_data,
  345. surface_data.skin_data, surface_data.vertex_count, surface_data.index_data, surface_data.index_count, surface_data.aabb);
  346. CHECK(mesh2->get_surface_count() == 1);
  347. CHECK(mesh2->surface_get_primitive_type(0) == Mesh::PRIMITIVE_TRIANGLES);
  348. CHECK((mesh2->surface_get_format(0) & surface_data.format) != 0);
  349. CHECK(mesh2->get_aabb().is_equal_approx(surface_data.aabb));
  350. }
  351. SUBCASE("Removing a surface decreases surface count.") {
  352. REQUIRE(mesh->get_surface_count() == 2);
  353. mesh->surface_remove(0);
  354. CHECK(mesh->get_surface_count() == 1);
  355. mesh->surface_remove(0);
  356. CHECK(mesh->get_surface_count() == 0);
  357. }
  358. SUBCASE("Remove the first surface and check the mesh's AABB.") {
  359. REQUIRE(mesh->get_surface_count() >= 1);
  360. mesh->surface_remove(0);
  361. const AABB box_aabb = AABB(-box_size / 2, box_size);
  362. CHECK(mesh->get_aabb().is_equal_approx(box_aabb));
  363. }
  364. SUBCASE("Remove the last surface and check the mesh's AABB.") {
  365. REQUIRE(mesh->get_surface_count() >= 1);
  366. mesh->surface_remove(mesh->get_surface_count() - 1);
  367. const AABB cylinder_aabb = AABB(Vector3(-cylinder_radius, -cylinder_height / 2, -cylinder_radius),
  368. Vector3(2 * cylinder_radius, cylinder_height, 2 * cylinder_radius));
  369. CHECK(mesh->get_aabb().is_equal_approx(cylinder_aabb));
  370. }
  371. SUBCASE("Remove all surfaces and check the mesh's AABB.") {
  372. while (mesh->get_surface_count()) {
  373. mesh->surface_remove(0);
  374. }
  375. CHECK(mesh->get_aabb() == AABB());
  376. }
  377. SUBCASE("Removing a non-existent surface causes error.") {
  378. ERR_PRINT_OFF
  379. mesh->surface_remove(42);
  380. ERR_PRINT_ON
  381. CHECK(mesh->get_surface_count() == 2);
  382. }
  383. }
  384. } // namespace TestArrayMesh