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