test_arraymesh.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/mesh.h"
  33. #include "scene/resources/primitive_meshes.h"
  34. #include "tests/test_macros.h"
  35. namespace TestArrayMesh {
  36. TEST_CASE("[SceneTree][ArrayMesh] Adding and modifying blendshapes.") {
  37. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  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 = memnew(CylinderMesh);
  68. Array cylinder_array{};
  69. cylinder_array.resize(Mesh::ARRAY_MAX);
  70. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  71. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  72. ERR_PRINT_OFF
  73. mesh->add_blend_shape(name_a);
  74. ERR_PRINT_ON
  75. CHECK(mesh->get_blend_shape_count() == 0);
  76. }
  77. SUBCASE("Change blend shape name after adding.") {
  78. mesh->add_blend_shape(name_a);
  79. mesh->set_blend_shape_name(0, name_b);
  80. CHECK(mesh->get_blend_shape_name(0) == name_b);
  81. }
  82. SUBCASE("Change blend shape name to the name of one already there, should append number to end") {
  83. mesh->add_blend_shape(name_a);
  84. mesh->add_blend_shape(name_b);
  85. mesh->set_blend_shape_name(0, name_b);
  86. String name_string = mesh->get_blend_shape_name(0);
  87. CHECK(name_string.contains("ShapeB"));
  88. CHECK(name_string.length() > static_cast<String>(name_b).size());
  89. }
  90. SUBCASE("Clear all blend shapes before surface has been added.") {
  91. mesh->add_blend_shape(name_a);
  92. mesh->add_blend_shape(name_b);
  93. CHECK(mesh->get_blend_shape_count() == 2);
  94. mesh->clear_blend_shapes();
  95. CHECK(mesh->get_blend_shape_count() == 0);
  96. }
  97. SUBCASE("Can't add surface with incorrect number of blend shapes.") {
  98. mesh->add_blend_shape(name_a);
  99. mesh->add_blend_shape(name_b);
  100. Ref<CylinderMesh> cylinder = memnew(CylinderMesh);
  101. Array cylinder_array{};
  102. ERR_PRINT_OFF
  103. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  104. ERR_PRINT_ON
  105. CHECK(mesh->get_surface_count() == 0);
  106. }
  107. SUBCASE("Can't clear blend shapes after surface had been added.") {
  108. mesh->add_blend_shape(name_a);
  109. mesh->add_blend_shape(name_b);
  110. Ref<CylinderMesh> cylinder = memnew(CylinderMesh);
  111. Array cylinder_array{};
  112. cylinder_array.resize(Mesh::ARRAY_MAX);
  113. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  114. Array blend_shape{};
  115. blend_shape.resize(Mesh::ARRAY_MAX);
  116. blend_shape[Mesh::ARRAY_VERTEX] = cylinder_array[Mesh::ARRAY_VERTEX];
  117. blend_shape[Mesh::ARRAY_NORMAL] = cylinder_array[Mesh::ARRAY_NORMAL];
  118. blend_shape[Mesh::ARRAY_TANGENT] = cylinder_array[Mesh::ARRAY_TANGENT];
  119. Array blend_shapes{};
  120. blend_shapes.push_back(blend_shape);
  121. blend_shapes.push_back(blend_shape);
  122. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, blend_shapes);
  123. ERR_PRINT_OFF
  124. mesh->clear_blend_shapes();
  125. ERR_PRINT_ON
  126. CHECK(mesh->get_blend_shape_count() == 2);
  127. }
  128. SUBCASE("Set the blend shape mode of ArrayMesh and underlying mesh RID.") {
  129. mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_RELATIVE);
  130. CHECK(mesh->get_blend_shape_mode() == Mesh::BLEND_SHAPE_MODE_RELATIVE);
  131. }
  132. }
  133. TEST_CASE("[SceneTree][ArrayMesh] Surface metadata tests.") {
  134. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  135. Ref<CylinderMesh> cylinder = memnew(CylinderMesh);
  136. Array cylinder_array{};
  137. cylinder_array.resize(Mesh::ARRAY_MAX);
  138. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  139. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  140. Ref<BoxMesh> box = memnew(BoxMesh);
  141. Array box_array{};
  142. box_array.resize(Mesh::ARRAY_MAX);
  143. box->create_mesh_array(box_array, Vector3(2.f, 1.2f, 1.6f));
  144. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, box_array);
  145. SUBCASE("Add 2 surfaces and count the number of surfaces in the mesh.") {
  146. REQUIRE(mesh->get_surface_count() == 2);
  147. }
  148. SUBCASE("Get the surface array from mesh.") {
  149. REQUIRE(mesh->surface_get_arrays(0)[0] == cylinder_array[0]);
  150. REQUIRE(mesh->surface_get_arrays(1)[0] == box_array[0]);
  151. }
  152. SUBCASE("Get the array length of a particular surface.") {
  153. CHECK(mesh->surface_get_array_len(0) == static_cast<Vector<Vector3>>(cylinder_array[RenderingServer::ARRAY_VERTEX]).size());
  154. CHECK(mesh->surface_get_array_len(1) == static_cast<Vector<Vector3>>(box_array[RenderingServer::ARRAY_VERTEX]).size());
  155. }
  156. SUBCASE("Get the index array length of a particular surface.") {
  157. CHECK(mesh->surface_get_array_index_len(0) == static_cast<Vector<Vector3>>(cylinder_array[RenderingServer::ARRAY_INDEX]).size());
  158. CHECK(mesh->surface_get_array_index_len(1) == static_cast<Vector<Vector3>>(box_array[RenderingServer::ARRAY_INDEX]).size());
  159. }
  160. SUBCASE("Get correct primitive type") {
  161. CHECK(mesh->surface_get_primitive_type(0) == Mesh::PRIMITIVE_TRIANGLES);
  162. CHECK(mesh->surface_get_primitive_type(1) == Mesh::PRIMITIVE_TRIANGLES);
  163. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, box_array);
  164. CHECK(mesh->surface_get_primitive_type(2) == Mesh::PRIMITIVE_TRIANGLE_STRIP);
  165. }
  166. SUBCASE("Returns correct format for the mesh") {
  167. int format = RS::ARRAY_FORMAT_BLEND_SHAPE_MASK | RS::ARRAY_FORMAT_TEX_UV | RS::ARRAY_FORMAT_INDEX;
  168. CHECK((mesh->surface_get_format(0) & format) != 0);
  169. CHECK((mesh->surface_get_format(1) & format) != 0);
  170. }
  171. SUBCASE("Set a surface name and retrieve it by name.") {
  172. mesh->surface_set_name(0, "surf1");
  173. CHECK(mesh->surface_find_by_name("surf1") == 0);
  174. CHECK(mesh->surface_get_name(0) == "surf1");
  175. }
  176. SUBCASE("Set material to two different surfaces.") {
  177. Ref<Material> mat = memnew(Material);
  178. mesh->surface_set_material(0, mat);
  179. CHECK(mesh->surface_get_material(0) == mat);
  180. mesh->surface_set_material(1, mat);
  181. CHECK(mesh->surface_get_material(1) == mat);
  182. }
  183. SUBCASE("Set same material multiple times doesn't change material of surface.") {
  184. Ref<Material> mat = memnew(Material);
  185. mesh->surface_set_material(0, mat);
  186. mesh->surface_set_material(0, mat);
  187. mesh->surface_set_material(0, mat);
  188. CHECK(mesh->surface_get_material(0) == mat);
  189. }
  190. SUBCASE("Set material of surface then change to different material.") {
  191. Ref<Material> mat1 = memnew(Material);
  192. Ref<Material> mat2 = memnew(Material);
  193. mesh->surface_set_material(1, mat1);
  194. CHECK(mesh->surface_get_material(1) == mat1);
  195. mesh->surface_set_material(1, mat2);
  196. CHECK(mesh->surface_get_material(1) == mat2);
  197. }
  198. SUBCASE("Get the LOD of the mesh.") {
  199. Dictionary lod{};
  200. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, TypedArray<Array>{}, lod);
  201. CHECK(mesh->surface_get_lods(2) == lod);
  202. }
  203. SUBCASE("Get the blend shape arrays from the mesh.") {
  204. TypedArray<Array> blend{};
  205. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array, blend);
  206. CHECK(mesh->surface_get_blend_shape_arrays(2) == blend);
  207. }
  208. }
  209. TEST_CASE("[SceneTree][ArrayMesh] Get/Set mesh metadata and actions") {
  210. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  211. Ref<CylinderMesh> cylinder = memnew(CylinderMesh);
  212. Array cylinder_array{};
  213. cylinder_array.resize(Mesh::ARRAY_MAX);
  214. cylinder->create_mesh_array(cylinder_array, 3.f, 3.f, 5.f);
  215. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
  216. Ref<BoxMesh> box = memnew(BoxMesh);
  217. Array box_array{};
  218. box_array.resize(Mesh::ARRAY_MAX);
  219. box->create_mesh_array(box_array, Vector3(2.f, 1.2f, 1.6f));
  220. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, box_array);
  221. SUBCASE("Set the shadow mesh.") {
  222. Ref<ArrayMesh> shadow = memnew(ArrayMesh);
  223. mesh->set_shadow_mesh(shadow);
  224. CHECK(mesh->get_shadow_mesh() == shadow);
  225. }
  226. SUBCASE("Set the shadow mesh multiple times.") {
  227. Ref<ArrayMesh> shadow = memnew(ArrayMesh);
  228. mesh->set_shadow_mesh(shadow);
  229. mesh->set_shadow_mesh(shadow);
  230. mesh->set_shadow_mesh(shadow);
  231. mesh->set_shadow_mesh(shadow);
  232. CHECK(mesh->get_shadow_mesh() == shadow);
  233. }
  234. SUBCASE("Set the same shadow mesh on multiple meshes.") {
  235. Ref<ArrayMesh> shadow = memnew(ArrayMesh);
  236. Ref<ArrayMesh> mesh2 = memnew(ArrayMesh);
  237. mesh->set_shadow_mesh(shadow);
  238. mesh2->set_shadow_mesh(shadow);
  239. CHECK(mesh->get_shadow_mesh() == shadow);
  240. CHECK(mesh2->get_shadow_mesh() == shadow);
  241. }
  242. SUBCASE("Set the shadow mesh and then change it.") {
  243. Ref<ArrayMesh> shadow = memnew(ArrayMesh);
  244. mesh->set_shadow_mesh(shadow);
  245. CHECK(mesh->get_shadow_mesh() == shadow);
  246. Ref<ArrayMesh> shadow2 = memnew(ArrayMesh);
  247. mesh->set_shadow_mesh(shadow2);
  248. CHECK(mesh->get_shadow_mesh() == shadow2);
  249. }
  250. SUBCASE("Set custom AABB.") {
  251. AABB bound{};
  252. mesh->set_custom_aabb(bound);
  253. CHECK(mesh->get_custom_aabb() == bound);
  254. }
  255. SUBCASE("Set custom AABB multiple times.") {
  256. AABB bound{};
  257. mesh->set_custom_aabb(bound);
  258. mesh->set_custom_aabb(bound);
  259. mesh->set_custom_aabb(bound);
  260. mesh->set_custom_aabb(bound);
  261. CHECK(mesh->get_custom_aabb() == bound);
  262. }
  263. SUBCASE("Set custom AABB then change to another AABB.") {
  264. AABB bound{};
  265. AABB bound2{};
  266. mesh->set_custom_aabb(bound);
  267. CHECK(mesh->get_custom_aabb() == bound);
  268. mesh->set_custom_aabb(bound2);
  269. CHECK(mesh->get_custom_aabb() == bound2);
  270. }
  271. SUBCASE("Clear all surfaces should leave zero count.") {
  272. mesh->clear_surfaces();
  273. CHECK(mesh->get_surface_count() == 0);
  274. }
  275. SUBCASE("Able to get correct mesh RID.") {
  276. RID rid = mesh->get_rid();
  277. CHECK(RS::get_singleton()->mesh_get_surface_count(rid) == 2);
  278. }
  279. SUBCASE("Create surface from raw SurfaceData data.") {
  280. RID mesh_rid = mesh->get_rid();
  281. RS::SurfaceData surface_data = RS::get_singleton()->mesh_get_surface(mesh_rid, 0);
  282. Ref<ArrayMesh> mesh2 = memnew(ArrayMesh);
  283. mesh2->add_surface(surface_data.format, Mesh::PRIMITIVE_TRIANGLES, surface_data.vertex_data, surface_data.attribute_data,
  284. surface_data.skin_data, surface_data.vertex_count, surface_data.index_data, surface_data.index_count, surface_data.aabb);
  285. CHECK(mesh2->get_surface_count() == 1);
  286. CHECK(mesh2->surface_get_primitive_type(0) == Mesh::PRIMITIVE_TRIANGLES);
  287. CHECK((mesh2->surface_get_format(0) & surface_data.format) != 0);
  288. CHECK(mesh2->get_aabb().is_equal_approx(surface_data.aabb));
  289. }
  290. }
  291. } // namespace TestArrayMesh
  292. #endif // TEST_ARRAYMESH_H