| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/resources/3d/height_map_shape_3d.h" |
| 34 |
#include "scene/resources/image_texture.h" |
| 35 |
|
| 36 |
#include "tests/test_macros.h" |
| 37 |
#include "tests/test_utils.h" |
| 38 |
|
| 39 |
namespace TestHeightMapShape3D { |
| 40 |
|
| 41 |
TEST_CASE("[SceneTree][HeightMapShape3D] Constructor") { |
| 42 |
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D); |
| 43 |
CHECK(height_map_shape->get_map_width() == 2); |
| 44 |
CHECK(height_map_shape->get_map_depth() == 2); |
| 45 |
CHECK(height_map_shape->get_map_data().size() == 4); |
| 46 |
CHECK(height_map_shape->get_min_height() == 0.0); |
| 47 |
CHECK(height_map_shape->get_max_height() == 0.0); |
| 48 |
} |
| 49 |
|
| 50 |
TEST_CASE("[SceneTree][HeightMapShape3D] set_map_width and get_map_width") { |
| 51 |
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D); |
| 52 |
height_map_shape->set_map_width(10); |
| 53 |
CHECK(height_map_shape->get_map_width() == 10); |
| 54 |
} |
| 55 |
|
| 56 |
TEST_CASE("[SceneTree][HeightMapShape3D] set_map_depth and get_map_depth") { |
| 57 |
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D); |
| 58 |
height_map_shape->set_map_depth(15); |
| 59 |
CHECK(height_map_shape->get_map_depth() == 15); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_CASE("[SceneTree][HeightMapShape3D] set_map_data and get_map_data") { |
| 63 |
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D); |
| 64 |
Vector<real_t> map_data; |
| 65 |
map_data.push_back(1.0); |
| 66 |
map_data.push_back(2.0); |
| 67 |
height_map_shape->set_map_data(map_data); |
| 68 |
CHECK(height_map_shape->get_map_data().size() == 4.0); |
| 69 |
CHECK(height_map_shape->get_map_data()[0] == 0.0); |
| 70 |
CHECK(height_map_shape->get_map_data()[1] == 0.0); |
| 71 |
} |
| 72 |
|
| 73 |
TEST_CASE("[SceneTree][HeightMapShape3D] get_min_height") { |
| 74 |
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D); |
| 75 |
height_map_shape->set_map_width(3); |
| 76 |
height_map_shape->set_map_depth(1); |
| 77 |
height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 }); |
| 78 |
CHECK(height_map_shape->get_min_height() == 0.5); |
| 79 |
} |
| 80 |
|
| 81 |
TEST_CASE("[SceneTree][HeightMapShape3D] get_max_height") { |
| 82 |
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D); |
| 83 |
height_map_shape->set_map_width(3); |
| 84 |
height_map_shape->set_map_depth(1); |
| 85 |
height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 }); |
| 86 |
CHECK(height_map_shape->get_max_height() == 2.0); |
| 87 |
} |
| 88 |
|
| 89 |
TEST_CASE("[SceneTree][HeightMapShape3D] update_map_data_from_image") { |
| 90 |
|
| 91 |
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D); |
| 92 |
|
| 93 |
|
| 94 |
Vector<uint8_t> image_data; |
| 95 |
image_data.push_back(0); |
| 96 |
image_data.push_back(128); |
| 97 |
image_data.push_back(255); |
| 98 |
image_data.push_back(64); |
| 99 |
|
| 100 |
Ref<Image> image = memnew(Image); |
| 101 |
image->set_data(2, 2, false, Image::FORMAT_R8, image_data); |
| 102 |
|
| 103 |
height_map_shape->update_map_data_from_image(image, 0.0, 10.0); |
| 104 |
|
| 105 |
|
| 106 |
Vector<real_t> expected_map_data = { 0.0, 5.0, 10.0, 2.5 }; |
| 107 |
Vector<real_t> actual_map_data = height_map_shape->get_map_data(); |
| 108 |
real_t tolerance = 0.1; |
| 109 |
|
| 110 |
for (int i = 0; i < expected_map_data.size(); ++i) { |
| 111 |
CHECK(Math::abs(actual_map_data[i] - expected_map_data[i]) < tolerance); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
CHECK(height_map_shape->get_min_height() == 0.0); |
| 116 |
CHECK(height_map_shape->get_max_height() == 10.0); |
| 117 |
} |
| 118 |
|
| 119 |
} |