这是我第一篇关于堆栈溢出的帖子!我正在将 SlimDX 用于我的团队正在制作的游戏,但我遇到了问题。我正在尝试从 Color4 对象中的 RGBA 值创建一个 ShaderResourceView。我已经搜索了我的问题的答案,这就是我所得到的。
private ShaderResourceView GetTexture(Device device, int width, int height, Color4 color)
{
//create the texture
Texture2D texture = null;
Texture2DDescription desc2 = new Texture2DDescription();
desc2.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0);
desc2.Width = width;
desc2.Height = height;
desc2.MipLevels = 1;
desc2.ArraySize = 1;
desc2.Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm;
desc2.Usage = ResourceUsage.Dynamic;
desc2.BindFlags = BindFlags.ShaderResource;
desc2.CpuAccessFlags = CpuAccessFlags.Write;
texture = new Texture2D(device, desc2);
// fill the texture with rgba values
DataRectangle rect = texture.Map(0, MapMode.WriteDiscard, MapFlags.None);
if (rect.Data.CanWrite)
{
for (int row = 0; row < texture.Description.Height; row++)
{
int rowStart = row * rect.Pitch;
rect.Data.Seek(rowStart, System.IO.SeekOrigin.Begin);
for (int col = 0; col < texture.Description.Width; col++)
{
rect.Data.WriteByte((byte)color.Red);
rect.Data.WriteByte((byte)color.Green);
rect.Data.WriteByte((byte)color.Blue);
rect.Data.WriteByte((byte)color.Alpha);
}
}
}
texture.Unmap(0);
// create shader resource that is what the renderer needs
ShaderResourceViewDescription desc = new ShaderResourceViewDescription();
desc.Format = texture.Description.Format;
desc.Dimension = ShaderResourceViewDimension.Texture2D;
desc.MostDetailedMip = 0;
desc.MipLevels = 1;
ShaderResourceView srv = new ShaderResourceView(device, texture, desc);
return srv;
}
我相信正在设置纹理的数据,但我不能确定,因为没有显示任何内容。我知道我的渲染器可以正常工作,因为我可以很好地从文件加载纹理,但我似乎有一个我找不到的问题。提前感谢您的帮助!