test_arraymesh.h 16 KB

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