You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试分块游戏世界瓦片错位问题

游戏瓦片分块渲染错位问题排查求助

我在将游戏世界划分为分块结构的代码中遇到瓦片渲染错位问题,噪声数据本身是正确的(此前使用相同数据的方案运行正常),希望能找出Bug所在。

组件定义

每个瓦片包含sprite、chunk_position和chunk_offset组件,定义如下:

/**
 * @brief Represents the position of a chunk within the world relative to other chunks.
 *
 * The chunk position specifies the coordinates (x, y) of a chunk within the world. Each chunk is a
 * 32x32 array of tiles, and the chunk position ranges between (0, 0) and (255, 255).
 */
struct chunk_position
{
    i8 x = 0;
    i8 y = 0;
};

/**
 * @brief Represents the offset of a tile within a chunk.
 *
 * The chunk offset specifies the position of a tile within a chunk. The offset ranges between
 * (0, 0) and (31, 31) and is relative to the bottom-left corner of the chunk.
 */
struct chunk_offset
{
    i8 x = 0;
    i8 y = 0;
};

世界生成代码

void sandbox_scene::create_chunked_demo_world() noexcept
{
    log::info("sandbox_scene::create_chunked_demo_world");

    FastNoise::SmartNode<> const fn_generator = FastNoise::NewFromEncodedNodeTree("CgACAAAAAAAAAAAAAIA/");

    entity const camera_entity = create_entity();
    detail::create_and_configure_camera_entity(camera_entity);

    std::vector<f32> noise_map(4194304);
    fn_generator->GenUniformGrid2D(noise_map.data(), 0, 0, 2048, 2048, 0.15F, 1339);
    clamp_noise_values(noise_map);
    std::reverse(noise_map.begin(), noise_map.end());

    auto const total_chunks = static_cast<const fl::usize>(64 * 64);
    auto const chunk_size   = static_cast<const fl::usize>(32 * 32);

    for (usize chunk_index = 0; chunk_index < total_chunks; chunk_index++)
    {
        i8 const chunk_x = static_cast<i8>(chunk_index % 64);
        i8 const chunk_y = static_cast<i8>(chunk_index / 64);

        for (usize chunk_offset_index = 0; chunk_offset_index < chunk_size; chunk_offset_index++)
        {
            i8 const chunk_offset_x = static_cast<i8>(chunk_offset_index % 32);
            i8 const chunk_offset_y = static_cast<i8>(chunk_offset_index / 32);

            entity tile_entity = create_entity();
            tile_entity.add_component<struct chunk_position>(chunk_x, chunk_y);
            tile_entity.add_component<struct chunk_offset>(chunk_offset_x, chunk_offset_y);

            f32 const noise_value = noise_map.back();
            noise_map.pop_back();
            glm::vec4 const colour = get_colour_for_value(noise_value);

            sprite* sprite_component        = &tile_entity.add_component<struct sprite>();
            sprite_component->texture       = nullptr;
            sprite_component->tint_color    = colour;
            sprite_component->tiling_factor = 1.0F;
        }
    }
}

渲染代码

m_current_scene->m_registry.view<struct sprite, struct chunk_position, struct chunk_offset>().each([](auto, struct sprite& sprite, struct chunk_position& position, struct chunk_offset& offset) {
    f32 const absolute_x = static_cast<f32>(position.x) * 32.0F + static_cast<f32>(offset.x);
    f32 const absolute_y = static_cast<f32>(position.y) * 32.0F + static_cast<f32>(offset.y);

    glm::vec3 const translation = {absolute_x, absolute_y, 0.0F};
    glm::mat4 const transform   = glm::translate(glm::mat4(1.0F), translation);

    renderer_2d::draw_sprite(transform, sprite);
});

问题表现

目前渲染出的瓦片存在明显错位,以下是效果截图:

内容的提问来源于stack exchange,提问作者Milan Dierick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 02:49:54