【发布时间】:2022-01-04 09:16:22
【问题描述】:
我想加载天空球,我知道如何使用 dds 文件来完成,但我想尝试从 6 个单独的纹理文件中完成。 我遇到的问题是,当我加载纹理立方体时,只有 3 个单独的纹理可见,其他 3 个不可见。我将向您展示我的代码以及它在 Nsight 中的外观。我现在只使用 6 个不同的统一颜色 png 文件,它们的大小都是 512x512。
std::vector<std::string> paths = { "../Resources/Textures/posX.png", "../Resources/Textures/negX.png",
"../Resources/Textures/posY.png",
"../Resources/Textures/negY.png",
"../Resources/Textures/posZ.png", "../Resources/Textures/negZ.png" };
ID3D11Texture2D* cubeTexture = NULL;
WRL::ComPtr<ID3D11ShaderResourceView> shaderResourceView = NULL;
//Description of each face
D3D11_TEXTURE2D_DESC texDesc = {};
texDesc.Width = 512;
texDesc.Height = 512;
texDesc.MipLevels = 1;
texDesc.ArraySize = 6;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.CPUAccessFlags = 0;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
//The Shader Resource view description
D3D11_SHADER_RESOURCE_VIEW_DESC SMViewDesc = {};
SMViewDesc.Format = texDesc.Format;
SMViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
SMViewDesc.TextureCube.MipLevels = texDesc.MipLevels;
SMViewDesc.TextureCube.MostDetailedMip = 0;
D3D11_SUBRESOURCE_DATA pData[6] = {};
for (int i = 0; i < 6; i++)
{
ID3D11Resource* res = nullptr;
std::wstring pathWString(paths[j].begin(), paths[j].end());
HRESULT hr = DirectX::CreateWICTextureFromFileEx(Renderer::getDevice(), pathWString.c_str(), 0, D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ, 0,
WIC_LOADER_FLAGS::WIC_LOADER_DEFAULT,
&res, 0);
assert(SUCCEEDED(hr));
D3D11_MAPPED_SUBRESOURCE destRes = {};
Renderer::getContext()->Map(res, 0, D3D11_MAP_READ, 0, &destRes);
pData[i].pSysMem = destRes.pData;
pData[i].SysMemPitch = destRes.RowPitch;
pData[i].SysMemSlicePitch = destRes.DepthPitch;
Renderer::getContext()->Unmap(res, 0);
RELEASE_COM(res);
}
Renderer::getDevice()->CreateTexture2D(&texDesc, &pData[0], &cubeTexture);
Renderer::getDevice()->CreateShaderResourceView(cubeTexture, &SMViewDesc, shaderResourceView.GetAddressOf());
在阅读文档时,它说子资源应该与 mip 级别有关。 如果我循环 9 次而不是 6 次,则显示其他 3 张图像而不是当前的 3 张图像。所有 6 张图像都应该具有唯一的颜色。
我在代码中所做的是创建一个 Texture2D 描述、一个着色器资源视图描述,然后我尝试使用 WIC 从导入的图像中获取数据,我将其放入资源中,然后将其映射到子资源结构。 查看子资源的地址时,所有 6 个始终是唯一的,因此它们似乎正确加载到纹理中,我尝试在行间距周围移动,改变图像的大小,但它似乎只影响纹理立方体内的单个图像,如果您理解我的意思,它似乎不会绕过重复项。
非常感谢任何帮助。
【问题讨论】:
-
这里的 HLSL 代码是什么?
-
很基本,没什么特别的:“VS” INPUT_PS_SKY 输出; float4x4 mvp = mul(g_world, g_vp); output.pos = mul(float4(input.pos, 1), mvp).xyww;输出.uv = 输入.pos; output.posW = mul(float4(input.pos, 1), g_world).xyz;输出.index = 0;返回输出; PS "return cubeMap.Sample(sampleWrap, input.uv);"
-
Cube-maps 使用三个索引值,而不是两个。它应该是一个
float3值。有关示例,请参见 EnvironmentMapEffect。 -
如果你指的是 uv,那么我使用的是 3,uvw,来自 pos 坐标。
-
return cubeMap.Sample(sampleWrap, input.uvw);
标签: directx-11 skybox