首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Texture2D正在变黑

Texture2D正在变黑
EN

Stack Overflow用户
提问于 2011-11-28 20:07:49
回答 1查看 549关注 0票数 3

我有一个从内容管道加载的Texture2D。这很好用,但是一旦我尝试在完全不同的Texture2D上使用SetData,我的游戏中的所有纹理都会变得完全黑色:

这是在我的HUDMeter类中,我希望这个类只是红色的

代码语言:javascript
复制
Texture2D colorGrad = Content.Load<Texture2D>(GradientAsset);

Color[,] pixels = new Color[colorGrad.Width, colorGrad.Height];

Color[] pixels1D = new Color[colorGrad.Width * colorGrad.Height];

pixels = GetRedChannel(colorGrad);

pixels1D = Color2DToColor1D(pixels, colorGrad.Width);

System.Diagnostics.Debug.WriteLine(pixels[32,32]);
Gradient = colorGrad;
Gradient.SetData<Color>(pixels1D);

这些是使用Riemers教程

代码语言:javascript
复制
protected Color[,] GetRedChannel(Texture2D texture)
{
    Color[,] pixels = TextureTo2DArray(texture);

    Color[,] output = new Color[texture.Width, texture.Height];

    for (int x = 0; x < texture.Width; x++)
    {
        for (int y = 0; y < texture.Height; y++)
        {
            output[x,y] = new Color(pixels[x,y].G, 0, 0);
        }
    }

    return output;
}

protected Color[,] TextureTo2DArray(Texture2D texture)
{
    Color[] colors1D = new Color[texture.Width * texture.Height];
    texture.GetData(colors1D);

    Color[,] colors2D = new Color[texture.Width, texture.Height];
    for (int x = 0; x < texture.Width; x++)
        for (int y = 0; y < texture.Height; y++)
            colors2D[x, y] = colors1D[x + y * texture.Width];

    return colors2D;
}

private Color[] Color2DToColor1D (Color[,] colors, int width)
{
    Color[] output = new Color[colors.Length];

    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < colors.Length / width; y++)
        {
            output[x + y * width] = colors[x % width, y % (colors.Length/width)];
        }
    }

    return output;
}

这是绘制精灵的代码,这很好用,这就是我绘制精灵的方式:

代码语言:javascript
复制
batch.Draw(meter.Gradient, new Vector2(X, Y), Color.White);

更新:

我发现那些不使用相同文件的精灵并不是黑色的。Texture2D.SetData<>()实际上改变了文件本身吗?这有什么用呢?

更新:

我只是尝试使用Alpha和RGB,它是有效的。我在想,其中一个转换方法出了问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-29 11:25:02

如果您这样做:

代码语言:javascript
复制
Texture2D textureA = Content.Load<Texture2D>("MyTexture");
Texture2D textureB = Content.Load<Texture2D>("MyTexture");

textureAtextureB指的是同一个对象。因此,如果您在其中一个函数上调用SetData,则会同时影响这两个函数。这是因为ContentManager保留了一个已经加载的资源的内部列表,因此它不必不断地重新加载相同的资源。

解决方案是创建一个相同大小的新Texture2D对象,在ContentManager加载的对象上调用GetData,然后在新纹理上调用SetData

示例(未测试):

代码语言:javascript
复制
Color[] buffer = new Color[textureA.Width * textureA.Height];
Texture2D textureB = new Texture2D(textureA.GraphicsDevice,
                                   textureA.Width,
                                   textureA.Height);
textureA.GetData(buffer);
textureB.SetData(buffer);

新纹理的Dispose()当你完成它(例如:在你的Game.UnloadContent方法中)。但是千万不要丢弃由ContentManager加载的对象(因为,就像我说过的,它是一个共享对象;请使用ContentManager.Unload )。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8295575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档