直接给 Assets<Texture> 资源添加纹理会导致 gpu_alloc 错误

问题描述

我正在从文件中加载纹理作为 Vec<u8> 并将该数据转换为纹理,当我将该纹理添加到资产资源以获取 Handle<Texture> 时,它会导致两个错误,第一个一个来自日志系统并说 gpu_alloc::block: Memory block wasn't deallocated 第二个(我假设是相关的)是 thread 'main' panicked at 'range end index 32896 out of range for slice of length 32768'。这一切似乎都源于这个代码

                 Ok(pak) => {
                    let mut assets : CurrentlyLoadedAssets = CurrentlyLoadedAssets::default();
                    for texture in pak.textures {

                        //Creates texture from data and loads it into the resource for a handle
                        let texture_asset: Texture = Texture::new(Extent3d { width: 32,height: 32,depth: 1 },TextureDimension::D2,texture.1,Rgba8UnormSrgb);

                        //This is causing problems
                        let texture_handle: Handle<Texture> = textures.add(texture_asset);

                        println!("Loading: {}",&texture.0);
                        assets.textures.insert(texture.0,texture_handle);
                        //Problems end here
                    }
                    println!("Done getting assets");
                    commands.insert_resource(assets);
                }

知道我需要改变什么来解决这个问题吗?

解决方法

我发现了问题。

let texture_asset: Texture = Texture::new(Extent3d { width: 32,height: 32,depth: 1 },TextureDimension::D2,texture.1,Rgba8UnormSrgb);

应该

texture_asset: Texture = Texture::new(Extent3d { width: 256,height: 256,Rgba8UnormSrgb);

不同之处在于宽度和高度必须与您尝试加载为 Vec<u8> 的图像相同,我使用的是 256x256 的测试图像而不是我要使用的 32x32 精灵使用。