问题描述
像素着色器未正确使用纹理。我正在我的像素着色器中加载两个单独的纹理。但是,大多数情况下,我只能显示 Texture1,即使在我尝试显示 Texture2 时也是如此。
Texture2D Texture1;
Texture2D Texture2;
SamplerState ss;
float4 main(float4 position: SV_POSITION,float4 color: COLOR,float2 texCoord: TEXCOORD) : SV_TARGET
{
float4 color1 = Texture1.Sample(ss,texCoord);
float4 color2 = Texture2.Sample(ss,texCoord);
// even though color2 is specified here,color1 ie Texture1 is still displayed
float4 finalColor = color2;
return finalColor;
}
上面的代码总是显示纹理 1(砖块): texture 1
奇怪的部分是,我可以显示第二个纹理,但前提是我将像素着色器修改为:
float4 main(float4 position: SV_POSITION,texCoord);
float4 finalColor = {0,0};
if (color1.z != color2.z) {
// no idea why,but setting finalColor to color2 outside of this if statement
// doesn't work.
finalColor = color2;
}
return finalColor;
}
这将显示第二个纹理(木头):texture 2
这是加载纹理的代码:
// load the texture
HRESULT hr = CreateWICTextureFromFile(m_dev.Get(),nullptr,L"bricks.png",&m_texture1,0);
hr = CreateWICTextureFromFile(m_dev.Get(),L"wood.png",&m_texture2,0);
...
m_devCon->PSSetShaderResources(0,1,m_texture1.GetAddressOf());
m_devCon->PSSetShaderResources(1,m_texture2.GetAddressOf());
这里发生了什么?我知道两个纹理都加载到像素着色器中的事实,因为如果我使用这个奇怪的技巧,我可以获得第二个纹理。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)