Skip to content

Commit

Permalink
Fix crash when using the Image node with a non-square image
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Aug 2, 2024
1 parent cdadba3 commit 23b548e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Primarily developped with Godot 4.3.
- Fixes
- `VoxelBlockyModelMesh`: Fixed materials present directly in the mesh resource were not applied (only overrides in the model or on the terrain were applied)
- `VoxelBlockyType`: Fixed configuration warning about missing variants when there is a base model specified
- `VoxelGeneratorGraph`: Fixed crash when using the `Image` node with a non-square image
- `VoxelStreamSQLite`:
- Fixed `set_key_cache_enabled(true)` caused nothing to load
- Fixed slow loading when the database path contains `res://` or `user://`
Expand Down
2 changes: 1 addition & 1 deletion generators/graph/nodes/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void register_image_nodes(Span<NodeType> types) {
const Image &im = *p.image;
// Cache image size to reduce API calls in GDExtension
const int w = im.get_width();
const int h = im.get_width();
const int h = im.get_height();
// TODO Optimized path for most used formats, `get_pixel` is kinda slow
if (p.filter == FILTER_NEAREST) {
for (uint32_t i = 0; i < out.size; ++i) {
Expand Down
1 change: 1 addition & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void run_voxel_tests() {
VOXEL_TEST(test_voxel_graph_image);
VOXEL_TEST(test_voxel_graph_many_weight_outputs);
VOXEL_TEST(test_voxel_graph_many_subdivisions);
VOXEL_TEST(test_voxel_graph_non_square_image);
VOXEL_TEST(test_island_finder);
VOXEL_TEST(test_unordered_remove_if);
VOXEL_TEST(test_instance_data_serialization);
Expand Down
33 changes: 33 additions & 0 deletions tests/voxel/test_voxel_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2189,4 +2189,37 @@ void test_voxel_graph_many_subdivisions() {
generator->generate_block(VoxelGenerator::VoxelQueryData{ vb, Vector3i(0, 0, 0), 0 });
}

void test_voxel_graph_non_square_image() {
// There was a bug where the Image node was using with for X and Y instead of using height for Y.

Ref<VoxelGeneratorGraph> generator;
generator.instantiate();
{
VoxelGraphFunction &g = **generator->get_main_function();

const uint32_t n_x = g.create_node(VoxelGraphFunction::NODE_INPUT_X);
const uint32_t n_y = g.create_node(VoxelGraphFunction::NODE_INPUT_Y);
const uint32_t n_z = g.create_node(VoxelGraphFunction::NODE_INPUT_Z);
const uint32_t n_image = g.create_node(VoxelGraphFunction::NODE_IMAGE_2D);
const uint32_t n_add = g.create_node(VoxelGraphFunction::NODE_ADD);
const uint32_t n_out_sdf = g.create_node(VoxelGraphFunction::NODE_OUTPUT_SDF);

Ref<Image> image = Image::create_empty(400, 300, false, Image::FORMAT_R8);
image->fill(Color(1, 0, 0));
g.set_node_param(n_image, 0, image);

g.add_connection(n_x, 0, n_image, 0);
g.add_connection(n_z, 0, n_image, 1);
g.add_connection(n_y, 0, n_add, 0);
g.add_connection(n_image, 0, n_add, 1);
g.add_connection(n_add, 0, n_out_sdf, 0);

CompilationResult result = generator->compile(false);
ZN_TEST_ASSERT(result.success);
}

const VoxelSingleValue sd = generator->generate_single(Vector3i(405, 2, 305), VoxelBuffer::CHANNEL_SDF);
ZN_TEST_ASSERT(sd.f > 2.9f && sd.f < 3.1);
}

} // namespace zylann::voxel::tests
1 change: 1 addition & 0 deletions tests/voxel/test_voxel_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void test_voxel_graph_image();
void test_voxel_graph_many_weight_outputs();
void test_image_range_grid();
void test_voxel_graph_many_subdivisions();
void test_voxel_graph_non_square_image();

} // namespace zylann::voxel::tests

Expand Down

0 comments on commit 23b548e

Please sign in to comment.