使用SlimDX从RGBA值创建纹理

本文关键字:创建 纹理 RGBA SlimDX 使用 | 更新日期: 2023-09-27 18:18:20

这是我第一篇关于堆栈溢出的文章!我在团队制作的一款游戏中使用了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;
    }

我相信纹理的数据正在设置,但我不能确定,因为没有显示。我知道我的渲染器可以工作,因为我可以从文件中加载纹理,但我似乎有一个问题,我找不到。提前感谢您的帮助!

使用SlimDX从RGBA值创建纹理

我的代码一直都是正确的。我觉得自己像个白痴,但我发现了我的问题,我没有为纹理设置alpha值,所以它实际上是被绘制的,我只是看不见它>_<;总是犯简单的错误,是吗?感谢所有的观看。